| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "IO.h" |
| 8 |
|
| 9 |
#include <hdf5.h> |
| 10 |
#include <ios> |
| 11 |
#include <iostream> |
| 12 |
#include <memory> |
| 13 |
#include <stdexcept> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
class HDF5NativeWriter |
| 17 |
{ |
| 18 |
|
| 19 |
public: |
| 20 |
HDF5NativeWriter(const std::string &fileName); |
| 21 |
~HDF5NativeWriter(); |
| 22 |
|
| 23 |
bool Advance(); |
| 24 |
void Close(); |
| 25 |
void CheckWriteGroup(); |
| 26 |
|
| 27 |
void WriteScalar(const std::string &varName, const void *data, hid_t h5Type); |
| 28 |
void WriteSimple(const std::string &varName, int dimSize, const void *data, hid_t h5Type, |
| 29 |
const hsize_t *shape, const hsize_t *offset, const hsize_t *count); |
| 30 |
|
| 31 |
void applyMetadataCacheEviction(); |
| 32 |
void WriteSimpleWithChunking(const std::string &varName, int dimSize, const void *data, |
| 33 |
hid_t h5Type, const hsize_t *shape, const hsize_t *offset, |
| 34 |
const hsize_t *count); |
| 35 |
int m_CurrentTimeStep; |
| 36 |
unsigned int m_TotalTimeSteps; |
| 37 |
bool m_Chunking; |
| 38 |
|
| 39 |
private: |
| 40 |
hid_t m_FilePropertyListId; |
| 41 |
hid_t m_FileId; |
| 42 |
hid_t m_GroupId; |
| 43 |
}; |
| 44 |
|
| 45 |
HDF5NativeWriter::HDF5NativeWriter(const std::string &fileName) |
| 46 |
: m_CurrentTimeStep(0), m_TotalTimeSteps(0), m_Chunking(false) |
| 47 |
{ |
| 48 |
m_FilePropertyListId = H5Pcreate(H5P_FILE_ACCESS); |
| 49 |
|
| 50 |
// read a file collectively |
| 51 |
H5Pset_fapl_mpio(m_FilePropertyListId, MPI_COMM_WORLD, MPI_INFO_NULL); |
| 52 |
|
| 53 |
m_FileId = H5Fcreate(fileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, m_FilePropertyListId); |
| 54 |
|
| 55 |
if (m_FileId < 0) |
| 56 |
{ |
| 57 |
throw std::runtime_error("Unable to open " + fileName + " for reading"); |
| 58 |
} |
| 59 |
|
| 60 |
applyMetadataCacheEviction(); |
| 61 |
|
| 62 |
std::string ts0 = "/Step0"; |
| 63 |
|
| 64 |
m_GroupId = H5Gcreate2(m_FileId, ts0.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
| 65 |
if (m_GroupId < 0) |
| 66 |
{ |
| 67 |
throw std::runtime_error("HDF5: Unable to create group " + ts0); |
| 68 |
} |
| 69 |
|
| 70 |
std::string prefix = "chunking"; |
| 71 |
if (fileName.substr(0, prefix.size()) == prefix) |
| 72 |
{ |
| 73 |
m_Chunking = true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
HDF5NativeWriter::~HDF5NativeWriter() { Close(); } |
| 78 |
|
| 79 |
void HDF5NativeWriter::applyMetadataCacheEviction() |
| 80 |
{ |
| 81 |
#ifdef NEVER |
| 82 |
/* |
| 83 |
see |
| 84 |
https://lists.hdfgroup.org/pipermail/hdf-forum_lists.hdfgroup.org/2011-February/004201.html |
| 85 |
John said the code below worked for the paper but not anymore after |
| 86 |
updates |
| 87 |
*/ |
| 88 |
H5AC_cache_config_t mdc_config; |
| 89 |
mdc_config.version = H5AC__CURR_CACHE_CONFIG_VERSION; |
| 90 |
H5Pget_mdc_config(m_FilePropertyListId, &mdc_config); |
| 91 |
mdc_config.evictions_enabled = 0; // FALSE |
| 92 |
mdc_config.incr_mode = H5C_incr__off; |
| 93 |
mdc_config.decr_mode = H5C_decr__off; |
| 94 |
H5Pset_mdc_config(m_FilePropertyListId, &mdc_config); |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
void HDF5NativeWriter::Close() |
| 99 |
{ |
| 100 |
if (m_FileId < 0) |
| 101 |
return; |
| 102 |
|
| 103 |
hid_t s = H5Screate(H5S_SCALAR); |
| 104 |
hid_t attr = H5Acreate(m_FileId, "NumSteps", H5T_NATIVE_UINT, s, H5P_DEFAULT, H5P_DEFAULT); |
| 105 |
unsigned int totalTimeSteps = m_CurrentTimeStep + 1; |
| 106 |
|
| 107 |
if (m_GroupId < 0) |
| 108 |
{ |
| 109 |
totalTimeSteps = m_CurrentTimeStep; |
| 110 |
} |
| 111 |
H5Awrite(attr, H5T_NATIVE_UINT, &totalTimeSteps); |
| 112 |
H5Sclose(s); |
| 113 |
H5Aclose(attr); |
| 114 |
|
| 115 |
if (m_GroupId >= 0) |
| 116 |
{ |
| 117 |
H5Gclose(m_GroupId); |
| 118 |
m_GroupId = -1; |
| 119 |
} |
| 120 |
|
| 121 |
H5Fclose(m_FileId); |
| 122 |
m_FileId = -1; |
| 123 |
H5Pclose(m_FilePropertyListId); |
| 124 |
} |
| 125 |
|
| 126 |
bool HDF5NativeWriter::Advance() |
| 127 |
{ |
| 128 |
if (m_GroupId >= 0) |
| 129 |
{ |
| 130 |
H5Gclose(m_GroupId); |
| 131 |
m_GroupId = -1; |
| 132 |
} |
| 133 |
|
| 134 |
++m_CurrentTimeStep; |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
void HDF5NativeWriter::CheckWriteGroup() |
| 140 |
{ |
| 141 |
if (m_GroupId >= 0) |
| 142 |
{ |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
std::string timeStepName = "/Step" + std::to_string(m_CurrentTimeStep); |
| 147 |
m_GroupId = H5Gcreate2(m_FileId, timeStepName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
| 148 |
if (m_GroupId < 0) |
| 149 |
{ |
| 150 |
throw std::runtime_error("HDF5: Unable to create group " + timeStepName); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void HDF5NativeWriter::WriteScalar(const std::string &varName, const void *data, hid_t h5Type) |
| 155 |
{ |
| 156 |
CheckWriteGroup(); |
| 157 |
// scalar |
| 158 |
hid_t filespaceID = H5Screate(H5S_SCALAR); |
| 159 |
hid_t dsetID = H5Dcreate(m_GroupId, varName.c_str(), h5Type, filespaceID, H5P_DEFAULT, |
| 160 |
H5P_DEFAULT, H5P_DEFAULT); |
| 161 |
hid_t plistID = H5Pcreate(H5P_DATASET_XFER); |
| 162 |
H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE); |
| 163 |
|
| 164 |
herr_t status = H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, plistID, data); |
| 165 |
|
| 166 |
if (status < 0) |
| 167 |
{ |
| 168 |
std::cerr << " Write failed. " << std::endl; |
| 169 |
} |
| 170 |
H5Sclose(filespaceID); |
| 171 |
H5Dclose(dsetID); |
| 172 |
} |
| 173 |
|
| 174 |
void HDF5NativeWriter::WriteSimple(const std::string &varName, int dimSize, const void *data, |
| 175 |
hid_t h5Type, const hsize_t *shape, const hsize_t *offset, |
| 176 |
const hsize_t *count) |
| 177 |
{ |
| 178 |
CheckWriteGroup(); |
| 179 |
hid_t fileSpace = H5Screate_simple(dimSize, shape, NULL); |
| 180 |
|
| 181 |
hid_t dsetID = H5Dcreate(m_GroupId, varName.c_str(), h5Type, fileSpace, H5P_DEFAULT, |
| 182 |
H5P_DEFAULT, H5P_DEFAULT); |
| 183 |
|
| 184 |
hid_t memSpace = H5Screate_simple(dimSize, count, NULL); |
| 185 |
|
| 186 |
// Select hyperslab |
| 187 |
fileSpace = H5Dget_space(dsetID); |
| 188 |
H5Sselect_hyperslab(fileSpace, H5S_SELECT_SET, offset, NULL, count, NULL); |
| 189 |
|
| 190 |
// Create property list for collective dataset write. |
| 191 |
|
| 192 |
hid_t plistID = H5Pcreate(H5P_DATASET_XFER); |
| 193 |
H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE); |
| 194 |
|
| 195 |
herr_t status; |
| 196 |
|
| 197 |
status = H5Dwrite(dsetID, h5Type, memSpace, fileSpace, plistID, data); |
| 198 |
|
| 199 |
if (status < 0) |
| 200 |
{ |
| 201 |
// error |
| 202 |
std::cerr << " Write failed. " << std::endl; |
| 203 |
} |
| 204 |
|
| 205 |
H5Dclose(dsetID); |
| 206 |
H5Sclose(fileSpace); |
| 207 |
H5Sclose(memSpace); |
| 208 |
H5Pclose(plistID); |
| 209 |
} |
| 210 |
|
| 211 |
void HDF5NativeWriter::WriteSimpleWithChunking(const std::string &varName, int dimSize, |
| 212 |
const void *data, hid_t h5Type, const hsize_t *shape, |
| 213 |
const hsize_t *offset, const hsize_t *count) |
| 214 |
{ |
| 215 |
CheckWriteGroup(); |
| 216 |
hid_t fileSpace = H5Screate_simple(dimSize, shape, NULL); |
| 217 |
|
| 218 |
hid_t dsetPid = H5Pcreate(H5P_DATASET_CREATE); |
| 219 |
H5Pset_chunk(dsetPid, dimSize, count); |
| 220 |
|
| 221 |
size_t bytes = H5Tget_size(h5Type); |
| 222 |
for (int i = 0; i < dimSize; i++) |
| 223 |
{ |
| 224 |
bytes *= count[i]; |
| 225 |
} |
| 226 |
hid_t access_plistid = H5Pcreate(H5P_DATASET_ACCESS); |
| 227 |
H5Pset_chunk_cache(access_plistid, 101, bytes, 1); |
| 228 |
|
| 229 |
hid_t dsetID = H5Dcreate(m_GroupId, varName.c_str(), h5Type, fileSpace, H5P_DEFAULT, dsetPid, |
| 230 |
access_plistid); |
| 231 |
|
| 232 |
hid_t memSpace = H5Screate_simple(dimSize, count, NULL); |
| 233 |
|
| 234 |
// Select hyperslab |
| 235 |
fileSpace = H5Dget_space(dsetID); |
| 236 |
H5Sselect_hyperslab(fileSpace, H5S_SELECT_SET, offset, NULL, count, NULL); |
| 237 |
|
| 238 |
// Create property list for collective dataset write. |
| 239 |
|
| 240 |
hid_t plistID = H5Pcreate(H5P_DATASET_XFER); |
| 241 |
H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE); |
| 242 |
|
| 243 |
herr_t status; |
| 244 |
|
| 245 |
status = H5Dwrite(dsetID, h5Type, memSpace, fileSpace, plistID, data); |
| 246 |
|
| 247 |
if (status < 0) |
| 248 |
{ |
| 249 |
// error |
| 250 |
std::cerr << " Write failed. " << std::endl; |
| 251 |
} |
| 252 |
|
| 253 |
H5Dclose(dsetID); |
| 254 |
H5Sclose(fileSpace); |
| 255 |
H5Sclose(memSpace); |
| 256 |
H5Pclose(plistID); |
| 257 |
|
| 258 |
H5Pclose(dsetPid); |
| 259 |
H5Pclose(access_plistid); |
| 260 |
} |
| 261 |
|
| 262 |
// |
| 263 |
// |
| 264 |
std::shared_ptr<HDF5NativeWriter> h5writer; |
| 265 |
// HDF5NativeWriter* h5writer; |
| 266 |
|
| 267 |
IO::IO(const Settings &s, MPI_Comm comm) |
| 268 |
{ |
| 269 |
m_outputfilename = MakeFilename(s.outputfile, ".h5"); |
| 270 |
|
| 271 |
if (s.outputfile[0] == '0') |
| 272 |
{ |
| 273 |
std::cout << " no writer. " << std::endl; |
| 274 |
h5writer = nullptr; |
| 275 |
return; |
| 276 |
} |
| 277 |
h5writer = std::make_shared<HDF5NativeWriter>(m_outputfilename); |
| 278 |
|
| 279 |
if (h5writer == nullptr) |
| 280 |
throw std::ios_base::failure("ERROR: failed to open ADIOS h5writer\n"); |
| 281 |
} |
| 282 |
|
| 283 |
IO::~IO() |
| 284 |
{ |
| 285 |
if (h5writer != nullptr) |
| 286 |
{ |
| 287 |
h5writer->Close(); |
| 288 |
} |
| 289 |
// delete h5writer; |
| 290 |
} |
| 291 |
|
| 292 |
void IO::write(int step, const HeatTransfer &ht, const Settings &s, MPI_Comm comm) |
| 293 |
{ |
| 294 |
if (h5writer == nullptr) |
| 295 |
{ |
| 296 |
return; |
| 297 |
} |
| 298 |
std::vector<hsize_t> dims = {s.gndx, s.gndy}; |
| 299 |
std::vector<hsize_t> offset = {s.offsx, s.offsy}; |
| 300 |
std::vector<hsize_t> count = {s.ndx, s.ndy}; |
| 301 |
|
| 302 |
if (h5writer->m_Chunking) |
| 303 |
{ |
| 304 |
h5writer->WriteSimpleWithChunking("T", 2, ht.data_noghost().data(), H5T_NATIVE_DOUBLE, |
| 305 |
dims.data(), offset.data(), count.data()); |
| 306 |
} |
| 307 |
else |
| 308 |
{ |
| 309 |
h5writer->WriteSimple("T", 2, ht.data_noghost().data(), H5T_NATIVE_DOUBLE, dims.data(), |
| 310 |
offset.data(), count.data()); |
| 311 |
} |
| 312 |
|
| 313 |
h5writer->WriteScalar("gndy", &(s.gndy), H5T_NATIVE_UINT); |
| 314 |
h5writer->WriteScalar("gndx", &(s.gndx), H5T_NATIVE_UINT); |
| 315 |
|
| 316 |
h5writer->Advance(); |
| 317 |
} |