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

我怕爱的太早我们不能终老 提交于 2019-12-03 14:29:43

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