54 if (
static_cast<int>(s1) == 3 ||
static_cast<int>(s2) == 3)
continue;
83 int writeCheckpoint(
const std::string &dataFilePath,
const bool append =
false)
const override
85 H5Eset_auto(H5E_DEFAULT, NULL, NULL);
89 if (append && H5Fis_hdf5(dataFilePath.c_str()) > 0) file = H5Fopen(dataFilePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
90 else file = H5Fcreate(dataFilePath.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
95 H5Gget_num_objs(file, &numObjects);
97 for (
int i = 0; i < int(numObjects); ++i)
99 if (H5Gget_objtype_by_idx(file, i) == H5G_GROUP)
103 const int groupNameMaxLength = 32;
104 char groupName[groupNameMaxLength];
105 H5Gget_objname_by_idx(file, i, groupName, groupNameMaxLength);
107 hid_t group = H5Gopen(file, groupName, H5P_DEFAULT);
108 hid_t attr = H5Aopen(group,
"cutoff", H5P_DEFAULT);
110 H5Aread(attr, H5T_NATIVE_FLOAT, &c);
116 Log::log << Log::LogLevel::Warning <<
"Found existing checkpoint at cutoff " + std::to_string(
cutoff) +
". Skipping checkpoint." << Log::endl;
122 std::string checkpointName =
"checkpoint_" + std::to_string(checkpointId);
125 hid_t group = H5Gcreate(file, checkpointName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
126 hsize_t attrSpaceSize[1] = { 1 };
127 const int attrSpaceDim = 1;
128 hid_t attrSpace = H5Screate_simple(attrSpaceDim, attrSpaceSize, NULL);
129 hid_t attr = H5Acreate(group,
"cutoff", H5T_NATIVE_FLOAT, attrSpace, H5P_DEFAULT, H5P_DEFAULT);
130 H5Awrite(attr, H5T_NATIVE_FLOAT, &
cutoff);
135 auto writeCheckpointDataset = [&group](
const std::string &identifier,
const int size,
const float *data)
137 const int dataSpaceDim = 1;
138 const hsize_t dataSpaceSize[1] = { (hsize_t)size };
139 hid_t dataSpace = H5Screate_simple(dataSpaceDim, dataSpaceSize, NULL);
140 hid_t dataset = H5Dcreate(group, identifier.c_str(), H5T_NATIVE_FLOAT, dataSpace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
141 H5Dwrite(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
145 writeCheckpointDataset(
"cutoff", 1, &
cutoff);
162 bool readCheckpoint(
const std::string &dataFilePath,
const int checkpointId)
override
164 H5Eset_auto(H5E_DEFAULT, NULL, NULL);
167 hid_t file = H5Fopen(dataFilePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
171 std::string checkpointName;
172 if (checkpointId >= 0) checkpointName =
"checkpoint_" + std::to_string(checkpointId);
176 H5Gget_num_objs(file, &numObjects);
177 for (
int i =
int(numObjects) - 1; i >= 0; --i)
179 if (H5Gget_objtype_by_idx(file, i) == H5G_GROUP)
181 checkpointName =
"checkpoint_" + std::to_string(i);
187 hid_t group = H5Gopen(file, checkpointName.c_str(), H5P_DEFAULT);
188 if (group < 0)
return false;
191 auto readDataset = [&group](
const std::string &name,
float *data)->
bool
193 hid_t dataset = H5Dopen(group, name.c_str(), H5P_DEFAULT);
194 if (dataset < 0)
return false;
195 H5Dread(dataset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
199 if (!readDataset(
"cutoff", &
cutoff))
return false;