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). |
39 |
< |
* [4] Vardeman & Gezelter, in progress (2009). |
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 |
48 |
|
*/ |
49 |
|
|
49 |
– |
#include "io/ifstrstream.hpp" |
50 |
– |
|
50 |
|
#ifdef IS_MPI |
51 |
|
#include <mpi.h> |
52 |
|
#endif |
53 |
|
|
54 |
+ |
#include "io/ifstrstream.hpp" |
55 |
+ |
|
56 |
|
namespace OpenMD { |
57 |
|
|
58 |
|
/** Constructs an object of class ifstream. */ |
59 |
|
#ifdef IS_MPI |
60 |
|
ifstrstream::ifstrstream() |
61 |
|
: std::basic_istream<char, std::char_traits<char> >(0), |
62 |
< |
internalStringBuf_(), isRead(false) { |
62 |
> |
internalStringBuf_(), isRead(false) { |
63 |
|
this->init(&internalStringBuf_); |
64 |
|
} |
65 |
|
#else |
66 |
|
ifstrstream::ifstrstream() |
67 |
|
: std::basic_istream<char, std::char_traits<char> >(0), |
68 |
< |
internalFileBuf_(), isRead(false) { |
68 |
> |
internalFileBuf_(), isRead(false) { |
69 |
|
this->init(&internalFileBuf_); |
70 |
|
} |
71 |
|
#endif |
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, |
77 |
> |
* default value is ios_base::in |
78 |
> |
* @param checkFilename Flags indicating checking the file name in parallel |
79 |
|
*/ |
80 |
|
#ifdef IS_MPI |
81 |
|
ifstrstream::ifstrstream(const char* filename, std::ios_base::openmode mode, bool checkFilename) |
94 |
|
} |
95 |
|
#endif |
96 |
|
/** |
97 |
< |
* virtual destructor will close the file(in single mode) and clear the stream buffer |
97 |
> |
* virtual destructor will close the file (in single mode) and clear |
98 |
> |
* the stream buffer |
99 |
|
*/ |
100 |
|
ifstrstream::~ifstrstream(){ |
101 |
|
close(); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
106 |
< |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
107 |
< |
* brocasting, every nodes fall back to stringstream (parallel mode). |
108 |
< |
* @filename String containing the name of the file to be opened |
109 |
< |
* @mode Flags describing the requested i/o mode for the file |
110 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
105 |
> |
* Opens a file and associates a buffer with the specified file to |
106 |
> |
* perform the i/o operations (single mode). The master node reads a |
107 |
> |
* file and broadcasts its content to the other slave nodes. After |
108 |
> |
* broadcasting, all nodes fall back to stringstream (parallel |
109 |
> |
* mode). |
110 |
> |
* @param filename String containing the name of the file to be opened |
111 |
> |
* @param mode Flags describing the requested i/o mode for the file |
112 |
> |
* @param checkFilename Flags indicating checking the file name in parallel |
113 |
|
*/ |
114 |
|
void ifstrstream::open(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
115 |
|
|
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
< |
* Tests if the stream is currently associated with a valid buffer. |
123 |
< |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
124 |
< |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
122 |
> |
* Tests if the stream is currently associated with a valid buffer. |
123 |
> |
* @return true if a file has successfully been opened (single mode) |
124 |
> |
* or the whole file has been read and spread among all of the |
125 |
> |
* processors (parallel mode), otherwise false is returned |
126 |
|
*/ |
127 |
|
bool ifstrstream::is_open ( ) { |
128 |
|
#ifdef IS_MPI |
136 |
|
/** |
137 |
|
* In single mode, closes a file. The stream's file buffer is |
138 |
|
* released from its association with the currently open file. In |
139 |
< |
* parallel mode, clean the |
139 |
> |
* parallel mode, cleans up. |
140 |
|
*/ |
141 |
|
void ifstrstream::close() { |
142 |
|
#ifndef IS_MPI |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* Internal function used to open the file |
166 |
< |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
167 |
< |
* otherwise return false |
168 |
< |
* @filename String containing the name of the file to be opened |
169 |
< |
* @mode Flags describing the requested i/o mode for the file |
170 |
< |
* @todo use try - catch syntax to make the program more readable |
171 |
< |
*/ |
165 |
> |
* Internal function used to open the file |
166 |
> |
* @return true if succesfully opens a file (single mode) or gets |
167 |
> |
* the file content (parallel mode) otherwise return false |
168 |
> |
* @param filename String containing the name of the file to be opened |
169 |
> |
* @param mode Flags describing the requested i/o mode for the file |
170 |
> |
* @param checkFilename Flags indicating checking the file name in parallel |
171 |
> |
* @todo use try - catch syntax to make the program more readable |
172 |
> |
*/ |
173 |
|
|
174 |
|
bool ifstrstream::internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
175 |
|
|
176 |
|
#ifdef IS_MPI |
177 |
< |
int commStatus; |
177 |
> |
//int commStatus; |
178 |
|
long fileSize; |
179 |
|
char* fbuf; |
180 |
|
int filenameLen; |
183 |
|
int myRank; |
184 |
|
int masterNode; |
185 |
|
|
186 |
< |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
186 |
> |
myRank = MPI::COMM_WORLD.Get_rank(); |
187 |
|
masterNode = 0; |
188 |
|
|
189 |
|
if (myRank == masterNode) { |
192 |
|
|
193 |
|
//check the filename is the same |
194 |
|
filenameLen = strlen(filename); |
195 |
< |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
196 |
< |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
195 |
> |
MPI::COMM_WORLD.Bcast(&filenameLen, 1, MPI::INT, masterNode); |
196 |
> |
MPI::COMM_WORLD.Bcast((void*)filename, filenameLen, MPI::CHAR, |
197 |
> |
masterNode); |
198 |
|
|
199 |
|
diffFilename = 0; |
200 |
< |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
200 |
> |
MPI::COMM_WORLD.Allreduce(&diffFilename, &error, 1, MPI::INT, MPI::SUM); |
201 |
|
|
202 |
|
//if file names are different just return false |
203 |
|
if (error > 0) |
222 |
|
if (fin.fail()) |
223 |
|
fileSize = FileIOError; |
224 |
|
|
225 |
< |
//brocasting the file size |
226 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
225 |
> |
//broadcast the file size |
226 |
> |
MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); |
227 |
|
|
228 |
|
if (fileSize < 0) { |
229 |
|
fin.close(); |
230 |
< |
delete fbuf; |
230 |
> |
delete[] fbuf; |
231 |
|
|
232 |
|
return false; |
233 |
|
} |
234 |
|
|
235 |
< |
// make a c-style std::string and brocasting it |
235 |
> |
// make a c-style std::string and broadcast it |
236 |
|
fbuf[fileSize] = '\0'; |
237 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
237 |
> |
MPI::COMM_WORLD.Bcast(fbuf, fileSize + 1, MPI::CHAR, masterNode); |
238 |
|
|
239 |
|
//close the file and delete the buffer |
240 |
|
fin.close(); |
241 |
|
internalStringBuf_.str(fbuf); |
242 |
< |
delete [] fbuf; |
242 |
> |
delete[] fbuf; |
243 |
|
}else{ |
244 |
|
fileSize = FileNotExists; |
245 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
245 |
> |
MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); |
246 |
|
return false; |
247 |
|
} |
248 |
|
|
250 |
|
|
251 |
|
//check file name |
252 |
|
if (checkFilename) { |
253 |
< |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
253 |
> |
MPI::COMM_WORLD.Bcast(&filenameLen, 1, MPI::INT, masterNode); |
254 |
|
|
255 |
|
char * masterFilename = new char[filenameLen]; |
256 |
< |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
256 |
> |
MPI::COMM_WORLD.Bcast(masterFilename, filenameLen, MPI::CHAR, |
257 |
> |
masterNode); |
258 |
|
|
259 |
|
if( strcmp(masterFilename, filename) == 0) |
260 |
|
diffFilename = 0; |
261 |
|
else |
262 |
|
diffFilename = 1; |
263 |
|
|
264 |
< |
delete masterFilename; |
264 |
> |
delete[] masterFilename; |
265 |
|
|
266 |
< |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
266 |
> |
MPI::COMM_WORLD.Allreduce(&diffFilename, &error, 1, MPI::INT, MPI::SUM); |
267 |
|
|
268 |
|
if (error > 0) |
269 |
|
return false; |
270 |
|
} |
271 |
|
//get file size |
272 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
272 |
> |
MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); |
273 |
|
|
274 |
|
if (fileSize >= 0 ) { |
275 |
|
fbuf = new char[fileSize+1]; |
276 |
|
assert(fbuf); |
277 |
|
|
278 |
|
//receive file content |
279 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
279 |
> |
MPI::COMM_WORLD.Bcast(fbuf, fileSize + 1, MPI::CHAR, masterNode); |
280 |
|
|
281 |
|
internalStringBuf_.str(fbuf); |
282 |
|
delete [] fbuf; |