ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/InitializeFromFile.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/InitializeFromFile.cpp (file contents):
Revision 378 by mmeineke, Fri Mar 21 17:42:12 2003 UTC vs.
Revision 436 by chuckv, Fri Mar 28 21:45:03 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines