C++ mpi module for stochmagnet_main Package
MPI_Type.h
1 #ifndef MPI_Type_H
2 #define MPI_Type_H
3 
4 //mpi object
5 #include "MPI_Object.h"
6 
7 
8 //MPI data type
9 #define tMPIType MPI_Datatype
10 
11 //MPI octet type
12 #define tMPIByte MPI_Aint
13 
14 //MPI count type of a certain type
15 #define tMPICount int
16 
17 //MPI flag type as a flag type
18 #define tMPITag int
19 
20 //MPI core id type
21 #define tMPICoreId int
22 
23 //MPI integer
24 #define tMPIInteger int
25 #define tMPIIndex int
26 
27 //MPI boolean
28 #define tMPIBoolean int
29 
30 //index of nullcore
31 #define NULL_CORE MPI_PROC_NULL
32 
33 //MPI error type
34 #define tMPIError int
35 #define tMPIErrorMode MPI_Errhandler
36 
37 //MPI communicator type
38 #define tMPIComm MPI_Comm
39 
40 //view type
41 #define tMPIView MPI_Datatype
42 
43 //MPI status of message
44 #define tMPIStatus MPI_Status
45 #define tMPIRequest MPI_Request
46 
47 //MPI arithmetic operation
48 #define tMPIOperation MPI_Op
49 
50 //IO Mode
51 #define tMPIIOMode int
52 
53 //IO file
54 #define tMPIIOFile MPI_File
55 
56 //IO file position
57 #define tMPIIOIndex MPI_Offset
58 
67 class MPI_Type : public MPI_Object {
68 
69 
70  // ATTRIBUTES
71 
72 public:
73 
74 
75 
76 private:
77  tMPIType mType;
78  tBoolean mIsRegistered;
79 public:
80  // METHODS
81 
82  // CONSTRUCTORS
83 
86  MPI_Type(void) {
87  mIsRegistered=false;
88  }
89 
90 
91 
92  // DESTRUCTORS
93 
94 
97  virtual ~MPI_Type(void) {
98  unregisterType();
99 
100  }
101 
102 public:
103  //memory management
104  //=================
107  virtual tMemSize getMemorySize() const override {
108  return sizeof(*this)+getContentsMemorySize();
109  }
110 
119  virtual tMemSize getContentsMemorySize() const override {
120  tMemSize mem=MPI_Object::getContentsMemorySize();
121  return mem;
122  }
123 
124 private:
125  // SET methods
126 
129  inline void registerType() {
130  if (mIsRegistered) unregisterType();
131  MPI_Type::Register(mType);
132  mIsRegistered=true;
133  }
136  inline void unregisterType() {
137  if (mIsRegistered) MPI_Type::Unregister(mType);
138  mIsRegistered=false;
139  }
140 public:
141 
142  // GET methods
143 
151  template<typename T>
152  inline tBoolean createContiguousType(const tMPICount& N) {
153  unregisterType();
154  tBoolean ret=(CreateContiguousType<T>(N,mType)==MPI_SUCCESS);
155  registerType();
156  return ret;
157  }
172  template<typename T>
173  inline tBoolean createConstantStepType(const tMPICount& N,
174  const tMPICount& elementSize,
175  const tMPICount& step) {
176  unregisterType();
177  tBoolean ret=(CreateConstantStepType<T>(N,elementSize,step,mType)==MPI_SUCCESS);
178  registerType();
179  return ret;
180  }
191  inline tBoolean createConstantOctetStepType(const tMPICount& nElements,
192  const tMPICount& elementsNumber,
193  const tMPIByte& stepInOctets,
194  const tMPIType& oldType) {
195  unregisterType();
196  tBoolean ret=(CreateConstantOctetStepType(nElements,elementsNumber,stepInOctets,oldType,mType)==MPI_SUCCESS);
197  registerType();
198  return ret;
199  }
200 
201 
214  template<typename T>
215  inline tBoolean createVariableStepType(const tMPICount& nElements,
216  const std::valarray<tMPICount>& elementSizes,
217  const std::valarray<tMPICount>& elementIndices) {
218  unregisterType();
219  tBoolean ret=(CreateVariableStepType<T>(nElements,elementSizes,elementIndices,mType)==MPI_SUCCESS);
220  registerType();
221  return ret;
222  }
223 
236  inline tBoolean createVariableOctetStepType(const tMPICount& nElements,
237  const std::valarray<tMPICount>& elementSizes,
238  const std::valarray<tMPIByte>& elementOctetIndices,
239  const tMPIType& oldType) {
240  unregisterType();
241  tBoolean ret=(CreateVariableOctetStepType(nElements,elementSizes,elementOctetIndices,oldType,mType)==MPI_SUCCESS);
242  registerType();
243  return ret;
244  }
245 
246 
256  inline tBoolean createStructType(const tMPICount& nElements,
257  const std::valarray<tMPICount>& elementSizes,
258  const std::valarray<tMPIByte>& elementOctetIndices,
259  const std::valarray<tMPIType>& elementTypes) {
260  unregisterType();
261  tBoolean ret=(CreateStructType(nElements,elementSizes,elementOctetIndices,elementTypes,mType)==MPI_SUCCESS);
262  registerType();
263  return ret;
264 
265  }
266 
267 
268 
269 public:
270 
274  inline const tMPIType& getType() const {
275  return mType;
276  }
277 
278 
279 
280  //primary type manupulation
281  //==========================
282 
283 public:
288  template<class T>
289  inline static tMPIType GetPrimaryType() {
290  return MPI_Type::GetPrimaryType(functions_type::getTypeName<T>());
291  }
292 
297  inline static tMPIType GetPrimaryType(const tString& typeName) {
298  tMPIType mpiType=0;
299 
300  switch(typeName[0]) {
301  case 'b'://boolean
302  mpiType=MPI_LOGICAL;
303  break;
304  case 'c'://char
305  mpiType=MPI_CHAR;
306  break;
307  case 's'://short
308  mpiType=MPI_SHORT;
309  break;
310  case 'i'://int
311  mpiType=MPI_INT;
312  break;
313  case 'f'://float
314  mpiType=MPI_FLOAT;
315  break;
316  case 'd'://double
317  mpiType=MPI_DOUBLE;
318  break;
319  case 'l'://long
320  switch(typeName[5]) {
321  case 'i'://int
322  mpiType=MPI_LONG;
323  break;
324  case 'l'://long
325  mpiType=MPI_LONG_LONG;
326  break;
327  case 'd'://double
328  mpiType=MPI_LONG_DOUBLE;
329  break;
330  }
331  break;
332  case 'u'://unsigned
333  switch(typeName[9]) {
334  case 'c'://char
335  mpiType=MPI_UNSIGNED_CHAR;
336  break;
337  case 's'://short
338  mpiType=MPI_UNSIGNED_SHORT;
339  break;
340  case 'i'://int
341  mpiType=MPI_UNSIGNED;
342  break;
343  case 'l'://long
344  mpiType=(typeName.length()>14)?MPI_UNSIGNED_LONG_LONG:MPI_UNSIGNED_LONG;
345  break;
346  }
347  break;
348  default:
349  throw CORE_Exception("mpi/MPI_Object",
350  "GetVTKType("+typeName+")",
351  "unknown type");
352  }
353 
354  return mpiType;
355  }
356 
357 
358  //new type creator
359  //==================
368  template<typename T>
369  inline static tMPIError CreateContiguousType(const tMPICount& N,tMPIType& newType) {
370  return MPI_Type_contiguous(N,MPI_Type::GetPrimaryType<T>(),&newType);
371  }
380  inline static tMPIError CreateContiguousType(const tMPICount& N,const tMPIType& oldType,tMPIType& newType) {
381  return MPI_Type_contiguous(N,oldType,&newType);
382  }
383 
384 
400  template<typename T>
401  inline static tMPIError CreateConstantStepType(const tMPICount& nElements,
402  const tMPICount& elementSize,const tMPICount& step,
403  tMPIType& newType) {
404  return MPI_Type_vector(nElements,elementSize,step,MPI_Type::GetPrimaryType<T>(),&newType);
405  }
416  inline static tMPIError CreateConstantStepType(const tMPICount& nElements,
417  const tMPICount& elementSize,const tMPICount& step,
418  const tMPIType& oldType,
419  tMPIType& newType) {
420  return MPI_Type_vector(nElements,elementSize,step,oldType,&newType);
421  }
422 
434  inline static tMPIError CreateConstantOctetStepType(const tMPICount& nElements,
435  const tMPICount& elementsNumber,
436  const tMPIByte& stepInOctets,
437  const tMPIType& oldType,
438  tMPIType& newType) {
439  return MPI_Type_create_hvector(nElements,elementsNumber,stepInOctets,oldType,&newType);
440  }
441 
442 
443 
457  template<typename T>
458  inline static tMPIError CreateVariableStepType(const tMPICount& nElements,
459  const std::valarray<tMPICount>& elementSizes,
460  const std::valarray<tMPICount>& elementIndices,
461  tMPIType& newType) {
462  return MPI_Type_indexed(nElements,
463  &elementSizes[0],
464  &elementIndices[0],
465  MPI_Type::GetPrimaryType<T>(),
466  &newType);
467  }
468 
482  inline static tMPIError CreateVariableOctetStepType(const tMPICount& nElements,
483  const std::valarray<tMPICount>& elementSizes,
484  const std::valarray<tMPIByte>& elementOctetIndices,
485  const tMPIType& oldType,
486  tMPIType& newType) {
487  return MPI_Type_create_hindexed(nElements,&elementSizes[0],&elementOctetIndices[0],oldType,&newType);
488  }
489 
490 
501  inline static tMPIError CreateStructType(const tMPICount& nElements,
502  const std::valarray<tMPICount>& elementSizes,
503  const std::valarray<tMPIByte>& elementOctetIndices,
504  const std::valarray<tMPIType>& elementTypes,
505  tMPIType& newType) {
506  return MPI_Type_create_struct(nElements,&elementSizes[0],&elementOctetIndices[0],&elementTypes[0],&newType);
507 
508  }
509 
517  template<typename T,tUCInt N>
518  static void CreateFaceTypes(const tUCInt& dim,
519  const std::array<tInteger,N>& Ns,
520  std::array<MPI_Type,N>& faceTypes) {
521 
522  //value=d+dim*(i+j*Nx+k*Nx*Ny)
523 
524  //size of real
525  int mpiSizeOfT;
526  MPI_Type::GetSize<T>(mpiSizeOfT);
527 
528  //create the type face
529  switch(Ns.size()) {
530  case 1://DIM=1
531  faceTypes[0].template createContiguousType<T>(dim);
532  break;
533  case 2://DIM=2
534  //face normal to y : tILine
535  faceTypes[1].template createContiguousType<T>(dim*Ns[0]);
536  //face normal to x : tJLine
537  faceTypes[0].template createConstantStepType<T>(Ns[1],dim,Ns[0]*dim);//Ny elements dy=Nx x dim
538  break;
539  case 3://DIM=3
540  //index = dim * (i+Nx.j+Nx.Ny.k)
541  //face normal to z
542  faceTypes[2].template createContiguousType<T>(dim*Ns[0]*Ns[1]);//Nx.Ny.dim elements
543 
544  //method 1:
545  //tMPIType tKLine;//Kline
546  //MPI_Type::CreateConstantStepType<T>(Ns[2],dim,Ns[0]*Ns[1]*dim,tKLine);//Nz elements dz=Nx*Ny
547  tMPIType tILine;//Iline
548  MPI_Type::CreateContiguousType<tReal>(Ns[0]*dim,tILine);//1 element, size=Ns[0]x dim contiguous elements
549  tMPIType tJLine;//JLine
550  MPI_Type::CreateConstantStepType<tReal>(Ns[1],dim,Ns[0]*dim,tJLine);//Ns[1] elements of size dim x Ns[1] dy=Ns[0]*dim
551 
552  //face normal to y : i in [0,Nx[ k in [0,Nz[ index=dim*i+dim*Nx.Ny.k (+ dim * j * Nx)
553  //faceTypes[1].template createConstantOctetStepType(Ns[0],1,mpiSizeOfT,tKLine);//Nx x KLine(), dx=1
554  //Nz x ILine, dz=(Nx).(Ny).dim.sizeof(T) size=Ns[2]*dim*Ns[0]
555  faceTypes[1].createConstantOctetStepType(Ns[2],1,dim*Ns[0]*Ns[1]*mpiSizeOfT,tILine);
556  //face normal to x : j in [0,Ny[ k in [0,Nz[ index=dim*Nx*j + dim*Nx.Ny.k (dim*i)
557  //Nz x JLine, dz=(Nx).(Ny)*dim.sizeof(T) size=Ns[2]*dim*Ns[1]
558  faceTypes[0].createConstantOctetStepType(Ns[2],1,dim*Ns[0]*Ns[1]*mpiSizeOfT,tJLine);
559 
560 
561  break;
562  }
563  }
564 
565 
566  //type modification
567  //================
568 
578  inline static tMPIError Resize(const tMPIType dataType,
579  const tMPIByte& lb,
580  const tMPIByte& extent,
581  tMPIType& newDataType) {
582  return MPI_Type_create_resized(dataType,lb,extent,&newDataType);
583  }
584 
585 
586  //committed new type
587  //===================
588 
592  inline static tMPIError Register(tMPIType& type) {
593  return MPI_Type_commit(&type);
594  }
598  inline static tMPIError Unregister(tMPIType& type) {
599  return MPI_Type_free(&type);
600  }
601 
602 
603  //type analysis
604  //=============
605 
606 
612  template<typename T>
613  inline static tMPIError GetSize(int& mpiSize) {
614  return MPI_Type_size(MPI_Type::GetPrimaryType<T>(),&mpiSize);
615  }
621  inline static tMPIError GetSize(const tMPIType& mpiType,int& mpiSize) {
622  return MPI_Type_size(mpiType,&mpiSize);
623  }
630  inline static tMPIError GetExtent(const tMPIType& mpiType,tMPIByte& start,tMPIByte& mpiSize) {
631  return MPI_Type_get_extent(mpiType,&start,&mpiSize);
632  }
633 
639  template<typename T>
640  inline static tMPIError GetExtent(tMPIByte& start,tMPIByte& mpiSize) {
641  return MPI_Type_get_extent(GetPrimaryType<T>(),&start,&mpiSize);
642  }
643 
644 
645 };
646 
647 
648 
649 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
This class is a base class of E-MicromM core package.
Definition: MPI_Object.h:32
This class is a base class of E-MicromM core package.
Definition: MPI_Type.h:67
static tMPIError CreateContiguousType(const tMPICount &N, const tMPIType &oldType, tMPIType &newType)
create a MPI array type of size N
Definition: MPI_Type.h:380
tBoolean createVariableStepType(const tMPICount &nElements, const std::valarray< tMPICount > &elementSizes, const std::valarray< tMPICount > &elementIndices)
create a variable step data type
Definition: MPI_Type.h:215
static tMPIError Unregister(tMPIType &type)
unregister the type
Definition: MPI_Type.h:598
static tMPIError CreateVariableStepType(const tMPICount &nElements, const std::valarray< tMPICount > &elementSizes, const std::valarray< tMPICount > &elementIndices, tMPIType &newType)
create a variable step data type
Definition: MPI_Type.h:458
static tMPIError CreateContiguousType(const tMPICount &N, tMPIType &newType)
create a MPI array type of size N
Definition: MPI_Type.h:369
tBoolean createConstantStepType(const tMPICount &N, const tMPICount &elementSize, const tMPICount &step)
create a constant step data type
Definition: MPI_Type.h:173
tBoolean createContiguousType(const tMPICount &N)
create a MPI array type of size N
Definition: MPI_Type.h:152
static tMPIError CreateConstantStepType(const tMPICount &nElements, const tMPICount &elementSize, const tMPICount &step, tMPIType &newType)
create a constant step data type
Definition: MPI_Type.h:401
static tMPIError Resize(const tMPIType dataType, const tMPIByte &lb, const tMPIByte &extent, tMPIType &newDataType)
resize the type
Definition: MPI_Type.h:578
static tMPIError GetSize(int &mpiSize)
get the mpi size of a type in octet
Definition: MPI_Type.h:613
static tMPIError GetExtent(tMPIByte &start, tMPIByte &mpiSize)
get the mpi size (in octet) of the element including the alignment
Definition: MPI_Type.h:640
static tMPIError Register(tMPIType &type)
register the type
Definition: MPI_Type.h:592
static tMPIType GetPrimaryType()
get the MPI type of the template type T
Definition: MPI_Type.h:289
static tMPIError CreateStructType(const tMPICount &nElements, const std::valarray< tMPICount > &elementSizes, const std::valarray< tMPIByte > &elementOctetIndices, const std::valarray< tMPIType > &elementTypes, tMPIType &newType)
create a structure type
Definition: MPI_Type.h:501
tBoolean createVariableOctetStepType(const tMPICount &nElements, const std::valarray< tMPICount > &elementSizes, const std::valarray< tMPIByte > &elementOctetIndices, const tMPIType &oldType)
create a variable step data type
Definition: MPI_Type.h:236
const tMPIType & getType() const
get the type
Definition: MPI_Type.h:274
static tMPIError GetSize(const tMPIType &mpiType, int &mpiSize)
get the mpi size of a type in octets
Definition: MPI_Type.h:621
static tMPIError GetExtent(const tMPIType &mpiType, tMPIByte &start, tMPIByte &mpiSize)
get the mpi size (in octet) between the first element (in octet) and the last element of the mpi type...
Definition: MPI_Type.h:630
static tMPIType GetPrimaryType(const tString &typeName)
get the primary MPI type of the template type
Definition: MPI_Type.h:297
static void CreateFaceTypes(const tUCInt &dim, const std::array< tInteger, N > &Ns, std::array< MPI_Type, N > &faceTypes)
create a face types of element element of type T
Definition: MPI_Type.h:518
MPI_Type(void)
create
Definition: MPI_Type.h:86
tBoolean createStructType(const tMPICount &nElements, const std::valarray< tMPICount > &elementSizes, const std::valarray< tMPIByte > &elementOctetIndices, const std::valarray< tMPIType > &elementTypes)
create a structure type
Definition: MPI_Type.h:256
static tMPIError CreateConstantOctetStepType(const tMPICount &nElements, const tMPICount &elementsNumber, const tMPIByte &stepInOctets, const tMPIType &oldType, tMPIType &newType)
create a constant step data type
Definition: MPI_Type.h:434
virtual ~MPI_Type(void)
destroy
Definition: MPI_Type.h:97
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_Type.h:119
static tMPIError CreateConstantStepType(const tMPICount &nElements, const tMPICount &elementSize, const tMPICount &step, const tMPIType &oldType, tMPIType &newType)
create a constant step data type
Definition: MPI_Type.h:416
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: MPI_Type.h:107
static tMPIError CreateVariableOctetStepType(const tMPICount &nElements, const std::valarray< tMPICount > &elementSizes, const std::valarray< tMPIByte > &elementOctetIndices, const tMPIType &oldType, tMPIType &newType)
create a variable step data type
Definition: MPI_Type.h:482
tBoolean createConstantOctetStepType(const tMPICount &nElements, const tMPICount &elementsNumber, const tMPIByte &stepInOctets, const tMPIType &oldType)
create a constant step data type
Definition: MPI_Type.h:191