C++ mpi module for stochmagnet_main Package
MPI_GraphGridEnvironment.hpp
1 #ifndef MPI_GraphGridEnvironment_HPP
2 #define MPI_GraphGridEnvironment_HPP
3 
4 template<tUCInt N,tUCInt D>
5 tBoolean MPI_GraphGridEnvironment<N,D>::LoadGridCartFile(const tString& cartFile,
6  const MPI_Environment& env,
7  tMPICoreId& nCores,
8  std::array<tInteger,N>& gridsNumber,
9  std::array<tInteger,N>& coreIndices,
10  std::map<tUCInt,tMPICoreId>& dirNeighborCores,
11  tBoolean& isCoreInGrid) {
12 
13 
14 
15  //initialize the output data
16  memset(gridsNumber.data(),0,N*sizeof(tInteger));//nGrids=0
17  memset(coreIndices.data(),0,N*sizeof(tInteger));//nGrids=0
18 
19 
20  isCoreInGrid=false;
21  nCores=0;
22 
23  //raeding status
24  tMPIStatus status;
25 
26  //MPI IO File
27  MPI_IOFile file;
28 
29  //set the file name to load
30  file.setFileName(cartFile);
31 
32  //open the file for all cores of environment
33  tBoolean ok=file.open(env,file.READ_ONLY );
34  if (!ok) {
35  //file does not exists
36  std::cout<<"file "<<cartFile<<" does not exists\n";
37  return false;
38  }
39 
40  //max number of elements to read
41  tMPIIOIndex s=0;
42  file.getSize(s);
43 
44  //mpi size of tRelativeInteger
45  int mpiRIntSize;
46  MPI_Type::GetSize<tRelativeInteger>(mpiRIntSize);
47  s/=mpiRIntSize;
48 
49  //read the values
50  std::valarray<tRelativeInteger> gridValues(s);
51  ok=ok && file.allRead(gridValues,status);
52  ok=ok && file.close();
53  if (!ok) {
54  std::cout<<" error in reading "<<cartFile<<"\n";
55  return false;
56  }
57 
58 
59 
60  //number of values with directional neighbors
62  tUCInt index;//iterator on P
63 
64  tIndex nThValues=3+gridsNumber.size()+coreIndices.size()+P;
65  nThValues*=functions_array::product<tRelativeInteger,tRelativeInteger,N>(&gridValues[3]);//number of cores
66 
67  //test if only the neighbors is theface neighbor
68  if (nThValues!=gridValues.size()) {
69  std::cout<<" bad number of values in "<<cartFile<<" : "<<gridValues.size()<<" !="<<nThValues<<" gridValues:"<<functions_array::toString(gridValues)<<"\n";
70  return false;
71  }
72 
73  //std::cout<<"read from "<<cartFile<<" coreId="<<env.getCoreId()<<" values:"<<functions_functions::toString(gridValues)<<" nFaceValues:"<<nFaceValues<<" , nValues:"<<gridValues.size()<<" isFaceValues:"<<isFaceValues<<" \n";
74 
75  //iterator on grid cart values
76  const tRelativeInteger *iValues=&gridValues[0];
77 
78  //end iterator on grid cart values
79  const tRelativeInteger *eValues=iValues+gridValues.size();
80 
81  //id of core
82  tMPICoreId coreId;
83 
84  //index of not empty core
85  tMPICoreId iCore=0;
86  //full indicator
87  tRelativeInteger fullIndicator;
88 
89  //number of grids
90  std::array<tInteger,N> nGrids;
91 
92  //indices of grid
93  std::array<tInteger,N> indices;
94 
95  //neighbor per direction
96  std::map<tUCInt,tMPICoreId> dirNeighbors;
97 
98  //map coreId of the env -> graph grid coreId
99  std::map<tMPICoreId,tMPICoreId> coresMap;
100  do {
101  //original core id
102  coreId=(*iValues);iValues++;
103  //number of cores with not empty indicators
104  nCores=(*iValues);iValues++;
105  //fullIndicator
106  fullIndicator=(*iValues);iValues++;
107  //original number of grid
108  for(auto& Nd:nGrids) {
109  Nd=(*iValues);iValues++;
110  }
111  //indices of core
112  for(auto& Id:indices) {
113  Id=(*iValues);iValues++;
114  }
115 
116  //neighbor per direction of cores
117  dirNeighbors.clear();
118  for(index=0;index<P;index++) {
119  if ((*iValues)!=NULL_CORE) dirNeighbors[index]=(*iValues);
120  iValues++;
121  }
122 
123  //core is unneeded : ignore
124  if (env.getCoreId()>=nCores) {
125  isCoreInGrid=false;
126  return true;
127  }
128  //core is needed
129  if (fullIndicator>0) {
130  //the core is in the graph cart
131  if (iCore==env.getCoreId()) {
132  //the current process is the core
133  //register its corresponing data
134  gridsNumber=nGrids;
135  coreIndices=indices;
136  dirNeighborCores=dirNeighbors;
137  }
138  //register the core
139  coresMap[coreId]=iCore;
140  iCore++;
141  }
142  } while ((iValues!=eValues)&&(iCore<nCores));
143 
144 
145  //reorder the neihgbor grids indices
146  std::map<tMPICoreId,tMPICoreId>::const_iterator iGridCoreId;
147  std::map<tUCInt,tMPICoreId>::iterator iDirNeighbor=dirNeighborCores.begin();
148  while (iDirNeighbor!=dirNeighborCores.end()) {
149  iGridCoreId=coresMap.find(iDirNeighbor->second);
150  if (iGridCoreId!=coresMap.end()) {
151  iDirNeighbor->second=iGridCoreId->second;
152  iDirNeighbor++;
153  }else {
154  dirNeighborCores.erase(iDirNeighbor);
155  iDirNeighbor=dirNeighborCores.begin();
156  }
157 
158  }
159 
160  isCoreInGrid=true;
161 
162 
163  return true;
164 }
165 
166 
167 
168 template<tUCInt N,tUCInt D>
169 template<typename Q>
171  const tInteger& nFullCores,
172  const tInteger& fullId,
173  const std::map<tUCInt,Q>& fullDirNeighbor,
174  const tString& fileName) {
175 
176  //grids number
177  const std::array<tMPICoreId,N>& nGrids=env.getGridSize();
178  //grid indices
179  const std::array<tMPICoreId,N>& indices=env.getIndices();
180  //dir neighbors
181  const std::map<tUCInt,tMPICoreId>& dirNeighbors=env.getDirectionalNeighbors();
182 
183  //max number of directional neighbors
185 
186  tUCInt index;//iterator on [0,P[
187 
188  //number of values to save
189  tInteger nGridValues=3+nGrids.size()+indices.size()+P;
190 
191  //grid values to save
192  std::valarray<tRelativeInteger> gridValues(nGridValues);
193 
194  //set the cart grid values
195  tRelativeInteger *iGridValues=&gridValues[0];
196 
197  //line to write on file
198  *iGridValues=env.getCoreId();iGridValues++;//coreId
199  *iGridValues=nFullCores;iGridValues++;//number of not empty cores
200  *iGridValues=fullId;iGridValues++;//full indicator
201  for(const auto& i:nGrids) {*iGridValues=i;iGridValues++;}//number of cores per direction
202  for(const auto& i:indices) {*iGridValues=i;iGridValues++;}//indices of the grid per direction
203 
204  //set neighbor to NULL if neighbor core is empty or this is empty
205  typename std::map<tUCInt,Q>::const_iterator iFullDirNeighbor;
206  std::map<tUCInt,tMPICoreId>::const_iterator iDirNeighbors;
207  for(index=0;index<P;index++) {
208  iFullDirNeighbor=fullDirNeighbor.find(index);
209  iDirNeighbors=dirNeighbors.find(index);
210 
211  (*iGridValues)=(
212  (fullId==0) ||//enpty core -> no neighbor
213  (iDirNeighbors==dirNeighbors.end()) ||//no neighbor
214  ((iFullDirNeighbor!=fullDirNeighbor.end())&&//full neigbor indicator with empty data
215  (iFullDirNeighbor->second==(Q)false)) ||
216  (iFullDirNeighbor==fullDirNeighbor.end())//no in full neighbor : empty by default
217  )?NULL_CORE:iDirNeighbors->second;
218 
219  //iterators at next neighbor grid
220  iGridValues++;
221  }//dir neighbor grid loop
222 
223 
224  //std::cout<<"write "<<env.getCoreId()<<" values:"<<functions_string::toString(gridValues)<<"\n";
225  MPI_IOFile file;
226  file.setFileName(fileName);
227  tBoolean ok=file.open(env,file.WRITE_ONLY | file.CREATE);
228  ok=ok && file.writeOrdered(gridValues);
229  ok=ok && file.close();
230  //std::cout<<"end of saving "<<env.getCoreId()<<"\n";
231  return ok;
232 }
233 
234 
235 
236 #endif
This class is a grid Environment of templated dimension N=1,2,3.
Definition: MPI_DirectionalGridEnvironment.h:30
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
const std::map< tUCInt, tMPICoreId > & getDirectionalNeighbors() const
get the neighbor cores in all directions
Definition: MPI_DirectionalGridEnvironment.h:259
This class is a Environment class to define MPI world.
Definition: MPI_Environment.h:36
const tMPICoreId & getCoreId() const
get the id of the current process of this environment
Definition: MPI_Environment.h:200
static tBoolean SaveGridCartFile(const MPI_DirectionalGridEnvironment< N, D > &env, const tInteger &nFullCores, const tInteger &fullId, const std::map< tUCInt, Q > &isDirNeighborFull, const tString &fileName)
save the grid cart file
Definition: MPI_GraphGridEnvironment.hpp:170
static tBoolean LoadGridCartFile(const tString &fileName, const MPI_Environment &env, tMPICoreId &nCores, std::array< tInteger, N > &gridsNumber, std::array< tInteger, N > &coreIndices, std::map< tUCInt, tMPICoreId > &dirNeighbors, tBoolean &isCoreInGrid)
save the grid cart file
Definition: MPI_GraphGridEnvironment.hpp:5
const std::array< tMPICoreId, N > & getGridSize() const
get the grids number
Definition: MPI_GridEnvironment.h:200
const std::array< tMPICoreId, N > & getIndices() const
get the indices of the current core within the grid environment
Definition: MPI_GridEnvironment.h:219
This class manges the reading & writing in one file.
Definition: MPI_IOFile.h:72
tBoolean allRead(const tMPICount &n, T *values, tMPIStatus &status)
read the value at implicit shared position with blocking action
Definition: MPI_IOFile.h:421
tBoolean close()
close the file
Definition: MPI_IOFile.h:215
tBoolean open(const MPI_Environment &env, const tMPIIOMode &mode)
open the file for the environment
Definition: MPI_IOFile.h:187
tBoolean getSize(tMPIIOIndex &s) const
get the size of the file
Definition: MPI_IOFile.h:602
static const tMPIIOMode READ_ONLY
read only mode
Definition: MPI_IOFile.h:78
static const tMPIIOMode CREATE
file creation tag
Definition: MPI_IOFile.h:87
void setFileName(const tString &fn)
set the file name
Definition: MPI_IOFile.h:150
tBoolean writeOrdered(const tString &str)
write the value at shared core by explicit file cursor position with blocking action
Definition: MPI_IOFile.h:271
static const tMPIIOMode WRITE_ONLY
write only mode
Definition: MPI_IOFile.h:81