1 #include "CheckpointableFileStream.hpp" 11 initialize(path, newFile, checkpointer, objName, verifyWrites);
18 string const &objName) {
19 initialize(path, newFile, checkpointer, objName, checkpointer->doesVerifyWrites());
22 void CheckpointableFileStream::initialize(
26 string const &objName,
29 checkpointer->getMPIBlock()->
getRank() != 0,
30 "CheckpointableFileStream (path \"%s\") called by non-root process.\n",
34 bool createFile = newFile;
38 if (PV_stat(fullPath.c_str(), &statbuf) != 0) {
39 if (errno == ENOENT) {
41 "%s: file \"%s\" does not exist. Creating new file.\n",
48 "%s: error checking whether file \"%s\" exists: %s \n",
56 char fullPathCopy[fullPath.size() + 1];
57 std::memcpy(fullPathCopy, fullPath.c_str(), fullPath.size());
58 fullPathCopy[fullPath.size()] =
'\0';
59 char *dirName = dirname(fullPathCopy);
60 ensureDirExists(checkpointer->getMPIBlock(), dirName);
61 FileStream fileStream(fullPath.c_str(), std::ios_base::out, verifyWrites);
66 setOutStream(mFStream);
67 openFile(fullPath.c_str(), std::ios_base::in | std::ios_base::out, verifyWrites);
69 registerData(checkpointer);
72 void CheckpointableFileStream::setDescription() {
73 description =
"CheckpointableFileStream \"";
74 description.append(mObjName).append(
"\"");
77 Response::Status CheckpointableFileStream::respond(std::shared_ptr<BaseMessage const> message) {
78 auto status = Response::NO_ACTION;
79 if (message ==
nullptr) {
84 dynamic_cast<ProcessCheckpointReadMessage const *>(message.get())) {
85 status = respondProcessCheckpointRead(castMessage);
90 Response::Status CheckpointableFileStream::respondProcessCheckpointRead(
93 return Response::SUCCESS;
96 Response::Status CheckpointableFileStream::registerData(
Checkpointer *checkpointer) {
97 auto status = CheckpointerDataInterface::registerData(checkpointer);
101 checkpointer->registerCheckpointData<
long>(
102 mObjName, string(
"FileStreamRead"), &mFileReadPos, (std::size_t)1,
false,
false);
103 checkpointer->registerCheckpointData<
long>(
104 mObjName, string(
"FileStreamWrite"), &mFileWritePos, (std::size_t)1,
false,
false);
105 return Response::SUCCESS;
111 void CheckpointableFileStream::syncFilePos() {
112 if (mFileReadPos != getInPos()) {
113 setInPos(mFileReadPos,
true);
115 if (mFileWritePos != getOutPos()) {
116 setOutPos(mFileWritePos,
true);
120 void CheckpointableFileStream::updateFilePos() {
121 mFileReadPos = getInPos();
122 mFileWritePos = getOutPos();
125 void CheckpointableFileStream::write(
void const *data,
long length) {
127 FileStream::write(data, length);
131 void CheckpointableFileStream::read(
void *data,
long length) {
133 FileStream::read(data, length);
137 void CheckpointableFileStream::setOutPos(
long pos,
bool fromBeginning) {
138 if (!fromBeginning) {
141 FileStream::setOutPos(pos, fromBeginning);
145 void CheckpointableFileStream::setInPos(
long pos,
bool fromBeginning) {
146 if (!fromBeginning) {
149 FileStream::setInPos(pos, fromBeginning);
CheckpointableFileStream(string const &path, bool newFile, Checkpointer *checkpointer, string const &objName, bool verifyWrites)
static bool completed(Status &a)
std::string makeOutputPathFilename(std::string const &path)