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

Comparing trunk/OOPSE/libmdtools/DumpWriter.cpp (file contents):
Revision 415 by gezelter, Wed Mar 26 22:24:49 2003 UTC vs.
Revision 440 by mmeineke, Tue Apr 1 16:49:17 2003 UTC

# Line 4 | Line 4
4  
5   #ifdef IS_MPI
6   #include <mpi.h>
7 + #include <mpi++.h>
8   #include "mpiSimulation.hpp"
9 < #define TAKE_THIS_TAG 0
9 > #define TAKE_THIS_TAG_CHAR 1
10 > #define TAKE_THIS_TAG_INT 2
11 >
12 > namespace dWrite{
13 >  void nodeZeroError( void );
14 >  void anonymousNodeDie( void );
15 > }
16 >
17 > using namespace dWrite;
18   #endif //is_mpi
19  
20   #include "ReadWrite.hpp"
21   #include "simError.h"
22  
14 #define GAME_OVER -1
15
23   DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
24  
25    entry_plug = the_entry_plug;
# Line 66 | Line 73 | void DumpWriter::writeDump( double currentTime ){
73    char tempBuffer[BUFFERSIZE];
74    char writeLine[BUFFERSIZE];
75  
76 <  int i;
76 >  int i, j, which_node, done, game_over, which_atom, local_index;
77    double q[4];
78    DirectionalAtom* dAtom;
79    int nAtoms = entry_plug->n_atoms;
# Line 120 | Line 127 | void DumpWriter::writeDump( double currentTime ){
127    outFile.flush();
128  
129   #else // is_mpi
130 +
131 +  // first thing first, suspend fatalities.
132 +  painCave.isEventLoop = 1;
133 +
134 +  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
135 +  int haveError;
136 +
137 +  MPI::Status istatus;
138 +  int *AtomToProcMap = mpiSim->getAtomToProcMap();
139    
124  MPI_Status istatus[MPI_STATUS_SIZE];
125  
140    // write out header and node 0's coordinates
141    
142    if( worldRank == 0 ){
# Line 132 | Line 146 | void DumpWriter::writeDump( double currentTime ){
146              << entry_plug->box_x << "\t"
147              << entry_plug->box_y << "\t"
148              << entry_plug->box_z << "\n";
149 <    
150 <    for (i = 0 ; i < mpiPlug->nAtomsGlobal; i++ ) {
151 <      // Get the Node number which has this molecule:
149 >    outFile.flush();
150 >    for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
151 >      // Get the Node number which has this atom;
152        
153        which_node = AtomToProcMap[i];    
154        
155 <      if (which_node == mpiPlug->myNode) {
142 <        
143 <        sprintf( tempBuffer,
144 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
145 <                 atoms[i]->getType(),
146 <                 atoms[i]->getX(),
147 <                 atoms[i]->getY(),
148 <                 atoms[i]->getZ(),
149 <                 atoms[i]->get_vx(),
150 <                 atoms[i]->get_vy(),
151 <                 atoms[i]->get_vz());
152 <        strcpy( writeLine, tempBuffer );
155 >      if (which_node == 0 ) {
156          
157 <        if( atoms[i]->isDirectional() ){
158 <          
159 <          dAtom = (DirectionalAtom *)atoms[i];
160 <          dAtom->getQ( q );
161 <          
159 <          sprintf( tempBuffer,
160 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
161 <                   q[0],
162 <                   q[1],
163 <                   q[2],
164 <                   q[3],
165 <                   dAtom->getJx(),
166 <                   dAtom->getJy(),
167 <                   dAtom->getJz());
168 <          strcat( writeLine, tempBuffer );
157 >        haveError = 0;
158 >        which_atom = i;
159 >        local_index=-1;        
160 >        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
161 >          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
162          }
163 <        else
164 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
165 <        
166 <      } else {
167 <        
168 <        MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
169 <        MPI::COMM_WORLD.Receive(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
170 <                                TAKE_THIS_TAG, istatus);
163 >        if (local_index != -1) {
164 >          //format the line
165 >          sprintf( tempBuffer,
166 >                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
167 >                   atoms[local_index]->getType(),
168 >                   atoms[local_index]->getX(),
169 >                   atoms[local_index]->getY(),
170 >                   atoms[local_index]->getZ(),
171 >                   atoms[local_index]->get_vx(),
172 >                   atoms[local_index]->get_vy(),
173 >                   atoms[local_index]->get_vz()); // check here.
174 >          strcpy( writeLine, tempBuffer );
175 >          
176 >          if( atoms[local_index]->isDirectional() ){
177 >            
178 >            dAtom = (DirectionalAtom *)atoms[local_index];
179 >            dAtom->getQ( q );
180 >            
181 >            sprintf( tempBuffer,
182 >                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
183 >                     q[0],
184 >                     q[1],
185 >                     q[2],
186 >                     q[3],
187 >                     dAtom->getJx(),
188 >                     dAtom->getJy(),
189 >                     dAtom->getJz());
190 >            strcat( writeLine, tempBuffer );
191 >            
192 >          }
193 >          else
194 >            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
195 >        }
196 >        else {
197 >          sprintf(painCave.errMsg,
198 >                  "Atom %d not found on processor %d\n",
199 >                  i, worldRank );
200 >          haveError= 1;
201 >          simError();
202 >        }
203 >        
204 >        if(haveError) nodeZeroError();
205 >
206        }
207 +      else {
208 +        myStatus = 1;
209 +        MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, which_node,
210 +                             TAKE_THIS_TAG_INT);
211 +        MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT);
212 +        MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
213 +                             TAKE_THIS_TAG_CHAR, istatus);
214 +        MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, which_node,
215 +                             TAKE_THIS_TAG_INT, istatus);
216 +        
217 +        if(!myStatus) nodeZeroError();
218 +
219 +      }
220        
221        outFile << writeLine;
222 +      outFile.flush();
223      }
224      
225      // kill everyone off:
226 <    for (j = 0; j < mpiPlug->numberProcessors; j++) {
227 <      MPI::COMM_WORLD.Send(GAME_OVER, 1, MPI_INT, j, TAKE_THIS_TAG);
226 >    myStatus = -1;
227 >    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
228 >      MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, j,
229 >                           TAKE_THIS_TAG_INT);
230      }
231  
232    } else {
233      
234      done = 0;
235      while (!done) {
236 <      MPI::COMM_WORLD.Receive(&which_atom, 1, MPI_INT, 0,
237 <                              TAKE_THIS_TAG, istatus);
236 >      
237 >      MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, 0,
238 >                           TAKE_THIS_TAG_INT, istatus);
239  
240 <      if (which_atom == GAME_OVER) {
241 <        done=1;
242 <        continue;
198 <      } else {
240 >      if(!myStatus) anonymousNodeDie();
241 >      
242 >      if(myStatus < 0) break;
243  
244 <        //format the line
244 >      MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
245 >                           TAKE_THIS_TAG_INT, istatus);
246 >      
247 >      myStatus = 1;
248 >      local_index=-1;        
249 >      for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
250 >        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
251 >      }
252 >      if (local_index != -1) {
253 >        //format the line
254          sprintf( tempBuffer,
255                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
256 <                 atoms[which_atom]->getType(),
257 <                 atoms[which_atom]->getX(),
258 <                 atoms[which_atom]->getY(),
259 <                 atoms[which_atom]->getZ(),
260 <                 atoms[which_atom]->get_vx(),
261 <                 atoms[which_atom]->get_vy(),
262 <                 atoms[which_atom]->get_vz()); // check here.
256 >                 atoms[local_index]->getType(),
257 >                 atoms[local_index]->getX(),
258 >                 atoms[local_index]->getY(),
259 >                 atoms[local_index]->getZ(),
260 >                 atoms[local_index]->get_vx(),
261 >                 atoms[local_index]->get_vy(),
262 >                 atoms[local_index]->get_vz()); // check here.
263          strcpy( writeLine, tempBuffer );
264 +        
265 +        if( atoms[local_index]->isDirectional() ){
266            
267 <        if( atoms[which_atom]->isDirectional() ){
213 <            
214 <          dAtom = (DirectionalAtom *)atoms[which_atom];
267 >          dAtom = (DirectionalAtom *)atoms[local_index];
268            dAtom->getQ( q );
269 <            
269 >          
270            sprintf( tempBuffer,
271                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
272                     q[0],
# Line 225 | Line 278 | void DumpWriter::writeDump( double currentTime ){
278                     dAtom->getJz());
279            strcat( writeLine, tempBuffer );
280          }
281 <        else
281 >        else{
282            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
283 <        
284 <        MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
285 <                             TAKE_THIS_TAG);
283 >        }
284 >      }
285 >      else {
286 >        sprintf(painCave.errMsg,
287 >                "Atom %d not found on processor %d\n",
288 >                which_atom, worldRank );
289 >        myStatus = 0;
290 >        simError();
291 >
292 >        strcpy( writeLine, "Hello, I'm an error.\n");
293        }
294 +
295 +      MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
296 +                           TAKE_THIS_TAG_CHAR);
297 +      MPI::COMM_WORLD.Send( &myStatus, 1, MPI_INT, 0,
298 +                            TAKE_THIS_TAG_INT);
299      }
300    }  
301    outFile.flush();
302    sprintf( checkPointMsg,
303             "Sucessfully took a dump.\n");
304    MPIcheckPoint();
305 +
306 + // last  thing last, enable  fatalities.
307 +  painCave.isEventLoop = 0;
308 +
309   #endif // is_mpi
310   }
311  
312   void DumpWriter::writeFinal(){
313 <  
313 >
314    char finalName[500];
315    ofstream finalOut;
316 +
317 +  const int BUFFERSIZE = 2000;
318 +  char tempBuffer[BUFFERSIZE];
319 +  char writeLine[BUFFERSIZE];  
320 +
321 +  double q[4];
322 +  DirectionalAtom* dAtom;
323 +  int nAtoms = entry_plug->n_atoms;
324 +  Atom** atoms = entry_plug->atoms;
325 +  int i, j, which_node, done, game_over, which_atom, local_index;
326    
327 +  
328   #ifdef IS_MPI
329    if(worldRank == 0 ){
330   #endif // is_mpi
# Line 278 | Line 358 | void DumpWriter::writeFinal(){
358    finalOut << entry_plug->box_x << "\t"
359             << entry_plug->box_y << "\t"
360             << entry_plug->box_z << "\n";
361 <    
361 >  
362    for( i=0; i<nAtoms; i++ ){
363        
364      sprintf( tempBuffer,
# Line 318 | Line 398 | void DumpWriter::writeFinal(){
398  
399   #else // is_mpi
400    
401 <  MPI_Status istatus[MPI_STATUS_SIZE];
402 <  
401 >  // first thing first, suspend fatalities.
402 >  painCave.isEventLoop = 1;
403 >
404 >  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
405 >  int haveError;
406 >
407 >  MPI::Status istatus;
408 >  int *AtomToProcMap = mpiSim->getAtomToProcMap();
409 >
410    // write out header and node 0's coordinates
411    
412 +  haveError = 0;
413    if( worldRank == 0 ){
414      finalOut << mpiSim->getTotAtoms() << "\n";
415      
# Line 329 | Line 417 | void DumpWriter::writeFinal(){
417              << entry_plug->box_y << "\t"
418              << entry_plug->box_z << "\n";
419      
420 <    for (i = 0 ; i < mpiPlug->nAtomsGlobal; i++ ) {
420 >    for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
421        // Get the Node number which has this molecule:
422        
423        which_node = AtomToProcMap[i];    
424        
425 <      if (which_node == mpiPlug->myNode) {
426 <        
427 <        sprintf( tempBuffer,
428 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
429 <                 atoms[i]->getType(),
430 <                 atoms[i]->getX(),
343 <                 atoms[i]->getY(),
344 <                 atoms[i]->getZ(),
345 <                 atoms[i]->get_vx(),
346 <                 atoms[i]->get_vy(),
347 <                 atoms[i]->get_vz());
348 <        strcpy( writeLine, tempBuffer );
349 <        
350 <        if( atoms[i]->isDirectional() ){
351 <          
352 <          dAtom = (DirectionalAtom *)atoms[i];
353 <          dAtom->getQ( q );
354 <          
355 <          sprintf( tempBuffer,
356 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
357 <                   q[0],
358 <                   q[1],
359 <                   q[2],
360 <                   q[3],
361 <                   dAtom->getJx(),
362 <                   dAtom->getJy(),
363 <                   dAtom->getJz());
364 <          strcat( writeLine, tempBuffer );
425 >      if (which_node == mpiSim->getMyNode()) {
426 >
427 >        which_atom = i;
428 >        local_index=-1;        
429 >        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
430 >          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
431          }
432 <        else
433 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
432 >        if (local_index != -1) {        
433 >          sprintf( tempBuffer,
434 >                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
435 >                   atoms[local_index]->getType(),
436 >                   atoms[local_index]->getX(),
437 >                   atoms[local_index]->getY(),
438 >                   atoms[local_index]->getZ(),
439 >                   atoms[local_index]->get_vx(),
440 >                   atoms[local_index]->get_vy(),
441 >                   atoms[local_index]->get_vz());
442 >          strcpy( writeLine, tempBuffer );
443 >          
444 >          if( atoms[local_index]->isDirectional() ){
445 >            
446 >            dAtom = (DirectionalAtom *)atoms[local_index];
447 >            dAtom->getQ( q );
448 >            
449 >            sprintf( tempBuffer,
450 >                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
451 >                     q[0],
452 >                     q[1],
453 >                     q[2],
454 >                     q[3],
455 >                     dAtom->getJx(),
456 >                     dAtom->getJy(),
457 >                     dAtom->getJz());
458 >            strcat( writeLine, tempBuffer );
459 >          }
460 >          else
461 >            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
462 >        }
463 >        else {
464 >          sprintf(painCave.errMsg,
465 >                  "Atom %d not found on processor %d\n",
466 >                  i, worldRank );
467 >          haveError= 1;
468 >          simError();
469 >        }
470 >
471 >        if(haveError) nodeZeroError();
472 >    
473 >      }
474 >      else {
475          
476 <      } else {
477 <        
478 <        MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
479 <        MPI::COMM_WORLD.Receive(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
480 <                                TAKE_THIS_TAG, istatus);
476 >        myStatus = 1;
477 >        MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, which_node,
478 >                             TAKE_THIS_TAG_INT);
479 >        MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT);
480 >        MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
481 >                             TAKE_THIS_TAG_CHAR, istatus);
482 >        MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, which_node,
483 >                             TAKE_THIS_TAG_INT, istatus);
484 >        
485 >        if(!myStatus) nodeZeroError();
486        }
487        
488        finalOut << writeLine;
489      }
490      
491      // kill everyone off:
492 <    for (j = 0; j < mpiPlug->numberProcessors; j++) {
493 <      MPI::COMM_WORLD.Send(GAME_OVER, 1, MPI_INT, j, TAKE_THIS_TAG);
492 >    myStatus = -1;
493 >    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
494 >      MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, j,
495 >                           TAKE_THIS_TAG_INT);
496      }
497  
498    } else {
499      
500      done = 0;
501      while (!done) {
388      MPI::COMM_WORLD.Receive(&which_atom, 1, MPI_INT, 0,
389                              TAKE_THIS_TAG, istatus);
502  
503 <      if (which_atom == GAME_OVER) {
504 <        done=1;
505 <        continue;
506 <      } else {
503 >      MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, 0,
504 >                           TAKE_THIS_TAG_INT, istatus);
505 >      
506 >      if(!myStatus) anonymousNodeDie();
507 >      
508 >      if(myStatus < 0) break;
509 >      
510 >      MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
511 >                           TAKE_THIS_TAG_INT, istatus);
512 >      
513 >      myStatus = 1;
514 >      local_index=-1;        
515 >      for (j=0; j < mpiSim->getMyNlocal(); j++) {
516 >        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
517 >      }
518 >      if (local_index != -1) {
519  
520 <        //format the line
520 >        //format the line
521          sprintf( tempBuffer,
522                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
523 <                 atoms[which_atom]->getType(),
524 <                 atoms[which_atom]->getX(),
525 <                 atoms[which_atom]->getY(),
526 <                 atoms[which_atom]->getZ(),
527 <                 atoms[which_atom]->get_vx(),
528 <                 atoms[which_atom]->get_vy(),
529 <                 atoms[which_atom]->get_vz()); // check here.
523 >                 atoms[local_index]->getType(),
524 >                 atoms[local_index]->getX(),
525 >                 atoms[local_index]->getY(),
526 >                 atoms[local_index]->getZ(),
527 >                 atoms[local_index]->get_vx(),
528 >                 atoms[local_index]->get_vy(),
529 >                 atoms[local_index]->get_vz()); // check here.
530          strcpy( writeLine, tempBuffer );
531 +        
532 +        if( atoms[local_index]->isDirectional() ){
533            
534 <        if( atoms[which_atom]->isDirectional() ){
409 <            
410 <          dAtom = (DirectionalAtom *)atoms[which_atom];
534 >          dAtom = (DirectionalAtom *)atoms[local_index];
535            dAtom->getQ( q );
536 <            
536 >          
537            sprintf( tempBuffer,
538                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
539                     q[0],
# Line 421 | Line 545 | void DumpWriter::writeFinal(){
545                     dAtom->getJz());
546            strcat( writeLine, tempBuffer );
547          }
548 <        else
548 >        else{
549            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
550 <        
551 <        MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
552 <                             TAKE_THIS_TAG);
550 >        }
551 >      }
552 >      else {
553 >        sprintf(painCave.errMsg,
554 >                "Atom %d not found on processor %d\n",
555 >                which_atom, worldRank );
556 >        myStatus = 0;
557 >        simError();
558 >        
559 >        strcpy( writeLine, "Hello, I'm an error.\n");
560        }
561 +
562 +      MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
563 +                           TAKE_THIS_TAG_CHAR);
564 +      MPI::COMM_WORLD.Send( &myStatus, 1, MPI_INT, 0,
565 +                            TAKE_THIS_TAG_INT);
566      }
567 <  }  
567 >  }
568    finalOut.flush();
569    sprintf( checkPointMsg,
570             "Sucessfully took a dump.\n");
571    MPIcheckPoint();
572 <
572 >  
573    if( worldRank == 0 ) finalOut.close();    
574   #endif // is_mpi
575   }
576 +
577 +
578 +
579 + #ifdef IS_MPI
580 +
581 + // a couple of functions to let us escape the write loop
582 +
583 + void dWrite::nodeZeroError( void ){
584 +  int j, myStatus;
585 +  
586 +  myStatus = 0;
587 +  for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
588 +    MPI::COMM_WORLD.Send( &myStatus, 1, MPI_INT, j,
589 +                          TAKE_THIS_TAG_INT);
590 +  }  
591 +  
592 +
593 +  MPI_Finalize();
594 +  exit (0);
595 +  
596 + }
597 +
598 + void dWrite::anonymousNodeDie( void ){
599 +
600 +  MPI_Finalize();
601 +  exit (0);
602 + }
603 +
604 + #endif //is_mpi

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines