Salome HOME
70cf77a355377716a1058a17e107661b1afdd799
[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   int GetParallelElement() override;
85   int GetDumpElement();
86
87   // Parallelims paramaters
88   int GetParallelismMethod() {return _method;};
89   void SetParallelismMethod(int aMethod) {_method = aMethod;};
90
91   int GetParallelismDimension() {return _paraDim;};
92   void SetParallelismDimension(int aDim) {_paraDim = aDim;};
93
94   // Mutlithreading parameters
95   int GetNbThreads() {return _NbThreads;};
96   void SetNbThreads(long nbThreads);
97
98   // Multinode parameters
99   std::string GetResource() {return _resource;};
100   void SetResource(std::string aResource) {_resource = aResource;};
101
102   int GetNbProc() {return _nbProc;};
103   void SetNbProc(long nbProc) {_nbProc = nbProc;};
104
105   int GetNbProcPerNode() {return _nbProcPerNode;};
106   void SetNbProcPerNode(long nbProcPerNodes) {_nbProcPerNode = nbProcPerNodes;};
107
108   int GetNbNode() {return _nbNode;};
109   void SetNbNode(long nbNodes) {_nbNode = nbNodes;};
110
111   std::string GetWcKey() {return _wcKey;};
112   void SetWcKey(std::string wcKey) {_wcKey = wcKey;};
113
114   std::string GetWalltime() {return _walltime;};
115   void SetWalltime(std::string walltime) {_walltime = walltime;};
116
117   // Parallel computation
118   bool ComputeSubMeshes(
119             SMESH_Gen* gen,
120             SMESH_Mesh & aMesh,
121             const TopoDS_Shape & aShape,
122             const ::MeshDimension       aDim,
123             TSetOfInt*                  aShapesId /*=0*/,
124             TopTools_IndexedMapOfShape* allowedSubShapes,
125             SMESH_subMesh::compute_event &computeEvent,
126             const bool includeSelf,
127             const bool complexShapeFirst,
128             const bool   aShapeOnly) override;
129
130  protected:
131   SMESH_ParallelMesh():SMESH_Mesh() {};
132   SMESH_ParallelMesh(const SMESH_ParallelMesh& aMesh):SMESH_Mesh(aMesh) {};
133  private:
134   // Mutex for multhitreading write in SMESH_Mesh
135 #ifndef WIN32
136   boost::mutex _my_lock;
137   // thread pool for computation
138   boost::asio::thread_pool *     _pool = nullptr;
139 #endif
140   boost::filesystem::path tmp_folder;
141   int _method = ParallelismMethod::MultiThread;
142   int _paraDim = 3;
143
144   int _NbThreads = std::thread::hardware_concurrency();
145
146   int _nbProc = 1;
147   int _nbProcPerNode = 1;
148   int _nbNode = 1;
149   std::string _resource = "";
150   std::string _wcKey = "P11N0:SALOME";
151   std::string _walltime = "01:00:00";
152 };
153 #endif