2012-03-28 87 views
0

你好我得到一個奇怪的錯誤OpenMP的「共享」無效「的#pragma OMP

#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的編譯的規則,共享是允許的,但在這裏IAM得到一個編譯錯誤,只是因爲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

'shared'被允許'OMP for'?你從哪裏得到這個?根據這份文件,'共享'不列爲可用。 http://publib.boulder.ibm.com/infocenter/cellcomp/v101v121/index.jsp?topic=/com.ibm.xlcpp101.cell.doc/compiler_ref/prag_omp_for.html – 2012-03-28 10:29:01

+0

所以則只有平行「編譯OMP平行爲「允許共享? – user602774 2012-03-28 10:37:23

+4

是 - 描述變量共享的子句與並行區域相關聯,而不是並行區域內的工作共享構造。所以你把它們放在#pragma omp平行線上,而不是#pragma omp for line。您可以通過在並行塊內本地定義變量來幫助自己;那些都是私人的。 – 2012-03-28 11:30:53

回答

1

從評論你的問題,我明白了固定的代碼應該是這樣的:

#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); 

}