--- branches/development/src/io/ifstrstream.cpp 2011/11/22 20:38:56 1665 +++ trunk/src/io/ifstrstream.cpp 2014/02/26 14:14:50 1969 @@ -35,77 +35,80 @@ * * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). - * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). */ /** - * @file basic_ifstrstream.cpp + * @file ifstrstream.cpp * @author Teng Lin * @date 10/14/2004 * @version 1.0 */ -#include "io/ifstrstream.hpp" - #ifdef IS_MPI #include #endif +#include "io/ifstrstream.hpp" + namespace OpenMD { /** Constructs an object of class ifstream. */ #ifdef IS_MPI ifstrstream::ifstrstream() : std::basic_istream >(0), - internalStringBuf_(), isRead(false) { + internalStringBuf_(), isRead(false) { this->init(&internalStringBuf_); } #else ifstrstream::ifstrstream() : std::basic_istream >(0), - internalFileBuf_(), isRead(false) { + internalFileBuf_(), isRead(false) { this->init(&internalFileBuf_); } #endif /** * Explicit constructor - * @filename String containing the name of the file to be opened - * @mode Flags describing the requested i/o mode for the file, default value is ios_base::in - * @checkFilename Flags indicating checking the file name in parallel + * @param filename String containing the name of the file to be opened + * @param mode Flags describing the requested i/o mode for the file, + * default value is ios_base::in + * @param checkFilename Flags indicating checking the file name in parallel */ #ifdef IS_MPI ifstrstream::ifstrstream(const char* filename, std::ios_base::openmode mode, bool checkFilename) : std::basic_istream >(0), - internalStringBuf_(), isRead(false) { + internalStringBuf_(), isRead(false) { this->init(&internalStringBuf_); isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); } #else ifstrstream::ifstrstream(const char* filename, std::ios_base::openmode mode, bool checkFilename) : std::basic_istream >(0), - internalFileBuf_(), isRead(false) { + internalFileBuf_(), isRead(false) { this->init(&internalFileBuf_); isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); } #endif /** - * virtual destructor will close the file(in single mode) and clear the stream buffer + * virtual destructor will close the file (in single mode) and clear + * the stream buffer */ - ifstrstream::~ifstrstream(){ - close(); + ifstrstream::~ifstrstream(){ close(); } /** - * Opens a file and associats a buffer with the specified file to perform the i/o operations - * (single mode). Master reads a file and brocasts its content to the other slave nodes. After - * brocasting, every nodes fall back to stringstream (parallel mode). - * @filename String containing the name of the file to be opened - * @mode Flags describing the requested i/o mode for the file - * @checkFilename Flags indicating checking the file name in parallel + * Opens a file and associates a buffer with the specified file to + * perform the i/o operations (single mode). The master node reads a + * file and broadcasts its content to the other slave nodes. After + * broadcasting, all nodes fall back to stringstream (parallel + * mode). + * @param filename String containing the name of the file to be opened + * @param mode Flags describing the requested i/o mode for the file + * @param checkFilename Flags indicating checking the file name in parallel */ void ifstrstream::open(const char* filename, std::ios_base::openmode mode, bool checkFilename){ @@ -115,9 +118,10 @@ namespace OpenMD { } /** - * Tests if the stream is currently associated with a valid buffer. - * @return true if a file has successfully been opened (single mode) or the whole file is read - * and spreaded among all of the processors (parallel mode), otherwise false is returned + * Tests if the stream is currently associated with a valid buffer. + * @return true if a file has successfully been opened (single mode) + * or the whole file has been read and spread among all of the + * processors (parallel mode), otherwise false is returned */ bool ifstrstream::is_open ( ) { #ifdef IS_MPI @@ -131,7 +135,7 @@ namespace OpenMD { /** * In single mode, closes a file. The stream's file buffer is * released from its association with the currently open file. In - * parallel mode, clean the + * parallel mode, cleans up. */ void ifstrstream::close() { #ifndef IS_MPI @@ -157,18 +161,19 @@ namespace OpenMD { } /** - * Internal function used to open the file - * @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) - * otherwise return false - * @filename String containing the name of the file to be opened - * @mode Flags describing the requested i/o mode for the file - * @todo use try - catch syntax to make the program more readable - */ + * Internal function used to open the file + * @return true if succesfully opens a file (single mode) or gets + * the file content (parallel mode) otherwise return false + * @param filename String containing the name of the file to be opened + * @param mode Flags describing the requested i/o mode for the file + * @param checkFilename Flags indicating checking the file name in parallel + * @todo use try - catch syntax to make the program more readable + */ bool ifstrstream::internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ #ifdef IS_MPI - int commStatus; + //int commStatus; long fileSize; char* fbuf; int filenameLen; @@ -177,7 +182,8 @@ namespace OpenMD { int myRank; int masterNode; - commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); + MPI_Comm_rank( MPI_COMM_WORLD, &myRank); + masterNode = 0; if (myRank == masterNode) { @@ -186,11 +192,13 @@ namespace OpenMD { //check the filename is the same filenameLen = strlen(filename); - commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); - commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); + MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); + MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, + masterNode, MPI_COMM_WORLD); diffFilename = 0; - commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, + MPI_COMM_WORLD); //if file names are different just return false if (error > 0) @@ -215,27 +223,27 @@ namespace OpenMD { if (fin.fail()) fileSize = FileIOError; - //brocasting the file size - commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); + //broadcast the file size + MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); if (fileSize < 0) { fin.close(); - delete fbuf; + delete[] fbuf; return false; } - // make a c-style std::string and brocasting it + // make a c-style std::string and broadcast it fbuf[fileSize] = '\0'; - commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); + MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); //close the file and delete the buffer fin.close(); internalStringBuf_.str(fbuf); - delete [] fbuf; + delete[] fbuf; }else{ fileSize = FileNotExists; - commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); + MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); return false; } @@ -243,32 +251,34 @@ namespace OpenMD { //check file name if (checkFilename) { - commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); + MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); char * masterFilename = new char[filenameLen]; - commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); + MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, + masterNode, MPI_COMM_WORLD); if( strcmp(masterFilename, filename) == 0) diffFilename = 0; else diffFilename = 1; - delete masterFilename; + delete[] masterFilename; - commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, + MPI_COMM_WORLD); if (error > 0) return false; } //get file size - commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); + MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); if (fileSize >= 0 ) { fbuf = new char[fileSize+1]; assert(fbuf); //receive file content - commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); + MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); internalStringBuf_.str(fbuf); delete [] fbuf;