1

私は次のようなコードを持っています:

for(i=0; i<max;i++){
   for(j=0; j<max2;j++)
     //stuff
}

for(i=0; i<max;i++){
   for(j=0; j<max2;j++)
     //other stuff
}

for(i=0; i<max;i++){
   for(j=0; j<max2;j++)
     //final stuff
}

OpenMPを使用してこれを並列化したい。最良の方法は何ですか?#pragma omp parallel private(i)私は最初と#pragma omp forすべてのjループの前にをやってみました。これは私が意味することです:

 #pragma omp parallel private(i)
 {
 for(i=0; i<max;i++){
   #pragma omp for
   for (j=0; j<max2;j++){
   //and so on and so forth

問題は、これではパフォーマンスがまったく向上しないということです。これは、3つのforループが並行して実行されていないためだと思います...これらの3つを同時に実行できれば、パフォーマンスが向上すると思います。何か案は?ありがとう!

4

1 に答える 1

2

簡単な修正は、反復セクションを作成し、これを並列化することです。

#pragma omp for
for (k=0;k<3;k++){
  if (k==0) do_stuff();
  if (k==1) do_other_stuff();
  if (k==2) do_other_other_stuff();
}

より良い修正は、omp sectionsディレクティブを使用することです。(ここからの解決策)

#pragma omp parallel sections
{
  #pragma omp section
  {
     /* Executes in thread 1 */
     do_stuff();
  } 
  #pragma omp section
  {
    /* Executes in thread 2 */ 
    do_other_stuff();   
  } 
  #pragma omp section
  {
    /* Executes in thread 3 */
    do_other_other_stuff();
  }   
}
于 2012-11-27T11:41:22.760 に答える