如何使用 OpenMP 使这个数组总和并行化? 什么应该共享,什么应该是私有(private)的?
这是数组求和的代码..
main()
{
int a[10], i, n, sum=0;
printf("enter no. of elements");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for (i=0;i<n;i++)
sum=sum+a[i];
for(i=0;i<n;i++)
printf("\n a[%d] = %d", i, a[i]);
printf("\n sum = %d",sum);
}
最佳答案
你应该像这样使用缩减:
#pragma omp parallel for reduction (+:sum)
for (int i=0;i<n;i++)
sum=sum+a[i];
关于c - 如何使用 OpenMP 并行化这个数组总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27056090/