From 4a3a3e3b6b112f853eb85977467a222148a21dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Wed, 2 Jul 2025 13:44:49 +0200 Subject: [PATCH] feat: Implement checkpointing Co-authored-by: hmars-t --- src/CMakeLists.txt | 31 +++++++++++++++++++++---------- src/IO/Datatypes.hpp | 9 +++++++++ src/IO/HDF5Functions.hpp | 8 ++++++++ src/IO/checkpoint.cpp | 28 ++++++++++++++++++++++++++++ src/poet.cpp | 14 ++++++++++++++ 5 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 src/IO/Datatypes.hpp create mode 100644 src/IO/HDF5Functions.hpp create mode 100644 src/IO/checkpoint.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ca39b6106..71c37d908 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,27 @@ +include(FetchContent) +FetchContent_Declare( + cli11 + QUIET + GIT_REPOSITORY https://github.com/CLIUtils/CLI11.git + GIT_TAG v2.4.2 +) + +FetchContent_Declare( + highfive + QUIET + GIT_REPOSITORY https://github.com/highfive-devs/highfive.git + GIT_TAG v3.0.0 +) + +FetchContent_MakeAvailable(cli11) +FetchContent_MakeAvailable(highfive) + add_library(POETLib Init/InitialList.cpp Init/GridInit.cpp Init/DiffusionInit.cpp Init/ChemistryInit.cpp + IO/checkpoint.cpp DataStructures/Field.cpp Transport/DiffusionModule.cpp Chemistry/ChemistryModule.cpp @@ -30,18 +49,10 @@ target_link_libraries( PUBLIC RRuntime PUBLIC IPhreeqcPOET PUBLIC tug - PUBLIC MPI::MPI_C + PUBLIC MPI::MPI_C + PUBLIC HighFive::HighFive ) -include(FetchContent) -FetchContent_Declare( - cli11 - QUIET - GIT_REPOSITORY https://github.com/CLIUtils/CLI11.git - GIT_TAG v2.4.2 -) - -FetchContent_MakeAvailable(cli11) # add_library(poetlib # Base/Grid.cpp diff --git a/src/IO/Datatypes.hpp b/src/IO/Datatypes.hpp new file mode 100644 index 000000000..c20fb3b79 --- /dev/null +++ b/src/IO/Datatypes.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +struct Checkpoint_s { + poet::Field &field; + uint32_t iteration; +}; \ No newline at end of file diff --git a/src/IO/HDF5Functions.hpp b/src/IO/HDF5Functions.hpp new file mode 100644 index 000000000..e7954a2f7 --- /dev/null +++ b/src/IO/HDF5Functions.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include +#include "Datatypes.hpp" + +int write_checkpoint(const std::string &file_path, struct Checkpoint_s &&checkpoint); + +int read_checkpoint(const std::string &file_path, struct Checkpoint_s &checkpoint); \ No newline at end of file diff --git a/src/IO/checkpoint.cpp b/src/IO/checkpoint.cpp new file mode 100644 index 000000000..f197c22f5 --- /dev/null +++ b/src/IO/checkpoint.cpp @@ -0,0 +1,28 @@ +#include "IO/Datatypes.hpp" +#include +#include + +int write_checkpoint(const std::string &file_path, struct Checkpoint_s &&checkpoint){ + + // TODO: errorhandling + H5Easy::File file(file_path, H5Easy::File::Overwrite); + + + H5Easy::dump(file, "/MetaParam/Iterations", checkpoint.iteration); + H5Easy::dump(file, "/Grid/Names", checkpoint.field.GetProps()); + H5Easy::dump(file, "/Grid/Chemistry", checkpoint.field.As2DVector()); + + return 0; +} + +int read_checkpoint(const std::string &file_path, struct Checkpoint_s &checkpoint){ + + H5Easy::File file(file_path, H5Easy::File::ReadOnly); + + checkpoint.iteration = H5Easy::load(file, "/MetaParam/Iterations"); + + checkpoint.field = H5Easy::load>>(file, "/Grid/Chemistry"); + + return 0; +} + diff --git a/src/poet.cpp b/src/poet.cpp index 742f4c395..2e204bdfc 100644 --- a/src/poet.cpp +++ b/src/poet.cpp @@ -26,6 +26,8 @@ #include "CLI/CLI.hpp" #include "Chemistry/ChemistryModule.hpp" #include "DataStructures/Field.hpp" +#include "IO/Datatypes.hpp" +#include "IO/HDF5Functions.hpp" #include "Init/InitialList.hpp" #include "Transport/DiffusionModule.hpp" @@ -393,6 +395,18 @@ static Rcpp::List RunMasterLoop(RInsidePOET &R, const RuntimeParameters ¶ms, // store_result is TRUE) call_master_iter_end(R, diffusion.getField(), chem.getField()); + // TODO: write checkpoint + // checkpoint struct --> field and iteration + + if (iter == 1) { + write_checkpoint("checkpoint1.hdf5", + {.field = chem.getField(), .iteration = iter}); + } else if (iter == 2) { + Checkpoint_s checkpoint_read{.field = chem.getField()}; + read_checkpoint("checkpoint1.hdf5", checkpoint_read); + iter = checkpoint_read.iteration; + } + diffusion.getField().update(chem.getField()); MSG("End of *coupling* iteration " + std::to_string(iter) + "/" +