2

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

#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;

我应该如何定义它?

4

2 回答 2

1

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

#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 是一个本地指针数组,用作每个线程数据的引用目录。

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

于 2014-04-13T07:25:39.037 回答
0

由于您使用的符号是非标准的(尽管我认为它遵循UPC - Unified Parallel C - 规范),我们只能猜测您可能需要什么。突出显示您正在使用的内容会很有帮助,因为(何时)它是不寻常的。

shared [ N ]在符号的一种可能解释下,这看起来表面上是合理的。

#define SIZE 10000
#define NUMTHREADS 25
#define FRACSIZE (SIZE/NUMTHREADS)

shared [ FRACSIZE ] int table[SIZE];
shared [ 1 ]        int *area_ptr[NUMTHREADS];
于 2012-01-01T22:13:13.410 回答