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 (int i=0;i<n;i++)
sum=sum+a[i];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With