1 |
/* |
2 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
* |
4 |
* The University of Notre Dame grants you ("Licensee") a |
5 |
* non-exclusive, royalty free, license to use, modify and |
6 |
* redistribute this software in source and binary code form, provided |
7 |
* that the following conditions are met: |
8 |
* |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
* notice, this list of conditions and the following disclaimer in the |
14 |
* documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* This software is provided "AS IS," without a warranty of any |
18 |
* kind. All express or implied conditions, representations and |
19 |
* warranties, including any implied warranty of merchantability, |
20 |
* fitness for a particular purpose or non-infringement, are hereby |
21 |
* excluded. The University of Notre Dame and its licensors shall not |
22 |
* be liable for any damages suffered by licensee as a result of |
23 |
* using, modifying or distributing the software or its |
24 |
* derivatives. In no event will the University of Notre Dame or its |
25 |
* licensors be liable for any lost revenue, profit or data, or for |
26 |
* direct, indirect, special, consequential, incidental or punitive |
27 |
* damages, however caused and regardless of the theory of liability, |
28 |
* arising out of the use of or inability to use software, even if the |
29 |
* University of Notre Dame has been advised of the possibility of |
30 |
* such damages. |
31 |
* |
32 |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
* research, please cite the appropriate papers when you publish your |
34 |
* work. Good starting points are: |
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). |
40 |
*/ |
41 |
|
42 |
/** |
43 |
* @file basic_ifstrstream.hpp |
44 |
* @author Teng Lin |
45 |
* @date 10/14/2004 |
46 |
* @version 1.0 |
47 |
*/ |
48 |
|
49 |
#ifndef IO_IFSTRSTREAM_HPP |
50 |
#define IO_IFSTRSTREAM_HPP |
51 |
|
52 |
#include <cassert> |
53 |
#include <cstring> |
54 |
#include <fstream> |
55 |
#include <sstream> |
56 |
|
57 |
#ifdef IS_MPI |
58 |
#include <mpi.h> |
59 |
#endif |
60 |
|
61 |
namespace OpenMD { |
62 |
|
63 |
/** |
64 |
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
65 |
* @brief basic_ifstrstream class provides a stream interface to read data from files. |
66 |
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
67 |
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
68 |
* After brocasting, every node will fall back to stringstream.</p> |
69 |
* |
70 |
* @code |
71 |
* const int MAXLEN = 1024; |
72 |
* char buffer[MAXLEN]; |
73 |
* ifstrstream in; |
74 |
* in.open("Shapes.frc"); |
75 |
* if (in.is_open()) { |
76 |
* in.getline(buffer, MAXLEN); |
77 |
* } |
78 |
* in.close(); |
79 |
* @endcode |
80 |
*/ |
81 |
template <class _CharT, class _Traits, class _Alloc> |
82 |
class basic_ifstrstream : public std::basic_istream<_CharT, _Traits> { |
83 |
public: |
84 |
//traits |
85 |
typedef _CharT char_type; |
86 |
typedef typename _Traits::int_type int_type; |
87 |
typedef typename _Traits::pos_type pos_type; |
88 |
typedef typename _Traits::off_type off_type; |
89 |
typedef _Traits traits_type; |
90 |
|
91 |
typedef std::basic_ios<_CharT, _Traits> _Basic_ios; |
92 |
typedef std::basic_istream<_CharT, _Traits> _Base; |
93 |
|
94 |
#ifdef IS_MPI |
95 |
typedef std::basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
96 |
#else |
97 |
typedef std::basic_filebuf<_CharT, _Traits> _Buf; |
98 |
#endif |
99 |
|
100 |
static const int FileNotExists = -1; |
101 |
static const int FileIOError = -2; |
102 |
|
103 |
public: |
104 |
|
105 |
/** Constructs an object of class ifstream. */ |
106 |
basic_ifstrstream() |
107 |
: std::basic_istream<_CharT, _Traits>(0), |
108 |
internalBuf_(), isRead(false) { |
109 |
|
110 |
this->init(&internalBuf_); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Explicit constructor |
115 |
* @filename String containing the name of the file to be opened |
116 |
* @mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
117 |
* @checkFilename Flags indicating checking the file name in parallel |
118 |
*/ |
119 |
explicit basic_ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false) |
120 |
: std::basic_istream<_CharT, _Traits>(0), |
121 |
internalBuf_(), isRead(false) { |
122 |
|
123 |
this->init(&internalBuf_); |
124 |
isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); |
125 |
} |
126 |
|
127 |
/** |
128 |
* virtual destructor will close the file(in single mode) and clear the stream buffer |
129 |
*/ |
130 |
~basic_ifstrstream(){ |
131 |
close(); |
132 |
} |
133 |
|
134 |
/** |
135 |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
136 |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
137 |
* brocasting, every nodes fall back to stringstream (parallel mode). |
138 |
* @filename String containing the name of the file to be opened |
139 |
* @mode Flags describing the requested i/o mode for the file |
140 |
* @checkFilename Flags indicating checking the file name in parallel |
141 |
*/ |
142 |
void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false){ |
143 |
|
144 |
if (!isRead ) { |
145 |
isRead = internalOpen(filename, mode, checkFilename); |
146 |
} |
147 |
} |
148 |
|
149 |
/** |
150 |
* Tests if the stream is currently associated with a valid buffer. |
151 |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
152 |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
153 |
*/ |
154 |
bool is_open ( ) { |
155 |
#ifdef IS_MPI |
156 |
return isRead; |
157 |
#else |
158 |
//single version fall back to ifstream |
159 |
return internalBuf_.is_open(); |
160 |
#endif |
161 |
} |
162 |
|
163 |
/** |
164 |
* In single mode, closes a file. The stream's file buffer is released from its association with |
165 |
* the currently open file. In parallel mode, clean the |
166 |
*/ |
167 |
void close() { |
168 |
#ifndef IS_MPI |
169 |
//single version fall back to ifstream |
170 |
if (!internalBuf_.close()) |
171 |
this->setstate(std::ios_base::failbit); |
172 |
#endif |
173 |
|
174 |
isRead = false; |
175 |
} |
176 |
|
177 |
/** |
178 |
* Gets the stream buffer object associated with the stream |
179 |
* @return A pointer to the stream buffe object(filebuf in single mode, stringbuf in |
180 |
* parallel mode) associated with the stream. |
181 |
*/ |
182 |
_Buf* rdbuf() const{ |
183 |
return static_cast<_Buf*>(&internalBuf_); |
184 |
} |
185 |
|
186 |
private: |
187 |
|
188 |
/** |
189 |
* Internal function used to open the file |
190 |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
191 |
* otherwise return false |
192 |
* @filename String containing the name of the file to be opened |
193 |
* @mode Flags describing the requested i/o mode for the file |
194 |
* @todo use try - catch syntax to make the program more readable |
195 |
*/ |
196 |
bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
197 |
|
198 |
#ifdef IS_MPI |
199 |
int commStatus; |
200 |
long fileSize; |
201 |
char* fbuf; |
202 |
int filenameLen; |
203 |
int diffFilename; |
204 |
int error; |
205 |
int myRank; |
206 |
int masterNode; |
207 |
|
208 |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
209 |
masterNode = 0; |
210 |
|
211 |
if (myRank == masterNode) { |
212 |
|
213 |
if (checkFilename) { |
214 |
|
215 |
//check the filename is the same |
216 |
filenameLen = strlen(filename); |
217 |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
218 |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
219 |
|
220 |
diffFilename = 0; |
221 |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
222 |
|
223 |
//if file names are different just return false |
224 |
if (error > 0) |
225 |
return false; |
226 |
} |
227 |
|
228 |
std::ifstream fin(filename, mode); |
229 |
|
230 |
if (fin.is_open()) { |
231 |
|
232 |
fin.seekg(0, std::ios::end); |
233 |
fileSize = fin.tellg(); |
234 |
fin.seekg(0, std::ios::beg); |
235 |
|
236 |
// '\0' need one more char |
237 |
fbuf = new char[fileSize+1]; |
238 |
|
239 |
assert(fbuf != 0); |
240 |
|
241 |
fin.read(fbuf, fileSize); |
242 |
|
243 |
if (fin.fail()) |
244 |
fileSize = FileIOError; |
245 |
|
246 |
//brocasting the file size |
247 |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
248 |
|
249 |
if (fileSize < 0) { |
250 |
fin.close(); |
251 |
delete fbuf; |
252 |
|
253 |
return false; |
254 |
} |
255 |
|
256 |
// make a c-style std::string and brocasting it |
257 |
fbuf[fileSize] = '\0'; |
258 |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
259 |
|
260 |
//close the file and delete the buffer |
261 |
fin.close(); |
262 |
internalBuf_.str(fbuf); |
263 |
delete [] fbuf; |
264 |
}else{ |
265 |
fileSize = FileNotExists; |
266 |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
267 |
return false; |
268 |
} |
269 |
|
270 |
} else{ //slave nodes |
271 |
|
272 |
//check file name |
273 |
if (checkFilename) { |
274 |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
275 |
|
276 |
char * masterFilename = new char[filenameLen]; |
277 |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
278 |
|
279 |
if( strcmp(masterFilename, filename) == 0) |
280 |
diffFilename = 0; |
281 |
else |
282 |
diffFilename = 1; |
283 |
|
284 |
delete masterFilename; |
285 |
|
286 |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
287 |
|
288 |
if (error > 0) |
289 |
return false; |
290 |
} |
291 |
//get file size |
292 |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
293 |
|
294 |
if (fileSize >= 0 ) { |
295 |
fbuf = new char[fileSize+1]; |
296 |
assert(fbuf); |
297 |
|
298 |
//receive file content |
299 |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
300 |
|
301 |
internalBuf_.str(fbuf); |
302 |
delete [] fbuf; |
303 |
|
304 |
} else if (fileSize == FileNotExists ) { |
305 |
return false; |
306 |
|
307 |
} else if (fileSize == FileIOError ) { |
308 |
return false; |
309 |
} |
310 |
} |
311 |
|
312 |
#else |
313 |
//in single version, fall back to ifstream |
314 |
if (!internalBuf_.open(filename, mode)) { |
315 |
this->setstate(std::ios_base::failbit); |
316 |
return false; |
317 |
} |
318 |
|
319 |
#endif |
320 |
this->clear(); |
321 |
return true; |
322 |
} |
323 |
|
324 |
_Buf internalBuf_; /** internal stream buffer */ |
325 |
bool isRead; /** file opened flag */ |
326 |
}; |
327 |
|
328 |
typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream; |
329 |
}//namespace OpenMD |
330 |
#endif //IO_IFSTRSTREAM_HPP |