Nested loop in OpenMP

筅森魡賤 提交于 2019-12-25 03:20:54

问题


I need to run a short outer loop and a long inner loop. I would like to parallelize the latter and not the former. The reason is that there is an array that is updated after the inner loop has run. The code I am using is the following

#pragma omp parallel{
for(j=0;j<3;j++){
  s=0;
  #pragma omp for reduction(+:s)
     for(i=0;i<10000;i++)
        s+=1;

  A[j]=s;
}
}

This actually hangs. The following works just fine, but I'd rather avoid the overhead of starting a new parallel region since this was preceded by another.

for(j=0;j<3;j++){
  s=0;
  #pragma omp parallel for reduction(+:s)
     for(i=0;i<10000;i++)
        s+=1;

  A[j]=s;

}

What is the correct (and fastest) way of doing this?


回答1:


The following example should work as expected:

#include<iostream>

using namespace std;

int main(){

  int s;
  int A[3];

#pragma omp parallel
  { // Note that I moved the curly bracket
    for(int j = 0; j < 3; j++) {
#pragma omp single         
      s = 0;
#pragma omp for reduction(+:s)
      for(int i=0;i<10000;i++) {
        s+=1;
      } // Implicit barrier here    
#pragma omp single
      A[j]=s; // This statement needs synchronization
    } // End of the outer for loop
  } // End of the parallel region

  for (int jj = 0; jj < 3; jj++)
    cout << A[jj] << endl;
  return 0;
}

An example of compilation and execution is:

> g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> g++ -fopenmp -Wall main.cpp
> export OMP_NUM_THREADS=169
> ./a.out 
10000
10000
10000


来源:https://stackoverflow.com/questions/16515473/nested-loop-in-openmp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!