打开MP强制定期同步

时间:2018-10-03 18:23:36

标签: c++ parallel-processing openmp

我正在尝试使用Open MP(C ++)在3D Ising晶格上并行化用于Monte Carlo Simulations的并行回火代码。在这里,N(通常为8)独立的Monte Carlo模拟在不同的温度下运行。模拟及其所有变量/数量彼此完全独立。 定期必须停止仿真并尝试交换其配置-这是麻烦所在。尝试使用屏障强制停止线程(以便可以在不更改数量的情况下正确进行交换)失败,并且编译器给出错误:“工作共享区域可能未紧密嵌套在工作共享内部,关键,有序,主要或明确的任务区域。”

有什么办法解决吗?如何重新组织代码,使其能够编译和运行?我已经浏览了许多类似的线程,但就我而言,循环根本无法轻易分离(必须保留模拟进度)。

这是我的代码的粗略概述(省略了很多内容)。

#include "omp.h"//and other headers

#define LSize 10
#define TotalRuns 10
#define MC_WarmUP 10000
#define MC_Samples 50000

using namespace std;
int main()
{
     //rng initialized
     int numruns;
     int Lattice[TotalRuns][LSize][LSize][LSize];//explicit (for swapping)

     omp_set_num_threads(TotalRuns);
     #pragma omp parallel for//run simulations entirely independently
     for (numruns=0;numruns<TotalRuns;numruns++)
     {
          int counter,counter2,counter3;
          double SimEnergy[TotalRuns];
          double Temp;
          //file setup (each simulation also prints its results to file)
          //Lattice[numruns] is initialized to random +1/-1's

          for (counter=70;counter>1;counter++)//i.e for a number of temperatures
          {
              Temp = 0.1*(double)counter + 0.1*(double)numruns;//set temperature
              for (counter2=0;counter2<MC_WarmUP;counter2++)//equilibrate the simulation
              {
                   //perform Monte Carlo iterations here

              }
              for (counter2=0;counter2<(NSamples/SwapSteps);counter2++)//sampling phase
              {
                   for (counter3=0;counter3<SwapSteps;counter3++)//take samples ever SwapSteps interations
                   {
                        //perform Monte Carlo iterations here
                        //save simulation quantities periodically
                   }
                   SimEnergy[numruns] = E_Calc(Lattice,numruns);//calculates the energy of the lattice for a given simulation

                   #pragma omp barrier//need everything to stop at this point
                   #pragma omp single//only one thread does this
                   {
                        //bunch of stuff here but most importantly
                        swap(Lattice[N],Lattice[M])//may occur where N,M are between 0 .. TotalRuns and N!=M.
                   }
              }
              //save averaged data to file
          }
     }
     return 0;
}

代码非常简单,也许不是解决问题的最佳方法。真正的问题是,如何冻结或锁定线程,以使交换可以在交换操作期间不修改Lattice的情况下进行?任何帮助表示赞赏!

0 个答案:

没有答案