我试图并行化以下程序,但不知道如何减少数组.我知道不可能这样做,但还有其他选择吗?谢谢.(我添加了对m的减少,这是错误的,但希望就如何做到这一点提出建议.)
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <omp.h>
using namespace std;
int main ()
{
int A [] = {84, 30, 95, 94, 36, 73, 52, 23, 2, 13};
int S [10];
time_t start_time = time(NULL);
#pragma omp parallel for private(m) reduction(+:m)
for (int n=0 ; n<10 ; ++n ){
for (int m=0; m<=n; ++m){
S[n] += A[m];
}
}
time_t end_time = time(NULL);
cout << end_time-start_time;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如何使用OpenMP并行化这个数组和?什么应该分享,什么应该是私人的?
这是数组和的代码..
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);
}
Run Code Online (Sandbox Code Playgroud)