C++ mpi module for stochmagnet_main Package
MPI_DirectionalGridEnvironment.h
1 #ifndef MPI_DirectionalGridEnvironment_H
2 #define MPI_DirectionalGridEnvironment_H
3 
4 //inherited class object
5 #include "MPI_GridEnvironment.h"
6 
7 
29 template<tUCInt N,tUCInt D=1>
31 
32  //attributes
33 public:
34 
35 
36 private :
37 
38  //type of class & super class
41 
42 
43  //depth of neighbor (in template for future use (D=2 also ?)
44  static constexpr tUCInt N_DIRECTIONS=2*D+1;
45 
46  //not null neighbors direction index <-> coreId
47  std::map<tUCInt,tMPICoreId> mDirectionalNeighbors;
48 
49 
50 
51 
52 
53 protected:
54  // CONSTRUCTORS
55 
59 
60  }
73  MPI_DirectionalGridEnvironment(int& argc,char * argv[],const tBoolean& isOpenMPEnabled) : MPI_GridEnvironment<N>(argc,argv,isOpenMPEnabled) {
74 
75  }
76 
77 
78  // DESTRUCTORS
82 
83 
84  }
85 
86 public:
87 
88 
89  //memory management
90  //=================
93  virtual tMemSize getMemorySize() const override{
94  return sizeof(*this)+getContentsMemorySize();
95  }
96 
105  virtual tMemSize getContentsMemorySize() const override {
106  tMemSize mem=SuperClass::getContentsMemorySize();
107  mem+=mDirectionalNeighbors.size()*(sizeof(tUCInt)+sizeof(tMPICoreId));
108  return mem;
109  }
110 
111 public:
112 
121  inline static CORE_UniquePointer<SelfClass> New(const std::array<tMPICoreId,N>& nCoresPerDirection,
122  int& argc,char * argv[],const tBoolean& isOpenMPEnabled) {
123 
124  CORE_UniquePointer<SelfClass> p(new SelfClass(argc,argv,isOpenMPEnabled),
126 
127  //std::cout<<" world:"<<parent.getWorld()<<" N:"<<((int)N)<<" nCoresPerDirection:"<<core_arrays::toString(nCoresPerDirection)<<" Periodicity:"<<core_arrays::toString(isPeriodicPerDirection)<<"\n";
128 
129  //not periodic per default
130  std::array<tMPIBoolean,N> isPeriodicPerDirection;
131  for(auto& p: isPeriodicPerDirection) p=false;
132 
133  //set the periodicity per option
134  tString gridP="";
135  if (CORE_OptionsList::ReadOption(argc,argv,false,"cores-grid-periodicity",gridP)) {
136  std::vector<tBoolean> Vs;
137  functions_array::parse(gridP,Vs);
138  for(int i=Vs.size();i<N;i++) {
139  Vs.push_back(false);
140  }
141  auto iVs=Vs.cbegin();
142  for(auto& p:isPeriodicPerDirection) {
143  p=(*iVs);
144  iVs++;
145  }
146  }
147 
148  MPI_Cart_create(MPI_COMM_WORLD,
149  (int) N,nCoresPerDirection.data(),isPeriodicPerDirection.data(),
150  true,
151  &p->getWorld());
152 
153  if (p->getWorld()==MPI_COMM_NULL) {
154  //process can not be added in the grid environement
155  p.reset();
156  return p;
157  }
158  p->initialize();
159  return p;
160 
161  };
162 protected :
163 
164 
165 
166  //accessor methods
167  //================
168 
169 
170 public:
171 
172 
173  //Grid number & grid indices
174  //============================
175 
176 
177  //neighbors data
178  //===============
179 
180  //directional neighbors
181  //---------------------
182 
185  inline constexpr tUCInt GetNeighborsDepth() {
186  return D;
187  }
188 
191  inline static tUCInt GetMaximumDirectionalNeighborsNumber() {
192  return functions_numeric::pow<tUCInt,N>(N_DIRECTIONS);
193  }
194 
195 
201  inline static tUCInt GetFaceDirectionalIndex(const tUCInt& f,std::array<tCInt,N>& dir) {
202  functions_array::reset(dir);
203  dir[f/2]=(f%2==0)?-1:1;
204  return GetDirectionIndex(dir);
205  }
210  inline static tUCInt GetFaceDirectionalIndex(const tUCInt& f) {
211  std::array<tCInt,N> dir;
212  return GetFaceDirectionalIndex(f,dir);
213  }
214 
218  inline void getFaceNeighbors(std::array<tMPICoreId,2*N>& neighbors) const {
219  std::array<tCInt,N> dir;//direction of neighbor for face
220  functions_array::reset(dir);//dir=0
221  tMPICoreId* iNeighbors=&neighbors[0];
222  for (auto& dk:dir) {//loop on dierction
223  dk=-1;//first face
224  (*iNeighbors)=getDirectionalNeighbor(dir);
225  iNeighbors++;
226 
227  dk=1;//second face
228  (*iNeighbors)=getDirectionalNeighbor(dir);
229  iNeighbors++;
230 
231  dk=0;//reset to 0
232 
233  }
234 
235  }
240  inline tMPICoreId getDirectionalNeighbor(const std::array<tCInt,N>& dir) const {
241  return getDirectionalNeighbor(GetLevelDirectionIndex<1>(dir.data()));
242  }
243 
244 
250  inline tMPICoreId getDirectionalNeighbor(const tUCInt& dirIndex) const {
251  std::map<tUCInt,tMPICoreId>::const_iterator iNeighbors=mDirectionalNeighbors.find(dirIndex);
252  if (iNeighbors==mDirectionalNeighbors.end()) return NULL_CORE;
253  return iNeighbors->second;
254 
255  }
259  inline const std::map<tUCInt,tMPICoreId>& getDirectionalNeighbors() const {
260  return mDirectionalNeighbors;
261 
262  }
263 
267  inline void setDirectionalNeighbors(const std::map<tUCInt,tMPICoreId>& neighbors) {
268  mDirectionalNeighbors=neighbors;
269  }
270 
271 public:
272 
279  inline static void GetDirection(const tUCInt& index,std::array<tCInt,N>& dir) {
280  GetLevelDirection<1>(index,dir.data());
281  }
287  inline static tUCInt GetDirectionIndex(const std::array<tCInt,N>& dir) {
288  return GetLevelDirectionIndex<1>(dir.data());
289  }
290 
291 
292 
293 public:
294 
295 
296 
297 
303  virtual void initialize() {
304 
306 
307 
308 
309  }
310 
311 private:
315  template<tUCInt i> requires (i==N)
316  inline static tMPICoreId GetLevelDirectionIndex(const tCInt* dir) {
317  return (*dir)+D;
318  }
322  template<tUCInt i> requires (i<N)
323  inline static tMPICoreId GetLevelDirectionIndex(const tCInt* dir) {
324  return (*dir)+D+N_DIRECTIONS*GetLevelDirectionIndex<i+1>(dir+1);
325  }
329  template<tUCInt i> requires (i==N)
330  inline static void GetLevelDirection(const tUCInt& index,tCInt* dir) {
331  (*dir)=index;
332  (*dir)-=D;
333  }
337  template<tUCInt i> requires (i<N)
338  inline static void GetLevelDirection(const tUCInt& index,tCInt* dir) {
339  (*dir)=index%N_DIRECTIONS;
340  (*dir)-=D;
341  GetLevelDirection<i+1>(index/N_DIRECTIONS,dir+1);
342  }
343 
344 
345 
346 };
347 
348 
349 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
static tBoolean ReadOption(int nArgs, char *argv[], const tBoolean &isCaseSensitive, const tString &optionName, tString &optionValue)
read the only the option with optionName in command line
Definition: CORE_OptionsList.cpp:260
This class is a grid Environment of templated dimension N=1,2,3.
Definition: MPI_DirectionalGridEnvironment.h:30
static tUCInt GetFaceDirectionalIndex(const tUCInt &f, std::array< tCInt, N > &dir)
get the face index in direction
Definition: MPI_DirectionalGridEnvironment.h:201
tMPICoreId getDirectionalNeighbor(const std::array< tCInt, N > &dir) const
get the neighbor core in the dierction dir
Definition: MPI_DirectionalGridEnvironment.h:240
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: MPI_DirectionalGridEnvironment.h:105
static tUCInt GetFaceDirectionalIndex(const tUCInt &f)
get the face index in direction
Definition: MPI_DirectionalGridEnvironment.h:210
static tUCInt GetMaximumDirectionalNeighborsNumber()
get the maximum direction number : (2*D+1)^N when D is the neighbor Depth (D=0,1,2)
Definition: MPI_DirectionalGridEnvironment.h:191
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: MPI_DirectionalGridEnvironment.h:93
static tUCInt GetDirectionIndex(const std::array< tCInt, N > &dir)
get the direction index from dir index=dir[0]+1+3*(dir[1]+1+3*(dir[2]+1)) in {0,27}
Definition: MPI_DirectionalGridEnvironment.h:287
virtual void initialize()
initialize the environment
Definition: MPI_DirectionalGridEnvironment.h:303
tMPICoreId getDirectionalNeighbor(const tUCInt &dirIndex) const
get the neighbor core
Definition: MPI_DirectionalGridEnvironment.h:250
static CORE_UniquePointer< SelfClass > New(const std::array< tMPICoreId, N > &nCoresPerDirection, int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a new cart environment
Definition: MPI_DirectionalGridEnvironment.h:121
void setDirectionalNeighbors(const std::map< tUCInt, tMPICoreId > &neighbors)
set neighbors grids per face
Definition: MPI_DirectionalGridEnvironment.h:267
constexpr tUCInt GetNeighborsDepth()
get the depth of neighbors
Definition: MPI_DirectionalGridEnvironment.h:185
static void GetDirection(const tUCInt &index, std::array< tCInt, N > &dir)
get the direction dir from index: index=dir[0]+1+D*(dir[1]+1+3*(dir[2]+1)) in {0,27}
Definition: MPI_DirectionalGridEnvironment.h:279
MPI_DirectionalGridEnvironment(int &argc, char *argv[], const tBoolean &isOpenMPEnabled)
create a root environment
Definition: MPI_DirectionalGridEnvironment.h:73
const std::map< tUCInt, tMPICoreId > & getDirectionalNeighbors() const
get the neighbor cores in all directions
Definition: MPI_DirectionalGridEnvironment.h:259
virtual ~MPI_DirectionalGridEnvironment(void)
destroy
Definition: MPI_DirectionalGridEnvironment.h:81
MPI_DirectionalGridEnvironment()
create a root environment
Definition: MPI_DirectionalGridEnvironment.h:58
void getFaceNeighbors(std::array< tMPICoreId, 2 *N > &neighbors) const
get the coreId in canonical face
Definition: MPI_DirectionalGridEnvironment.h:218
This class is a grid Environment of templated dimension N=1,2,3.
Definition: MPI_GridEnvironment.h:34
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