| 35 |
|
* |
| 36 |
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
| 37 |
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
| 38 |
< |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
| 38 |
> |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
| 39 |
|
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
| 40 |
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
| 41 |
|
*/ |
| 42 |
|
|
| 43 |
|
/** |
| 44 |
< |
* @file basic_ifstrstream.cpp |
| 44 |
> |
* @file ifstrstream.cpp |
| 45 |
|
* @author Teng Lin |
| 46 |
|
* @date 10/14/2004 |
| 47 |
|
* @version 1.0 |
| 72 |
|
|
| 73 |
|
/** |
| 74 |
|
* Explicit constructor |
| 75 |
< |
* @filename String containing the name of the file to be opened |
| 76 |
< |
* @mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
| 77 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
| 75 |
> |
* @param filename String containing the name of the file to be opened |
| 76 |
> |
* @param mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
| 77 |
> |
* @param checkFilename Flags indicating checking the file name in parallel |
| 78 |
|
*/ |
| 79 |
|
#ifdef IS_MPI |
| 80 |
|
ifstrstream::ifstrstream(const char* filename, std::ios_base::openmode mode, bool checkFilename) |
| 103 |
|
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
| 104 |
|
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
| 105 |
|
* brocasting, every nodes fall back to stringstream (parallel mode). |
| 106 |
< |
* @filename String containing the name of the file to be opened |
| 107 |
< |
* @mode Flags describing the requested i/o mode for the file |
| 108 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
| 106 |
> |
* @param filename String containing the name of the file to be opened |
| 107 |
> |
* @param mode Flags describing the requested i/o mode for the file |
| 108 |
> |
* @param checkFilename Flags indicating checking the file name in parallel |
| 109 |
|
*/ |
| 110 |
|
void ifstrstream::open(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
| 111 |
|
|
| 160 |
|
* Internal function used to open the file |
| 161 |
|
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
| 162 |
|
* otherwise return false |
| 163 |
< |
* @filename String containing the name of the file to be opened |
| 164 |
< |
* @mode Flags describing the requested i/o mode for the file |
| 163 |
> |
* @param filename String containing the name of the file to be opened |
| 164 |
> |
* @param mode Flags describing the requested i/o mode for the file |
| 165 |
> |
* @param checkFilename Flags indicating checking the file name in parallel |
| 166 |
|
* @todo use try - catch syntax to make the program more readable |
| 167 |
|
*/ |
| 168 |
|
|
| 169 |
|
bool ifstrstream::internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
| 170 |
|
|
| 171 |
|
#ifdef IS_MPI |
| 172 |
< |
int commStatus; |
| 172 |
> |
//int commStatus; |
| 173 |
|
long fileSize; |
| 174 |
|
char* fbuf; |
| 175 |
|
int filenameLen; |
| 178 |
|
int myRank; |
| 179 |
|
int masterNode; |
| 180 |
|
|
| 181 |
< |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
| 181 |
> |
myRank = MPI::COMM_WORLD.Get_rank(); |
| 182 |
|
masterNode = 0; |
| 183 |
|
|
| 184 |
|
if (myRank == masterNode) { |
| 187 |
|
|
| 188 |
|
//check the filename is the same |
| 189 |
|
filenameLen = strlen(filename); |
| 190 |
< |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 191 |
< |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 190 |
> |
MPI::COMM_WORLD.Bcast(&filenameLen, 1, MPI::INT, masterNode); |
| 191 |
> |
MPI::COMM_WORLD.Bcast((void*)filename, filenameLen, MPI::CHAR, |
| 192 |
> |
masterNode); |
| 193 |
|
|
| 194 |
|
diffFilename = 0; |
| 195 |
< |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 196 |
< |
|
| 195 |
> |
MPI::COMM_WORLD.Allreduce(&diffFilename, &error, 1, MPI::INT, MPI::SUM); |
| 196 |
> |
|
| 197 |
|
//if file names are different just return false |
| 198 |
|
if (error > 0) |
| 199 |
|
return false; |
| 217 |
|
if (fin.fail()) |
| 218 |
|
fileSize = FileIOError; |
| 219 |
|
|
| 220 |
< |
//brocasting the file size |
| 221 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 220 |
> |
//broadcast the file size |
| 221 |
> |
MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); |
| 222 |
|
|
| 223 |
|
if (fileSize < 0) { |
| 224 |
|
fin.close(); |
| 225 |
< |
delete fbuf; |
| 225 |
> |
delete[] fbuf; |
| 226 |
|
|
| 227 |
|
return false; |
| 228 |
|
} |
| 229 |
|
|
| 230 |
< |
// make a c-style std::string and brocasting it |
| 230 |
> |
// make a c-style std::string and broadcast it |
| 231 |
|
fbuf[fileSize] = '\0'; |
| 232 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 232 |
> |
MPI::COMM_WORLD.Bcast(fbuf, fileSize + 1, MPI::CHAR, masterNode); |
| 233 |
|
|
| 234 |
|
//close the file and delete the buffer |
| 235 |
|
fin.close(); |
| 236 |
|
internalStringBuf_.str(fbuf); |
| 237 |
< |
delete [] fbuf; |
| 237 |
> |
delete[] fbuf; |
| 238 |
|
}else{ |
| 239 |
|
fileSize = FileNotExists; |
| 240 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 240 |
> |
MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); |
| 241 |
|
return false; |
| 242 |
|
} |
| 243 |
|
|
| 245 |
|
|
| 246 |
|
//check file name |
| 247 |
|
if (checkFilename) { |
| 248 |
< |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 248 |
> |
MPI::COMM_WORLD.Bcast(&filenameLen, 1, MPI::INT, masterNode); |
| 249 |
|
|
| 250 |
|
char * masterFilename = new char[filenameLen]; |
| 251 |
< |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 251 |
> |
MPI::COMM_WORLD.Bcast(masterFilename, filenameLen, MPI::CHAR, |
| 252 |
> |
masterNode); |
| 253 |
|
|
| 254 |
|
if( strcmp(masterFilename, filename) == 0) |
| 255 |
|
diffFilename = 0; |
| 256 |
|
else |
| 257 |
|
diffFilename = 1; |
| 258 |
|
|
| 259 |
< |
delete masterFilename; |
| 259 |
> |
delete[] masterFilename; |
| 260 |
|
|
| 261 |
< |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 261 |
> |
MPI::COMM_WORLD.Allreduce(&diffFilename, &error, 1, MPI::INT, MPI::SUM); |
| 262 |
|
|
| 263 |
|
if (error > 0) |
| 264 |
|
return false; |
| 265 |
|
} |
| 266 |
|
//get file size |
| 267 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 267 |
> |
MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); |
| 268 |
|
|
| 269 |
|
if (fileSize >= 0 ) { |
| 270 |
|
fbuf = new char[fileSize+1]; |
| 271 |
|
assert(fbuf); |
| 272 |
|
|
| 273 |
|
//receive file content |
| 274 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 274 |
> |
MPI::COMM_WORLD.Bcast(fbuf, fileSize + 1, MPI::CHAR, masterNode); |
| 275 |
|
|
| 276 |
|
internalStringBuf_.str(fbuf); |
| 277 |
|
delete [] fbuf; |