diff --git a/doc/source/programmingguide.rst b/doc/source/programmingguide.rst index d8f989df..60c6da4c 100644 --- a/doc/source/programmingguide.rst +++ b/doc/source/programmingguide.rst @@ -743,7 +743,10 @@ adding ``-DIdefix_DEBUG=ON`` when calling cmake). This forces the code to log ea returned (this is achieved thanks to the ``idfx::pushRegion(std::string)`` and ``idfx::popRegion()`` which are found at the beginning and end of each function). In addition, ``Idefix_DEBUG`` will force *Idefix* to wait for each ``idefix_for`` to finish before continuing to the next instruction (otherwise, ``idefix_for`` are asynchronous when using -an accelerator). This simplifies the detection and identification of bugs in ``idefix_for`` loops. Note that all of these +an accelerator). This simplifies the detection and identification of bugs in ``idefix_for`` loops. Finally, ``Idefix_DEBUG`` +stops HDF5 (XDMF) outputs from recording the access/modification/change/birth times of their objects, so that runs producing +identical data (e.g. a restart and the uninterrupted run) produce byte-identical ``.h5`` files. Outside of debug builds, these +files differ by these timestamps only, and ``h5diff`` can be used to compare their content. Note that all of these debugging features induce a large overhead, and should therefore not be activated in production runs. If you suspect an out-of-bound access, it may be worth enabling additionaly ``Kokkos_ENABLE_BOUNDS_CHECK`` that will check diff --git a/doc/source/reference/makefile.rst b/doc/source/reference/makefile.rst index 5f86c66f..8fd6bfd7 100644 --- a/doc/source/reference/makefile.rst +++ b/doc/source/reference/makefile.rst @@ -44,7 +44,8 @@ Several options can be enabled from the command line (or are accessible with ``c ``-D Idefix_DEBUG=ON`` Enable debug options in *Idefix*. This triggers a lot of outputs, and automatic bound checks of array accesses. As a result, this - option makes the code very slow. + option makes the code very slow. It also stops HDF5 outputs from recording object timestamps, so that identical data produce + byte-identical ``.h5`` files. ``-D Idefix_RUNTIME_CHECKS=ON`` Include (potentially expensive) runtime sanity checks implemented with ``RUNTIME_CHECK_HOST`` and ``RUNTIME_CHECK_KERNEL``. diff --git a/src/main.cpp b/src/main.cpp index b46c8a43..488d7aed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -229,7 +229,7 @@ int main( int argc, char* argv[] ) { break; } if(input.maxCycles>=0) { - if(Tint.GetNCycles() >= input.maxCycles) { + if(Tint.GetNCyclesThisRun() >= input.maxCycles) { idfx::cout << "Main: Reached maximum number of integration cycles." << std::endl; break; } @@ -247,7 +247,7 @@ int main( int argc, char* argv[] ) { n_seconds = divres.rem; double perfs = timer.seconds() / grid.np_int[IDIR] / grid.np_int[JDIR] - / grid.np_int[KDIR] / Tint.GetNCycles() * idfx::psize; + / grid.np_int[KDIR] / Tint.GetNCyclesThisRun() * idfx::psize; idfx::cout << "Main: Reached t=" << data.t << std::endl; idfx::cout << "Main: Completed in "; @@ -277,8 +277,8 @@ int main( int argc, char* argv[] ) { idfx::cout << "s"; } idfx::cout << " "; - idfx::cout << "and " << Tint.GetNCycles() << " cycle"; - if (Tint.GetNCycles() != 1) { + idfx::cout << "and " << Tint.GetNCyclesThisRun() << " cycle"; + if (Tint.GetNCyclesThisRun() != 1) { idfx::cout << "s"; } idfx::cout << std::endl; diff --git a/src/output/dump.cpp b/src/output/dump.cpp index 6d4da610..29d43c43 100644 --- a/src/output/dump.cpp +++ b/src/output/dump.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "dump.hpp" #include "dataBlockHost.hpp" #include "gridHost.hpp" @@ -218,16 +219,20 @@ Dump::~Dump() { } void Dump::WriteString(IdfxFileHandler fileHdl, char *str, int size) { + /* Pad with zeros after the terminating null: the callers' buffers are not initialised + beyond it, and writing them as they are would make identical dumps differ. */ + std::vector padded(size, 0); + std::strncpy(padded.data(), str, size); #ifdef WITH_MPI MPI_Status status; MPI_SAFE_CALL(MPI_File_set_view(fileHdl, this->offset, MPI_BYTE, MPI_CHAR, "native", MPI_INFO_NULL )); if(idfx::prank==0) { - MPI_SAFE_CALL(MPI_File_write(fileHdl, str, size, MPI_CHAR, &status)); + MPI_SAFE_CALL(MPI_File_write(fileHdl, padded.data(), size, MPI_CHAR, &status)); } offset=offset+size; #else - if(fwrite (str, sizeof(char), size, fileHdl) != size) { + if(fwrite (padded.data(), sizeof(char), size, fileHdl) != size) { IDEFIX_ERROR("Unable to write to file. Check your filesystem permissions and disk quota."); } #endif diff --git a/src/output/xdmf.cpp b/src/output/xdmf.cpp index aceda037..1f6a195e 100644 --- a/src/output/xdmf.cpp +++ b/src/output/xdmf.cpp @@ -289,6 +289,10 @@ Xdmf::Xdmf(Input &input, DataBlock *datain) { } #endif #endif + + // Register variables that are required in restart dumps + if(data->dump.get() != nullptr) + data->dump->RegisterVariable(&xdmfFileNumber, "xdmfFileNumber"); } int Xdmf::Write() { @@ -321,6 +325,22 @@ int Xdmf::Write() { filename = outputDirectory/ssfileName.str(); filename_xmf = outputDirectory/ssfileNameXmf.str(); + // By default HDF5 records access/modification/change/birth times in the object headers, + // so files holding identical data differ byte for byte (h5diff, which compares the + // content, still reports them as identical). Debug builds do not record these times, + // so that outputs can be compared byte for byte, e.g. a restart vs the uninterrupted run. + #ifdef DEBUG + constexpr bool trackTimes = false; + #else + constexpr bool trackTimes = true; + #endif + fcpl = H5Pcreate(H5P_FILE_CREATE); // applies to the root group + H5Pset_obj_track_times(fcpl, trackTimes); + gcpl = H5Pcreate(H5P_GROUP_CREATE); + H5Pset_obj_track_times(gcpl, trackTimes); + dcpl = H5Pcreate(H5P_DATASET_CREATE); + H5Pset_obj_track_times(dcpl, trackTimes); + #ifdef WITH_MPI hid_t file_access = H5Pcreate(H5P_FILE_ACCESS); // #if MPI_POSIX == YES @@ -328,16 +348,17 @@ int Xdmf::Write() { // #else H5Pset_fapl_mpio(file_access, MPI_COMM_WORLD, MPI_INFO_NULL); // #endif - hid_t fileHdf = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, file_access); + hid_t fileHdf = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, fcpl, file_access); H5Pclose(file_access); #else - hid_t fileHdf = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + hid_t fileHdf = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, fcpl, H5P_DEFAULT); #endif hid_t group_fields; // = static_cast(malloc(sizeof(hid_t))); std::stringstream ssgroup_name; ssgroup_name << "/Timestep_" << xdmfFileNumber; - hid_t timestep = H5Gcreate(fileHdf, ssgroup_name.str().c_str(), 0); + hid_t timestep = H5Gcreate2(fileHdf, ssgroup_name.str().c_str(), H5P_DEFAULT, + gcpl, H5P_DEFAULT); WriteHeader(fileHdf, ssfileName.str(), filename_xmf, data->t, timestep, group_fields); @@ -427,6 +448,9 @@ int Xdmf::Write() { H5Gclose(group_fields); // Close group "vars" H5Gclose(timestep); H5Fclose(fileHdf); + H5Pclose(dcpl); + H5Pclose(gcpl); + H5Pclose(fcpl); xdmfFileNumber++; // Make file number @@ -578,7 +602,7 @@ void Xdmf::WriteHeader( H5Sclose(strspace); /* Create group_fields "vars" (cell-centered vars) */ - group_fields = H5Gcreate(timestep, "vars", 0); + group_fields = H5Gcreate2(timestep, "vars", H5P_DEFAULT, gcpl, H5P_DEFAULT); /* Define "coords" attribute of group_fields "vars" */ dimstr = 3; @@ -598,7 +622,7 @@ void Xdmf::WriteHeader( hsize_t count[DIMENSIONS]; /* Create group "cell_coords" (centered mesh) */ - group = H5Gcreate(fileHdf, "cell_coords", 0); + group = H5Gcreate2(fileHdf, "cell_coords", H5P_DEFAULT, gcpl, H5P_DEFAULT); #if (DIMENSIONS == 1) || (DIMENSIONS == 3) for (int dir = 0; dir < DIMENSIONS; dir++) { @@ -640,7 +664,8 @@ void Xdmf::WriteHeader( DUMP_DATATYPE *cell_mesh; for (int dir = 0; dir < 3; dir++) { - dataset = H5Dcreate(group, directions[dir].c_str(), H5_DUMP_DATATYPE, dataspace, H5P_DEFAULT); + dataset = H5Dcreate(group, directions[dir].c_str(), H5_DUMP_DATATYPE, dataspace, + dcpl); cell_mesh = Kokkos::subview (this->cell_coord, dir, Kokkos::ALL(), @@ -664,7 +689,7 @@ void Xdmf::WriteHeader( H5Gclose(group); /* Close group "cell_coords" */ /* Create group "node_coords" (node mesh) */ - group = H5Gcreate(fileHdf, "node_coords", 0); + group = H5Gcreate2(fileHdf, "node_coords", H5P_DEFAULT, gcpl, H5P_DEFAULT); #if (DIMENSIONS == 1) || (DIMENSIONS == 3) for (int dir = 0; dir < DIMENSIONS; dir++) { @@ -713,7 +738,8 @@ void Xdmf::WriteHeader( #endif for (int dir = 0; dir < 3; dir++) { - dataset = H5Dcreate(group, directions[dir].c_str(), H5_DUMP_DATATYPE, dataspace, H5P_DEFAULT); + dataset = H5Dcreate(group, directions[dir].c_str(), H5_DUMP_DATATYPE, dataspace, + dcpl); DUMP_DATATYPE *node_mesh = Kokkos::subview (this->node_coord, dir, @@ -828,7 +854,7 @@ void Xdmf::WriteScalar( // We define the dataset that contain the fields. dataset = H5Dcreate(group_fields, var_name.c_str(), H5_DUMP_DATATYPE, - dataspace, H5P_DEFAULT); + dataspace, dcpl); #ifdef WITH_MPI err = H5Dwrite(dataset, H5_DUMP_DATATYPE, memspace, dataspace, plist_id_mpiio, Vin); diff --git a/src/output/xdmf.hpp b/src/output/xdmf.hpp index 8af8348f..0ff76492 100644 --- a/src/output/xdmf.hpp +++ b/src/output/xdmf.hpp @@ -100,6 +100,9 @@ class Xdmf { // output directory std::filesystem::path outputDirectory; + // HDF5 creation property lists for the file, groups and datasets (set in Write()) + hid_t fcpl, gcpl, dcpl; + #ifdef WITH_MPI int mpi_data_start[3]; int mpi_data_size[3]; diff --git a/src/timeIntegrator.cpp b/src/timeIntegrator.cpp index e23808f0..54ea6206 100644 --- a/src/timeIntegrator.cpp +++ b/src/timeIntegrator.cpp @@ -70,6 +70,8 @@ TimeIntegrator::TimeIntegrator(Input & input, DataBlock & data) { data.t=0.0; ncycles=0; + // dumps without this field (older code) leave it at 0, i.e. the old behaviour + data.dump->RegisterVariable(&ncycles, "ncycles"); if(nstages==2) { wc[0] = 0.5; @@ -119,7 +121,8 @@ void TimeIntegrator::ShowLog(DataBlock &data) { int col_width{16}; - if (ncycles == 0) { + if (!logHeaderDone) { + logHeaderDone = true; idfx::cout << "TimeIntegrator: "; idfx::cout << std::setw(col_width) << "time"; idfx::cout << " | " << std::setw(col_width) << "cycle"; @@ -267,6 +270,13 @@ void TimeIntegrator::Cycle(DataBlock &data) { idfx::pushRegion("TimeIntegrator::Cycle"); + if(firstCycle) { + // capture the cycle count at the start of this run (0 for a fresh run, + // the value stored in the dump for a restart) + ncyclesStart = ncycles; + firstCycle = false; + } + if(ncycles%cyclePeriod==0) ShowLog(data); // Launch user step before everything @@ -435,6 +445,10 @@ int64_t TimeIntegrator::GetNCycles() { return(ncycles); } +int64_t TimeIntegrator::GetNCyclesThisRun() { + return(ncycles-ncyclesStart); +} + // Check whether our maximumruntime has been reached. Reduce the results on all of the cores // to make sure they stop simultaneously even if running time are not perfectly in sync bool TimeIntegrator::CheckForMaxRuntime() { diff --git a/src/timeIntegrator.hpp b/src/timeIntegrator.hpp index fc828889..f707a560 100644 --- a/src/timeIntegrator.hpp +++ b/src/timeIntegrator.hpp @@ -27,6 +27,7 @@ class TimeIntegrator { public: int64_t GetNCycles(); // Get current number of cycles + int64_t GetNCyclesThisRun(); // Get number of cycles performed since (re)start // Constructor from input and given datablock TimeIntegrator(Input &, DataBlock &); @@ -61,7 +62,10 @@ class TimeIntegrator { real cfl; // CFL number real cflMaxVar; // Max CFL variation number real maxdivB{0}; // Maximum allowed divB - int64_t ncycles; // # of cycles + int64_t ncycles; // # of cycles (stored in dumps as int64, resumed on restart) + bool firstCycle{true}; // ncyclesStart has not yet been captured + int64_t ncyclesStart{0}; // value of ncycles at the start of this run + bool logHeaderDone{false}; // the log header has been printed double computeLastLog; // Timer for actual computeTime