Salome HOME
fixed bos#37786: SMESH_MGAdaptTests_without_session random failures
[modules/smesh.git] / src / SMESH / SMESH_ParallelMesh.cxx
index 6d70e15a289e84aac197dbc3e8f8d949fb479530..2bce451f3bd50ca67746b48fce689dc9f3c079a6 100644 (file)
   #include <windows.h>
 #endif
 
-#ifndef WIN32
 #include <boost/filesystem.hpp>
 namespace fs=boost::filesystem;
-#endif
 
 #ifndef WIN32
 #include <boost/asio.hpp>
@@ -43,12 +41,6 @@ namespace fs=boost::filesystem;
 
 #include <utilities.h>
 
-#ifdef _DEBUG_
-static int MYDEBUG = 1;
-#else
-static int MYDEBUG = 0;
-#endif
-
 SMESH_ParallelMesh::SMESH_ParallelMesh(int               theLocalId,
                        SMESH_Gen*        theGen,
                        bool              theIsEmbeddedMode,
@@ -58,21 +50,51 @@ SMESH_ParallelMesh::SMESH_ParallelMesh(int               theLocalId,
                                                                   theDocument)
 {
   MESSAGE("SMESH_ParallelMesh::SMESH_ParallelMesh(int localId)");
-#ifndef WIN32
-  _NbThreads = std::thread::hardware_concurrency();
-#else
-  _NbThreads = 0;
-#endif
   CreateTmpFolder();
 };
 
 SMESH_ParallelMesh::~SMESH_ParallelMesh()
+{
+  cleanup();
+};
+
+void SMESH_ParallelMesh::cleanup()
 {
   DeletePoolThreads();
-  if(!MYDEBUG)
+  std::cout << "Keeping tmp folder" << keepingTmpFolfer() << std::endl;
+  if(!keepingTmpFolfer())
+  {
+    MESSAGE("Set SMESH_KEEP_TMP to > 0 to keep temporary folders")
     DeleteTmpFolder();
+  }
 };
 
+//=============================================================================
+/*!
+ * \brief Checking if we should keep the temporary folder
+ *        They are kept if the variable SMESH_KEEP_TMP is set to higher than 0
+ */
+//=============================================================================
+bool SMESH_ParallelMesh::keepingTmpFolfer()
+{
+  const char* envVar = std::getenv("SMESH_KEEP_TMP");
+  std::cout << "smesh_keep_tmp: " << envVar << std::endl;
+
+  if (envVar && (envVar[0] != '\0'))
+  {
+    try
+    {
+      const long long numValue = std::stoll(envVar);
+      return numValue > 0;
+    }
+    catch(const std::exception& e)
+    {
+      std::cerr << e.what() << '\n';
+    }
+  }
+
+  return false;
+};
 
 
 //=============================================================================
@@ -82,11 +104,9 @@ SMESH_ParallelMesh::~SMESH_ParallelMesh()
 //=============================================================================
 void SMESH_ParallelMesh::CreateTmpFolder()
 {
-#ifndef WIN32
   // Temporary folder that will be used by parallel computation
   tmp_folder = fs::temp_directory_path()/fs::unique_path(fs::path("SMESH_%%%%-%%%%"));
   fs::create_directories(tmp_folder);
-#endif
 }
 //
 //=============================================================================
@@ -96,11 +116,43 @@ void SMESH_ParallelMesh::CreateTmpFolder()
 //=============================================================================
 void SMESH_ParallelMesh::DeleteTmpFolder()
 {
-#ifndef WIN32
+    MESSAGE("Deleting temporary folder" << tmp_folder.string());
     fs::remove_all(tmp_folder);
-#endif
 }
 
+//=============================================================================
+/*!
+ * \brief Get the number of Threads to be used for the pool of Threads
+ */
+//=============================================================================
+int SMESH_ParallelMesh::GetPoolNbThreads()
+{
+  int nbThreads = -1;
+
+  if(_method == ParallelismMethod::MultiThread){
+    nbThreads = _NbThreads;
+  }else if( _method == ParallelismMethod::MultiNode){
+    //TODO: Check of that is the right way
+    nbThreads = std::max(_nbProc, _nbNode*_nbProcPerNode);
+  } else {
+    throw SALOME_Exception("Unknown method "+std::to_string(_method));
+  }
+
+  return nbThreads;
+}
+
+//=============================================================================
+/*!
+ * \brief Set Number of thread for multithread run
+ */
+//=============================================================================
+void SMESH_ParallelMesh::SetNbThreads(long nbThreads)
+{
+  if(nbThreads < 1)
+    throw SALOME_Exception("Number of threads should be higher than 1");
+  _NbThreads=nbThreads;
+};
+
 bool SMESH_ParallelMesh::ComputeSubMeshes(
           SMESH_Gen* gen,
           SMESH_Mesh & aMesh,