17 #ifndef __ESYS_FILEWRITER_H__
18 #define __ESYS_FILEWRITER_H__
43 bool openFile(std::string filename,
size_t initialSize=0)
53 std::ifstream f(filename.c_str());
56 if (std::remove(filename.c_str())) {
63 std::cerr <<
"Error removing " << filename <<
"!" << std::endl;
67 MPI_Info mpiInfo = MPI_INFO_NULL;
68 int amode = MPI_MODE_CREATE|MPI_MODE_WRONLY|MPI_MODE_UNIQUE_OPEN;
69 mpiErr = MPI_File_open(
mpiComm, const_cast<char*>(filename.c_str()),
70 amode, mpiInfo, &fileHandle);
71 if (mpiErr == MPI_SUCCESS) {
72 mpiErr = MPI_File_set_view(fileHandle, 0, MPI_CHAR, MPI_CHAR,
73 const_cast<char*>(
"native"), mpiInfo);
75 if (mpiErr == MPI_SUCCESS) {
76 mpiErr = MPI_File_set_size(fileHandle, initialSize);
78 if (mpiErr != MPI_SUCCESS) {
79 std::cerr <<
"Error opening " << filename <<
" for parallel writing!" << std::endl;
85 std::ios_base::openmode mode = std::ios_base::binary;
86 ofs.open(filename.c_str(), mode);
87 success = !
ofs.fail();
88 if (success && initialSize>0) {
89 ofs.seekp(initialSize-1,
ofs.beg).put(0).seekp(0,
ofs.beg);
90 success = !
ofs.fail();
102 std::string contents = oss.str();
103 int mpiErr = MPI_File_write_ordered(
104 fileHandle, const_cast<char*>(contents.c_str()),
105 contents.length(), MPI_CHAR, &mpiStatus);
106 oss.str(std::string());
111 oss.str(std::string());
123 std::string contents = oss.str();
124 int mpiErr = MPI_File_write_shared(
125 fileHandle, const_cast<char*>(contents.c_str()),
126 contents.length(), MPI_CHAR, &mpiStatus);
127 oss.str(std::string());
132 oss.str(std::string());
138 bool writeAt(std::ostringstream& oss,
long offset)
144 std::string contents = oss.str();
145 int mpiErr = MPI_File_write_at(
146 fileHandle, offset, const_cast<char*>(contents.c_str()),
147 contents.length(), MPI_CHAR, &mpiStatus);
148 oss.str(std::string());
154 oss.str(std::string());
164 MPI_File_close(&fileHandle);
185 #endif // __ESYS_FILEWRITER_H__
FileWriter()
Definition: esysFileWriter.h:33
int mpiRank
Definition: esysFileWriter.h:172
bool writeOrdered(std::ostringstream &oss)
Definition: esysFileWriter.h:96
#define MPI_INT
Definition: Esys_MPI.h:32
bool openFile(std::string filename, size_t initialSize=0)
Definition: esysFileWriter.h:43
int MPI_Comm
Definition: Esys_MPI.h:29
bool writeAt(std::ostringstream &oss, long offset)
Definition: esysFileWriter.h:138
Definition: esysFileWriter.h:30
int mpiSize
Definition: esysFileWriter.h:172
int MPI_Status
Definition: Esys_MPI.h:31
void close()
Definition: esysFileWriter.h:160
void * mpiComm
Definition: esysFileWriter.h:177
bool writeShared(std::ostringstream &oss)
Definition: esysFileWriter.h:117
std::ofstream ofs
Definition: esysFileWriter.h:179