Why does while loop in an OMP parallel section fail to terminate when termination condition depends on update from different section

偶尔善良 提交于 2020-01-01 05:29:09

问题


Is the C++ code below legal, or is there a problem with my compiler? The code was complied into a shared library using

gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)

and openMP and then called via R 2.15.2.

int it=0;
#pragma omp parallel sections shared(it)
   {
      #pragma omp section
      {
           std::cout<<"Entering section A"<<std::endl;
           for(it=0;it<10;it++)
           {
                   std::cout<<"Iteration "<<it<<std::endl;

           }
           std::cout<<"Leaving section A with it="<<it<<std::endl;
      }

      #pragma omp section
      {
           std::cout<<"Entering section B with it="<<it<<std::endl;
           while(it<10)
           {
                   1;
           }
           std::cout<<"Leaving section B"<<std::endl;
  }
}

I obtain the following output (apologies for interweaving output from 2 threads but I think it is interpretable):

Entering section A
Iteration Entering section B with it=0
0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
Leaving section A with it=10

The program then stalls: section B seems to get stuck in the while loop. Since the variable 'it' is shared I do not understand why the while loop doesn't terminate when section A is complete.


回答1:


That is because shared variable only means it it the same for all threads, but programmer still has to synchronize access by hands

SHARED Clause

It is the programmer's responsibility to ensure that multiple threads properly access SHARED variables (such as via CRITICAL sections)

So, you can, for example, flush variables after first section is completed:

      #pragma omp section
      {
           std::cout<<"Entering section A"<<std::endl;
           for(it=0;it<10;it++)
           {
                   std::cout<<"Iteration "<<it<<std::endl;

           }
           #pragma omp flush(it)
           std::cout<<"Leaving section A with it="<<it<<std::endl;
      }

      #pragma omp section
      {
           std::cout<<"Entering section B with it="<<it<<std::endl;
           while(it<10)
           {
            #pragma omp flush(it)
                   1;
           }
           std::cout<<"Leaving section B"<<std::endl;
      }


来源:https://stackoverflow.com/questions/16305432/why-does-while-loop-in-an-omp-parallel-section-fail-to-terminate-when-terminatio

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