--- trunk/OOPSE/libmdtools/DumpWriter.cpp 2004/01/13 20:04:28 934 +++ trunk/OOPSE/libmdtools/DumpWriter.cpp 2004/01/30 21:47:22 1000 @@ -28,7 +28,6 @@ DumpWriter::DumpWriter( SimInfo* the_entry_plug ){ if(worldRank == 0 ){ #endif // is_mpi - dumpFile.open(entry_plug->sampleName, ios::out | ios::trunc ); if( !dumpFile ){ @@ -40,15 +39,6 @@ DumpWriter::DumpWriter( SimInfo* the_entry_plug ){ simError(); } - finalOut.open( entry_plug->finalName, ios::out | ios::trunc ); - if( !finalOut ){ - sprintf( painCave.errMsg, - "Could not open \"%s\" for final dump output.\n", - entry_plug->finalName ); - painCave.isFatal = 1; - simError(); - } - #ifdef IS_MPI } @@ -68,7 +58,6 @@ DumpWriter::~DumpWriter( ){ #endif // is_mpi dumpFile.close(); - finalOut.close(); #ifdef IS_MPI } @@ -102,20 +91,31 @@ void DumpWriter::sortByGlobalIndex(){ indexArray.clear(); - for(int i = 0; i < mpiSim->getMyNlocal();i++) + for(int i = 0; i < mpiSim->getMyNlocal();i++) indexArray.push_back(make_pair(i, atoms[i]->getGlobalIndex())); sort(indexArray.begin(), indexArray.end(), indexSortingCriterion); } + #endif void DumpWriter::writeDump(double currentTime){ + ofstream finalOut; vector fileStreams; #ifdef IS_MPI if(worldRank == 0 ){ - finalOut.seekp(0); +#endif + finalOut.open( entry_plug->finalName, ios::out | ios::trunc ); + if( !finalOut ){ + sprintf( painCave.errMsg, + "Could not open \"%s\" for final dump output.\n", + entry_plug->finalName ); + painCave.isFatal = 1; + simError(); + } +#ifdef IS_MPI } #endif // is_mpi @@ -123,21 +123,42 @@ void DumpWriter::writeDump(double currentTime){ fileStreams.push_back(&dumpFile); writeFrame(fileStreams, currentTime); + +#ifdef IS_MPI + finalOut.close(); +#endif } void DumpWriter::writeFinal(double currentTime){ + ofstream finalOut; vector fileStreams; #ifdef IS_MPI if(worldRank == 0 ){ - finalOut.seekp(0); +#endif // is_mpi + + finalOut.open( entry_plug->finalName, ios::out | ios::trunc ); + + if( !finalOut ){ + sprintf( painCave.errMsg, + "Could not open \"%s\" for final dump output.\n", + entry_plug->finalName ); + painCave.isFatal = 1; + simError(); + } + +#ifdef IS_MPI } #endif // is_mpi fileStreams.push_back(&finalOut); writeFrame(fileStreams, currentTime); + +#ifdef IS_MPI + finalOut.close(); +#endif } @@ -146,13 +167,50 @@ void DumpWriter::writeFrame( vector& outFil const int BUFFERSIZE = 2000; const int MINIBUFFERSIZE = 100; - char tempBuffer[BUFFERSIZE]; + char tempBuffer[BUFFERSIZE]; char writeLine[BUFFERSIZE]; int i, k; #ifdef IS_MPI + /********************************************************************* + * Documentation? You want DOCUMENTATION? + * + * Why all the potatoes below? + * + * To make a long story short, the original version of DumpWriter + * worked in the most inefficient way possible. Node 0 would + * poke each of the node for an individual atom's formatted data + * as node 0 worked its way down the global index. This was particularly + * inefficient since the method blocked all processors at every atom + * (and did it twice!). + * + * An intermediate version of DumpWriter could be described from Node + * zero's perspective as follows: + * + * 1) Have 100 of your friends stand in a circle. + * 2) When you say go, have all of them start tossing potatoes at + * you (one at a time). + * 3) Catch the potatoes. + * + * It was an improvement, but MPI has buffers and caches that could + * best be described in this analogy as "potato nets", so there's no + * need to block the processors atom-by-atom. + * + * This new and improved DumpWriter works in an even more efficient + * way: + * + * 1) Have 100 of your friend stand in a circle. + * 2) When you say go, have them start tossing 5-pound bags of + * potatoes at you. + * 3) Once you've caught a friend's bag of potatoes, + * toss them a spud to let them know they can toss another bag. + * + * How's THAT for documentation? + * + *********************************************************************/ + int *potatoes; int myPotato; @@ -330,17 +388,17 @@ void DumpWriter::writeFrame( vector& outFil } else { haveError = 0; - which_atom = i; + which_atom = i; - local_index = indexArray[currentIndex].first; - - if (which_atom == indexArray[currentIndex].second) { + local_index = indexArray[currentIndex].first; - atomTypeString = atoms[local_index]->getType(); - - atoms[local_index]->getPos(pos); - atoms[local_index]->getVel(vel); - + if (which_atom == indexArray[currentIndex].second) { + + atomTypeString = atoms[local_index]->getType(); + + atoms[local_index]->getPos(pos); + atoms[local_index]->getVel(vel); + atomData6[0] = pos[0]; atomData6[1] = pos[1]; atomData6[2] = pos[2]; @@ -372,51 +430,51 @@ void DumpWriter::writeFrame( vector& outFil } } else { - sprintf(painCave.errMsg, - "Atom %d not found on processor %d\n", - i, worldRank ); - haveError= 1; - simError(); - } + sprintf(painCave.errMsg, + "Atom %d not found on processor %d, currentIndex = %d, local_index = %d\n", + which_atom, worldRank, currentIndex, local_index ); + haveError= 1; + simError(); + } if(haveError) DieDieDie(); - currentIndex ++; + currentIndex++; } // If we've survived to here, format the line: if (!isDirectional) { sprintf( writeLine, - "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t", - atomTypeString, - atomData6[0], - atomData6[1], - atomData6[2], - atomData6[3], - atomData6[4], - atomData6[5]); - - strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); + "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t", + atomTypeString, + atomData6[0], + atomData6[1], + atomData6[2], + atomData6[3], + atomData6[4], + atomData6[5]); + strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" ); + } else { - sprintf( writeLine, - "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", - atomTypeString, - atomData13[0], - atomData13[1], - atomData13[2], - atomData13[3], - atomData13[4], - atomData13[5], - atomData13[6], - atomData13[7], - atomData13[8], - atomData13[9], - atomData13[10], - atomData13[11], - atomData13[12]); + sprintf( writeLine, + "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", + atomTypeString, + atomData13[0], + atomData13[1], + atomData13[2], + atomData13[3], + atomData13[4], + atomData13[5], + atomData13[6], + atomData13[7], + atomData13[8], + atomData13[9], + atomData13[10], + atomData13[11], + atomData13[12]); } @@ -429,11 +487,11 @@ void DumpWriter::writeFrame( vector& outFil sprintf( checkPointMsg, "Sucessfully took a dump.\n"); - + MPIcheckPoint(); - + delete[] potatoes; - + } else { // worldRank != 0, so I'm a remote node. @@ -450,24 +508,25 @@ void DumpWriter::writeFrame( vector& outFil if (AtomToProcMap[i] == worldRank) { if (myPotato + 3 >= MAXTAG) { - + // The potato was going to exceed the maximum value, // so wrap this processor potato back to 0 (and block until // node 0 says we can go: - + MPI_Recv(&myPotato, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &istatus); } which_atom = i; - local_index = indexArray[currentIndex].first; + + local_index = indexArray[currentIndex].first; - if (which_atom == indexArray[currentIndex].second) { + if (which_atom == indexArray[currentIndex].second) { atomTypeString = atoms[local_index]->getType(); - - atoms[local_index]->getPos(pos); - atoms[local_index]->getVel(vel); - + + atoms[local_index]->getPos(pos); + atoms[local_index]->getVel(vel); + atomData6[0] = pos[0]; atomData6[1] = pos[1]; atomData6[2] = pos[2]; @@ -499,13 +558,13 @@ void DumpWriter::writeFrame( vector& outFil } } else { - sprintf(painCave.errMsg, - "Atom %d not found on processor %d\n", - i, worldRank ); - haveError= 1; - simError(); - } - + sprintf(painCave.errMsg, + "Atom %d not found on processor %d, currentIndex = %d, local_index = %d\n", + which_atom, worldRank, currentIndex, local_index ); + haveError= 1; + simError(); + } + strncpy(MPIatomTypeString, atomTypeString, MINIBUFFERSIZE); // null terminate the string before sending (just in case):