Elbeem: Use pragma directive instead of overriding number of omp threads
authorSergey Sharybin <sergey.vfx@gmail.com>
Fri, 27 Sep 2013 18:55:15 +0000 (18:55 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Fri, 27 Sep 2013 18:55:15 +0000 (18:55 +0000)
Global OpenMP threads override is not a good idea because this would affect
all possible OpenMP blocks running at the same time as simulation.

And that was actually a big on restoring number of threads: code needed to
store current number of threads, not maximal one.

intern/elbeem/intern/simulation_object.cpp
intern/elbeem/intern/solver_interface.cpp
intern/elbeem/intern/solver_interface.h
intern/elbeem/intern/solver_main.cpp

index 9e98adfc734f22f4cea760662cc4bb3b9428266c..99c3b170634c5f6c9ec271a7e270d6be69caa8db 100644 (file)
 //! lbm factory functions
 LbmSolverInterface* createSolver();
 
-#if PARALLEL==1
-static int omp_threadcache;
-#endif
-
 /******************************************************************************
  * Constructor
  *****************************************************************************/
@@ -71,10 +67,6 @@ SimulationObject::~SimulationObject()
        if(mpParam)          delete mpParam;
        if(mpParts)          delete mpParts;
        debMsgStd("SimulationObject",DM_MSG,"El'Beem Done!\n",10);
-#if (PARALLEL == 1)
-       omp_set_num_threads(omp_threadcache);
-       printf("Resetting omp_threads to cached value %d \n", omp_threadcache);
-#endif
 }
 
 
@@ -185,9 +177,7 @@ int SimulationObject::initializeLbmSimulation(ntlRenderGlobals *glob)
                mpLbm->setSmoothing(1.0 * mpElbeemSettings->surfaceSmoothing, 1.0 * mpElbeemSettings->surfaceSmoothing);
                mpLbm->setIsoSubdivs(mpElbeemSettings->surfaceSubdivs);
 #if PARALLEL==1
-               omp_threadcache = omp_get_max_threads();
-               omp_set_num_threads(mpElbeemSettings->threads);
-               printf("Setting omp_threads to usersetting %d \n", mpElbeemSettings->threads);
+               mpLbm->setNumOMPThreads(mpElbeemSettings->threads);
 #endif
                mpLbm->setSizeX(mpElbeemSettings->resolutionxyz);
                mpLbm->setSizeY(mpElbeemSettings->resolutionxyz);
index c3421fe1968d29aa56e7d4faf8149907de4786c5..4284a56f2fd5ac86f822c07058b0d2abb9e5e921 100644 (file)
@@ -64,7 +64,10 @@ LbmSolverInterface::LbmSolverInterface() :
        mCppfStage(0),
        mDumpRawText(false),
        mDumpRawBinary(false),
-       mDumpRawBinaryZip(true)
+       mDumpRawBinaryZip(true),
+#if PARALLEL==1
+       mNumOMPThreads(1)
+#endif  // PARALLEL==1
 {
 #if ELBEEM_PLUGIN==1
        if(gDebugLevel<=1) setSilent(true);
@@ -670,6 +673,12 @@ void LbmSolverInterface::markedClearList() {
        mMarkedCells.clear();
 }
 
+#if PARALLEL==1
+void LbmSolverInterface::setNumOMPThreads(int num_threads) {
+       mNumOMPThreads = num_threads;
+}
+#endif  // PARALLEL==1
+
 /*******************************************************************************/
 /*! string helper functions */
 /*******************************************************************************/
index 8f3181a307d314c58775367a2796e14e3586d466..e25c59e2d50e2a743ec5c562c73cde7ec5ea8384 100644 (file)
@@ -452,7 +452,9 @@ class LbmSolverInterface
                CellIdentifierInterface* markedAdvanceCell();
                void markedClearList();
 
-
+#if PARALLEL==1
+               void setNumOMPThreads(int num_threads);
+#endif  // PARALLEL==1
        protected:
 
                /*! abort simulation on error... */
@@ -595,6 +597,10 @@ class LbmSolverInterface
                bool mDumpRawText;
                bool mDumpRawBinary;
                bool mDumpRawBinaryZip;
+
+#if PARALLEL==1
+               int mNumOMPThreads;
+#endif  // PARALLEL==1
 };
 
 
index 99278657fd9cbfd3d3244289b2370df84f450814..46af6740cf1580ed5a365a4e6c609316c93263de 100644 (file)
@@ -378,7 +378,7 @@ LbmFsgrSolver::mainLoop(int lev)
        const int gridLoopBound=1;
        GRID_REGION_INIT();
 #if PARALLEL==1
-#pragma omp parallel default(shared) \
+#pragma omp parallel default(shared) num_threads(mNumOMPThreads) \
   reduction(+: \
          calcCurrentMass,calcCurrentVolume, \
                calcCellsFilled,calcCellsEmptied, \
@@ -1119,7 +1119,7 @@ LbmFsgrSolver::preinitGrids()
        
                GRID_REGION_INIT();
 #if PARALLEL==1
-#pragma omp parallel default(shared) \
+#pragma omp parallel default(shared) num_threads(mNumOMPThreads) \
   reduction(+: \
          calcCurrentMass,calcCurrentVolume, \
                calcCellsFilled,calcCellsEmptied, \
@@ -1156,7 +1156,7 @@ LbmFsgrSolver::standingFluidPreinit()
 
        GRID_REGION_INIT();
 #if PARALLEL==1
-#pragma omp parallel default(shared) \
+#pragma omp parallel default(shared) num_threads(mNumOMPThreads) \
   reduction(+: \
          calcCurrentMass,calcCurrentVolume, \
                calcCellsFilled,calcCellsEmptied, \