4 Commits
Author SHA1 Message Date
csendlinger 444ca56108 Bugfix: offset calculation has been corrected 2026-11-24 10:29:48 +01:00
csendlinger 5f866c04b4 Formatting 2026-11-24 10:01:23 +01:00
csendlinger 776de1f593 Forgot the header files... 2026-11-24 09:29:51 +01:00
csendlinger c6687a23c3 Now writing the json file to the output hdf5 2026-11-24 09:28:57 +01:00
9 changed files with 74 additions and 39 deletions
+1
View File
@@ -15,6 +15,7 @@ class MpiParticleReader {
MpiParticleReader(std::string abs_filename_without_extension, int restart_at); MpiParticleReader(std::string abs_filename_without_extension, int restart_at);
void readParticles(std::vector<particle::SerializedParticle>& storage_queue); void readParticles(std::vector<particle::SerializedParticle>& storage_queue);
void readParticles(std::vector<particle::SerializedParticle>& storage_queue, int nold);
private: private:
std::string absolute_filename_; std::string absolute_filename_;
+2
View File
@@ -46,9 +46,11 @@ class Restart {
// only load in the potential and calc the electric field // only load in the potential and calc the electric field
void restartPoissonSolver(PoissonSolver* solver); void restartPoissonSolver(PoissonSolver* solver);
void restartRandomNumbers(RandomNumbers* random); void restartRandomNumbers(RandomNumbers* random);
void restartRandomNumbers(RandomNumbers* random, int nold);
void restartParticles(std::vector<particle::SerializedParticle>& storage_queue); void restartParticles(std::vector<particle::SerializedParticle>& storage_queue);
bool do_restart_; bool do_restart_;
int process_number_old_; // number of processes of the state the simulation is restarted from
std::string restart_identifier_; // i.e. "LastSyncPeriod" or "SyncPeriod_*" std::string restart_identifier_; // i.e. "LastSyncPeriod" or "SyncPeriod_*"
Hdf5Reader reader_; Hdf5Reader reader_;
MpiParticleReader particle_reader_; MpiParticleReader particle_reader_;
+2 -1
View File
@@ -43,7 +43,8 @@ class SimulationOutput {
void writeMetaData( void writeMetaData(
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species, std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species,
const std::shared_ptr<PoissonSolver>& poisson_solver); const std::shared_ptr<PoissonSolver>& poisson_solver,
std::string parameter_file_path);
void writeSimulationData( void writeSimulationData(
int cpu_sync, int cpu_sync,
+2
View File
@@ -83,6 +83,8 @@ class Simulation {
int timestep = 0; int timestep = 0;
int injectionGroupOffset = 0; int injectionGroupOffset = 0;
std::string parameter_file_path_;
}; };
#endif /* SIMULATION_H */ #endif /* SIMULATION_H */
+10 -10
View File
@@ -20,7 +20,7 @@ blitz::Array<double, 3> Hdf5Reader::readD3Double(std::string path) {
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -50,7 +50,7 @@ blitz::Array<double, 2> Hdf5Reader::readD2Double(std::string path) {
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -79,7 +79,7 @@ blitz::Array<double, 1> Hdf5Reader::readD1Double(std::string path) {
hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); // H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -109,7 +109,7 @@ double Hdf5Reader::readDouble(std::string path) {
hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); // H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -139,7 +139,7 @@ int Hdf5Reader::readInt(std::string path) {
hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); hid_t dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); // H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -170,7 +170,7 @@ blitz::Array<double, 4> Hdf5Reader::readD4Double(std::string path) {
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -200,7 +200,7 @@ blitz::Array<double, 5> Hdf5Reader::readD5Double(std::string path) {
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -230,7 +230,7 @@ blitz::Array<long int, 4> Hdf5Reader::readD4LongInt(std::string path) {
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -260,7 +260,7 @@ blitz::Array<long int, 5> Hdf5Reader::readD5LongInt(std::string path) {
dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT); dataset = H5Dopen2(file, path.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
@@ -293,7 +293,7 @@ std::stringstream Hdf5Reader::readEngineString(std::string path) {
hid_t dataset = H5Dopen2(file, (path + "/string").c_str(), H5P_DEFAULT); hid_t dataset = H5Dopen2(file, (path + "/string").c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset); hid_t dataspace = H5Dget_space(dataset);
if (dataset < 0) { if (dataset < 0) {
ERROR("The given dataset could not be opened"); ERROR("The given dataset could not be opened " + path);
} }
// turn error report back onby hdf5 because of check H5Dopen2 // turn error report back onby hdf5 because of check H5Dopen2
// H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT); // H5Eset_auto(H5E_DEFAULT, H5E_DEFAULT, H5E_DEFAULT);
+17 -14
View File
@@ -162,8 +162,10 @@ void MpiParticleReader::readParticles(
io_group_size += 1; io_group_size += 1;
} }
#ifdef debug_level3
fprintf(stdout, "<%d>: io_group: %d, io_rank: %d, io_group_size: %d\n", fprintf(stdout, "<%d>: io_group: %d, io_rank: %d, io_group_size: %d\n",
MpiCommunication::mpiRank(), io_group, io_rank, io_group_size); MpiCommunication::mpiRank(), io_group, io_rank, io_group_size);
#endif
#ifdef debug #ifdef debug
@@ -239,14 +241,6 @@ void MpiParticleReader::readParticles(
} }
storage_queue.reserve(cpu_header.num_particles); storage_queue.reserve(cpu_header.num_particles);
#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
{
std::stringstream out;
out << io_rank << ": trying to read " << cpu_header.num_particles;
out << " many particles";
DEBUG_LEVEL_1(out.str());
}
#endif
// particle number if they were equally distributed across procs // particle number if they were equally distributed across procs
size_t particle_number = cpu_header.num_particles / io_group_size; size_t particle_number = cpu_header.num_particles / io_group_size;
@@ -256,14 +250,22 @@ void MpiParticleReader::readParticles(
// calculate offset from particles read by other processes in this io_group // calculate offset from particles read by other processes in this io_group
std::uint64_t this_offset = cpu_header.file_offset; std::uint64_t this_offset = cpu_header.file_offset;
this_offset += io_rank*particle_number*sizeof(particle::SerializedParticle); this_offset += io_rank*particle_number*sizeof(particle::SerializedParticle);
this_offset = io_rank < overflow ? this_offset + io_rank*particle_number*sizeof(particle::SerializedParticle) // fprintf(stdout, "<%d>: offset: %ld, numpart: %ld\n", MpiCommunication::mpiRank(), this_offset, particle_number);
: this_offset + overflow*particle_number*sizeof(particle::SerializedParticle); this_offset = io_rank < overflow ? this_offset + io_rank*sizeof(particle::SerializedParticle)
: this_offset + overflow*sizeof(particle::SerializedParticle);
// fprintf(stdout, "<%d>: offset: %ld, sizeof(SerializedParticle): %ld\n", MpiCommunication::mpiRank(), this_offset, sizeof(particle::SerializedParticle));
// correct number of particles for this proc // correct number of particles for this proc
particle_number = (io_rank < overflow) ? particle_number = (io_rank < overflow) ? particle_number + 1 : particle_number;
particle_number + 1 : particle_number;
fprintf(stdout, "<%d>: pn: %ld, thisoffset: %ld\n",MpiCommunication::mpiRank(), particle_number, this_offset - cpu_header.file_offset); #if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
{
std::stringstream out;
out << io_rank << ": trying to read " << cpu_header.num_particles;
out << " many particles";
DEBUG_LEVEL_1(out.str());
}
#endif
// set file view to the part where the particles are stored // set file view to the part where the particles are stored
status = MPI_File_set_view(file, this_offset, MPI_DOUBLE, status = MPI_File_set_view(file, this_offset, MPI_DOUBLE,
@@ -282,8 +284,9 @@ void MpiParticleReader::readParticles(
")!"); ")!");
} }
#ifdef debug #ifdef debug_level3
{ {
// print first and last loaded particle to check if the alignment is sensible (i.e. no two processes load the same particle)
std::stringstream out; std::stringstream out;
out << "group: " << io_group << ", rank: "<< io_rank; out << "group: " << io_group << ", rank: "<< io_rank;
out << ": number of particles in queue: " << storage_queue.size(); out << ": number of particles in queue: " << storage_queue.size();
+17 -10
View File
@@ -51,23 +51,30 @@ void Restart::restart(
SIM_INFO("Restarting from earlier state."); SIM_INFO("Restarting from earlier state.");
if(process_number_old_ < MpiCommunication::mpiSize()) { if(process_number_old_ < MpiCommunication::mpiSize()) {
#ifdef debug_level1 SIM_INFO("Warning: Restarting from a state with fewer processes. (#old: " +
std::to_string(process_number_old_) +
", #now: " +
std::to_string(MpiCommunication::mpiSize()) + ")");
#ifdef debug_level1
DEBUG_LEVEL_1("Restarting rng"); DEBUG_LEVEL_1("Restarting rng");
#endif #endif
restartRandomNumbers(random_numbers, process_number_old_); restartRandomNumbers(random_numbers, process_number_old_);
#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
DEBUG_LEVEL_1("Reloading particles."); DEBUG_LEVEL_1("Reloading particles.");
#endif #endif
particle_reader_.readParticles(storage_queue, process_number_old_); particle_reader_.readParticles(storage_queue, process_number_old_);
} else { } else {
#ifdef debug_level1 #ifdef debug_level1
DEBUG_LEVEL_1("Restarting rng"); DEBUG_LEVEL_1("Restarting rng");
#endif #endif
restartRandomNumbers(random_numbers); restartRandomNumbers(random_numbers);
#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3)
DEBUG_LEVEL_1("Reloading particles."); DEBUG_LEVEL_1("Reloading particles.");
#endif #endif
particle_reader_.readParticles(storage_queue); particle_reader_.readParticles(storage_queue);
} }
@@ -136,12 +143,12 @@ void Restart::restartRandomNumbers(RandomNumbers* random, int nold) {
// draw the next value of the current engine // draw the next value of the current engine
size_t seed = (*engine)(); size_t seed = (*engine)();
fprintf(stdout, "<%d>: io_group: %d, seed: %ld\n", MpiCommunication::mpiRank(), io_group, seed); // fprintf(stdout, "<%d>: io_group: %d, seed: %ld\n", MpiCommunication::mpiRank(), io_group, seed);
// use this value as a seed and create a temporary mt19937 object // use this value as a seed and create a temporary mt19937 object
std::mt19937 tmp(seed); std::mt19937 tmp(seed);
// serialize and de-serialize it to set the pointer of the actual engine // serialize and de-serialize it to set the pointer of the actual engine (likely not the best implementation)
engine_state << tmp; engine_state << tmp;
engine_state >> *engine; engine_state >> *engine;
} }
+21 -2
View File
@@ -1,3 +1,4 @@
#include <fstream>
#include "mpi.h" #include "mpi.h"
#include "IO/simulation_output.h" #include "IO/simulation_output.h"
#include "IO/hdf5_writer.h" #include "IO/hdf5_writer.h"
@@ -43,7 +44,8 @@ void SimulationOutput::setParticleOutput(OutputTask task) {
void SimulationOutput::writeMetaData( void SimulationOutput::writeMetaData(
std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species, std::map<std::string, std::shared_ptr<species::TrackedSpecie>>& species,
const std::shared_ptr<PoissonSolver>& poisson_solver) { const std::shared_ptr<PoissonSolver>& poisson_solver,
std::string parameter_file_path) {
if (MpiCommunication::mpi_rank == 0) { if (MpiCommunication::mpi_rank == 0) {
for (auto& pair : species) { for (auto& pair : species) {
writeMetaData(pair.second.get()); writeMetaData(pair.second.get());
@@ -66,10 +68,27 @@ void SimulationOutput::writeMetaData(
writeMetaData(poisson_solver.get(), specie_path); writeMetaData(poisson_solver.get(), specie_path);
} }
// write the number of mpi processes used in the current simulation // write the number of mpi processes used in the current simulation
{ {
writer.write(MpiCommunication::mpiSize(), "/NumberProcesses"); writer.write(MpiCommunication::mpiSize(), "/NumberProcesses");
} }
// write the json file to the hdf output
{
std::stringstream file;
std::ifstream istream(parameter_file_path.c_str());
istream >> file.rdbuf();
// find occurance of last slash in the path of the parameter
size_t slshidx = parameter_file_path.rfind("/");
// if its a relative path append a slash otherwise use the substring from the last slash onwards
std::string hdf5path = (slshidx == std::string::npos) ?
"/" + parameter_file_path :
parameter_file_path.substr(slshidx);
writer.write(file.str(), hdf5path);
}
} }
} }
+2 -2
View File
@@ -39,7 +39,7 @@
#include "tools/simulation_time.h" #include "tools/simulation_time.h"
Simulation::Simulation(std::string parameter_filename) Simulation::Simulation(std::string parameter_filename)
: parameter_(parameter_filename) { : parameter_(parameter_filename), parameter_file_path_(parameter_filename) {
init(parameter_); init(parameter_);
} }
@@ -112,7 +112,7 @@ void Simulation::init(Parameter& parameter) {
#ifdef debug #ifdef debug
DEBUG_LEVEL_0("Output meta data."); DEBUG_LEVEL_0("Output meta data.");
#endif #endif
output_->writeMetaData(tracked_species_, poisson_solver_); output_->writeMetaData(tracked_species_, poisson_solver_, parameter_file_path_);
} }
int eventsThisCpu(int events_all_cpus) { int eventsThisCpu(int events_all_cpus) {