C++ mpi module for stochmagnet_main Package
MPI_Environment.h
1 #ifndef MPI_Environment_H
2 #define MPI_Environment_H
3 
4 //inherited class object
5 #include "MPI_Object.h"
6 
7 //MPI types header
8 #include "MPI_Type.h"
9 
36 class MPI_Environment : public MPI_Object {
37 
38  //attributes
39 private :
40 
41  //name of the environment
42  tMPIComm mWorld;
43  //number of cores of the nevironments
44  tMPICoreId mCoresNumber;
45  //core id
46  tMPICoreId mCoreId;
47 
48  //root environment
49  tBoolean mIsRootEnvironment;
50 
51 protected:
52  // CONSTRUCTORS
53 
57 
58  }
59 
72  MPI_Environment(int& argc,char * argv[],const tBoolean& isOpenMPEnabled) {
73  if (isOpenMPEnabled) {
74  int provided, required=MPI_THREAD_FUNNELED;
75  MPI_Init_thread(&argc,&argv,required,&provided);
76  } else {
77  MPI_Init(&argc,&argv);
78  }
79  mWorld=MPI_COMM_WORLD;
80  mIsRootEnvironment=false;
81  }
82 
83  // DESTRUCTORS
86  virtual ~MPI_Environment(void) {
87 
88  if (mWorld==MPI_COMM_WORLD) {
89  MPI_Finalize();
90  } else if (mWorld!=MPI_COMM_NULL) {
91  MPI_Comm_free(&mWorld);
92  if (mIsRootEnvironment) {
93  MPI_Finalize();
94  }
95  }
96 
97  }
98 
99 protected :
103  virtual void initialize() {
104  MPI_Comm_size(mWorld,&mCoresNumber);
105  MPI_Comm_rank(mWorld,&mCoreId);
106  }
107 
108 public:
109 
116  inline static CORE_UniquePointer<MPI_Environment> New(int argc,char *argv[],const tBoolean& isOpenMPEnabled) {
117  CORE_UniquePointer<MPI_Environment> p(new MPI_Environment(argc,argv,isOpenMPEnabled),
119  p->initialize();
120  return p;
121  };
127  inline static CORE_UniquePointer<MPI_Environment> New(int argc,char *argv[]) {
128  return New(argc,argv,false);
129  };
130 
131 
132  //memory management
133  //=================
136  virtual tMemSize getMemorySize() const override {
137  return sizeof(*this)+getContentsMemorySize();
138  }
139 
148  virtual tMemSize getContentsMemorySize() const override {
149  tMemSize mem=MPI_Object::getContentsMemorySize();
150  return mem;
151  }
152 
153 protected:
156  inline void setIsRootEnvironment(const tBoolean& isRoot) {
157  mIsRootEnvironment=isRoot;
158  }
159 public:
160  //accessor methods
161  //----------------
162 
165  inline const tMPIComm& getWorld() const {
166  return mWorld;
167  }
168 
169 //protected:
172  inline tMPIComm& getWorld() {
173  return mWorld;
174  }
175 public:
176 
180  inline const tMPICoreId& getCoresNumber() const {
181  return mCoresNumber;
182  }
186  inline static void GetCoresNumber(tMPICoreId& coresNumber) {
187  MPI_Comm_size(MPI_COMM_WORLD,&coresNumber);
188  }
193  inline static void GetCoresNumber(const MPI_Environment& env,tMPICoreId& coresNumber) {
194  MPI_Comm_size(env.getWorld(),&coresNumber);
195  }
196 
200  inline const tMPICoreId& getCoreId() const {
201  return mCoreId;
202  }
203 
204 
205 public:
206 
210  inline static void GetCoreId(tMPICoreId& id) {
211  MPI_Comm_rank(MPI_COMM_WORLD,&id);
212  }
217  inline static void GetCoreId(const MPI_Environment& env,tMPICoreId& id) {
218  MPI_Comm_rank(env.getWorld(),&id);
219  }
220 
221 
222  //time management methods
223  //-----------------------
227  inline static tReal GetTime() {
228  return MPI_Wtime();
229  }
230 
231  //process killer methods
232  //-----------------------
233 
234 
240  inline static tMPIError Abort(const MPI_Environment& env,const tMPIError& errCode) {
241  return MPI_Abort(env.getWorld(),errCode);
242  }
247  inline static tMPIError Abort(const tMPIError& errCode) {
248  return MPI_Abort(MPI_COMM_WORLD,errCode);
249  }
250 
255  inline tMPIError abort(const tMPIError& errCode) const {
256  return MPI_Abort(mWorld,errCode);
257  }
258 
259  //syncronize methods
260  //==================
265  inline static tBoolean Synchronize(const MPI_Environment& env) {
266  //std::cout<<"begin MPI_WorldToWorldMessage::Synchronize(env) coreId="<<env.getCoreId()<<"\n";
267  return (MPI_Barrier(env.getWorld())==MPI_SUCCESS);
268  //std::cout<<"end MPI_WorldToWorldMessage::Synchronize(env) coreId="<<env.getCoreId()<<"\n";
269  }
272  inline static tBoolean Synchronize() {
273  //std::cout<<"begin MPI_WorldToWorldMessage::Synchronize()"<<"\n";
274  return (MPI_Barrier(MPI_COMM_WORLD)==MPI_SUCCESS);
275  //std::cout<<"end MPI_WorldToWorldMessage::Synchronize()"<<"\n";
276  }
277 
280  inline tBoolean synchronize() const {
281  //std::cout<<"begin MPI_WorldToWorldMessage::Synchronize()"<<"\n";
282  return (MPI_Barrier(getWorld())==MPI_SUCCESS);
283  //std::cout<<"end MPI_WorldToWorldMessage::Synchronize()"<<"\n";
284  }
285 
286  //Wait methods
287  //============
288 
293  inline static tBoolean Wait(tMPIRequest& request,tMPIStatus& status) {
294  return (MPI_Wait(&request,&status)==MPI_SUCCESS);
295  }
299  static inline tBoolean Wait(tMPIRequest& request) {
300  return (MPI_Wait(&request,MPI_STATUS_IGNORE)==MPI_SUCCESS);
301  }
302 
308  inline static tBoolean WaitAll(const tMPICount& nRequests,tMPIRequest requests[],tMPIStatus statuses[]) {
309  return (MPI_Waitall(nRequests,requests,statuses)==MPI_SUCCESS);
310  }
311 
316  static inline tBoolean WaitAll(std::valarray<tMPIRequest>& requests) {
317  return (MPI_Waitall(requests.size(),
318  &requests[0],
319  MPI_STATUSES_IGNORE)==MPI_SUCCESS);
320  }
326  template<size_t D>
327  static inline tBoolean WaitAll(std::array<tMPIRequest,D>& requests) {
328  return (MPI_Waitall(D,
329  requests.data(),
330  MPI_STATUSES_IGNORE)==MPI_SUCCESS);
331  }
337  static inline tBoolean WaitAll(const tMPICount& nRequests,tMPIRequest* requests) {
338  return (MPI_Waitall(nRequests,
339  requests,
340  MPI_STATUSES_IGNORE)==MPI_SUCCESS);
341  }
342 
343  //test messages methods
344  //======================
345 
351  static inline tBoolean IsFinished(tMPIRequest& request,tMPIInteger& isFinished) {
352  MPI_Test(&request,&isFinished,MPI_STATUS_IGNORE);
353  return (isFinished==1);
354  }
360  static inline tBoolean IsAllFinished(std::valarray<tMPIRequest>& requests,tMPIInteger& isFinished) {
361  tBoolean isAllFinished=true;
362  for(auto& req:requests) {
363  MPI_Test(&req,&isFinished,MPI_STATUS_IGNORE);
364  isAllFinished=isAllFinished && (isFinished==1);
365  }
366  return isAllFinished;
367  }
368 
374  inline static tBoolean IsFinished(tMPIRequest& request,tMPIStatus& status,tMPIInteger& isFinished) {
375  MPI_Test(&request,&isFinished,&status);
376  return (isFinished==1);
377  }
386  inline tBoolean isMessageAvaliable(tMPICoreId& source,
387  tMPITag& tag,
388  tMPIBoolean& isAvailable,
389  tMPIStatus& status) const {
390  return (IsMessageAvaliable(*this,source,tag,isAvailable,status));
391  }
392 
401  static inline tBoolean IsMessageAvaliable(const MPI_Environment& env,
402  tMPICoreId& source,
403  tMPITag& tag,
404  tMPIBoolean& isAvailable,
405  tMPIStatus& status) {
406 
407  MPI_Iprobe(MPI_ANY_SOURCE,MPI_ANY_TAG,env.getWorld(),&isAvailable,&status);
408  if (isAvailable==1) {
409  source=status.MPI_SOURCE;
410  tag=status.MPI_TAG;
411  return true;
412  }
413  return false;
414  }
422  static inline tBoolean IsMessageAvaliable(const MPI_Environment& env,
423  const tMPITag& tag,
424  const tMPICoreId& source,
425  tMPIBoolean& isAvailable) {
426  return (MPI_Iprobe(source,tag,env.getWorld(),&isAvailable,MPI_STATUS_IGNORE)==MPI_SUCCESS);
427  }
428 
437  static inline tBoolean IsTagMessageAvaliable(const MPI_Environment& env,
438  const tMPITag& tag,
439  tMPICoreId& source,
440  tMPIBoolean& isAvailable,
441  tMPIStatus& status) {
442  MPI_Iprobe(MPI_ANY_SOURCE,tag,env.getWorld(),&isAvailable,&status);
443  if (isAvailable==1) {
444  source=status.MPI_SOURCE;
445  return true;
446  }
447  return false;
448  }
449 
450 
451 
456  template<typename T>
457  inline static tBoolean GetCount(const tMPIStatus& status,tMPICount& nElements) {
458  return (MPI_Get_count(&status,MPI_Type::GetPrimaryType<T>(),&nElements)==MPI_SUCCESS);
459  }
460 
461 
462 
463 };
464 
465 
466 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
This class is a Environment class to define MPI world.
Definition: MPI_Environment.h:36
static tBoolean IsTagMessageAvaliable(const MPI_Environment &env, const tMPITag &tag, tMPICoreId &source, tMPIBoolean &isAvailable, tMPIStatus &status)
test if the message with tag is avalaible
Definition: MPI_Environment.h:437
const tMPICoreId & getCoreId() const
get the id of the current process of this environment
Definition: MPI_Environment.h:200
static void GetCoreId(tMPICoreId &id)
get the id of the current process of common environment
Definition: MPI_Environment.h:210
tMPIComm & getWorld()
get the world of the environment for reading
Definition: MPI_Environment.h:172
tBoolean isMessageAvaliable(tMPICoreId &source, tMPITag &tag, tMPIBoolean &isAvailable, tMPIStatus &status) const
test if the message is available frm the environment this
Definition: MPI_Environment.h:386
const tMPICoreId & getCoresNumber() const
get the number of cores of this environment of common environment
Definition: MPI_Environment.h:180
static tBoolean Wait(tMPIRequest &request)
wait for the request is finished
Definition: MPI_Environment.h:299
static tBoolean Synchronize()
wait that all the cores has finished her job
Definition: MPI_Environment.h:272
static CORE_UniquePointer< MPI_Environment > New(int argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a test class
Definition: MPI_Environment.h:116
void setIsRootEnvironment(const tBoolean &isRoot)
set true when the environemnt created is a root one but not a common world
Definition: MPI_Environment.h:156
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: MPI_Environment.h:136
tMPIError abort(const tMPIError &errCode) const
abort the process on all cores with the error code
Definition: MPI_Environment.h:255
static tBoolean WaitAll(std::array< tMPIRequest, D > &requests)
wait for all the requests are finished
Definition: MPI_Environment.h:327
static CORE_UniquePointer< MPI_Environment > New(int argc, char *argv[])
create a test class
Definition: MPI_Environment.h:127
static void GetCoresNumber(tMPICoreId &coresNumber)
get the number of cores
Definition: MPI_Environment.h:186
MPI_Environment(int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a root environment
Definition: MPI_Environment.h:72
static tBoolean Wait(tMPIRequest &request, tMPIStatus &status)
wait the end the the communication by blocking
Definition: MPI_Environment.h:293
static tBoolean IsMessageAvaliable(const MPI_Environment &env, const tMPITag &tag, const tMPICoreId &source, tMPIBoolean &isAvailable)
test if the message with tag from source is available
Definition: MPI_Environment.h:422
static tBoolean WaitAll(const tMPICount &nRequests, tMPIRequest requests[], tMPIStatus statuses[])
wait the end the the communication by blocking
Definition: MPI_Environment.h:308
static tBoolean IsMessageAvaliable(const MPI_Environment &env, tMPICoreId &source, tMPITag &tag, tMPIBoolean &isAvailable, tMPIStatus &status)
test if a message is available form any tag and any source
Definition: MPI_Environment.h:401
static tBoolean IsFinished(tMPIRequest &request, tMPIStatus &status, tMPIInteger &isFinished)
return true if the communication is finished
Definition: MPI_Environment.h:374
static tBoolean Synchronize(const MPI_Environment &env)
wait that all the core of an environment has finished her job
Definition: MPI_Environment.h:265
static tMPIError Abort(const MPI_Environment &env, const tMPIError &errCode)
abort the process on all cores of the environment with the eroor code
Definition: MPI_Environment.h:240
virtual ~MPI_Environment(void)
destroy
Definition: MPI_Environment.h:86
static tReal GetTime()
get the time
Definition: MPI_Environment.h:227
static void GetCoreId(const MPI_Environment &env, tMPICoreId &id)
get the id of the current process of common environment
Definition: MPI_Environment.h:217
static tBoolean IsAllFinished(std::valarray< tMPIRequest > &requests, tMPIInteger &isFinished)
test if the requets is finished
Definition: MPI_Environment.h:360
tBoolean synchronize() const
wait that all the cores has finished her job
Definition: MPI_Environment.h:280
static void GetCoresNumber(const MPI_Environment &env, tMPICoreId &coresNumber)
get the number of cores
Definition: MPI_Environment.h:193
const tMPIComm & getWorld() const
get the world of the environment for reading
Definition: MPI_Environment.h:165
static tBoolean WaitAll(std::valarray< tMPIRequest > &requests)
wait for all the requests are finished
Definition: MPI_Environment.h:316
static tBoolean GetCount(const tMPIStatus &status, tMPICount &nElements)
get the number of element of the message
Definition: MPI_Environment.h:457
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_Environment.h:148
MPI_Environment()
create a root environment
Definition: MPI_Environment.h:56
virtual void initialize()
initialize the environment Sets the cores number and the core id of the environment
Definition: MPI_Environment.h:103
static tBoolean WaitAll(const tMPICount &nRequests, tMPIRequest *requests)
wait for all the requests are finished
Definition: MPI_Environment.h:337
static tBoolean IsFinished(tMPIRequest &request, tMPIInteger &isFinished)
test if the requets is finished
Definition: MPI_Environment.h:351
static tMPIError Abort(const tMPIError &errCode)
abort the process on all cores with the error code
Definition: MPI_Environment.h:247
This class is a base class of E-MicromM core package.
Definition: MPI_Object.h:32
This class is a Test class for EMicroM package which uses the module tests.
Definition: MPI_Test.h:15