Commit 47b58d16 authored by Berenger Bramas's avatar Berenger Bramas
Browse files

Add possible output class using mpi io or hdf5 -- not used in the code yet

parent 7681c302
#ifndef PARTICLES_WRITER_HDF_HPP
#define PARTICLES_WRITER_HDF_HPP
#include <hdf5.h>
#include <cstdlib>
#include <mpi.h>
#include <cassert>
class ParticlesWriterHDF{
static const int nbValuesPerParticles = 3;
static const int dim = 4;
const MPI_Comm comm;
const std::string filename;
MPI_File mpifile;
int nbTimeSteps;
int nbClouds;
int nbParticles;
hid_t plist_id;
hid_t file_id;
hid_t dset_id;
public:
ParticlesWriterHDF(const MPI_Comm& inComm, const std::string& inFilename,
const int inNbTimeSteps, const int inNbClouds, const int inNbParticles)
: comm(inComm), filename(inFilename),
nbTimeSteps(inNbTimeSteps), nbClouds(inNbClouds), nbParticles(inNbParticles){
hid_t plist_id_par = H5Pcreate(H5P_FILE_ACCESS);
int retTest = H5Pset_fapl_mpio(plist_id_par, MPI_COMM_WORLD, MPI_INFO_NULL);
assert(retTest >= 0);
// Parallel HDF5 write
file_id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC | H5F_ACC_DEBUG/*H5F_ACC_EXCL*/, H5P_DEFAULT/*H5F_ACC_RDWR*/, plist_id_par);
assert(file_id >= 0);
H5Pclose(plist_id_par);
const hsize_t dimsf[dim] = {nbTimeSteps, nbClouds, nbParticles, 3};
hid_t filespace = H5Screate_simple(dim, dimsf, NULL);
assert(filespace >= 0);
dset_id = H5Dcreate(file_id, "dataset1", H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
assert(dset_id >= 0);
H5Sclose(filespace);
}
~ParticlesWriterHDF(){
H5Dclose(dset_id);
H5Pclose(plist_id);
H5Fclose(file_id);
}
void writeParticles(const double* particlesData,
const int timeIndex,
const int cloudIndex,
const int particleOffset,
const int inParticlesToWrite){
assert(timeIndex < nbTimeSteps);
assert(cloudIndex < nbClouds);
assert(particleOffset < nbParticles);
assert(particleOffset+inParticlesToWrite <= nbParticles);
const hsize_t count[dim] = {1, 1, inParticlesToWrite, 3};
const hsize_t offset[dim] = {timeIndex, cloudIndex, particleOffset, 0};
hid_t memspace = H5Screate_simple(dim, count, NULL);
hid_t filespace = H5Dget_space(dset_id);
H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);
plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
herr_t status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, memspace, filespace,
plist_id, particlesData);
assert(status >= 0);
H5Sclose(memspace);
H5Sclose(filespace);
}
};
#endif // PARTICLES_WRITER_HDF_HPP
#ifndef PARTICLES_WRITER_MPIIO_HPP
#define PARTICLES_WRITER_MPIIO_HPP
#include <string>
#include <mpi.h>
#include <cassert>
#include "scope_timer.hpp"
class ParticlesWriterMpiio{
protected:
static const int nbValuesPerParticles = 3;
const MPI_Comm comm;
const std::string filename;
MPI_File mpifile;
int nbTimeSteps;
int nbClouds;
int nbParticles;
public:
ParticlesWriterMpiio(const MPI_Comm& inComm, const std::string& inFilename,
const int inNbTimeSteps, const int inNbClouds, const int inNbParticles)
: comm(inComm), filename(inFilename),
nbTimeSteps(inNbTimeSteps), nbClouds(inNbClouds), nbParticles(inNbParticles){
{
TIMEZONE("ParticlesWriterMpiio::MPI_File_open");
int mpiRet = MPI_File_open(comm, filename.c_str(),
MPI_MODE_CREATE|MPI_MODE_WRONLY, MPI_INFO_NULL, &mpifile);
assert(mpiRet == MPI_SUCCESS);
}
{
TIMEZONE("ParticlesWriterMpiio::MPI_File_set_size");
mpiRet = MPI_File_set_size(mpifile, nbTimeSteps*nbClouds*nbParticles*sizeof(double)*3);
assert(mpiRet == MPI_SUCCESS);
}
}
~ParticlesWriterMpiio(){
TIMEZONE("ParticlesWriterMpiio::MPI_File_close");
int mpiRet = MPI_File_close(&mpifile);
assert(mpiRet == MPI_SUCCESS);
}
void writeParticles(const double* particlesData,
const int timeIndex,
const int cloudIndex,
const int particleOffset,
const int inParticlesToWrite){
TIMEZONE("ParticlesWriterMpiio::MPI_File_close");
assert(timeIndex < nbTimeSteps);
assert(cloudIndex < nbClouds);
assert(particleOffset < nbParticles);
assert(particleOffset+inParticlesToWrite <= nbParticles);
const MPI_Offset globalParticlesOffset = timeIndex*nbParticles*nbClouds
+ cloudIndex*nbParticles
+ particleOffset;
const MPI_Offset writingOffset = globalParticlesOffset * sizeof(double)*nbValuesPerParticles;
mpiRet = MPI_File_write_at(mpifile, writingOffset,
particlesData, inParticlesToWrite*nbValuesPerParticles, MPI_DOUBLE,
MPI_STATUS_IGNORE);
assert(mpiRet == MPI_SUCCESS);
}
};
#endif // PARTICLES_WRITER_MPIIO_HPP
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment