我在 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/