如何使用OpenMP并行化此数组总和? [英] How to parallelize this array sum using OpenMP?
本文介绍了如何使用OpenMP并行化此数组总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用OpenMP使此数组总和并行化? 应该共享什么,什么应该私有?
How can I make this array sum is parallelized using OpenMP ? what should be shared, and what should be private ?
这是数组求和的代码..
Here is the code for array sum ..
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);
}
推荐答案
您应该像这样使用归约:
You should use reduction like this:
#pragma omp parallel for reduction (+:sum)
for (i=0;i<n;i++)
sum=sum+a[i];
这篇关于如何使用OpenMP并行化此数组总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文