0
我想在C編寫代碼使用openmp運行在我的雙核心機器上,並從數組中讀取int值,並將結果放入另一個數組,但我得到錯誤的結果。這是我的代碼:錯誤的結果私人陣列openmp
#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
int array[10];
int array2[10];
int np,iam;
int main() {
array[0]=12; // 0
array[1]=21; // 21
array[2]=13; // 26
array[3]=31; // 93
array[4]=40; // 160
array[5]=32; // 160
array[6]=16; // 96
array[7]=51; // 357
array[8]=7; // 56
array[9]=23; // 207
int i;
#pragma omp parallel for private(i,array)
for(i=0;i<10;i++){
#if defined (_OPENMP)
np = omp_get_num_threads();
iam = omp_get_thread_num();
#endif
printf("iam %d and i am doing % d\n",iam,i);
array2[i] = array[i]*i;
}
for(i=0;i<10;i++){
printf("%d ",array2[i]);
}
printf("\n");
return EXIT_SUCCESS;
}
非水貨版本的結果是:
iam 0 and i am doing 0
iam 0 and i am doing 1
iam 0 and i am doing 2
iam 0 and i am doing 3
iam 0 and i am doing 4
iam 0 and i am doing 5
iam 0 and i am doing 6
iam 0 and i am doing 7
iam 0 and i am doing 8
iam 0 and i am doing 9
0 21 26 93 160 160 96 357 56 207
並行版本的結果是:
iam 0 and i am doing 0
iam 0 and i am doing 1
iam 0 and i am doing 2
iam 0 and i am doing 3
iam 0 and i am doing 4
iam 1 and i am doing 5
iam 1 and i am doing 6
iam 1 and i am doing 7
iam 1 and i am doing 8
iam 1 and i am doing 9
0 -1217558997 1859518440 6 0 -1792801609 1284743096 -364 -1166026656 0
編輯1:我需要的數組 「數組」 到是私人的。在這種情況下它沒有任何意義,但我正在嘗試使用一個數組作爲私有,以便在稍後我正在處理的另一個案例中使用它。