Salome HOME
fixed bos#37786: SMESH_MGAdaptTests_without_session random failures
[modules/smesh.git] / src / SMESH / SMESH_ParallelMesh.hxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SMESH_ParallelMesh.hxx
24 //  Author : Yoann AUDOUIN, EDF
25 //  Module : SMESH
26 //
27 #ifndef _SMESH_PARALLELMESH_HXX_
28 #define _SMESH_PARALLELMESH_HXX_
29
30 #include "SMESH_Mesh.hxx"
31
32 #ifndef WIN32
33 #include <boost/asio.hpp>
34 #endif
35
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_subMesh.hxx"
38 #ifdef WIN32
39 #include <thread>
40 #include <boost/filesystem.hpp>
41 #endif
42 enum ParallelismMethod {MultiThread, MultiNode};
43
44 class SMESH_EXPORT SMESH_ParallelMesh: public SMESH_Mesh
45 {
46  public:
47   SMESH_ParallelMesh(int               theLocalId,
48                        SMESH_Gen*        theGen,
49                        bool              theIsEmbeddedMode,
50                        SMESHDS_Document* theDocument);
51
52   ~SMESH_ParallelMesh();
53
54   // Locking mechanism
55   #ifndef WIN32
56   void Lock() override {_my_lock.lock();};
57   void Unlock() override {_my_lock.unlock();};
58   // We need to recreate the pool afterthe join
59   void wait() override {_pool->join(); DeletePoolThreads(); InitPoolThreads(); };
60   #endif
61
62   // Thread Pool
63 #ifndef WIN32
64   void InitPoolThreads() {_pool = new boost::asio::thread_pool(GetPoolNbThreads());};
65   boost::asio::thread_pool* GetPool() {return _pool;};
66   void DeletePoolThreads() {delete _pool;};
67 #else
68   void InitPoolThreads() {};
69   void* GetPool() {return NULL;};
70   void DeletePoolThreads(){};
71 #endif
72
73   int GetPoolNbThreads();
74
75   // Temporary folder
76   bool keepingTmpFolfer();
77   void CreateTmpFolder();
78   void DeleteTmpFolder();
79   boost::filesystem::path GetTmpFolder() {return tmp_folder;};
80   void cleanup();
81
82   //
83   bool IsParallel() override {return true;};
84
85   // Parallelims paramaters
86   int GetParallelismMethod() {return _method;};
87   void SetParallelismMethod(int aMethod) {_method = aMethod;};
88
89   // Mutlithreading parameters
90   int GetNbThreads() {return _NbThreads;};
91   void SetNbThreads(long nbThreads);
92
93   // Multinode parameters
94   std::string GetResource() {return _resource;};
95   void SetResource(std::string aResource) {_resource = aResource;};
96
97   int GetNbProc() {return _nbProc;};
98   void SetNbProc(long nbProc) {_nbProc = nbProc;};
99
100   int GetNbProcPerNode() {return _nbProcPerNode;};
101   void SetNbProcPerNode(long nbProcPerNodes) {_nbProcPerNode = nbProcPerNodes;};
102
103   int GetNbNode() {return _nbNode;};
104   void SetNbNode(long nbNodes) {_nbNode = nbNodes;};
105
106   std::string GetWcKey() {return _wcKey;};
107   void SetWcKey(std::string wcKey) {_wcKey = wcKey;};
108
109   std::string GetWalltime() {return _walltime;};
110   void SetWalltime(std::string walltime) {_walltime = walltime;};
111
112   // Parallel computation
113   bool ComputeSubMeshes(
114             SMESH_Gen* gen,
115             SMESH_Mesh & aMesh,
116             const TopoDS_Shape & aShape,
117             const ::MeshDimension       aDim,
118             TSetOfInt*                  aShapesId /*=0*/,
119             TopTools_IndexedMapOfShape* allowedSubShapes,
120             SMESH_subMesh::compute_event &computeEvent,
121             const bool includeSelf,
122             const bool complexShapeFirst,
123             const bool   aShapeOnly) override;
124
125  protected:
126   SMESH_ParallelMesh():SMESH_Mesh() {};
127   SMESH_ParallelMesh(const SMESH_ParallelMesh& aMesh):SMESH_Mesh(aMesh) {};
128  private:
129   // Mutex for multhitreading write in SMESH_Mesh
130 #ifndef WIN32
131   boost::mutex _my_lock;
132   // thread pool for computation
133   boost::asio::thread_pool *     _pool = nullptr;
134 #endif
135   boost::filesystem::path tmp_folder;
136   int _method = ParallelismMethod::MultiThread;
137
138   int _NbThreads = std::thread::hardware_concurrency();
139
140   int _nbProc = 1;
141   int _nbProcPerNode = 1;
142   int _nbNode = 1;
143   std::string _resource = "";
144   std::string _wcKey = "P11N0:SALOME";
145   std::string _walltime = "01:00:00";
146 };
147 #endif