123 lines
2.7 KiB
C++
123 lines
2.7 KiB
C++
#ifndef MPI_PARTICLE_FORMAT_H
|
|
#define MPI_PARTICLE_FORMAT_H
|
|
|
|
#include <cstdint>
|
|
|
|
#include "mpi.h"
|
|
/* File format (as in ACRONYM):
|
|
*
|
|
* mpi_io_header
|
|
* <padding>
|
|
* cpu_header[n_cpus]
|
|
* <padding>
|
|
* data_of_cpu_1 <padding>
|
|
* data_of_cpu_2 <padding>
|
|
* ...
|
|
*/
|
|
|
|
/* file header*/
|
|
struct mpi_io_header_t {
|
|
std::uint64_t magic;
|
|
std::uint64_t cpu_header_offset;
|
|
std::uint32_t num_cpus;
|
|
double dt;
|
|
|
|
bool verify() {
|
|
// Sanity Check if file is the right format
|
|
if(magic != ((std::uint64_t)'KA'<<32) + 'RL') {
|
|
return false;
|
|
}
|
|
if(dt < 0) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
/* per-CPU header */
|
|
struct cpu_header_t {
|
|
std::uint64_t num_particles;
|
|
std::uint64_t file_offset;
|
|
};
|
|
|
|
// seems to work only if alignment is a power of 2
|
|
static std::uint64_t pad_to_blocksize(std::uint64_t size, std::uint64_t alignment) {
|
|
size &= ~(alignment - 1);
|
|
size += alignment;
|
|
return size;
|
|
}
|
|
|
|
#ifdef MPI_VERSION
|
|
|
|
#ifndef MPI_UINT32_T
|
|
#define MPI_UNIT32_T MPI_UNSIGNED
|
|
#endif
|
|
|
|
#ifndef MPI_UINT64_T
|
|
#define MPI_UNIT64_T MPI_UNSIGNED_LONG_LONG
|
|
#endif
|
|
|
|
/* Custom MPI data types for the header structs */
|
|
static MPI_Datatype build_file_header_type() {
|
|
int status;
|
|
MPI_Datatype type;
|
|
int blocklengths[4];
|
|
MPI_Aint disps[4];
|
|
MPI_Datatype types[4];
|
|
|
|
disps[0] = offsetof(mpi_io_header_t, magic);
|
|
types[0] = MPI_UINT64_T;
|
|
blocklengths[0] = 1;
|
|
|
|
disps[1] = offsetof(mpi_io_header_t, cpu_header_offset);
|
|
types[1] = MPI_UINT64_T;
|
|
blocklengths[1] = 1;
|
|
|
|
disps[2] = offsetof(mpi_io_header_t, num_cpus);
|
|
types[2] = MPI_UINT32_T;
|
|
blocklengths[2] = 1;
|
|
|
|
disps[3] = offsetof(mpi_io_header_t, dt);
|
|
types[3] = MPI_DOUBLE;
|
|
blocklengths[3] = 1;
|
|
|
|
status = MPI_Type_create_struct(4, blocklengths, disps, types, &type);
|
|
status |= MPI_Type_commit(&type);
|
|
|
|
if(status) {
|
|
fprintf(stderr, "Creation of the data type \"file_header\" failed!\n");
|
|
return 0;
|
|
}
|
|
|
|
return type;
|
|
}
|
|
|
|
static MPI_Datatype build_cpu_header_type() {
|
|
int status;
|
|
MPI_Datatype type;
|
|
int blocklengths[2];
|
|
MPI_Aint disps[2];
|
|
MPI_Datatype types[2];
|
|
|
|
disps[0] = offsetof(cpu_header_t, num_particles);
|
|
types[0] = MPI_UINT64_T;
|
|
blocklengths[0] = 1;
|
|
|
|
disps[1] = offsetof(cpu_header_t, file_offset);
|
|
types[1] = MPI_UINT64_T;
|
|
blocklengths[1] = 1;
|
|
|
|
status = MPI_Type_create_struct(2, blocklengths, disps, types, &type);
|
|
status |= MPI_Type_commit(&type);
|
|
|
|
if(status) {
|
|
fprintf(stderr, "Creation of the data type \"cpu_header\" failed!\n");
|
|
return 0;
|
|
}
|
|
|
|
return type;
|
|
}
|
|
|
|
#endif /* MPI_VERSION */
|
|
|
|
#endif /* MPI_PARTICLE_FORMAT_H */ |