13 #include "HDFDatabase.h"
14 #include "Utilities.h"
37 CAROM_VERIFY(errf >= 0);
38 #ifndef DEBUG_CHECK_ASSERTIONS
46 const std::string& file_name,
49 CAROM_VERIFY(!file_name.empty());
50 std::string ext_filename(file_name);
52 if (comm != MPI_COMM_NULL)
54 MPI_Comm_rank(comm, &
d_rank);
56 sprintf(tmp,
".%06d",
d_rank);
62 hid_t file_id = H5Fcreate(ext_filename.c_str(),
66 bool result = file_id >= 0;
77 const std::string& file_name,
78 const std::string& type,
81 CAROM_VERIFY(!file_name.empty());
82 std::string ext_filename(file_name);
84 if (comm != MPI_COMM_NULL)
86 MPI_Comm_rank(comm, &
d_rank);
88 sprintf(tmp,
".%06d",
d_rank);
94 CAROM_VERIFY(type ==
"r" || type ==
"wr");
98 file_id = H5Fopen(ext_filename.c_str(),
102 else if (type ==
"wr")
104 file_id = H5Fopen(ext_filename.c_str(),
108 bool result = file_id >= 0;
109 CAROM_VERIFY(result);
123 CAROM_VERIFY(errf >= 0);
137 const std::string& key,
138 const int*
const data,
140 const bool distributed)
142 CAROM_VERIFY(!key.empty());
143 CAROM_VERIFY(data !=
nullptr);
144 CAROM_VERIFY(nelements > 0);
146 hsize_t dim[] = {
static_cast<hsize_t
>(nelements) };
147 hid_t space = H5Screate_simple(1, dim, 0);
148 CAROM_VERIFY(space >= 0);
150 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
165 CAROM_VERIFY(dataset >= 0);
167 herr_t errf = H5Dwrite(dataset,
173 CAROM_VERIFY(errf >= 0);
178 errf = H5Sclose(space);
179 CAROM_VERIFY(errf >= 0);
181 errf = H5Dclose(dataset);
182 CAROM_VERIFY(errf >= 0);
183 #ifndef DEBUG_CHECK_ASSERTIONS
184 CAROM_NULL_USE(errf);
190 const std::string& key,
191 const double*
const data,
193 const bool distributed)
195 CAROM_VERIFY(!key.empty());
196 CAROM_VERIFY(data !=
nullptr);
197 CAROM_VERIFY(nelements > 0);
199 hsize_t dim[] = {
static_cast<hsize_t
>(nelements) };
200 hid_t space = H5Screate_simple(1, dim, 0);
201 CAROM_VERIFY(space >= 0);
203 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
218 CAROM_VERIFY(dataset >= 0);
220 herr_t errf = H5Dwrite(dataset,
226 CAROM_VERIFY(errf >= 0);
231 errf = H5Sclose(space);
232 CAROM_VERIFY(errf >= 0);
234 errf = H5Dclose(dataset);
235 CAROM_VERIFY(errf >= 0);
236 #ifndef DEBUG_CHECK_ASSERTIONS
237 CAROM_NULL_USE(errf);
243 const std::string& key,
244 const std::vector<double>& data,
246 const bool distributed)
253 const std::string& key,
256 const bool distributed)
258 if (nelements == 0)
return;
260 CAROM_VERIFY(!key.empty());
261 #ifndef DEBUG_CHECK_ASSERTIONS
262 CAROM_NULL_USE(nelements);
265 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
266 hid_t dset = H5Dopen(
d_group_id, key.c_str(), H5P_DEFAULT);
268 hid_t dset = H5Dopen(
d_group_id, key.c_str());
270 CAROM_VERIFY(dset >= 0);
272 hid_t dspace = H5Dget_space(dset);
273 CAROM_VERIFY(dspace >= 0);
275 hsize_t nsel = H5Sget_select_npoints(dspace);
276 CAROM_VERIFY(
static_cast<int>(nsel) == nelements);
280 errf = H5Dread(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
281 CAROM_VERIFY(errf >= 0);
284 errf = H5Sclose(dspace);
285 CAROM_VERIFY(errf >= 0);
287 errf = H5Dclose(dset);
288 CAROM_VERIFY(errf >= 0);
289 #ifndef DEBUG_CHECK_ASSERTIONS
290 CAROM_NULL_USE(errf);
296 CAROM_VERIFY(!key.empty());
298 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
299 hid_t dset = H5Dopen(
d_group_id, key.c_str(), H5P_DEFAULT);
301 hid_t dset = H5Dopen(
d_group_id, key.c_str());
303 CAROM_VERIFY(dset >= 0);
305 hid_t dspace = H5Dget_space(dset);
306 CAROM_VERIFY(dspace >= 0);
308 hsize_t nsel = H5Sget_select_npoints(dspace);
310 herr_t errf = H5Sclose(dspace);
311 CAROM_VERIFY(errf >= 0);
313 errf = H5Dclose(dset);
314 CAROM_VERIFY(errf >= 0);
315 #ifndef DEBUG_CHECK_ASSERTIONS
316 CAROM_NULL_USE(errf);
319 return static_cast<int>(nsel);
324 const std::string& key,
327 const bool distributed)
329 if (nelements == 0)
return;
331 CAROM_VERIFY(!key.empty());
332 #ifndef DEBUG_CHECK_ASSERTIONS
333 CAROM_NULL_USE(nelements);
336 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
337 hid_t dset = H5Dopen(
d_group_id, key.c_str(), H5P_DEFAULT);
339 hid_t dset = H5Dopen(
d_group_id, key.c_str());
341 CAROM_VERIFY(dset >= 0);
343 hid_t dspace = H5Dget_space(dset);
344 CAROM_VERIFY(dspace >= 0);
346 hsize_t nsel = H5Sget_select_npoints(dspace);
347 CAROM_VERIFY(
static_cast<int>(nsel) == nelements);
351 errf = H5Dread(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
352 CAROM_VERIFY(errf >= 0);
355 errf = H5Sclose(dspace);
356 CAROM_VERIFY(errf >= 0);
358 errf = H5Dclose(dset);
359 CAROM_VERIFY(errf >= 0);
360 #ifndef DEBUG_CHECK_ASSERTIONS
361 CAROM_NULL_USE(errf);
367 const std::string& key,
370 const std::vector<int>& idx,
371 const bool distributed)
379 std::vector<double> alldata(nelements);
382 for (
int i = 0; i < nelements; ++i)
386 data[k++] = alldata[i];
393 CAROM_VERIFY(k == idx.size());
399 const std::string& key,
405 const bool distributed)
407 CAROM_VERIFY(!key.empty());
408 #ifndef DEBUG_CHECK_ASSERTIONS
409 CAROM_NULL_USE(nelements);
412 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
413 hid_t dset = H5Dopen(
d_group_id, key.c_str(), H5P_DEFAULT);
415 hid_t dset = H5Dopen(
d_group_id, key.c_str());
417 CAROM_VERIFY(dset >= 0);
419 hid_t dspace = H5Dget_space(dset);
420 CAROM_VERIFY(dspace >= 0);
422 hsize_t nsel = H5Sget_select_npoints(dspace);
427 hsize_t num_blocks[1] = {(hsize_t) nelements/block_size};
428 hsize_t buffer_array_size[1] = {(hsize_t) nelements};
429 hsize_t offsets[1] = {(hsize_t) offset};
430 hsize_t strides[1] = {(hsize_t) stride};
431 hsize_t block_sizes[1] = {(hsize_t) block_size};
432 hid_t nodespace = H5Screate_simple(1,buffer_array_size,NULL);
435 H5Sselect_hyperslab(dspace, H5S_SELECT_SET, offsets,
436 strides, num_blocks, block_sizes);
438 errf = H5Dread(dset, H5T_NATIVE_DOUBLE, nodespace, dspace, H5P_DEFAULT, data);
439 CAROM_VERIFY(errf >= 0);
442 errf = H5Sclose(dspace);
443 CAROM_VERIFY(errf >= 0);
445 errf = H5Dclose(dset);
446 CAROM_VERIFY(errf >= 0);
447 #ifndef DEBUG_CHECK_ASSERTIONS
448 CAROM_NULL_USE(errf);
454 const std::string& key)
459 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
460 hid_t this_set = H5Dopen(
d_group_id, key.c_str(), H5P_DEFAULT);
462 hid_t this_set = H5Dopen(
d_group_id, key.c_str());
469 herr_t errf = H5Dclose(this_set);
470 CAROM_VERIFY(errf >= 0);
471 #ifndef DEBUG_CHECK_ASSERTIONS
472 CAROM_NULL_USE(errf);
482 const std::string& key)
484 bool is_double =
false;
487 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
488 hid_t this_set = H5Dopen(
d_group_id, key.c_str(), H5P_DEFAULT);
490 hid_t this_set = H5Dopen(
d_group_id, key.c_str());
497 herr_t errf = H5Dclose(this_set);
498 CAROM_VERIFY(errf >= 0);
499 #ifndef DEBUG_CHECK_ASSERTIONS
500 CAROM_NULL_USE(errf);
513 hid_t attr_id = H5Screate(H5S_SCALAR);
514 CAROM_VERIFY(attr_id >= 0);
516 #if (H5_VERS_MAJOR > 1) || ((H5_VERS_MAJOR == 1) && (H5_VERS_MINOR > 6))
517 hid_t attr = H5Acreate(dataset_id,
524 hid_t attr = H5Acreate(dataset_id,
530 CAROM_VERIFY(attr >= 0);
532 herr_t errf = H5Awrite(attr, H5T_NATIVE_INT, &type_key);
533 CAROM_VERIFY(errf >= 0);
535 errf = H5Aclose(attr);
536 CAROM_VERIFY(errf >= 0);
538 errf = H5Sclose(attr_id);
539 CAROM_VERIFY(errf >= 0);
540 #ifndef DEBUG_CHECK_ASSERTIONS
541 CAROM_NULL_USE(errf);
549 hid_t attr = H5Aopen_name(dataset_id,
"Type");
550 CAROM_VERIFY(attr >= 0);
553 herr_t errf = H5Aread(attr, H5T_NATIVE_INT, &type_key);
554 CAROM_VERIFY(errf >= 0);
556 errf = H5Aclose(attr);
557 CAROM_VERIFY(errf >= 0);
558 #ifndef DEBUG_CHECK_ASSERTIONS
559 CAROM_NULL_USE(errf);
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL)
Creates a new database file with the supplied name.
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL)
Opens an existing database file with the supplied name.
bool d_is_file
True if the HDF5 database is mounted to a file.
static const int KEY_DOUBLE_ARRAY
The key representing a double array.
virtual ~HDFDatabase()
Destructor.
bool isInteger(const std::string &key)
Returns true if the specified key represents an integer entry. If the key does not exist or if the st...
bool isDouble(const std::string &key)
Returns true if the specified key represents a double entry. If the key does not exist or if the stri...
int d_rank
MPI process rank.
virtual void getIntegerArray(const std::string &key, int *data, int nelements, const bool distributed=false)
Reads an array of integers associated with the supplied key from the currently open HDF5 database fil...
virtual bool create(const std::string &file_name, const MPI_Comm comm=MPI_COMM_NULL) override
Creates a new HDF5 database file with the supplied name.
int readAttribute(hid_t dataset_id)
Read an attribute from the specified dataset.
virtual bool open(const std::string &file_name, const std::string &type, const MPI_Comm comm=MPI_COMM_NULL) override
Opens an existing HDF5 database file with the supplied name.
virtual void getDoubleArray(const std::string &key, double *data, int nelements, const bool distributed=false)
Reads an array of doubles associated with the supplied key from the currently open HDF5 database file...
hid_t d_group_id
ID of the group attached to the database.
HDFDatabase()
Default constructor.
static const int KEY_INT_ARRAY
The key representing an integer array.
virtual int getDoubleArraySize(const std::string &key)
Count the number of elements in an array of doubles associated with the supplied key from the current...
virtual void putDoubleArray(const std::string &key, const double *const data, int nelements, const bool distributed=false)
Writes an array of doubles associated with the supplied key to the currently open HDF5 database file.
virtual void putIntegerArray(const std::string &key, const int *const data, int nelements, const bool distributed=false)
Writes an array of integers associated with the supplied key to the currently open HDF5 database file...
virtual void writeAttribute(int type_key, hid_t dataset_id)
Write an attribute to the specified dataset.
virtual bool close()
Closes the currently open HDF5 database file.
hid_t d_file_id
ID of file attached to database or -1 if not mounted to a file.
virtual void putDoubleVector(const std::string &key, const std::vector< double > &data, int nelements, const bool distributed=false)
Writes a vector of doubles associated with the supplied key to the currently open HDF5 database file.