From c33629cb88d09ecddfe6e1cbf8cfd20ce8c303da Mon Sep 17 00:00:00 2001 From: csendlinger Date: Tue, 27 Aug 2024 14:46:48 +0200 Subject: [PATCH] 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 --- include/IO/hdf5_reader.h | 3 + include/IO/hdf5_writer.h | 4 +- include/constants/sim_constants.h | 4 + src/IO/hdf5_reader.cpp | 136 ++++++++++++++++++ src/IO/hdf5_writer.cpp | 26 ++-- .../simulation_output_generator.cpp | 5 +- 6 files changed, 165 insertions(+), 13 deletions(-) diff --git a/include/IO/hdf5_reader.h b/include/IO/hdf5_reader.h index b1ff685..5ea16ac 100755 --- a/include/IO/hdf5_reader.h +++ b/include/IO/hdf5_reader.h @@ -9,9 +9,12 @@ class Hdf5Reader { Hdf5Reader(std::string directory, std::string filename); Hdf5Reader(std::string absolute_filename); + blitz::Array readD4Double(std::string path); blitz::Array readD3Double(std::string path); blitz::Array readD2Double(std::string path); blitz::Array readD1Double(std::string path); + std::stringstream readEngineString(std::string path); + blitz::Array readD4LongInt(std::string path); private: std::string absolute_filename_; diff --git a/include/IO/hdf5_writer.h b/include/IO/hdf5_writer.h index 9493171..bd823c3 100755 --- a/include/IO/hdf5_writer.h +++ b/include/IO/hdf5_writer.h @@ -24,11 +24,9 @@ class Hdf5Writer { void write(const std::string& data, std::string path); void writeRNG(const std::string& data, std::string path_base); // void writeRNG(const std::string path_base); + std::string AbsoluteFileName() const; private: - // right now from observations of the size of the statestring, whether best choice: uncertain - // also not sure if this length is platform-independent - const size_t stateStringSizeMax_ = 6730; std::string absolute_filename_; void createFolder(std::string directory); void createFile(std::string absolute_filename); diff --git a/include/constants/sim_constants.h b/include/constants/sim_constants.h index 7889edf..4f7277a 100755 --- a/include/constants/sim_constants.h +++ b/include/constants/sim_constants.h @@ -10,6 +10,10 @@ static const double minimal_macroscopic_cross_section = std::numeric_limits::min() * 1E5; static const double random_lower_limit = 1E-15; static const double compare_epsilon = 1E-10; +// maximum length for the string produced by the "<<"-operator +// of the mt19973 engine. ("empirical" value, might be too low, +// or depend on the system) +static const size_t engine_string_length_max = 6730; } // namespace SimConstants diff --git a/src/IO/hdf5_reader.cpp b/src/IO/hdf5_reader.cpp index 4705235..0b42d60 100755 --- a/src/IO/hdf5_reader.cpp +++ b/src/IO/hdf5_reader.cpp @@ -4,6 +4,7 @@ #include #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 Hdf5Reader::readD1Double(std::string path) { H5Dclose(dataset); H5Fclose(file); return data.copy(); +} + +blitz::Array 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 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 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 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; } \ No newline at end of file diff --git a/src/IO/hdf5_writer.cpp b/src/IO/hdf5_writer.cpp index 2698c3e..2bfd9a6 100755 --- a/src/IO/hdf5_writer.cpp +++ b/src/IO/hdf5_writer.cpp @@ -5,6 +5,7 @@ #include #include #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) { diff --git a/src/generators/simulation_output_generator.cpp b/src/generators/simulation_output_generator.cpp index bacb33b..79b00dd 100755 --- a/src/generators/simulation_output_generator.cpp +++ b/src/generators/simulation_output_generator.cpp @@ -157,15 +157,16 @@ std::shared_ptr buildSimulationOutput( { int intervall = json_int(json_object_by_path(object, {"RestartInfo", "Intervall"}), -1); - DEBUG_LEVEL_2("Try value of Intervall as string"); + // DEBUG_LEVEL_2("Try value of Intervall as string"); if (intervall == -1 && json_string(json_object_by_path( object, {"RestartInfo", "Intervall"})) == "EachSync") { intervall = 1; - } else { + } else if (intervall == -1) { intervall = 0; } ASSERT_NOTNEGATIVE(&intervall, "The intervall for RestartInfo has to be either 0" ",if no output is required or positive otherwise"); + DEBUG_LEVEL_3("Restart Interval: " + std::to_string(intervall)); bool overwrite_last = json_bool( json_object_by_path(object, {"RestartInfo", "OverwriteLast"}));