18#ifndef __ESCRIPT_FILEWRITER_H__
19#define __ESCRIPT_FILEWRITER_H__
21#include <escript/EsysMPI.h>
49 bool openFile(std::string filename,
size_t initialSize=0,
50 bool binary=
false,
bool append=
false)
65 std::ifstream f(filename.c_str());
68 if (std::remove(filename.c_str())) {
75 std::cerr <<
"Error removing " << filename <<
"!"
81 MPI_Info mpiInfo = MPI_INFO_NULL;
82 int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN;
84 amode |= MPI_MODE_APPEND;
86 mpiErr = MPI_File_open(
mpiComm,
const_cast<char*
>(filename.c_str()),
87 amode, mpiInfo, &fileHandle);
88 if (mpiErr == MPI_SUCCESS) {
89 mpiErr = MPI_File_set_view(fileHandle, 0, MPI_CHAR, MPI_CHAR,
90 const_cast<char*
>(
"native"), mpiInfo);
92 if (mpiErr == MPI_SUCCESS) {
94 mpiErr = MPI_File_seek_shared(fileHandle, 0, MPI_SEEK_END);
96 mpiErr = MPI_File_set_size(fileHandle, initialSize);
99 if (mpiErr != MPI_SUCCESS) {
100 char errorstr[MPI_MAX_ERROR_STRING];
102 MPI_Error_string(mpiErr, errorstr, &len);
103 std::cerr <<
"Error opening " << filename
104 <<
" for parallel writing: " << errorstr << std::endl;
110 std::ios_base::openmode mode =
111 (binary ? std::ios_base::binary : std::ios_base::out);
113 mode |= std::ios_base::app;
115 ofs.open(filename.c_str(), mode);
116 success = !
ofs.fail();
117 if (success && initialSize>0 && !append) {
118 ofs.seekp(initialSize-1,
ofs.beg).put(0).seekp(0,
ofs.beg);
119 success = !
ofs.fail();
135 std::string contents = oss.str();
136 int mpiErr = MPI_File_write_ordered(
137 fileHandle,
const_cast<char*
>(contents.c_str()),
138 contents.length(), MPI_CHAR, &mpiStatus);
139 oss.str(std::string());
144 oss.str(std::string());
159 std::string contents = oss.str();
160 int mpiErr = MPI_File_write_shared(
161 fileHandle,
const_cast<char*
>(contents.c_str()),
162 contents.length(), MPI_CHAR, &mpiStatus);
163 oss.str(std::string());
168 oss.str(std::string());
174 bool writeAt(std::ostringstream& oss,
long offset)
183 std::string contents = oss.str();
184 int mpiErr = MPI_File_write_at(
185 fileHandle, offset,
const_cast<char*
>(contents.c_str()),
186 contents.length(), MPI_CHAR, &mpiStatus);
187 oss.str(std::string());
193 oss.str(std::string());
206 MPI_File_close(&fileHandle);
215#pragma clang diagnostic push
216#pragma clang diagnostic ignored "-Wunused-private-field"
219#pragma clang diagnostic pop
#define MPI_COMM_NULL
Definition: EsysMPI.h:51
#define MPI_INT
Definition: EsysMPI.h:48
int MPI_Comm
Definition: EsysMPI.h:44
int MPI_Status
Definition: EsysMPI.h:47
#define MPI_MAX
Definition: EsysMPI.h:56
Definition: FileWriter.h:30
bool writeOrdered(std::ostringstream &oss)
Definition: FileWriter.h:126
FileWriter(MPI_Comm comm=MPI_COMM_NULL)
Definition: FileWriter.h:32
void close()
Definition: FileWriter.h:199
bool writeAt(std::ostringstream &oss, long offset)
Definition: FileWriter.h:174
bool writeShared(std::ostringstream &oss)
Definition: FileWriter.h:150
bool m_open
Definition: FileWriter.h:221
std::ofstream ofs
Definition: FileWriter.h:225
int mpiSize
Definition: FileWriter.h:220
~FileWriter()
Definition: FileWriter.h:43
int mpiRank
Definition: FileWriter.h:218
bool openFile(std::string filename, size_t initialSize=0, bool binary=false, bool append=false)
Definition: FileWriter.h:49
MPI_Comm mpiComm
Definition: FileWriter.h:217
Definition: AbstractContinuousDomain.cpp:23