# | Line 12 | Line 12 | |
---|---|---|
12 | #include "simError.h" | |
13 | ||
14 | #ifdef IS_MPI | |
15 | + | #include <mpi.h> |
16 | #include "mpiSimulation.hpp" | |
17 | + | #define TAKE_THIS_TAG_CHAR 0 |
18 | + | #define TAKE_THIS_TAG_INT 1 |
19 | ||
20 | < | #define TAKE_THIS_TAG 0 |
20 | > | namespace initFile{ |
21 | > | void nodeZeroError( void ); |
22 | > | void anonymousNodeDie( void ); |
23 | > | } |
24 | > | |
25 | > | using namespace initFile; |
26 | > | |
27 | #endif // is_mpi | |
28 | ||
29 | InitializeFromFile :: InitializeFromFile( char *in_name ){ | |
# | Line 62 | Line 71 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
71 | ||
72 | void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){ | |
73 | ||
74 | < | int i; // loop counter |
74 | > | int i, j, done, which_node, which_atom; // loop counter |
75 | ||
76 | const int BUFFERSIZE = 2000; // size of the read buffer | |
77 | int n_atoms; // the number of atoms | |
# | Line 138 | Line 147 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
147 | // MPI Section of code.......... | |
148 | #else //IS_MPI | |
149 | ||
150 | < | int masterIndex; |
151 | < | int nodeAtomsStart; |
143 | < | int nodeAtomsEnd; |
144 | < | int mpiErr; |
145 | < | int sendError; |
150 | > | // first thing first, suspend fatalities. |
151 | > | painCave.isEventLoop = 1; |
152 | ||
153 | < | MPI_Status istatus[MPI_STATUS_SIZE]; |
153 | > | int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone |
154 | > | int haveError; |
155 | > | |
156 | > | MPI_Status istatus; |
157 | > | int *AtomToProcMap = mpiSim->getAtomToProcMap(); |
158 | ||
159 | + | |
160 | + | haveError = 0; |
161 | if (worldRank == 0) { | |
162 | + | |
163 | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); | |
164 | if( eof_test == NULL ){ | |
165 | sprintf( painCave.errMsg, | |
166 | "Error reading 1st line of %d \n ",c_in_name); | |
167 | < | painCave.isFatal = 1; |
167 | > | haveError = 1; |
168 | simError(); | |
169 | } | |
170 | ||
# | Line 168 | Line 181 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
181 | "Initialize from File error. %s n_atoms, %d, " | |
182 | "does not match the BASS file's n_atoms, %d.\n", | |
183 | c_in_name, n_atoms, entry_plug->n_atoms ); | |
184 | < | painCave.isFatal = 1; |
184 | > | haveError= 1; |
185 | simError(); | |
186 | } | |
187 | ||
# | Line 178 | Line 191 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
191 | if(eof_test == NULL){ | |
192 | sprintf( painCave.errMsg, | |
193 | "error in reading commment in %s\n", c_in_name); | |
194 | < | painCave.isFatal = 1; |
194 | > | haveError= 1; |
195 | simError(); | |
196 | } | |
197 | ||
198 | < | // Read Proc 0 share of the xyz file... |
199 | < | masterIndex = 0; |
200 | < | for( i=0; i <= mpiSim->getMyAtomEnd(); i++){ |
201 | < | |
198 | > | if(haveError) nodeZeroError(); |
199 | > | |
200 | > | for (i=0 ; i < mpiSim->getTotAtoms(); i++) { |
201 | > | |
202 | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); | |
203 | if(eof_test == NULL){ | |
204 | sprintf(painCave.errMsg, | |
# | Line 193 | Line 206 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
206 | "natoms = %d; index = %d\n" | |
207 | "error reading the line from the file.\n", | |
208 | c_in_name, n_atoms, i ); | |
209 | < | painCave.isFatal = 1; |
209 | > | haveError= 1; |
210 | simError(); | |
211 | } | |
212 | ||
213 | < | parseErr = parseDumpLine( read_buffer, i ); |
201 | < | if( parseErr != NULL ){ |
202 | < | strcpy( painCave.errMsg, parseErr ); |
203 | < | painCave.isFatal = 1; |
204 | < | simError(); |
205 | < | } |
206 | < | masterIndex++; |
207 | < | } |
208 | < | } |
213 | > | if(haveError) nodeZeroError(); |
214 | ||
215 | < | sprintf(checkPointMsg, |
216 | < | "Node 0 has successfully read positions from input file."); |
217 | < | MPIcheckPoint(); |
218 | < | |
219 | < | for (procIndex = 1; procIndex < mpiSim->getNumberProcessors(); |
220 | < | procIndex++){ |
221 | < | if (worldRank == 0) { |
222 | < | |
223 | < | mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, |
224 | < | istatus); |
225 | < | |
221 | < | mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, |
222 | < | istatus); |
223 | < | // Make sure where node 0 is reading from, matches where the receiving node |
224 | < | // expects it to be. |
225 | < | |
226 | < | if (masterIndex != nodeAtomsStart){ |
227 | < | sendError = 1; |
228 | < | mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
229 | < | sprintf(painCave.errMsg, |
230 | < | "Initialize from file error: atoms start index (%d) for " |
231 | < | "node %d not equal to master index (%d)",nodeAtomsStart,procIndex,masterIndex ); |
232 | < | painCave.isFatal = 1; |
233 | < | simError(); |
234 | < | } |
235 | < | sendError = 0; |
236 | < | mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
215 | > | // Get the Node number which wants this atom: |
216 | > | which_node = AtomToProcMap[i]; |
217 | > | if (which_node == 0) { |
218 | > | parseErr = parseDumpLine( read_buffer, i ); |
219 | > | if( parseErr != NULL ){ |
220 | > | strcpy( painCave.errMsg, parseErr ); |
221 | > | haveError = 1; |
222 | > | simError(); |
223 | > | } |
224 | > | if(haveError) nodeZeroError(); |
225 | > | } |
226 | ||
227 | < | for ( i = nodeAtomsStart; i <= nodeAtomsEnd; i++){ |
228 | < | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
229 | < | if(eof_test == NULL){ |
230 | < | |
231 | < | sprintf(read_buffer,"ERROR"); |
232 | < | mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
233 | < | |
234 | < | sprintf(painCave.errMsg, |
235 | < | "error in reading file %s\n" |
236 | < | "natoms = %d; index = %d\n" |
237 | < | "error reading the line from the file.\n", |
238 | < | c_in_name, n_atoms, i ); |
239 | < | painCave.isFatal = 1; |
251 | < | simError(); |
252 | < | } |
253 | < | |
254 | < | mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
255 | < | mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, |
256 | < | istatus); |
257 | < | if (sendError) MPIcheckPoint(); |
258 | < | |
259 | < | masterIndex++; |
227 | > | else { |
228 | > | |
229 | > | myStatus = 1; |
230 | > | MPI_Send(&myStatus, 1, MPI_INT, which_node, |
231 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
232 | > | MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, |
233 | > | TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); |
234 | > | MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, |
235 | > | MPI_COMM_WORLD); |
236 | > | MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, |
237 | > | MPI_COMM_WORLD, &istatus); |
238 | > | |
239 | > | if(!myStatus) nodeZeroError(); |
240 | } | |
241 | } | |
242 | + | myStatus = -1; |
243 | + | for (j = 0; j < mpiSim->getNumberProcessors(); j++) { |
244 | + | MPI_Send( &myStatus, 1, MPI_INT, j, |
245 | + | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
246 | + | } |
247 | + | |
248 | + | } else { |
249 | + | |
250 | + | done = 0; |
251 | + | while (!done) { |
252 | ||
253 | < | |
254 | < | else if(worldRank == procIndex){ |
265 | < | nodeAtomsStart = mpiSim->getMyAtomStart(); |
266 | < | nodeAtomsEnd = mpiSim->getMyAtomEnd(); |
267 | < | mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
268 | < | mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
253 | > | MPI_Recv(&myStatus, 1, MPI_INT, 0, |
254 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); |
255 | ||
256 | < | mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD, |
257 | < | istatus); |
258 | < | if (sendError) MPIcheckPoint(); |
256 | > | if(!myStatus) anonymousNodeDie(); |
257 | > | |
258 | > | if(myStatus < 0) break; |
259 | ||
260 | < | for ( i = 0; i < entry_plug->n_atoms; i++){ |
261 | < | |
262 | < | mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,0,TAKE_THIS_TAG,MPI_COMM_WORLD, |
263 | < | istatus); |
264 | < | |
265 | < | if(!strcmp(read_buffer, "ERROR")) MPIcheckPoint(); |
266 | < | |
267 | < | parseErr = parseDumpLine( read_buffer, i ); |
268 | < | if( parseErr != NULL ){ |
269 | < | sendError = 1; |
270 | < | mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
285 | < | |
286 | < | |
287 | < | strcpy( painCave.errMsg, parseErr ); |
288 | < | painCave.isFatal = 1; |
289 | < | simError(); |
290 | < | } |
291 | < | sendError = 0; |
292 | < | mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
260 | > | MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, |
261 | > | TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus); |
262 | > | MPI_Recv(&which_atom, 1, MPI_INT, 0, |
263 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); |
264 | > | |
265 | > | myStatus = 1; |
266 | > | parseErr = parseDumpLine( read_buffer, which_atom ); |
267 | > | if( parseErr != NULL ){ |
268 | > | strcpy( painCave.errMsg, parseErr ); |
269 | > | myStatus = 0;; |
270 | > | simError(); |
271 | } | |
272 | + | |
273 | + | MPI_Send( &myStatus, 1, MPI_INT, 0, |
274 | + | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
275 | + | |
276 | } | |
295 | – | sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex); |
296 | – | MPIcheckPoint(); |
277 | } | |
278 | < | |
278 | > | |
279 | > | // last thing last, enable fatalities. |
280 | > | painCave.isEventLoop = 0; |
281 | > | |
282 | #endif | |
283 | } | |
284 | ||
285 | + | char* InitializeFromFile::parseDumpLine(char* readLine, int globalIndex){ |
286 | ||
303 | – | char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ |
304 | – | |
287 | char *foo; // the pointer to the current string token | |
288 | ||
289 | double rx, ry, rz; // position place holders | |
# | Line 313 | Line 295 | char* InitializeFromFile::parseDumpLine(char* readLine | |
295 | Atom **atoms = entry_plug->atoms; | |
296 | DirectionalAtom* dAtom; | |
297 | ||
298 | < | int n_atoms; |
298 | > | int j, n_atoms, atomIndex; |
299 | ||
300 | #ifdef IS_MPI | |
301 | n_atoms = mpiSim->getTotAtoms(); | |
302 | + | atomIndex=-1; |
303 | + | for (j=0; j < mpiSim->getMyNlocal(); j++) { |
304 | + | if (atoms[j]->getGlobalIndex() == globalIndex) atomIndex = j; |
305 | + | } |
306 | + | if (atomIndex == -1) { |
307 | + | sprintf( painCave.errMsg, |
308 | + | "Initialize from file error. Atom at index %d " |
309 | + | "in file %s does not exist on processor %d .\n", |
310 | + | globalIndex, c_in_name, mpiSim->getMyNode() ); |
311 | + | return strdup( painCave.errMsg ); |
312 | + | } |
313 | #else | |
314 | n_atoms = entry_plug->n_atoms; | |
315 | + | atomIndex = globalIndex; |
316 | #endif // is_mpi | |
317 | ||
324 | – | |
318 | // set the string tokenizer | |
319 | ||
320 | foo = strtok(readLine, " ,;\t"); | |
# | Line 512 | Line 505 | char* InitializeFromFile::parseDumpLine(char* readLine | |
505 | ||
506 | return NULL; | |
507 | } | |
508 | + | |
509 | + | |
510 | + | #ifdef IS_MPI |
511 | + | |
512 | + | // a couple of functions to let us escape the read loop |
513 | + | |
514 | + | void initFile::nodeZeroError( void ){ |
515 | + | int j, myStatus; |
516 | + | |
517 | + | myStatus = 0; |
518 | + | for (j = 0; j < mpiSim->getNumberProcessors(); j++) { |
519 | + | MPI_Send( &myStatus, 1, MPI_INT, j, |
520 | + | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
521 | + | } |
522 | + | |
523 | + | |
524 | + | MPI_Finalize(); |
525 | + | exit (0); |
526 | + | |
527 | + | } |
528 | + | |
529 | + | void initFile::anonymousNodeDie( void ){ |
530 | + | |
531 | + | MPI_Finalize(); |
532 | + | exit (0); |
533 | + | } |
534 | + | |
535 | + | #endif //is_mpi |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |