--- trunk/OOPSE/libmdtools/DumpWriter.cpp 2003/03/28 22:34:02 437 +++ trunk/OOPSE/libmdtools/DumpWriter.cpp 2003/04/01 16:49:17 440 @@ -8,6 +8,13 @@ #include "mpiSimulation.hpp" #define TAKE_THIS_TAG_CHAR 1 #define TAKE_THIS_TAG_INT 2 + +namespace dWrite{ + void nodeZeroError( void ); + void anonymousNodeDie( void ); +} + +using namespace dWrite; #endif //is_mpi #include "ReadWrite.hpp" @@ -121,6 +128,12 @@ void DumpWriter::writeDump( double currentTime ){ #else // is_mpi + // first thing first, suspend fatalities. + painCave.isEventLoop = 1; + + int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone + int haveError; + MPI::Status istatus; int *AtomToProcMap = mpiSim->getAtomToProcMap(); @@ -141,6 +154,7 @@ void DumpWriter::writeDump( double currentTime ){ if (which_node == 0 ) { + haveError = 0; which_atom = i; local_index=-1; for (j=0; (jgetMyNlocal()) && (local_index < 0); j++) { @@ -180,17 +194,28 @@ void DumpWriter::writeDump( double currentTime ){ strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); } else { - strcpy( writeLine, "ATOM NOT FOUND ON THIS PROCESSOR"); + sprintf(painCave.errMsg, + "Atom %d not found on processor %d\n", + i, worldRank ); + haveError= 1; + simError(); } - } + + if(haveError) nodeZeroError(); + + } else { - - //std::cerr << "node 0: sending node " << which_node << " request for atom " << i << "\n"; + myStatus = 1; + MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, which_node, + TAKE_THIS_TAG_INT); MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT); - //std::cerr << "node 0: sent!\n"; MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node, TAKE_THIS_TAG_CHAR, istatus); - //std::cerr << "node 0: got this line: " << writeLine; + MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, which_node, + TAKE_THIS_TAG_INT, istatus); + + if(!myStatus) nodeZeroError(); + } outFile << writeLine; @@ -198,73 +223,89 @@ void DumpWriter::writeDump( double currentTime ){ } // kill everyone off: - game_over = -1; + myStatus = -1; for (j = 0; j < mpiSim->getNumberProcessors(); j++) { - MPI::COMM_WORLD.Send(&game_over, 1, MPI_INT, j, TAKE_THIS_TAG_INT); + MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, j, + TAKE_THIS_TAG_INT); } } else { done = 0; while (!done) { - //std::cerr << "node: " << mpiSim->getMyNode() << " Waiting for receive \n"; + + MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, 0, + TAKE_THIS_TAG_INT, istatus); + + if(!myStatus) anonymousNodeDie(); + + if(myStatus < 0) break; + MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, TAKE_THIS_TAG_INT, istatus); - //std::cerr << "node: " << mpiSim->getMyNode() << " got request for atom " << which_atom << "\n"; - if (which_atom == -1) { - done=1; - continue; - } else { - local_index=-1; - for (j=0; (jgetMyNlocal()) && (local_index < 0); j++) { - if (atoms[j]->getGlobalIndex() == which_atom) local_index = j; - } - if (local_index != -1) { - //format the line - sprintf( tempBuffer, - "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t", - atoms[local_index]->getType(), - atoms[local_index]->getX(), - atoms[local_index]->getY(), - atoms[local_index]->getZ(), - atoms[local_index]->get_vx(), - atoms[local_index]->get_vy(), - atoms[local_index]->get_vz()); // check here. - strcpy( writeLine, tempBuffer ); + + myStatus = 1; + local_index=-1; + for (j=0; (jgetMyNlocal()) && (local_index < 0); j++) { + if (atoms[j]->getGlobalIndex() == which_atom) local_index = j; + } + if (local_index != -1) { + //format the line + sprintf( tempBuffer, + "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t", + atoms[local_index]->getType(), + atoms[local_index]->getX(), + atoms[local_index]->getY(), + atoms[local_index]->getZ(), + atoms[local_index]->get_vx(), + atoms[local_index]->get_vy(), + atoms[local_index]->get_vz()); // check here. + strcpy( writeLine, tempBuffer ); + + if( atoms[local_index]->isDirectional() ){ - if( atoms[local_index]->isDirectional() ){ - - dAtom = (DirectionalAtom *)atoms[local_index]; - dAtom->getQ( q ); - - sprintf( tempBuffer, - "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", - q[0], - q[1], - q[2], - q[3], - dAtom->getJx(), - dAtom->getJy(), - dAtom->getJz()); - strcat( writeLine, tempBuffer ); - } - else - strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); - // std::cerr << "node: " << mpiSim->getMyNode() << " sending this line" << writeLine; - MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0, - TAKE_THIS_TAG_CHAR); - } else { - strcpy( writeLine, "ATOM NOT FOUND ON THIS PROCESSOR"); - MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0, - TAKE_THIS_TAG_CHAR); - } + dAtom = (DirectionalAtom *)atoms[local_index]; + dAtom->getQ( q ); + + sprintf( tempBuffer, + "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", + q[0], + q[1], + q[2], + q[3], + dAtom->getJx(), + dAtom->getJy(), + dAtom->getJz()); + strcat( writeLine, tempBuffer ); + } + else{ + strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); + } + } + else { + sprintf(painCave.errMsg, + "Atom %d not found on processor %d\n", + which_atom, worldRank ); + myStatus = 0; + simError(); + + strcpy( writeLine, "Hello, I'm an error.\n"); } + + MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0, + TAKE_THIS_TAG_CHAR); + MPI::COMM_WORLD.Send( &myStatus, 1, MPI_INT, 0, + TAKE_THIS_TAG_INT); } } outFile.flush(); sprintf( checkPointMsg, "Sucessfully took a dump.\n"); MPIcheckPoint(); + + // last thing last, enable fatalities. + painCave.isEventLoop = 0; + #endif // is_mpi } @@ -357,11 +398,18 @@ void DumpWriter::writeFinal(){ #else // is_mpi + // first thing first, suspend fatalities. + painCave.isEventLoop = 1; + + int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone + int haveError; + MPI::Status istatus; int *AtomToProcMap = mpiSim->getAtomToProcMap(); // write out header and node 0's coordinates + haveError = 0; if( worldRank == 0 ){ finalOut << mpiSim->getTotAtoms() << "\n"; @@ -413,89 +461,144 @@ void DumpWriter::writeFinal(){ strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); } else { - strcpy( writeLine, "ATOM NOT FOUND ON THIS PROCESSOR"); + sprintf(painCave.errMsg, + "Atom %d not found on processor %d\n", + i, worldRank ); + haveError= 1; + simError(); } + + if(haveError) nodeZeroError(); - } else { + } + else { + myStatus = 1; + MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, which_node, + TAKE_THIS_TAG_INT); MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT); MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node, - TAKE_THIS_TAG_CHAR, istatus); + TAKE_THIS_TAG_CHAR, istatus); + MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, which_node, + TAKE_THIS_TAG_INT, istatus); + + if(!myStatus) nodeZeroError(); } finalOut << writeLine; } // kill everyone off: - game_over = -1; - for (j = 0; j < mpiSim->getNumberProcessors(); j++) { - MPI::COMM_WORLD.Send(&game_over, 1, MPI_INT, j, TAKE_THIS_TAG_INT); + myStatus = -1; + for (j = 0; j < mpiSim->getNumberProcessors(); j++) { + MPI::COMM_WORLD.Send(&myStatus, 1, MPI_INT, j, + TAKE_THIS_TAG_INT); } } else { done = 0; while (!done) { + + MPI::COMM_WORLD.Recv(&myStatus, 1, MPI_INT, 0, + TAKE_THIS_TAG_INT, istatus); + + if(!myStatus) anonymousNodeDie(); + + if(myStatus < 0) break; + MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, TAKE_THIS_TAG_INT, istatus); + + myStatus = 1; + local_index=-1; + for (j=0; j < mpiSim->getMyNlocal(); j++) { + if (atoms[j]->getGlobalIndex() == which_atom) local_index = j; + } + if (local_index != -1) { - if (which_atom == -1) { - done=1; - continue; - } else { - - local_index=-1; - for (j=0; j < mpiSim->getMyNlocal(); j++) { - if (atoms[j]->getGlobalIndex() == which_atom) local_index = j; - } - if (local_index != -1) { - - //format the line - sprintf( tempBuffer, - "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t", - atoms[local_index]->getType(), - atoms[local_index]->getX(), - atoms[local_index]->getY(), - atoms[local_index]->getZ(), - atoms[local_index]->get_vx(), - atoms[local_index]->get_vy(), - atoms[local_index]->get_vz()); // check here. - strcpy( writeLine, tempBuffer ); + //format the line + sprintf( tempBuffer, + "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t", + atoms[local_index]->getType(), + atoms[local_index]->getX(), + atoms[local_index]->getY(), + atoms[local_index]->getZ(), + atoms[local_index]->get_vx(), + atoms[local_index]->get_vy(), + atoms[local_index]->get_vz()); // check here. + strcpy( writeLine, tempBuffer ); + + if( atoms[local_index]->isDirectional() ){ - if( atoms[local_index]->isDirectional() ){ - - dAtom = (DirectionalAtom *)atoms[local_index]; - dAtom->getQ( q ); - - sprintf( tempBuffer, - "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", - q[0], - q[1], - q[2], - q[3], - dAtom->getJx(), - dAtom->getJy(), - dAtom->getJz()); - strcat( writeLine, tempBuffer ); - } - else - strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); - - MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0, - TAKE_THIS_TAG_CHAR); - } else { - strcpy( writeLine, "ATOM NOT FOUND ON THIS PROCESSOR"); - MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0, - TAKE_THIS_TAG_CHAR); - } + dAtom = (DirectionalAtom *)atoms[local_index]; + dAtom->getQ( q ); + + sprintf( tempBuffer, + "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", + q[0], + q[1], + q[2], + q[3], + dAtom->getJx(), + dAtom->getJy(), + dAtom->getJz()); + strcat( writeLine, tempBuffer ); + } + else{ + strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); + } + } + else { + sprintf(painCave.errMsg, + "Atom %d not found on processor %d\n", + which_atom, worldRank ); + myStatus = 0; + simError(); + + strcpy( writeLine, "Hello, I'm an error.\n"); } + + MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0, + TAKE_THIS_TAG_CHAR); + MPI::COMM_WORLD.Send( &myStatus, 1, MPI_INT, 0, + TAKE_THIS_TAG_INT); } } finalOut.flush(); sprintf( checkPointMsg, "Sucessfully took a dump.\n"); MPIcheckPoint(); - + if( worldRank == 0 ) finalOut.close(); #endif // is_mpi } + + + +#ifdef IS_MPI + +// a couple of functions to let us escape the write loop + +void dWrite::nodeZeroError( void ){ + int j, myStatus; + + myStatus = 0; + for (j = 0; j < mpiSim->getNumberProcessors(); j++) { + MPI::COMM_WORLD.Send( &myStatus, 1, MPI_INT, j, + TAKE_THIS_TAG_INT); + } + + + MPI_Finalize(); + exit (0); + +} + +void dWrite::anonymousNodeDie( void ){ + + MPI_Finalize(); + exit (0); +} + +#endif //is_mpi