33 |
|
#ifndef IO_IFSTRSTREAM_HPP |
34 |
|
#define IO_IFSTRSTREAM_HPP |
35 |
|
|
36 |
+ |
#include <cassert> |
37 |
|
#include <cstring> |
38 |
|
#include <fstream> |
39 |
|
#include <sstream> |
43 |
|
#endif |
44 |
|
|
45 |
|
namespace oopse { |
46 |
< |
using namespace std; |
46 |
> |
|
47 |
|
/** |
48 |
|
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
49 |
< |
* @brief class provides a stream interface to read data from files. |
49 |
> |
* @brief basic_ifstrstream class provides a stream interface to read data from files. |
50 |
|
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
51 |
|
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
52 |
|
* After brocasting, every node will fall back to stringstream.</p> |
81 |
|
typedef basic_filebuf<_CharT, _Traits> _Buf; |
82 |
|
#endif |
83 |
|
|
84 |
< |
static const int FileNoExists = -1; |
84 |
> |
static const int FileNotExists = -1; |
85 |
|
static const int FileIOError = -2; |
86 |
|
|
87 |
|
public: |
176 |
|
* parallel mode) associated with the stream. |
177 |
|
*/ |
178 |
|
_Buf* rdbuf() const{ |
179 |
< |
return const_cast<_Buf*>(internalBuf_); |
179 |
> |
return static_cast<_Buf*>(internalBuf_); |
180 |
|
} |
181 |
|
|
182 |
|
private: |
210 |
|
|
211 |
|
//check the filename is the same |
212 |
|
filenameLen = strlen(filename); |
213 |
< |
commStatus = MPI_Bcast(filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
214 |
< |
commStatus = MPI_Bcast(filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
213 |
> |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
214 |
> |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
215 |
|
|
216 |
|
diffFilename = 0; |
217 |
< |
commStatus = MPI_Allreduce(diffFilename, error, 1, MPI_INT, MPI_COMM_WORLD); |
217 |
> |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
218 |
|
|
219 |
|
//if file names are different just return false |
220 |
|
if (error > 0) |
226 |
|
|
227 |
|
if (fin.is_open()) { |
228 |
|
|
229 |
< |
fin.in.seekg(0, ios::end); |
229 |
> |
fin.seekg(0, ios::end); |
230 |
|
fileSize = fin.tellg(); |
231 |
< |
|
231 |
> |
fin.seekg(0, ios::beg); |
232 |
> |
|
233 |
|
// '\0' need one more char |
234 |
|
fbuf = new char[fileSize+1]; |
235 |
|
|
250 |
|
return false; |
251 |
|
} |
252 |
|
|
253 |
< |
// make a c-style string and brocasting it |
253 |
> |
// make a c-style std::stringand brocasting it |
254 |
|
fbuf[fileSize] = '\0'; |
255 |
|
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
256 |
|
|
266 |
|
fin.close(); |
267 |
|
delete fbuf; |
268 |
|
}else{ |
269 |
< |
fileSize = FileNoExists; |
269 |
> |
fileSize = FileNotExists; |
270 |
|
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
271 |
|
return false; |
272 |
|
} |
273 |
|
|
274 |
< |
} else{ |
274 |
> |
} else{ //slave nodes |
275 |
> |
|
276 |
|
//check file name |
277 |
|
if (checkFilename) { |
278 |
< |
commStatus = MPI_Bcast(filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
278 |
> |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
279 |
|
|
280 |
|
char * masterFilename = new char[filenameLen]; |
281 |
|
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
287 |
|
|
288 |
|
delete masterFilename; |
289 |
|
|
290 |
< |
commStatus = MPI_Allreduce(diffFilename, error, 1, MPI_INT, MPI_COMM_WORLD); |
290 |
> |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
291 |
|
|
292 |
|
if (error > 0) |
293 |
|
return false; |
312 |
|
|
313 |
|
delete fbuf; |
314 |
|
|
315 |
< |
} else if (fileSize == FileNoExists ) { |
315 |
> |
} else if (fileSize == FileNotExists ) { |
316 |
|
return false; |
317 |
|
|
318 |
|
} else if (fileSize == FileIOError ) { |