Salome HOME
Replacing ctpl by boost::thread_pool
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
index a67b6f09b0e3a32a23b2a978ba0cff6ecb544962..45a0049b45c474f3449e37da3c31261c340f93b4 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "SMESH_Gen.hxx"
 
+#include "DriverMesh.hxx"
 #include "SMDS_Mesh.hxx"
 #include "SMDS_MeshElement.hxx"
 #include "SMDS_MeshNode.hxx"
@@ -47,6 +48,8 @@
 #include <TopoDS_Iterator.hxx>
 
 #include "memoire.h"
+#include <chrono>
+#include <functional>
 
 #ifdef WIN32
   #include <windows.h>
@@ -55,6 +58,9 @@
 #include <Basics_Utils.hxx>
 
 using namespace std;
+#include <boost/filesystem.hpp>
+#include <boost/asio.hpp>
+namespace fs = boost::filesystem;
 
 // Environment variable separator
 #ifdef WIN32
@@ -168,7 +174,7 @@ const std::function<void(int,
                          bool,
                          TopTools_IndexedMapOfShape *,
                          TSetOfInt*)>
-     parallel_compute([&] (int id,
+     compute_function([&] (int id,
                           SMESH_subMesh* sm,
                           SMESH_subMesh::compute_event event,
                           SMESH_subMesh *shapeSM,
@@ -176,7 +182,7 @@ const std::function<void(int,
                           TopTools_IndexedMapOfShape *allowedSubShapes,
                           TSetOfInt*                  aShapesId) -> void
 {
-    if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
+  if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
   {
     sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
     //setCurrentSubMesh( sm );
@@ -185,6 +191,7 @@ const std::function<void(int,
     sm->SetAllowedSubShapes( nullptr );
   }
 
+  // TODO: Check if this is necessary
   if ( aShapesId )
     aShapesId->insert( sm->GetId() );
 
@@ -218,9 +225,8 @@ bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
   const int  globalAlgoDim = 100;
 
   // Pool of thread for computation
-  if (!_pool){
-    _pool = new ctpl::thread_pool(2);
-  }
+  if(aMesh.IsParallel())
+    aMesh.InitPoolThreads();
 
   SMESH_subMeshIteratorPtr smIt;
 
@@ -243,8 +249,11 @@ bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
     // ===============================================
 
     TopAbs_ShapeEnum previousShapeType = TopAbs_VERTEX;
+    int nbThreads = aMesh.GetNbThreads();
+    auto begin = std::chrono::high_resolution_clock::now();
+
+
     smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
-    std::vector<std::future<void>> pending;
     while ( smIt->more() )
     {
       SMESH_subMesh* smToCompute = smIt->next();
@@ -254,18 +263,40 @@ bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
       const TopAbs_ShapeEnum shapeType = shape.ShapeType();
       if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
         continue;
+      if(shapeType==TopAbs_FACE||shapeType==TopAbs_EDGE)
+        aMesh.SetNbThreads(0);
+      else
+        aMesh.SetNbThreads(nbThreads);
+      //DEBUG std::cout << "Shape Type" << shapeType << " previous" << previousShapeType << std::endl;
+      if ((aMesh.IsParallel()||nbThreads!=0) && shapeType != previousShapeType) {
+        // Waiting for all threads for the previous type to end
+        aMesh.wait();
+
+        std::string file_name;
+        switch(previousShapeType){
+          case TopAbs_FACE:
+            file_name = "Mesh2D.med";
+            break;
+          case TopAbs_EDGE:
+            file_name = "Mesh1D.med";
+            break;
+          case TopAbs_VERTEX:
+            file_name = "Mesh0D.med";
+            break;
+          case TopAbs_SOLID:
+          default:
+            file_name = "";
+            break;
+        }
+        if(file_name != "")
+        {
+          fs::path mesh_file = fs::path(aMesh.tmp_folder) / fs::path(file_name);
+          // TODO: change mesh name
+          export_mesh(mesh_file.string(), aMesh, "Maillage_1");
 
-      std::cout << "Shape Type" << shapeType << " previous" << previousShapeType << std::endl;
-      if (shapeType != previousShapeType) {
-        // Waiting for all thread for the previous type to end
-        for(auto it =std::begin(pending); it != std::end(pending); ++it){
-          std::cout << "Waiting" << std::endl;
-          it->wait();
         }
-        cout << "Number of segments: " << aMesh.NbEdges() << endl;
         //Resetting threaded pool info
         previousShapeType = shapeType;
-        pending.clear();
       }
 
       // check for preview dimension limitations
@@ -276,30 +307,56 @@ bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
         smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
         continue;
       }
-      pending.push_back(_pool->push(parallel_compute, smToCompute, computeEvent,
-                           shapeSM, aShapeOnly, allowedSubShapes,
-                           aShapesId));
-      std::cout << "Launched " << smToCompute << " shape type " << shapeType << std::endl;
+      if(aMesh.IsParallel())
+      {
+        std::cout << "Submitting thread function " << std::endl;
+        boost::asio::post(*(aMesh._pool), [](){std::cerr<< "In Here" << std::endl;});
+        boost::asio::post(*(aMesh._pool), std::bind(compute_function, 1, smToCompute, computeEvent,
+                          shapeSM, aShapeOnly, allowedSubShapes,
+                          aShapesId));
+      } else {
+        auto begin2 = std::chrono::high_resolution_clock::now();
+
+        compute_function(1 ,smToCompute, computeEvent,
+                         shapeSM, aShapeOnly, allowedSubShapes,
+                         aShapesId);
+
 
 
+        if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
+           ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
+          ret = false;
+        else if ( aShapesId )
+          aShapesId->insert( smToCompute->GetId() );
+      }
     }
 
-    for(auto it =std::begin(pending); it != std::end(pending); ++it){
-      it->wait();
+    // TODO: Check error handling in parallel mode
+    if(aMesh.IsParallel()){
+      // Waiting for the thread for Solids to finish
+      aMesh.wait();
     }
-    pending.clear();
-    //aMesh.GetMeshDS()->Modified();
+
+    aMesh.GetMeshDS()->Modified();
+    auto end = std::chrono::high_resolution_clock::now();
+    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
+    std::cout << "Time for All: " << elapsed.count()*1e-9 << std::endl;
+
+    // Pool of thread for computation
+    if(aMesh.IsParallel())
+      aMesh.DeletePoolThreads();
+
     return ret;
   }
   else
   {
     // ================================================================
-    // Apply algos that do NOT require discreteized boundaries
+    // Apply algos that do NOT require discretized boundaries
     // ("all-dimensional") and do NOT support sub-meshes, starting from
     // the most complex shapes and collect sub-meshes with algos that
     // DO support sub-meshes
     // ================================================================
-
+    auto begin = std::chrono::high_resolution_clock::now();
     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
 
     // map to sort sm with same dim algos according to dim of
@@ -495,7 +552,12 @@ bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
             continue;
           sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
           setCurrentSubMesh( sm );
+          auto begin = std::chrono::high_resolution_clock::now();
           sm->ComputeStateEngine( computeEvent );
+          auto end = std::chrono::high_resolution_clock::now();
+          auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
+          std::cout << "Time for seq:alldim:compute: " << elapsed.count()*1e-9 << std::endl;
+
           setCurrentSubMesh( NULL );
           sm->SetAllowedSubShapes( nullptr );
           if ( aShapesId )
@@ -508,6 +570,10 @@ bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
     // mesh the rest sub-shapes starting from vertices
     // -----------------------------------------------
     ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
+    auto end = std::chrono::high_resolution_clock::now();
+    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
+    std::cout << "Time for All: " << elapsed.count()*1e-9 << std::endl;
+
   }
 
   MEMOSTAT;