Added new reading routines (engine state,

4d double, 4d int); fixed error in RestartInfo
output; added the maximum length of the engine
state string into sim_constants.h
This commit is contained in:
2024-08-27 14:46:48 +02:00
parent c14a5245b1
commit c33629cb88
6 changed files with 165 additions and 13 deletions
+136
View File
@@ -4,6 +4,7 @@
#include <hdf5_hl.h>
#include "simulation/errors.h"
#include "constants/sim_constants.h"
Hdf5Reader::Hdf5Reader(std::string directory, std::string filename)
: absolute_filename_((directory + "/" + filename + ".h5").c_str()) {}
@@ -99,4 +100,139 @@ blitz::Array<double, 1> Hdf5Reader::readD1Double(std::string path) {
H5Dclose(dataset);
H5Fclose(file);
return data.copy();
}
blitz::Array<double, 4> Hdf5Reader::readD4Double(std::string path) {
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
hid_t dataset;
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) {
ERROR("The given dataset could not be opened");
}
// turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
const int ndims = H5Sget_simple_extent_ndims(dataspace);
hsize_t dims[ndims];
if (ndims != 4) {
ERROR("The given dataset is not four-dimensional. It has dims: " + ndims);
}
H5Sget_simple_extent_dims(dataspace, dims, NULL);
blitz::Array<double, 4> data(dims[0], dims[1], dims[2], dims[3]);
H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
data.data());
H5Dclose(dataset);
H5Fclose(file);
return data.copy();
}
blitz::Array<long int, 4> Hdf5Reader::readD4LongInt(std::string path) {
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
H5Eset_auto(H5E_DEFAULT, NULL, NULL);
hid_t dataset;
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) {
ERROR("The given dataset could not be opened");
}
// turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
const int ndims = H5Sget_simple_extent_ndims(dataspace);
hsize_t dims[ndims];
if (ndims != 4) {
ERROR("The given dataset is not four-dimensional. It has dims: " + ndims);
}
H5Sget_simple_extent_dims(dataspace, dims, NULL);
blitz::Array<long int, 4> data(dims[0], dims[1], dims[2], dims[3]);
H5Dread(dataset, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT,
data.data());
H5Dclose(dataset);
H5Fclose(file);
return data.copy();
}
// reads the engine stringstream with the correct size from the file, such that it should be able
// to be used for the initialization of the random engine without any further modification
// path has to only be the path to the group that contains the two dataset "string" and "length"
std::stringstream Hdf5Reader::readEngineString(std::string path) {
std::stringstream out;
hid_t file = H5Fopen(absolute_filename_.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
// H5Eset_auto(H5E_DEFAULT, NULL, NULL);
hid_t dataset = H5Dopen2(file, (path + "/string").c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) {
ERROR("The given dataset could not be opened");
}
// turn error report back onby hdf5 because of check H5Dopen2
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
// might be beneficial to check if the datatype
// is the expected one (H5T_STRING)
hid_t dtype = H5Dget_type(dataset);
size_t size = H5Tget_size(dtype);
if(size != SimConstants::engine_string_length_max) {
ERROR("Size of the string is not the required size of 6730, size of string: "
+ std::to_string(size));
}
DEBUG_LEVEL_3("RestartInfo: " + std::to_string(H5Tdetect_class(dtype, H5T_STRING)));
const int ndims = H5Sget_simple_extent_ndims(dataspace);
hsize_t dims[ndims];
if (ndims != 1) {
ERROR("The given dataset is not one dimensional. It has dims: " + ndims);
}
DEBUG_LEVEL_3(std::to_string(ndims));
DEBUG_LEVEL_3("RestartInfo: " + std::to_string(size));
H5Sget_simple_extent_dims(dataspace, dims, NULL);
char data[SimConstants::engine_string_length_max];
H5Dread(dataset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT,
&data);
std::stringstream temp;
temp << data;
H5Tclose(dtype);
H5Sclose(dataspace);
H5Dclose(dataset);
dataset = H5Dopen2(file, (path + "/length").c_str(), H5P_DEFAULT);
dataspace = H5Dget_space(dataset);
if(dataspace < 0) {
ERROR("Dataspace for length could not be opened");
}
const int ndimsL = H5Sget_simple_extent_ndims(dataspace);
hsize_t dimsL[ndimsL];
if(ndimsL != 1) {
ERROR("length dataset is not 1-dimensional");
}
H5Sget_simple_extent_dims(dataspace, dimsL, NULL);
size_t length;
H5Dread(dataset, H5T_NATIVE_HSIZE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &length);
DEBUG_LEVEL_3("RestartInfo: read length from file: " + std::to_string(length));
H5Sclose(dataspace);
H5Dclose(dataset);
H5Fclose(file);
out << temp.str().substr(0, length);
DEBUG_LEVEL_3(std::to_string(out.str().size()));
return out;
}
+18 -8
View File
@@ -5,6 +5,7 @@
#include <sys/stat.h>
#include <iostream>
#include "simulation/errors.h"
#include "constants/sim_constants.h"
#include "simulation/mpi_communication.h"
@@ -412,6 +413,10 @@ void Hdf5Writer::write(const std::string& data, std::string path) {
H5Fclose(file);
}
// maybe split into a function that writes a string in parallel and one that writes an
// int in parallel. Would be a bit more effort but it would be more consistent with the
// way the rest of the writer functions work
// writes a string-representation of an random numbers engine (right now assumed mt19973)
// written output: input string padded with 0 at the end to achieve a fixed length
// and the size of the input string
@@ -424,10 +429,10 @@ void Hdf5Writer::writeRNG(const std::string& data, std::string path_base) {
* restarted), length of the max size uncertain, relatively large storage space usage (>6370 bytes/cpu/output)
* (comparision: HAWK: size of file ~ 3.4MB/sync, RestartInfo Size (estimate) ~ 81MB/sync (12800 procs))
*
* Second option: wrapper for the engine and count the times its been used and store that number.
* Also has an issue with restarts on a different platform (since engine implementation is platform dependent).
* Signigicantly lower storage space usage. Restoring the engine state would easy in implementation
* (potentially a bit more expensive in execution time: discard() function).
* Second option: wrapper for the engine and count the times its been used and store that number.
* Also has an issue with restarts on a different platform (since engine implementation is platform dependent).
* Signigicantly lower storage space usage. Restoring the engine state would easy in implementation
* (potentially a bit more expensive in execution time: discard() function).
*/
MPI_Info info;
@@ -452,15 +457,15 @@ void Hdf5Writer::writeRNG(const std::string& data, std::string path_base) {
size_t stateStringSize = out.size();
if(stateStringSize > stateStringSizeMax_) {
ERROR("Max Size (" + std::to_string(stateStringSizeMax_) + ") too small. String size: " + std::to_string(stateStringSize));
if(stateStringSize > SimConstants::engine_string_length_max) {
ERROR("Max Size (" + std::to_string(SimConstants::engine_string_length_max) + ") too small. String size: " + std::to_string(stateStringSize));
}
out.insert(out.end(), (stateStringSizeMax_ - stateStringSize), '0');
out.insert(out.end(), (SimConstants::engine_string_length_max - stateStringSize), '0');
// apperently it's not possible to use 'H5T_VARIABLE' with the MPI-I/O file driver
// (since it doesn't allow for variable size objects to be created, see: https://davis.lbl.gov/Manuals/HDF5-1.8.7/TechNotes/VLTypes.html)
hid_t dtype = H5Tcreate(H5T_STRING, stateStringSizeMax_);
hid_t dtype = H5Tcreate(H5T_STRING, SimConstants::engine_string_length_max);
// store old error stack info
H5E_auto2_t oldfunc;
@@ -510,6 +515,11 @@ void Hdf5Writer::writeRNG(const std::string& data, std::string path_base) {
H5Pclose(fapl);
}
// Debug utility
std::string Hdf5Writer::AbsoluteFileName() const {
return absolute_filename_;
}
// testing function for the parallel output
// void Hdf5Writer::writeRNG(std::string path_base) {