c - UPC 中指向共享数组的私有(private)指针数组

标签 c arrays pointers parallel-processing upc

我在 UPC 中编程,并且有一个在两个线程之间共享的数组。每个线程都有指向这些共享区域的私有(private)指针:

#define SIZE 10000
#define HALFSIZE (SIZE/2)

shared [ HALFSIZE ] int table [ SIZE ]; /* all areas */
shared [ HALFSIZE ] int *first_area_pt; /* points to first thread area */
shared [ HALFSIZE ] int *second_area_pt; /* points to second thread area */

现在我想要的不是 2 个,而是 N 个线程、N 个区域和 N 个指针。所以我需要这些指针的数组:

shared [ HALFSIZE ] int *first_area_pt;
shared [ HALFSIZE ] int *second_area_pt;

我应该如何定义它?

最佳答案

假设您使用的是静态线程编译环境,声明数组的更简洁的方法是这样的:

#define SIZE 10000

shared [*] int table [ SIZE ]; /* data automatically blocked across THREADS */

shared [1] int *base = (shared [1] int *)&table;

然后您可以使用循环基指针创建一个指向共享的指针,引用与线程 X 具有亲和性的数据,表达式如下:

  shared [] int *threadXdata = (shared [] int *)(base+X);

使用这种方法,您永远不需要为 THREADS 指针数组实例化存储。然而,如果你真的想要,你当然可以用这样的东西声明和初始化一个:

  shared [] int *threadptr[THREADS];
  for (int i=0; i < THREADS; i++) threadptr[i] = (shared [] int *)(base+i);
  ...
  threadptr[4][10] = 42; /* example access to element 10 on thread 4*/

这里的 threadptr 是一个本地指针数组,用作每个线程数据的引用目录。

请注意,以上所有内容都使用无限阻塞的共享指针来访问与单个线程具有亲和性的元素,因为每个 block 在逻辑上都是一个连续的数据 block ,没有跨线程包装。

关于c - UPC 中指向共享数组的私有(private)指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695847/

相关文章:

在 C 中复制两个包含 char 指针的结构

C、多维数组: array whose elements are one-dimensional arrays?

c - 需要创建一个返回 int 的函数,该函数基于哪个 char 参数具有更多大写字母

c++ - C++ 的箭头 (->) 运算符的正式名称是什么?

c - 是否可以应用抗锯齿/其他功能来更好地绘制拉伸(stretch)的 TIcon?

c - 这段代码中的CDECL是什么意思?

python - 2D 到 3D numpy 数组的高效转换

arrays - VBA创建一个包含公式的数组

java - 如何获取二维数组的行和并按从高到低的顺序排列?

c - 释放结构体双指针的正确方法