17 #ifndef __ESCRIPT_FILEWRITER_H__
18 #define __ESCRIPT_FILEWRITER_H__
20 #include <escript/EsysMPI.h>
48 bool openFile(std::string filename,
size_t initialSize=0,
49 bool binary=
false,
bool append=
false)
64 std::ifstream f(filename.c_str());
67 if (std::remove(filename.c_str())) {
74 std::cerr <<
"Error removing " << filename <<
"!"
80 MPI_Info mpiInfo = MPI_INFO_NULL;
81 int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN;
83 amode |= MPI_MODE_APPEND;
85 mpiErr = MPI_File_open(
mpiComm, const_cast<char*>(filename.c_str()),
86 amode, mpiInfo, &fileHandle);
87 if (mpiErr == MPI_SUCCESS) {
88 mpiErr = MPI_File_set_view(fileHandle, 0, MPI_CHAR, MPI_CHAR,
89 const_cast<char*>(
"native"), mpiInfo);
91 if (mpiErr == MPI_SUCCESS) {
93 mpiErr = MPI_File_seek_shared(fileHandle, 0, MPI_SEEK_END);
95 mpiErr = MPI_File_set_size(fileHandle, initialSize);
98 if (mpiErr != MPI_SUCCESS) {
99 char errorstr[MPI_MAX_ERROR_STRING];
101 MPI_Error_string(mpiErr, errorstr, &len);
102 std::cerr <<
"Error opening " << filename
103 <<
" for parallel writing: " << errorstr << std::endl;
109 std::ios_base::openmode mode =
110 (binary ? std::ios_base::binary : std::ios_base::out);
112 mode |= std::ios_base::app;
114 ofs.open(filename.c_str(), mode);
115 success = !
ofs.fail();
116 if (success && initialSize>0 && !append) {
117 ofs.seekp(initialSize-1,
ofs.beg).put(0).seekp(0,
ofs.beg);
118 success = !
ofs.fail();
134 std::string contents = oss.str();
135 int mpiErr = MPI_File_write_ordered(
136 fileHandle, const_cast<char*>(contents.c_str()),
137 contents.length(), MPI_CHAR, &mpiStatus);
138 oss.str(std::string());
143 oss.str(std::string());
158 std::string contents = oss.str();
159 int mpiErr = MPI_File_write_shared(
160 fileHandle, const_cast<char*>(contents.c_str()),
161 contents.length(), MPI_CHAR, &mpiStatus);
162 oss.str(std::string());
167 oss.str(std::string());
173 bool writeAt(std::ostringstream& oss,
long offset)
182 std::string contents = oss.str();
183 int mpiErr = MPI_File_write_at(
184 fileHandle, offset, const_cast<char*>(contents.c_str()),
185 contents.length(), MPI_CHAR, &mpiStatus);
186 oss.str(std::string());
192 oss.str(std::string());
205 MPI_File_close(&fileHandle);
214 #pragma clang diagnostic push
215 #pragma clang diagnostic ignored "-Wunused-private-field"
218 #pragma clang diagnostic pop
230 #endif // __ESCRIPT_FILEWRITER_H__