procs is larger than the number of procs of the restarted state. (Debug prints are still in the code)
310 lines
11 KiB
C++
310 lines
11 KiB
C++
#include "IO/mpi_particle_reader.h"
|
|
#include "IO/mpi_particle_format.h"
|
|
#include "simulation/mpi_communication.h"
|
|
#include "simulation/particle.h"
|
|
|
|
#include "mpi.h"
|
|
|
|
// filename has to NOT include the ".mpi" component!
|
|
MpiParticleReader::MpiParticleReader(std::string directory, std::string filename,
|
|
int restart_at):
|
|
absolute_filename_(directory + "/" + filename + "-CPUSync-"
|
|
+ std::to_string(restart_at) + ".mpi") {}
|
|
|
|
MpiParticleReader::MpiParticleReader(std::string absolute_filename):
|
|
absolute_filename_(absolute_filename) {}
|
|
|
|
MpiParticleReader::MpiParticleReader(std::string abs_filename_without_extension,
|
|
int restart_at):
|
|
absolute_filename_(abs_filename_without_extension + "-CPUSync-" +
|
|
std::to_string(restart_at) + ".mpi") {}
|
|
|
|
void MpiParticleReader::readParticles(
|
|
std::vector<particle::SerializedParticle>& storage_queue) {
|
|
MPI_Comm io_comm;
|
|
int io_rank;
|
|
|
|
io_comm = MPI_COMM_WORLD;
|
|
io_rank = MpiCommunication::mpiRank();
|
|
|
|
#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_rank;
|
|
|
|
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)
|
|
{
|
|
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
|
|
status = MPI_File_set_view(file, cpu_header.file_offset, MPI_DOUBLE,
|
|
particle_type, (char*)"native", info);
|
|
if(status) {
|
|
ERROR("File view for the Particles could not be set!");
|
|
}
|
|
|
|
storage_queue.resize(cpu_header.num_particles);
|
|
|
|
// read the particles
|
|
status = MPI_File_read_all(file, storage_queue.data(), cpu_header.num_particles,
|
|
particle_type, &mstatus);
|
|
if(status) {
|
|
ERROR("Particles could not be read (status: " + std::to_string(status) +
|
|
")!");
|
|
}
|
|
|
|
#ifdef debug_level1
|
|
{
|
|
std::stringstream out;
|
|
out << io_rank << ": 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();
|
|
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<particle::SerializedParticle>& 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
|
|
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);
|
|
} |