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();
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());