2013-01-18 72 views
0

第一種方法(並行內環):OpenMP的並行用於構建性能

for(j=0; j<LATTICE_VW; ++j) { 
    x = j*DX + LATTICE_W; 
    #pragma omp parallel for ordered private(y, prob) 
     for(i=0; i<LATTICE_VH; ++i) { 
      y = i*DY + LATTICE_S; 
      prob = psi[i][j].norm(); 

      #pragma omp ordered 
       out << x << " " << y << " " << prob << endl; 
     } 
} 

第二方法(並行外環):

#pragma omp parallel for ordered private(x, y, prob) 
    for(j=0; j<LATTICE_VW; ++j) { 
     x = j*DX + LATTICE_W; 
     for(i=0; i<LATTICE_VH; ++i) { 
      y = i*DY + LATTICE_S; 
      prob = psi[i][j].norm(); 

      #pragma omp ordered 
       out << x << " " << y << " " << prob << endl; 
     } 
    } 

第三方法(並行摺疊環路)

#pragma omp parallel for collapse(2) ordered private(x, y, prob) 
    for(j=0; j<LATTICE_VW; ++j) { 
     for(i=0; i<LATTICE_VH; ++i) { 
      x = j*DX + LATTICE_W; 
      y = i*DY + LATTICE_S; 
      prob = psi[i][j].norm(); 

      #pragma omp ordered 
       out << x << " " << y << " " << prob << endl; 
     } 
    } 

如果我要猜測我會說方法3應該是最快的。

然而,方法1是最快的,而第二和第三方都需要大約相同的時間量,就好像沒有並行化一樣。爲什麼發生這種情況?

+0

您是否從方法2獲得了正確的輸出?變量'y'和'prob'也應該是私人的。 – Novelocrat

+0

對不起,他們在那裏是私人的。剛剛編輯它 – lexsintra

+0

內外環的行程數是多少? –

回答

0

看看這個:

for(int x = 0; x < 4; ++x) 
    #pragma omp parallel for ordered 
    for(int y = 0; y < 4; ++y) 
    #pragma omp ordered 
    cout << x << ',' << y << " (by thread " << omp_get_thread_num() << ')' << endl; 

您有:

0,0 (by thread 0) 
0,1 (by thread 1) 
0,2 (by thread 2) 
0,3 (by thread 3) 
1,0 (by thread 0) 
1,1 (by thread 1) 
1,2 (by thread 2) 
1,3 (by thread 3) 

每個線程只是要等待一段cout所有的工作之前,可以並行進行。 但隨着:

#pragma omp parallel for ordered 
for(int x = 0; x < 4; ++x) 
    for(int y = 0; y < 4; ++y) 
    #pragma omp ordered 
    cout << x << ',' << y << " (by thread " << omp_get_thread_num() << ')' << endl; 

#pragma omp parallel for collapse(2) ordered 
for(int x = 0; x < 4; ++x) 
    for(int y = 0; y < 4; ++y) 
    #pragma omp ordered 
    cout << x << ',' << y << " (by thread " << omp_get_thread_num() << ')' << endl; 

的情況是:

0,0 (by thread 0) 
0,1 (by thread 0) 
0,2 (by thread 0) 
0,3 (by thread 0) 
1,0 (by thread 1) 
1,1 (by thread 1) 
1,2 (by thread 1) 
1,3 (by thread 1) 
2,0 (by thread 2) 
2,1 (by thread 2) 
2,2 (by thread 2) 
2,3 (by thread 2) 
3,0 (by thread 3) 
3,1 (by thread 3) 
3,2 (by thread 3) 
3,3 (by thread 3) 

所以thread 1必須等待thread 0完成所有的工作,纔可以cout第一次,幾乎沒有什麼可以同時完成。

嘗試在崩潰版本中添加schedule(static,1),它應該至少和第一個版本一樣好。