C++ mpi module for stochmagnet_main Package
MPI_CanonicalGridEnvironment.h
1 #ifndef MPI_CanonicalGridEnvironment_H
2 #define MPI_CanonicalGridEnvironment_H
3 
4 //inherited class object
5 #include "MPI_GridEnvironment.h"
6 
7 //numeric function headers
8 #include "core_numeric.h"
9 
31 template<tUCInt N>
33 
34  //attributes
35 public:
36 
37 
38 private :
41 
42 
43  //face neighbor core
44  std::array<tMPICoreId,2*N> mFaceNeighbors;
45 
46 
47 
48 
49 protected:
50  // CONSTRUCTORS
51 
55 
56  }
69  MPI_CanonicalGridEnvironment(int& argc,char * argv[],const tBoolean& isOpenMPEnabled) : SuperClass(argc,argv,isOpenMPEnabled) {
70 
71  }
72 
73 
74  // DESTRUCTORS
78 
79 
80  }
81 
82 public:
83 
84 
85  //memory management
86  //=================
89  virtual tMemSize getMemorySize() const override{
90  return sizeof(*this)+getContentsMemorySize();
91  }
92 
101  virtual tMemSize getContentsMemorySize() const override {
102  tMemSize mem=SuperClass::getContentsMemorySize();
103  mem+=sizeof(tMPICoreId)*mFaceNeighbors.size();
104  return mem;
105  }
106 
107 
108 
109 protected :
110 
111 
112 
113  //accessor methods
114  //================
115 public:
124  inline static CORE_UniquePointer<SelfClass> New(const std::array<tMPICoreId,N>& nCoresPerDirection,
125  const std::array<tBoolean,N>& isPeriodicPerDirection,
126  int& argc,char * argv[],const tBoolean& isOpenMPEnabled) {
127 
128  CORE_UniquePointer<SelfClass> p(new SelfClass(argc,argv,isOpenMPEnabled),
130 
131  //std::cout<<" world:"<<parent.getWorld()<<" N:"<<((int)N)<<" nCoresPerDirection:"<<core_arrays::toString(nCoresPerDirection)<<" Periodicity:"<<core_arrays::toString(isPeriodicPerDirection)<<"\n";
132 
133  MPI_Cart_create(MPI_COMM_WORLD,
134  (int) N,nCoresPerDirection.data(),isPeriodicPerDirection.data(),
135  true,
136  &p->getWorld());
137 
138  if (p->getWorld()==MPI_COMM_NULL) {
139  //process can not be added in the grid environement
140  p.reset();
141  return p;
142  }
143  p->initialize();
144  return p;
145 
146  };
152  inline static CORE_UniquePointer<SelfClass> New(const MPI_Environment& parent,
153  const std::array<tMPICoreId,N>& nCoresPerDirection) {
154  //no periodicity
155  std::array<tMPIBoolean,N> periodicity;
156  for(auto& Vi:periodicity) Vi=0;
157 
158  return New(parent,nCoresPerDirection,periodicity,true);
159  };
160 
168  inline static CORE_UniquePointer<SelfClass> New(const MPI_Environment& parent,
169  const std::array<tMPICoreId,N>& nCoresPerDirection,
170  const std::array<tMPIBoolean,N>& isPeriodicPerDirection,
171  const int& canCoreIdBeChanged) {
172  CORE_UniquePointer<SelfClass> p(new SelfClass(),
174 
175  //std::cout<<" world:"<<parent.getWorld()<<" N:"<<((int)N)<<" nCoresPerDirection:"<<core_arrays::toString(nCoresPerDirection)<<" Periodicity:"<<core_arrays::toString(isPeriodicPerDirection)<<"\n";
176 
177  MPI_Cart_create(parent.getWorld(),
178  (int) N,nCoresPerDirection.data(),isPeriodicPerDirection.data(),
179  canCoreIdBeChanged,
180  &p->getWorld());
181 
182  if (p->getWorld()==MPI_COMM_NULL) {
183  //process can not be added in the grid environement
184  p.reset();
185  return p;
186  }
187  p->initialize();
188  return p;
189  };
190 
198  inline static CORE_UniquePointer<SelfClass> New(const MPI_Environment& parent,
199  const std::array<tMPICoreId,N>& nCoresPerDirection,
200  const std::array<tBoolean,N>& isPeriodicPerDirection,
201  const int& canCoreIdBeChanged) {
202  std::array<int,N> periods;
203  int k=0;
204  for(auto& Pk:periods) {
205  Pk=isPeriodicPerDirection[k];
206  k++;
207  }
208  return New(parent,nCoresPerDirection,periods,canCoreIdBeChanged);
209  }
215  inline static CORE_UniquePointer<SelfClass> New(const MPI_CanonicalGridEnvironment<N+1>& parent,
216  const tUCInt& dir) {
217  CORE_UniquePointer<SelfClass> p(new SelfClass(),
219  //create the selected directions to keep
220  std::array<int,N+1> dims;
221  for(auto& Dk:dims) Dk=1;
222  dims[dir]=0;
223  MPI_Cart_sub(parent.getWorld(),&dims[0],&p->getWorld());
224  if (p->getWorld()==MPI_COMM_NULL) {
225  //process can not be added in the grid environement
226  p.reset();
227  return p;
228  }
229  p->initialize();
230  return p;
231  }
232 
233 public:
234 
235 
236  //Grid number & grid indices
237  //============================
238 
239  //neighbors data
240  //===============
241 
242  //directional neighbors
243  //---------------------
244 
245 
251  virtual void initialize() {
252 
254 
255  //set the neighbor only per face
257 
258 
259  }
260 
261 protected:
262 
268  inline void setFaceNeighbors(const std::array<tMPICoreId,N>& nGrids,
269  const std::array<tMPICoreId,N>& indices,
270  const std::array<tBoolean,N>& isPeriodic) {
271 
272 
273 
274  //orientation of face i {-1,1}
275  tCInt eps;
276 
277 
278 
279  //neighbor indices
280  std::array<tMPICoreId,N> neighborIndices=indices;
281 
282  //iterator on face neighbors
283  tMPICoreId *iFaceNeighbors=mFaceNeighbors.data();
284 
285  //indicates if the neighbor core is in the grid
286  tBoolean isInsideBound;
287 
288 
289  //if of the neigbor core
290  tMPICoreId neighborCoreId;
291 
292 
293  //iterator on the indices of core this
294  const tMPICoreId* iIndices=indices.data();
295  //iterator on index of neighbor cores
296  tMPICoreId *iNeighborIndices=neighborIndices.data();
297 
298  //iterator on periodicity
299  const tBoolean* iIsPeriodic=isPeriodic.data();
300 
301  for (const auto& Nk:nGrids) {//loop on number of core per dierction k in {0,1,2}
302 
303 
304  //neighbor indices initialize to the index of this core
305 
306  for(eps=-1;eps<2;eps+=2) {//orientation of face es in {-1,1}
307 
308 
309  //neighbor in direction -ek
310  //Dk=-1,+1
311  (*iNeighborIndices)=(*iIndices);
312  (*iNeighborIndices)+=eps;
313 
314  if (*iIsPeriodic) {//periodic case
315  (*iNeighborIndices)+=Nk;
316  (*iNeighborIndices)%=Nk;
317  }
318 
319  isInsideBound=((*iNeighborIndices)>=0) && ((*iNeighborIndices)<Nk);
320 
321  if (isInsideBound) {
322  MPI_Cart_rank(this->getWorld(),neighborIndices.data(),&neighborCoreId);
323 
324  //register the neighbor per face
325  (*iFaceNeighbors)=neighborCoreId;
326  } else {
327  (*iFaceNeighbors)=NULL_CORE;
328  }
329 
330  //ietartor at next orientation
331  iFaceNeighbors++;
332 
333  }
334  //reinitialize value before changing of dierction
335  (*iNeighborIndices)=(*iIndices);
336 
337  //iterator at next dierction k
338  iIndices++;
339  iNeighborIndices++;
340  iIsPeriodic++;
341  }//end loop on dierction k
342 
343 
344  }
345 
346 
347 public:
348 
349  //face neighbors
350  //================
355  inline const tMPICoreId& getFaceNeighbor(const tUCInt& f) const {
356  return mFaceNeighbors[f];
357  }
363  inline void getFaceNeighbors(const tUCInt& k,tMPICoreId& previousNeighbor,tMPICoreId& nextNeighbor) const {
364  const tMPICoreId *iNeighbors=&mFaceNeighbors[k+k];
365  previousNeighbor=*iNeighbors;iNeighbors++;
366  nextNeighbor=*iNeighbors;
367  }
368 
372  inline const std::array<tMPICoreId,2*N>& getFaceNeighbors() const {
373  return mFaceNeighbors;
374 
375  }
376 
377 
378 
379 
380 };
381 
382 
383 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
This class is a grid Environment of templated dimension N=1,2,3.
Definition: MPI_CanonicalGridEnvironment.h:32
virtual void initialize()
initialize the environment
Definition: MPI_CanonicalGridEnvironment.h:251
static CORE_UniquePointer< SelfClass > New(const MPI_Environment &parent, const std::array< tMPICoreId, N > &nCoresPerDirection, const std::array< tMPIBoolean, N > &isPeriodicPerDirection, const int &canCoreIdBeChanged)
create a new cart grid environment
Definition: MPI_CanonicalGridEnvironment.h:168
virtual ~MPI_CanonicalGridEnvironment(void)
destroy
Definition: MPI_CanonicalGridEnvironment.h:77
static CORE_UniquePointer< SelfClass > New(const MPI_CanonicalGridEnvironment< N+1 > &parent, const tUCInt &dir)
create a hyper plan of the grids perpendicular to dir
Definition: MPI_CanonicalGridEnvironment.h:215
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_CanonicalGridEnvironment.h:101
MPI_CanonicalGridEnvironment(int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a root environment
Definition: MPI_CanonicalGridEnvironment.h:69
static CORE_UniquePointer< SelfClass > New(const MPI_Environment &parent, const std::array< tMPICoreId, N > &nCoresPerDirection)
create a new cart grid environment
Definition: MPI_CanonicalGridEnvironment.h:152
void getFaceNeighbors(const tUCInt &k, tMPICoreId &previousNeighbor, tMPICoreId &nextNeighbor) const
get the neighbor cores
Definition: MPI_CanonicalGridEnvironment.h:363
MPI_CanonicalGridEnvironment()
create a root environment
Definition: MPI_CanonicalGridEnvironment.h:54
static CORE_UniquePointer< SelfClass > New(const MPI_Environment &parent, const std::array< tMPICoreId, N > &nCoresPerDirection, const std::array< tBoolean, N > &isPeriodicPerDirection, const int &canCoreIdBeChanged)
create a new cart grid environment
Definition: MPI_CanonicalGridEnvironment.h:198
const tMPICoreId & getFaceNeighbor(const tUCInt &f) const
get the neighbor core
Definition: MPI_CanonicalGridEnvironment.h:355
const std::array< tMPICoreId, 2 *N > & getFaceNeighbors() const
get the face neighbor cores
Definition: MPI_CanonicalGridEnvironment.h:372
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: MPI_CanonicalGridEnvironment.h:89
static CORE_UniquePointer< SelfClass > New(const std::array< tMPICoreId, N > &nCoresPerDirection, const std::array< tBoolean, N > &isPeriodicPerDirection, int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a new cart environment
Definition: MPI_CanonicalGridEnvironment.h:124
void setFaceNeighbors(const std::array< tMPICoreId, N > &nGrids, const std::array< tMPICoreId, N > &indices, const std::array< tBoolean, N > &isPeriodic)
set the face neighbors from directional neighbors
Definition: MPI_CanonicalGridEnvironment.h:268
This class is a Environment class to define MPI world.
Definition: MPI_Environment.h:36
const tMPIComm & getWorld() const
get the world of the environment for reading
Definition: MPI_Environment.h:165
This class is a grid Environment of templated dimension N=1,2,3.
Definition: MPI_GridEnvironment.h:34
const std::array< tMPICoreId, N > & getGridSize() const
get the grids number
Definition: MPI_GridEnvironment.h:200
const std::array< tBoolean, N > & getGridPeriodicity() const
return the periodicity of the grid
Definition: MPI_GridEnvironment.h:356
const std::array< tMPICoreId, N > & getIndices() const
get the indices of the current core within the grid environment
Definition: MPI_GridEnvironment.h:219
virtual void initialize()
initialize the environment
Definition: MPI_GridEnvironment.h:388
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_GridEnvironment.h:112
const tBoolean & isPeriodic(const tUCInt &k) const
return true if the grid is periodic in direction k
Definition: MPI_GridEnvironment.h:363