[問題] OpenMP平行處理的bug
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++ 2012
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
Tifflib,Armadillo:C++ linear algebra library
問題(Question):
平行處理迴圈 內的變數獨立處理
餵入的資料(Input):
txt文字檔 amd 16bit TIFF影像
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
出現R6010 abort() has been called
程式碼(Code):(請善用置底文網頁, 記得排版)
void test(double &X, double &Y, double &Z,double parameter[6]){
...略...
mat B(4,3); //design matrix
mat Un; // unknows,XYZ的修正量
mat C(4,1);
mat V;
mat InN; //Inv(BTPB)
mat BTPB, BTPC;
X = 0.0;
Y = 0.0;
Z = 0.0;
for(uint8 iter=0;iter<20;iter++){ //iterative caculation
.
.
.
略
.
.
.
BTPB.fill(0);
BTPC.fill(0);
BTPB = trans(B)*B;
BTPC = trans(B)*C;
Un = inv(BTPB)*BTPC;
InN = inv(BTPB);
X = X+Un(0,0); //bug點應該是在這
Y = Y+Un(1,0);
Z = Z+Un(2,0);
if(fabs(Un)<pow(10,-5)){iter = 100;} //停止迴圈門檻
}
}
main{
//-----parallel processing-----
int number=0;
for(int line=0;line<img.h;line++){
TIFFReadScanline(img.tif, buf, line);
#pragma omp parallel num_threads(3) //電腦的CPU是i5,只取3個TH
{
#pragma omp for reduction( +:number)
for(int samp=0;samp<img.w;samp++){
double X=0,Y=0,Z=0; //不清楚XYZ應該要怎麼放才對
int V = static_cast<uint16*>(buf)[(samp)*img.band];
if(V<=2000){
test(X,Y,Z,para[6]);
cout<< X <<"\t"<< Y <<"\t"<< Z <<endl;
number++;
}
}
}
}
補充說明(Supplement):
其實完整的code很長又很複雜難以解釋,所以自己做了一些精簡
自己try and error後大概有找到發生錯誤的地方,但是不知道該怎麼解決
函數test我有單獨測試過,在不用parallel時是可以作用的
但是放入parallel時就會出現R6010的錯誤
我自己大概知道是平行處理時,test裡面的XYZ被不同的TH共同使用
但是不知道該怎麼修改才能排除這狀態(openMP的新手)
還請各位專業的大大指導
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 58.182.249.166
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1429709488.A.7AF.html
→
04/22 21:36, , 1F
04/22 21:36, 1F
→
04/22 21:36, , 2F
04/22 21:36, 2F
→
04/22 22:11, , 3F
04/22 22:11, 3F
→
04/22 22:13, , 4F
04/22 22:13, 4F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章