--- trunk/OOPSE/libmdtools/InitializeFromFile.cpp 2003/03/21 17:42:12 378 +++ trunk/OOPSE/libmdtools/InitializeFromFile.cpp 2003/03/27 01:49:45 417 @@ -12,8 +12,9 @@ #include "simError.h" #ifdef IS_MPI +#include +#include #include "mpiSimulation.hpp" - #define TAKE_THIS_TAG 0 #endif // is_mpi @@ -62,7 +63,7 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){ - int i; // loop counter + int i, j, done, which_node, which_atom; // loop counter const int BUFFERSIZE = 2000; // size of the read buffer int n_atoms; // the number of atoms @@ -138,14 +139,9 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr // MPI Section of code.......... #else //IS_MPI - int masterIndex; - int nodeAtomsStart; - int nodeAtomsEnd; - int mpiErr; - int sendError; + MPI::Status istatus; + int *AtomToProcMap = mpiSim->getAtomToProcMap(); - MPI_Status istatus[MPI_STATUS_SIZE]; - if (worldRank == 0) { eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if( eof_test == NULL ){ @@ -182,10 +178,9 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr simError(); } - // Read Proc 0 share of the xyz file... - masterIndex = 0; - for( i=0; i <= mpiSim->getMyAtomEnd(); i++){ - + + for (i=0 ; i < mpiSim->getTotAtoms(); i++) { + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if(eof_test == NULL){ sprintf(painCave.errMsg, @@ -197,109 +192,52 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr simError(); } - parseErr = parseDumpLine( read_buffer, i ); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); - } - masterIndex++; - } - } - - sprintf(checkPointMsg, - "Node 0 has successfully read positions from input file."); - MPIcheckPoint(); - - for (procIndex = 1; procIndex < mpiSim->getNumberProcessors(); - procIndex++){ - if (worldRank == 0) { - - mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, - istatus); - - mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, - istatus); - // Make sure where node 0 is reading from, matches where the receiving node - // expects it to be. - - if (masterIndex != nodeAtomsStart){ - sendError = 1; - mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); - sprintf(painCave.errMsg, - "Initialize from file error: atoms start index (%d) for " - "node %d not equal to master index (%d)",nodeAtomsStart,procIndex,masterIndex ); - painCave.isFatal = 1; - simError(); + // Get the Node number which wants this atom: + which_node = AtomToProcMap[i]; + if (which_node == mpiSim->getMyNode()) { + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } + } else { + MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, + TAKE_THIS_TAG); + MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG); } - sendError = 0; - mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); - - for ( i = nodeAtomsStart; i <= nodeAtomsEnd; i++){ - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); - if(eof_test == NULL){ - - sprintf(read_buffer,"ERROR"); - mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); - - sprintf(painCave.errMsg, - "error in reading file %s\n" - "natoms = %d; index = %d\n" - "error reading the line from the file.\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); - } - - mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); - mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, - istatus); - if (sendError) MPIcheckPoint(); - - masterIndex++; - } + } + sprintf(read_buffer, "GAMEOVER"); + for (j = 0; j < mpiSim->getNumberProcessors(); j++) { + MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, j, + TAKE_THIS_TAG); } + } else { - else if(worldRank == procIndex){ - nodeAtomsStart = mpiSim->getMyAtomStart(); - nodeAtomsEnd = mpiSim->getMyAtomEnd(); - mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); - mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); - - mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD, - istatus); - if (sendError) MPIcheckPoint(); - - for ( i = 0; i < entry_plug->n_atoms; i++){ - - mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,0,TAKE_THIS_TAG,MPI_COMM_WORLD, - istatus); - - if(!strcmp(read_buffer, "ERROR")) MPIcheckPoint(); - - parseErr = parseDumpLine( read_buffer, i ); - if( parseErr != NULL ){ - sendError = 1; - mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); - - - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); - } - sendError = 0; - mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); + done = 0; + while (!done) { + MPI::COMM_WORLD.Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, + TAKE_THIS_TAG, istatus); + if (strcmp(read_buffer, "GAMEOVER")) { + done = 1; + continue; + } else { + MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, + TAKE_THIS_TAG, istatus); + parseErr = parseDumpLine( read_buffer, which_atom ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } } } - sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex); - MPIcheckPoint(); } - + #endif } - char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ char *foo; // the pointer to the current string token