Openmp'shared'对'#pragma omp无效

时间:2012-03-28 10:17:22

标签: c openmp

嗨,我得到一个奇怪的错误

#include <omp.h>
#define N 1000

main ()
{

int i, nthreads;
int chunk = 10;
float a[N], b[N], c[N], d[N];
double result =0;

#pragma omp parallel 
{

nthreads = omp_get_num_threads();
printf("no of threads %d", nthreads);

#pragma omp for  shared(a,b,c,d,result) private(i)  schedule(static,chunk) // line 18
for (i=0; i < N; i++){
  a[i] = i * 1.5;
  b[i] = i + 22.35;
}

#pragma omp barrier

#pragma omp for schedule(static,chunk) shared(a,b,c,d,result) private(i)  reduction(+:result)   // line 26
for(i=0; i < N; i++){
result = result + (a[i]+b[i]);
}

}
printf("value is %f", result);

}

根据规则的openmp pragma,允许共享,但这里只是因为iam在这里使用共享而得到编译错误。有人可以帮帮我。

test2.c:18: error: ‘shared’ is not valid for ‘#pragma omp for’
test2.c:26: error: ‘shared’ is not valid for ‘#pragma omp for’

1 个答案:

答案 0 :(得分:2)

从评论到你的问题,我明白固定代码应该是:

#include <omp.h>
#define N 1000

main ()
{

    int i, nthreads;
    int chunk = 10;
    float a[N], b[N], c[N], d[N];
    double result =0;

    #pragma omp parallel shared(a,b,c,d,result) private(i)
    {

        nthreads = omp_get_num_threads();
        printf("no of threads %d", nthreads);

        #pragma omp for schedule(static,chunk) // line 18
        for (i=0; i < N; i++){
          a[i] = i * 1.5;
          b[i] = i + 22.35;
        }

        #pragma omp barrier

        #pragma omp for schedule(static,chunk) reduction(+:result)   // line 26
        for(i=0; i < N; i++){
            result = result + (a[i]+b[i]);
        }

    }
    printf("value is %f", result);

}