--- trunk/mdtools/md_code/InitializeFromFile.cpp 2002/12/13 17:59:52 209 +++ trunk/mdtools/md_code/InitializeFromFile.cpp 2003/01/30 20:03:37 254 @@ -11,6 +11,9 @@ #include "ReadWrite.hpp" #include "simError.h" +#ifdef IS_MPI +#include "mpiSimulation.hpp" +#endif // is_mpi InitializeFromFile :: InitializeFromFile( char *in_name ){ #ifdef IS_MPI @@ -68,11 +71,11 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr char *eof_test; // ptr to see when we reach the end of the file char *parseErr; + int procIndex; + entry_plug = the_entry_plug; - entry_plug = the_entry_plug - #ifndef IS_MPI eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if( eof_test == NULL ){ @@ -133,9 +136,14 @@ 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[MPI_STATUS_SIZE]; - if (worldRank == 0) { eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if( eof_test == NULL ){ @@ -149,8 +157,11 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr Atom **atoms = entry_plug->atoms; DirectionalAtom* dAtom; + + // Check to see that the number of atoms in the intial configuration file is the + // same as declared in simBass. - if( n_atoms != entry_plug->mpiSim->getTotAtoms() ){ + if( n_atoms != mpiSim->getTotAtoms() ){ sprintf( painCave.errMsg, "Initialize from File error. %s n_atoms, %d, " "does not match the BASS file's n_atoms, %d.\n", @@ -168,33 +179,126 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr painCave.isFatal = 1; simError(); } - } - - for( i=0; i < n_atoms; i++){ + + // Read Proc 0 share of the xyz file... + masterIndex = 0; + for( i=0; i <= mpiSim->getMyAtomEnd(); i++){ - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); - if(eof_test == NULL){ - 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(); - } + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + if(eof_test == NULL){ + 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(); + } - parseErr = parseDumpLine( read_buffer, i ); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - 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,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); + + mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,MPI_ANY_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,MPI_ANY_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(); + } + sendError = 0; + mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_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,MPI_ANY_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,MPI_ANY_TAG,MPI_COMM_WORLD); + mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); + if (sendError) MPIcheckPoint(); + + masterIndex++; + } + } + + + else if(worldRank == procIndex){ + nodeAtomsStart = mpiSim->getMyAtomStart(); + nodeAtomsEnd = mpiSim->getMyAtomEnd(); + mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + + mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_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,MPI_ANY_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,MPI_ANY_TAG,MPI_COMM_WORLD); + + + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } + sendError = 0; + mpiErr = MPI_Send(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + } + } + sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex); + MPIcheckPoint(); } + #endif } -char* IntitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ +char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ char *foo; // the pointer to the current string token @@ -210,10 +314,10 @@ char* IntitializeFromFile::parseDumpLine(char* readLin int n_atoms; #ifdef IS_MPI - n_atoms = entry_plug->mpiSim->getTotAtoms(); + n_atoms = mpiSim->getTotAtoms(); #else n_atoms = entry_plug->n_atoms; -#endi // is_mpi +#endif // is_mpi // set the string tokenizer