#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& 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); }