diff --git a/examples/hello/hdf5Writer/helloHDF5Writer.cpp b/examples/hello/hdf5Writer/helloHDF5Writer.cpp index aaf85ec64265345c685d90234226fe473f7e6aff..07088fb2ee76ab6276b5214960cb3836ce153c80 100644 --- a/examples/hello/hdf5Writer/helloHDF5Writer.cpp +++ b/examples/hello/hdf5Writer/helloHDF5Writer.cpp @@ -8,7 +8,8 @@ #include <iostream> #include <vector> -#include "adios2.h" +#include <adios2.h> + #include <mpi.h> int main(int argc, char *argv[]) diff --git a/source/adios2/ADIOS.cpp b/source/adios2/ADIOS.cpp index db91c88f32038039e4341e4a99b93453a0f967e7..8fd661a80b0c3f3a15446728afa25955965cd928 100644 --- a/source/adios2/ADIOS.cpp +++ b/source/adios2/ADIOS.cpp @@ -196,9 +196,7 @@ std::shared_ptr<Engine> ADIOS::Open(const std::string &name, } else if (type == "HDF5Reader") // -Junmin { -//#if defined(ADIOS_HAVE_PHDF5) && defined(ADIOS_HAVE_MPI) -#ifdef ADIOS_HAVE_HDF5 - //#ifdef ADIOS_HAVE_MPI +#ifdef ADIOS2_HAVE_HDF5 return std::make_shared<HDF5Reader>(*this, name, accessMode, mpiComm, method); #else diff --git a/source/adios2/engine/hdf5/HDF5Common.cpp b/source/adios2/engine/hdf5/HDF5Common.cpp index 2b2e605ffa83e982914a6b2e32319d2c9398c599..e2a9c521ee6b08d50094910ea43e85920d395350 100644 --- a/source/adios2/engine/hdf5/HDF5Common.cpp +++ b/source/adios2/engine/hdf5/HDF5Common.cpp @@ -10,6 +10,7 @@ #include "HDF5Common.h" +#include <complex> #include <iostream> #include "adios2/ADIOSMPI.h" @@ -20,38 +21,37 @@ namespace adios #define H5_ERROR std::cout << "[ADIOS H5 ERROR] " HDF5Common::HDF5Common() -: m_WriteMode(false), m_Total_timestep(0), m_CurrentTimeStep(0) +: m_WriteMode(false), m_CurrentTimeStep(0), m_NumTimeSteps(0) { - m_DefH5T_COMPLEX_FLOAT = + m_DefH5TypeComplexFloat = H5Tcreate(H5T_COMPOUND, sizeof(std::complex<float>)); - H5Tinsert(m_DefH5T_COMPLEX_FLOAT, "freal", 0, H5T_NATIVE_FLOAT); - H5Tinsert(m_DefH5T_COMPLEX_FLOAT, "fimg", H5Tget_size(H5T_NATIVE_FLOAT), + H5Tinsert(m_DefH5TypeComplexFloat, "freal", 0, H5T_NATIVE_FLOAT); + H5Tinsert(m_DefH5TypeComplexFloat, "fimg", H5Tget_size(H5T_NATIVE_FLOAT), H5T_NATIVE_FLOAT); - m_DefH5T_COMPLEX_DOUBLE = + m_DefH5TypeComplexDouble = H5Tcreate(H5T_COMPOUND, sizeof(std::complex<double>)); - H5Tinsert(m_DefH5T_COMPLEX_DOUBLE, "dreal", 0, H5T_NATIVE_DOUBLE); - H5Tinsert(m_DefH5T_COMPLEX_DOUBLE, "dimg", H5Tget_size(H5T_NATIVE_DOUBLE), + H5Tinsert(m_DefH5TypeComplexDouble, "dreal", 0, H5T_NATIVE_DOUBLE); + H5Tinsert(m_DefH5TypeComplexDouble, "dimg", H5Tget_size(H5T_NATIVE_DOUBLE), H5T_NATIVE_DOUBLE); - m_DefH5T_COMPLEX_LongDOUBLE = + m_DefH5TypeComplexLongDouble = H5Tcreate(H5T_COMPOUND, sizeof(std::complex<long double>)); - H5Tinsert(m_DefH5T_COMPLEX_LongDOUBLE, "ldouble real", 0, + H5Tinsert(m_DefH5TypeComplexLongDouble, "ldouble real", 0, H5T_NATIVE_LDOUBLE); - H5Tinsert(m_DefH5T_COMPLEX_LongDOUBLE, "ldouble img", + H5Tinsert(m_DefH5TypeComplexLongDouble, "ldouble img", H5Tget_size(H5T_NATIVE_LDOUBLE), H5T_NATIVE_LDOUBLE); } -void HDF5Common::H5_Init(const std::string name, MPI_Comm m_MPIComm, - bool toWrite) +void HDF5Common::Init(const std::string name, MPI_Comm comm, bool toWrite) { m_WriteMode = toWrite; // - m_Plist_id = H5Pcreate(H5P_FILE_ACCESS); + m_PropertyListId = H5Pcreate(H5P_FILE_ACCESS); #ifdef ADIOS2_HAVE_MPI - H5Pset_fapl_mpio(m_Plist_id, m_MPIComm, MPI_INFO_NULL); + H5Pset_fapl_mpio(m_PropertyListId, comm, MPI_INFO_NULL); #endif std::string ts0 = "/TimeStep0"; @@ -61,32 +61,35 @@ void HDF5Common::H5_Init(const std::string name, MPI_Comm m_MPIComm, /* * Create a new file collectively and release property list identifier. */ - m_File_id = - H5Fcreate(name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, m_Plist_id); - if (m_File_id >= 0) + m_FileId = H5Fcreate(name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, + m_PropertyListId); + if (m_FileId >= 0) { - m_Group_id = H5Gcreate2(m_File_id, ts0.c_str(), H5P_DEFAULT, - H5P_DEFAULT, H5P_DEFAULT); + m_GroupId = H5Gcreate2(m_FileId, ts0.c_str(), H5P_DEFAULT, + H5P_DEFAULT, H5P_DEFAULT); + if (m_GroupId < 0) + { + std::clog << "H5Common::Init: ERROR" << std::endl; + throw std::runtime_error("HDF5: Unable to create group " + ts0); + } } } else { // read a file collectively - m_File_id = H5Fopen(name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); - if (m_File_id >= 0) + m_FileId = H5Fopen(name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); + if (m_FileId >= 0) { - m_Group_id = H5Gopen(m_File_id, ts0.c_str(), H5P_DEFAULT); + m_GroupId = H5Gopen(m_FileId, ts0.c_str(), H5P_DEFAULT); } } - H5Pclose(m_Plist_id); + H5Pclose(m_PropertyListId); } -// HDF5Common::~HDF5Common() {} - void HDF5Common::WriteTimeSteps() { - if (m_File_id < 0) + if (m_FileId < 0) { // std::cerr<<"[ADIOS HDF5Error]: Invalid file to record timestep // to."<<std::endl; @@ -101,29 +104,29 @@ void HDF5Common::WriteTimeSteps() hid_t s = H5Screate(H5S_SCALAR); - hid_t attr = H5Acreate(m_File_id, "NumTimeSteps", H5T_NATIVE_UINT, s, + hid_t attr = H5Acreate(m_FileId, "NumTimeSteps", H5T_NATIVE_UINT, s, H5P_DEFAULT, H5P_DEFAULT); - uint totalts = m_CurrentTimeStep + 1; + uint totalTimeSteps = m_CurrentTimeStep + 1; - if (m_Group_id < 0) + if (m_GroupId < 0) { - totalts = m_CurrentTimeStep; + totalTimeSteps = m_CurrentTimeStep; } - H5Awrite(attr, H5T_NATIVE_UINT, &totalts); + H5Awrite(attr, H5T_NATIVE_UINT, &totalTimeSteps); H5Sclose(s); H5Aclose(attr); } -int HDF5Common::GetNumTimeSteps() +unsigned int HDF5Common::GetNumTimeSteps() { if (m_WriteMode) { return -1; } - if (m_File_id < 0) + if (m_FileId < 0) { std::cerr << "[ADIOS HDF5Error]: Invalid file to read timestep attribute." @@ -131,62 +134,70 @@ int HDF5Common::GetNumTimeSteps() return -1; } - if (m_Total_timestep <= 0) + if (m_NumTimeSteps <= 0) { - hid_t attr = H5Aopen(m_File_id, "NumTimeSteps", H5P_DEFAULT); + hid_t attr = H5Aopen(m_FileId, "NumTimeSteps", H5P_DEFAULT); - H5Aread(attr, H5T_NATIVE_UINT, &m_Total_timestep); + H5Aread(attr, H5T_NATIVE_UINT, &m_NumTimeSteps); H5Aclose(attr); } - return m_Total_timestep; + return m_NumTimeSteps; } -void HDF5Common::H5_Close() +void HDF5Common::Close() { - if (m_File_id < 0) + if (m_FileId < 0) { return; } WriteTimeSteps(); - if (m_Group_id >= 0) + if (m_GroupId >= 0) { - H5Gclose(m_Group_id); + H5Gclose(m_GroupId); } - H5Fclose(m_File_id); - m_File_id = -1; - m_Group_id = -1; + H5Fclose(m_FileId); + m_FileId = -1; + m_GroupId = -1; } -void HDF5Common::H5_Advance(int totalts) +void HDF5Common::Advance() { - m_CurrentTimeStep++; - if (m_CurrentTimeStep > 0) + if (m_GroupId >= 0) { - H5Gclose(m_Group_id); - m_Group_id = -1; + H5Gclose(m_GroupId); + m_GroupId = -1; } - std::string tsname = "/TimeStep"; - tsname.append(std::to_string(m_CurrentTimeStep)); - if (m_WriteMode) { - // m_Group_id = H5Gcreate2(m_File_id, tsname.c_str(), H5P_DEFAULT, + // m_GroupId = H5Gcreate2(m_FileId, tsname.c_str(), H5P_DEFAULT, // H5P_DEFAULT, H5P_DEFAULT); } else { - if ((totalts > 0) && (totalts <= m_CurrentTimeStep)) + if (m_NumTimeSteps == 0) + { + GetNumTimeSteps(); + } + if (m_CurrentTimeStep + 1 >= m_NumTimeSteps) { return; } - // std::cout<<" ... current group "<<tsname.c_str()<<std::endl; - m_Group_id = H5Gopen(m_File_id, tsname.c_str(), H5P_DEFAULT); + + std::string timeStepName = + "/TimeStep" + std::to_string(m_CurrentTimeStep + 1); + m_GroupId = H5Gopen(m_FileId, timeStepName.c_str(), H5P_DEFAULT); + if (m_GroupId < 0) + { + throw std::runtime_error("HDF5: Unable to open group " + + timeStepName); + } } + ++m_CurrentTimeStep; } void HDF5Common::CheckWriteGroup() @@ -195,15 +206,18 @@ void HDF5Common::CheckWriteGroup() { return; } - - if (m_Group_id >= 0) + if (m_GroupId >= 0) { return; } - std::string tsname = "/TimeStep"; - tsname.append(std::to_string(m_CurrentTimeStep)); - m_Group_id = H5Gcreate2(m_File_id, tsname.c_str(), H5P_DEFAULT, H5P_DEFAULT, - H5P_DEFAULT); + std::string timeStepName = "/TimeStep" + std::to_string(m_CurrentTimeStep); + m_GroupId = H5Gcreate2(m_FileId, timeStepName.c_str(), H5P_DEFAULT, + H5P_DEFAULT, H5P_DEFAULT); + if (m_GroupId < 0) + { + throw std::runtime_error("HDF5: Unable to create group " + + timeStepName); + } } } diff --git a/source/adios2/engine/hdf5/HDF5Common.h b/source/adios2/engine/hdf5/HDF5Common.h index 367bad64ffa0c6b18ecc3eede7618b75eb3749a8..d5a6a77190b1ed2465bf62b82073835b061afe27 100644 --- a/source/adios2/engine/hdf5/HDF5Common.h +++ b/source/adios2/engine/hdf5/HDF5Common.h @@ -11,7 +11,8 @@ #ifndef ADIOS2_ENGINE_HDF5_HDF5COMMON_P_H_ #define ADIOS2_ENGINE_HDF5_HDF5COMMON_P_H_ -#include "adios2.h" +#include <string> + #include "adios2/ADIOSMPICommOnly.h" #include <hdf5.h> @@ -28,29 +29,29 @@ public: */ HDF5Common(); - void H5_Init(const std::string name, MPI_Comm m_MPIComm, bool toWrite); - void H5_Close(); - void H5_Advance(int totalts); + void Init(const std::string name, MPI_Comm comm, bool toWrite); + void Close(); + void Advance(); - int GetNumTimeSteps(); + unsigned int GetNumTimeSteps(); void WriteTimeSteps(); - hid_t m_Plist_id, m_File_id; - hid_t m_Group_id; + hid_t m_PropertyListId, m_FileId; + hid_t m_GroupId; - hid_t m_DefH5T_COMPLEX_DOUBLE; - hid_t m_DefH5T_COMPLEX_FLOAT; - hid_t m_DefH5T_COMPLEX_LongDOUBLE; + hid_t m_DefH5TypeComplexDouble; + hid_t m_DefH5TypeComplexFloat; + hid_t m_DefH5TypeComplexLongDouble; - int m_CurrentTimeStep; + unsigned int m_CurrentTimeStep; void CheckWriteGroup(); private: bool m_WriteMode; - int m_Total_timestep; + unsigned int m_NumTimeSteps; }; } // end namespace adios -#endif /* HDF5_COMMON_P_H_ */ +#endif /* ADIOS2_ENGINE_HDF5_HDF5COMMON_P_H_ */ diff --git a/source/adios2/engine/hdf5/HDF5ReaderP.cpp b/source/adios2/engine/hdf5/HDF5ReaderP.cpp index 8f3cb8f4ddbfbb2716e77ffd54290f6027e4830a..7f3ead729cae47aaa634e6a0d2365a2294584a06 100644 --- a/source/adios2/engine/hdf5/HDF5ReaderP.cpp +++ b/source/adios2/engine/hdf5/HDF5ReaderP.cpp @@ -33,7 +33,7 @@ bool HDF5Reader::isValid() { return false; } - if (m_H5File.m_File_id >= 0) + if (m_H5File.m_FileId >= 0) { return true; } @@ -47,7 +47,7 @@ void HDF5Reader::Init() ", in call to ADIOS Open or HDF5Reader constructor\n"); } - m_H5File.H5_Init(m_Name, m_MPIComm, false); + m_H5File.Init(m_Name, m_MPIComm, false); m_H5File.GetNumTimeSteps(); } @@ -180,26 +180,26 @@ template <class T> void HDF5Reader::UseHDFRead(const std::string &variableName, T *values, hid_t h5Type) { - hid_t datasetID = - H5Dopen(m_H5File.m_Group_id, variableName.c_str(), H5P_DEFAULT); + hid_t dataSetId = + H5Dopen(m_H5File.m_GroupId, variableName.c_str(), H5P_DEFAULT); if (m_RankMPI == 0) { std::cout << " opened to read: " << variableName << std::endl; } - if (datasetID < 0) + if (dataSetId < 0) { return; } - hid_t filespace = H5Dget_space(datasetID); + hid_t fileSpace = H5Dget_space(dataSetId); - if (filespace < 0) + if (fileSpace < 0) { return; } - int ndims = H5Sget_simple_extent_ndims(filespace); + int ndims = H5Sget_simple_extent_ndims(fileSpace); hsize_t dims[ndims]; - herr_t status_n = H5Sget_simple_extent_dims(filespace, dims, NULL); + herr_t status_n = H5Sget_simple_extent_dims(fileSpace, dims, NULL); hsize_t start[ndims] = {0}, count[ndims] = {0}, stride[ndims] = {1}; @@ -220,14 +220,14 @@ void HDF5Reader::UseHDFRead(const std::string &variableName, T *values, << std::endl; } - hid_t ret = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, start, stride, + hid_t ret = H5Sselect_hyperslab(fileSpace, H5S_SELECT_SET, start, stride, count, NULL); if (ret < 0) { return; } - hid_t mem_dataspace = H5Screate_simple(ndims, count, NULL); + hid_t memDataSpace = H5Screate_simple(ndims, count, NULL); int elementsRead = 1; for (int i = 0; i < ndims; i++) @@ -236,7 +236,7 @@ void HDF5Reader::UseHDFRead(const std::string &variableName, T *values, } T data_array[elementsRead]; - ret = H5Dread(datasetID, h5Type, mem_dataspace, filespace, H5P_DEFAULT, + ret = H5Dread(dataSetId, h5Type, memDataSpace, fileSpace, H5P_DEFAULT, data_array); for (int i = 0; i < elementsRead; i++) @@ -245,18 +245,14 @@ void HDF5Reader::UseHDFRead(const std::string &variableName, T *values, << std::endl; } - H5Sclose(mem_dataspace); + H5Sclose(memDataSpace); - H5Sclose(filespace); - H5Dclose(datasetID); + H5Sclose(fileSpace); + H5Dclose(dataSetId); } -void HDF5Reader::Advance(float timeoutSec) -{ - int totalts = m_H5File.GetNumTimeSteps(); - m_H5File.H5_Advance(totalts); -} +void HDF5Reader::Advance(float timeoutSec) { m_H5File.Advance(); } -void HDF5Reader::Close(const int transportIndex) { m_H5File.H5_Close(); } +void HDF5Reader::Close(const int transportIndex) { m_H5File.Close(); } } // end namespace adios diff --git a/source/adios2/engine/hdf5/HDF5WriterP.cpp b/source/adios2/engine/hdf5/HDF5WriterP.cpp index 89e0ec54d1c3a61641d64afb190798a8efbacdaf..784f1aea4267c84f47d071637f93a936bd6d6a51 100644 --- a/source/adios2/engine/hdf5/HDF5WriterP.cpp +++ b/source/adios2/engine/hdf5/HDF5WriterP.cpp @@ -40,7 +40,7 @@ void HDF5Writer::Init() ", in call to ADIOS Open or HDF5Writer constructor\n"); } - m_H5File.H5_Init(m_Name, m_MPIComm, true); + m_H5File.Init(m_Name, m_MPIComm, true); } void HDF5Writer::Write(Variable<char> &variable, const char *values) @@ -118,19 +118,19 @@ void HDF5Writer::Write(Variable<long double> &variable, void HDF5Writer::Write(Variable<std::complex<float>> &variable, const std::complex<float> *values) { - UseHDFWrite(variable, values, m_H5File.m_DefH5T_COMPLEX_FLOAT); + UseHDFWrite(variable, values, m_H5File.m_DefH5TypeComplexFloat); } void HDF5Writer::Write(Variable<std::complex<double>> &variable, const std::complex<double> *values) { - UseHDFWrite(variable, values, m_H5File.m_DefH5T_COMPLEX_DOUBLE); + UseHDFWrite(variable, values, m_H5File.m_DefH5TypeComplexDouble); } void HDF5Writer::Write(Variable<std::complex<long double>> &variable, const std::complex<long double> *values) { - UseHDFWrite(variable, values, m_H5File.m_DefH5T_COMPLEX_LongDOUBLE); + UseHDFWrite(variable, values, m_H5File.m_DefH5TypeComplexLongDouble); } // String version @@ -222,30 +222,30 @@ void HDF5Writer::Write(const std::string variableName, const std::complex<float> *values) { UseHDFWrite(m_ADIOS.GetVariable<std::complex<float>>(variableName), values, - m_H5File.m_DefH5T_COMPLEX_FLOAT); + m_H5File.m_DefH5TypeComplexFloat); } void HDF5Writer::Write(const std::string variableName, const std::complex<double> *values) { UseHDFWrite(m_ADIOS.GetVariable<std::complex<double>>(variableName), values, - m_H5File.m_DefH5T_COMPLEX_DOUBLE); + m_H5File.m_DefH5TypeComplexDouble); } void HDF5Writer::Write(const std::string variableName, const std::complex<long double> *values) { UseHDFWrite(m_ADIOS.GetVariable<std::complex<long double>>(variableName), - values, m_H5File.m_DefH5T_COMPLEX_LongDOUBLE); + values, m_H5File.m_DefH5TypeComplexLongDouble); } -void HDF5Writer::Advance(float timeoutSec) { m_H5File.H5_Advance(0); } +void HDF5Writer::Advance(float timeoutSec) { m_H5File.Advance(); } -void HDF5Writer::Close(const int transportIndex) { m_H5File.H5_Close(); } +void HDF5Writer::Close(const int transportIndex) { m_H5File.Close(); } template <class T> void HDF5Writer::UseHDFWrite(Variable<T> &variable, const T *values, - hid_t h5type) + hid_t h5Type) { m_H5File.CheckWriteGroup(); // here comes your magic at Writing now variable.m_UserValues has the data @@ -292,7 +292,7 @@ void HDF5Writer::UseHDFWrite(Variable<T> &variable, const T *values, hid_t fileSpace = H5Screate_simple(dimSize, dimsf.data(), NULL); hid_t dsetID = - H5Dcreate(m_H5File.m_Group_id, variable.m_Name.c_str(), h5type, + H5Dcreate(m_H5File.m_GroupId, variable.m_Name.c_str(), h5Type, fileSpace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); // H5Sclose(fileSpace); @@ -311,7 +311,7 @@ void HDF5Writer::UseHDFWrite(Variable<T> &variable, const T *values, #endif herr_t status; - status = H5Dwrite(dsetID, h5type, memSpace, fileSpace, plistID, values); + status = H5Dwrite(dsetID, h5Type, memSpace, fileSpace, plistID, values); if (status < 0) { diff --git a/source/adios2/engine/hdf5/HDF5WriterP.h b/source/adios2/engine/hdf5/HDF5WriterP.h index f915e9ccbe5978703a5b07e62b74086fabe3db4c..21151a18ab648cab1b8d389436a13dfb3518c3ef 100644 --- a/source/adios2/engine/hdf5/HDF5WriterP.h +++ b/source/adios2/engine/hdf5/HDF5WriterP.h @@ -98,7 +98,7 @@ private: HDF5Common m_H5File; template <class T> - void UseHDFWrite(Variable<T> &variable, const T *values, hid_t h5type); + void UseHDFWrite(Variable<T> &variable, const T *values, hid_t h5Type); }; } // end namespace adios