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);
227 #endif // __ESCRIPT_FILEWRITER_H__ bool openFile(std::string filename, size_t initialSize=0, bool binary=false, bool append=false)
Definition: FileWriter.h:48
Definition: FileWriter.h:28
bool writeOrdered(std::ostringstream &oss)
Definition: FileWriter.h:125
int MPI_Status
Definition: EsysMPI.h:44
Definition: AbstractContinuousDomain.cpp:22
#define MPI_MAX
Definition: EsysMPI.h:53
MPI_Comm mpiComm
Definition: FileWriter.h:214
std::ofstream ofs
Definition: FileWriter.h:221
~FileWriter()
Definition: FileWriter.h:42
#define MPI_INT
Definition: EsysMPI.h:45
bool writeShared(std::ostringstream &oss)
Definition: FileWriter.h:149
bool m_open
Definition: FileWriter.h:217
bool writeAt(std::ostringstream &oss, long offset)
Definition: FileWriter.h:173
#define MPI_COMM_NULL
Definition: EsysMPI.h:48
int mpiRank
Definition: FileWriter.h:215
FileWriter(MPI_Comm comm=MPI_COMM_NULL)
Definition: FileWriter.h:31
int MPI_Comm
Definition: EsysMPI.h:41
int mpiSize
Definition: FileWriter.h:216
void close()
Definition: FileWriter.h:198