我正在尝试使用omp实现listranking问题(也称为快捷方式),以获得数组W的sums前缀。我不知道我是否正确使用了flush杂注。在编译时,我有一个警告:“屏障区域可能不会紧密嵌套在工作共享、关键、有序、主任务区域或显式任务区域中”。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
main(int argc, char *argv[])
{
int Q[9]={1,2,3,4,5,6,7,8,0};
int W[8]={1,2,3,4,5,6,7,8};
int i,j=6,id;
printf("Before:\n");
for(j=0;j<8;j++)
printf("%d",W[j]);
printf("\n");
#pragma omp parallel for shared(Q,W) private(id) num_threads(7)
for (i=6; i>=0; i--)
{
id= omp_get_thread_num();
while((Q[i] !=0)&& (Q[Q[i]] !=0))
{
#pragma omp flush(W)
W[i]=W[i]+W[Q[i]];
#pragma omp flush(W)
printf("Am %d \t W[%d]= %d",id,i,W[i]);
#pragma omp barrier
#pragma omp flush(Q)
Q[i]=Q[Q[i]];
#pragma omp flush(Q)
printf("Am %d \n Q[%d]= %d",id,i,Q[i]);
};
}
printf("Result:\n");
for(j=0; j<8; j++)
printf("%d \t",W[j]);
printf("\n");}
请帮帮我!
发布于 2009-12-25 16:02:10
你不能在omp并行中使用屏障,你几乎只能使用omp并行区域中的屏障。
这样做的原因是,如果您的循环是从1到N,那么内部的屏障将有效地创建N个线程,如果N很大,则会对性能产生负面影响。
我没有在这里查找算法,但有两个合理的选择是重构以在障碍所在的地方一个接一个地使用2个并行for循环,或者重构您的算法以使用#杂注并行区域。
我查阅了列表排名算法,如果你必须使用openmp,你会很好地找到前缀sum或scan的实现。
-Rick
https://stackoverflow.com/questions/1959777
复制相似问题