From d82fb6dab84dfda81db23e795a169cdeccd6abe3 Mon Sep 17 00:00:00 2001 From: csendlinger Date: Mon, 23 Nov 2026 15:43:37 +0100 Subject: [PATCH] Restarting now should now work if the number of procs is larger than the number of procs of the restarted state. (Debug prints are still in the code) --- src/IO/mpi_particle_reader.cpp | 163 +++++++++++++++++++++++++++++++++ src/IO/restart.cpp | 72 +++++++++++++-- 2 files changed, 228 insertions(+), 7 deletions(-) diff --git a/src/IO/mpi_particle_reader.cpp b/src/IO/mpi_particle_reader.cpp index aa58b1a..8b7f836 100644 --- a/src/IO/mpi_particle_reader.cpp +++ b/src/IO/mpi_particle_reader.cpp @@ -136,6 +136,169 @@ void MpiParticleReader::readParticles( out << "\nspecname of first serialized particle: " << storage_queue[0].specieName(); DEBUG_LEVEL_1(out.str()); } +#endif + + // close everything + MPI_File_close(&file); + MPI_Type_free(&particle_type); + MPI_Type_free(&file_header_type); + MPI_Type_free(&cpu_header_type); + MPI_Info_free(&info); +} + +void MpiParticleReader::readParticles( + std::vector& storage_queue, int nold) { + MPI_Comm io_comm; + // io_group: from which previous process has to be read + // io_rank : the rank of this process within io_group + // io_group_size: the size of the group of processes with a specific io_group + int io_rank, io_group, io_group_size; + + io_comm = MPI_COMM_WORLD; + io_group = MpiCommunication::mpiRank() % nold; + io_rank = MpiCommunication::mpiRank() / nold; + io_group_size = MpiCommunication::mpiSize() / nold; + if(io_group < MpiCommunication::mpiSize() % nold) { + io_group_size += 1; + } + + fprintf(stdout, "<%d>: io_group: %d, io_rank: %d, io_group_size: %d\n", + MpiCommunication::mpiRank(), io_group, io_rank, io_group_size); + + +#ifdef debug + DEBUG_LEVEL_0("Reading particles from: " + absolute_filename_); +#endif + + int status; + + MPI_Datatype particle_type; + if(sizeof(particle::SerializedParticle) % sizeof(double) != 0) { + ERROR("Size of SerializedParticle is not a multiple of the size of double!"); + } + status = MPI_Type_contiguous(sizeof(particle::SerializedParticle)/sizeof(double), + MPI_DOUBLE, &particle_type); + status |= MPI_Type_commit(&particle_type); + + if(status) { + ERROR("Particle data type could not be defined!"); + } + + MPI_Info info; + MPI_Info_create(&info); + + // open file + MPI_File file; + MPI_Status mstatus; + +#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3) + DEBUG_LEVEL_1("Reading particles from: " + absolute_filename_); +#endif + + status = MPI_File_open(io_comm, (char*)absolute_filename_.c_str(), + MPI_MODE_RDONLY, info, &file); + if(status) { + ERROR("Particle file \'" + absolute_filename_ + "\' could not be opened!"); + } + + //read file header + MPI_Datatype file_header_type = build_file_header_type(); + mpi_io_header_t file_header; + status = MPI_File_set_view(file, 0, file_header_type, file_header_type, + (char*)"native", info); + if(status) { + ERROR("View for file header could not be set!"); + } + + status = MPI_File_read_all(file, &file_header, 1, file_header_type, &mstatus); + if(status) { + ERROR("File header could not be read (status: " + std::to_string(status) + + ")!"); + } + + if(!file_header.verify()) { + ERROR("File header does not verify!"); + } + + // read CPU header + std::uint64_t headeroffset = file_header.cpu_header_offset + + sizeof(cpu_header_t)*io_group; + + MPI_Datatype cpu_header_type = build_cpu_header_type(); + cpu_header_t cpu_header; + + status = MPI_File_set_view(file, headeroffset, cpu_header_type, cpu_header_type, + (char*)"native", info); + if(status) { + ERROR("File view for the CPU header could not be set!"); + } + + status = MPI_File_read_all(file, &cpu_header, 1, cpu_header_type, &mstatus); + if(status) { + ERROR("CPU header could not be read!"); + } + + 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 + size_t particle_number = cpu_header.num_particles / io_group_size; + + int overflow = cpu_header.num_particles % io_group_size; + + // calculate offset from particles read by other processes in this io_group + std::uint64_t this_offset = cpu_header.file_offset; + this_offset += io_rank*particle_number*sizeof(particle::SerializedParticle); + this_offset = io_rank < overflow ? this_offset + io_rank*particle_number*sizeof(particle::SerializedParticle) + : this_offset + overflow*particle_number*sizeof(particle::SerializedParticle); + + // correct number of particles for this proc + particle_number = (io_rank < overflow) ? + particle_number + 1 : particle_number; + + fprintf(stdout, "<%d>: pn: %ld, thisoffset: %ld\n",MpiCommunication::mpiRank(), particle_number, this_offset - cpu_header.file_offset); + + // set file view to the part where the particles are stored + status = MPI_File_set_view(file, this_offset, MPI_DOUBLE, + particle_type, (char*)"native", info); + if(status) { + ERROR("File view for the Particles could not be set!"); + } + + storage_queue.resize(particle_number); + + // read the particles + status = MPI_File_read_all(file, storage_queue.data(), particle_number, + particle_type, &mstatus); + if(status) { + ERROR("Particles could not be read (status: " + std::to_string(status) + + ")!"); + } + +#ifdef debug +{ + std::stringstream out; + out << "group: " << io_group << ", rank: "<< io_rank; + out << ": number of particles in queue: " << storage_queue.size(); + out << "\nposition of first serialized particle: " << storage_queue[0].cartesianPosition(); + out << "\nvelocity of first serialized particle: " << storage_queue[0].cartesianVelocity(); + out << "\nweight of first serialized particle: " << storage_queue[0].weight(); + out << "\nspecname of first serialized particle: " << storage_queue[0].specieName(); + out << "\n"; + out << "group: " << io_group << ", rank: "<< io_rank; + out << "\nposition of last serialized particle: " << storage_queue[particle_number - 1].cartesianPosition(); + out << "\nvelocity of last serialized particle: " << storage_queue[particle_number - 1].cartesianVelocity(); + out << "\nweight of last serialized particle: " << storage_queue[particle_number - 1].weight(); + out << "\nspecname of last serialized particle: " << storage_queue[particle_number - 1].specieName(); + DEBUG_LEVEL_1(out.str()); +} #endif // close everything diff --git a/src/IO/restart.cpp b/src/IO/restart.cpp index b12c7dd..e401dfc 100644 --- a/src/IO/restart.cpp +++ b/src/IO/restart.cpp @@ -39,10 +39,39 @@ void Restart::restart( if(!do_restart_) { return; } + + SIM_INFO("Reading number of previous processes."); + process_number_old_ = reader_.readInt("NumberProcesses"); + + if(process_number_old_ > MpiCommunication::mpiSize()) { + ERROR("Number of processes changes between restart! (old: " + std::to_string(process_number_old_) + + ", new: " + std::to_string(MpiCommunication::mpiSize()) + ")"); + } + + SIM_INFO("Restarting from earlier state."); + + if(process_number_old_ < MpiCommunication::mpiSize()) { #ifdef debug_level1 - DEBUG_LEVEL_1("Restarting rng"); + DEBUG_LEVEL_1("Restarting rng"); #endif - restartRandomNumbers(random_numbers); + restartRandomNumbers(random_numbers, process_number_old_); +#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3) + DEBUG_LEVEL_1("Reloading particles."); +#endif + particle_reader_.readParticles(storage_queue, process_number_old_); + + } else { +#ifdef debug_level1 + DEBUG_LEVEL_1("Restarting rng"); +#endif + restartRandomNumbers(random_numbers); +#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3) + DEBUG_LEVEL_1("Reloading particles."); +#endif + particle_reader_.readParticles(storage_queue); + + } + #ifdef debug_level1 DEBUG_LEVEL_1("Restarting poisson solver."); @@ -65,11 +94,6 @@ void Restart::restart( std::string path = "/RestartInfo/" + restart_identifier_ + "/InjectionGroupOffset"; injectionGroupOffset = reader_.readInt(path); } - -#if defined(debug_level1) || defined(debug_level2) || defined(debug_level3) - DEBUG_LEVEL_1("Reloading particles."); -#endif - particle_reader_.readParticles(storage_queue); } void Restart::restartRandomNumbers(RandomNumbers* random) { @@ -88,6 +112,40 @@ void Restart::restartRandomNumbers(RandomNumbers* random) { } } +void Restart::restartRandomNumbers(RandomNumbers* random, int nold) { + int io_group, io_rank; + io_group = MpiCommunication::mpiRank() % nold; + io_rank = MpiCommunication::mpiRank() / nold; + + std::string path = "/RestartInfo/" + restart_identifier_ + + "/CPU_" + std::to_string(io_group); + std::stringstream engine_state = reader_.readEngineString(path); + std::mt19937* engine = random->engine(); + engine_state >> *engine; + + std::stringstream check; + check << *engine; + + if(check.str() != engine_state.str()) { + ERROR("Something went wrong in the initialization of the " + "random engine."); + } + + // discard the first io_rank values + (*engine).discard(io_rank); + // draw the next value of the current engine + size_t seed = (*engine)(); + + 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 + std::mt19937 tmp(seed); + + // serialize and de-serialize it to set the pointer of the actual engine + engine_state << tmp; + engine_state >> *engine; +} + // only restart the time from which the simulation is restarted. void Restart::restartTimer(species::TrackedSpecie* reference_specie) { double time;