16 #ifndef __ESYS_FILEWRITER_H__
17 #define __ESYS_FILEWRITER_H__
42 bool openFile(std::string filename,
size_t initialSize=0)
52 std::ifstream f(filename.c_str());
55 if (std::remove(filename.c_str())) {
62 std::cerr <<
"Error removing " << filename <<
"!" << std::endl;
66 MPI_Info mpiInfo = MPI_INFO_NULL;
67 int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN;
68 mpiErr = MPI_File_open(
mpiComm, const_cast<char*>(filename.c_str()),
69 amode, mpiInfo, &fileHandle);
70 if (mpiErr == MPI_SUCCESS) {
71 mpiErr = MPI_File_set_view(fileHandle, 0, MPI_CHAR, MPI_CHAR,
72 const_cast<char*>(
"native"), mpiInfo);
74 if (mpiErr == MPI_SUCCESS) {
75 mpiErr = MPI_File_set_size(fileHandle, initialSize);
77 if (mpiErr != MPI_SUCCESS) {
78 std::cerr <<
"Error opening " << filename <<
" for parallel writing!" << std::endl;
84 std::ios_base::openmode mode = std::ios_base::binary;
85 ofs.open(filename.c_str(), mode);
86 success = !
ofs.fail();
87 if (success && initialSize>0) {
88 ofs.seekp(initialSize-1,
ofs.beg).put(0).seekp(0,
ofs.beg);
89 success = !
ofs.fail();
100 MPI_Status mpiStatus;
101 std::string contents = oss.str();
102 int mpiErr = MPI_File_write_ordered(
103 fileHandle, const_cast<char*>(contents.c_str()),
104 contents.length(), MPI_CHAR, &mpiStatus);
105 oss.str(std::string());
110 oss.str(std::string());
121 MPI_Status mpiStatus;
122 std::string contents = oss.str();
123 int mpiErr = MPI_File_write_shared(
124 fileHandle, const_cast<char*>(contents.c_str()),
125 contents.length(), MPI_CHAR, &mpiStatus);
126 oss.str(std::string());
131 oss.str(std::string());
137 bool writeAt(std::ostringstream& oss,
long offset)
142 MPI_Status mpiStatus;
143 std::string contents = oss.str();
144 int mpiErr = MPI_File_write_at(
145 fileHandle, offset, const_cast<char*>(contents.c_str()),
146 contents.length(), MPI_CHAR, &mpiStatus);
147 oss.str(std::string());
153 oss.str(std::string());
163 MPI_File_close(&fileHandle);
184 #endif // __ESYS_FILEWRITER_H__