diff --git a/examples/heatTransfer/write/CMakeLists.txt b/examples/heatTransfer/write/CMakeLists.txt index 7144a8e7720987406c27e6bbf61c2e31f7a0b4a7..5034755f77f8c54ea8e46f98c650624b17b0b01b 100644 --- a/examples/heatTransfer/write/CMakeLists.txt +++ b/examples/heatTransfer/write/CMakeLists.txt @@ -53,6 +53,26 @@ if(ADIOS_USE_MPI) ) endif() + + if(ADIOS_USE_HDF5) + find_package(HDF5 REQUIRED) + find_package(MPI COMPONENTS C REQUIRED) + + add_executable(heatTransfer_write_ph5 + main.cpp + HeatTransfer.cpp + Settings.cpp + IO_ph5.cpp + ) + target_include_directories(heatTransfer_write_ph5 + PRIVATE ${MPI_C_INCLUDE_PATH} ${HDF5_C_INCLUDE_DIRS} + ) + target_link_libraries(heatTransfer_write_ph5 + ${MPI_C_LIBRARIES} ${HDF5_C_LIBRARIES} + ) + endif() + + if(ADIOS_USE_HDF5) find_package(MPI COMPONENTS C REQUIRED) diff --git a/examples/heatTransfer/write/IO_ph5.cpp b/examples/heatTransfer/write/IO_ph5.cpp index bc0faa3d6977ee3f227d39ed995a5a37634f6c24..4d892157771ae2a4adecf99654c393c7f19355c6 100644 --- a/examples/heatTransfer/write/IO_ph5.cpp +++ b/examples/heatTransfer/write/IO_ph5.cpp @@ -21,179 +21,175 @@ class HDF5NativeWriter { public: - HDF5NativeWriter(const std::string& fileName); - ~HDF5NativeWriter(); + HDF5NativeWriter(const std::string &fileName); + ~HDF5NativeWriter(); - bool Advance(); - void Close(); - void CheckWriteGroup(); + bool Advance(); + void Close(); + void CheckWriteGroup(); - void WriteScalar(const std::string& varName, const void *data, hid_t h5Type); - void WriteSimple(const std::string& varName, int dimSize, const void *data, hid_t h5Type, const hsize_t* shape, const hsize_t* offset, const hsize_t* count); + void WriteScalar(const std::string &varName, const void *data, + hid_t h5Type); + void WriteSimple(const std::string &varName, int dimSize, const void *data, + hid_t h5Type, const hsize_t *shape, const hsize_t *offset, + const hsize_t *count); - int m_CurrentTimeStep; - unsigned int m_TotalTimeSteps; + int m_CurrentTimeStep; + unsigned int m_TotalTimeSteps; private: - hid_t m_FilePropertyListId; - hid_t m_FileId; - hid_t m_GroupId; + hid_t m_FilePropertyListId; + hid_t m_FileId; + hid_t m_GroupId; }; -HDF5NativeWriter::HDF5NativeWriter(const std::string& fileName) - : m_CurrentTimeStep(0), m_TotalTimeSteps(0) +HDF5NativeWriter::HDF5NativeWriter(const std::string &fileName) +: m_CurrentTimeStep(0), m_TotalTimeSteps(0) { - m_FilePropertyListId = H5Pcreate(H5P_FILE_ACCESS); + m_FilePropertyListId = H5Pcreate(H5P_FILE_ACCESS); #ifdef ADIOS2_HAVE_MPI - // read a file collectively - H5Pset_fapl_mpio(m_FilePropertyListId, MPI_COMM_WORLD, MPI_INFO_NULL); + // read a file collectively + H5Pset_fapl_mpio(m_FilePropertyListId, MPI_COMM_WORLD, MPI_INFO_NULL); #endif - m_FileId = H5Fcreate(fileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, - m_FilePropertyListId); + m_FileId = H5Fcreate(fileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, + m_FilePropertyListId); - if (m_FileId < 0) + if (m_FileId < 0) { - throw std::runtime_error("Unable to open " + fileName + " for reading"); + throw std::runtime_error("Unable to open " + fileName + " for reading"); } - std::string ts0 = "/TimeStep0"; + std::string ts0 = "/TimeStep0"; - m_GroupId = H5Gcreate2(m_FileId, ts0.c_str(), H5P_DEFAULT, - H5P_DEFAULT, H5P_DEFAULT); - if (m_GroupId < 0) + m_GroupId = H5Gcreate2(m_FileId, ts0.c_str(), H5P_DEFAULT, H5P_DEFAULT, + H5P_DEFAULT); + if (m_GroupId < 0) { - throw std::runtime_error("HDF5: Unable to create group " + ts0); + throw std::runtime_error("HDF5: Unable to create group " + ts0); } } -HDF5NativeWriter::~HDF5NativeWriter() -{ - Close(); -} +HDF5NativeWriter::~HDF5NativeWriter() { Close(); } void HDF5NativeWriter::Close() { - if (m_FileId < 0) - return; + if (m_FileId < 0) + return; - hid_t s = H5Screate(H5S_SCALAR); - hid_t attr = H5Acreate(m_FileId, "NumTimeSteps", H5T_NATIVE_UINT, s, - H5P_DEFAULT, H5P_DEFAULT); - uint totalTimeSteps = m_CurrentTimeStep + 1; + hid_t s = H5Screate(H5S_SCALAR); + hid_t attr = H5Acreate(m_FileId, "NumTimeSteps", H5T_NATIVE_UINT, s, + H5P_DEFAULT, H5P_DEFAULT); + uint totalTimeSteps = m_CurrentTimeStep + 1; - if (m_GroupId < 0) + if (m_GroupId < 0) { - totalTimeSteps = m_CurrentTimeStep; - } - H5Awrite(attr, H5T_NATIVE_UINT, &totalTimeSteps); - H5Sclose(s); - H5Aclose(attr); - + totalTimeSteps = m_CurrentTimeStep; + } + H5Awrite(attr, H5T_NATIVE_UINT, &totalTimeSteps); + H5Sclose(s); + H5Aclose(attr); - if (m_GroupId >= 0) + if (m_GroupId >= 0) { - H5Gclose(m_GroupId); - m_GroupId = -1; - } + H5Gclose(m_GroupId); + m_GroupId = -1; + } - H5Fclose(m_FileId); - m_FileId = -1; - H5Pclose(m_FilePropertyListId); + H5Fclose(m_FileId); + m_FileId = -1; + H5Pclose(m_FilePropertyListId); } bool HDF5NativeWriter::Advance() { - if (m_GroupId >= 0) + if (m_GroupId >= 0) { - H5Gclose(m_GroupId); - m_GroupId = -1; + H5Gclose(m_GroupId); + m_GroupId = -1; } - ++m_CurrentTimeStep; + ++m_CurrentTimeStep; - return true; + return true; } - void HDF5NativeWriter::CheckWriteGroup() { - if (m_GroupId >= 0) + if (m_GroupId >= 0) { - return; + return; } - 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) + 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); + throw std::runtime_error("HDF5: Unable to create group " + + timeStepName); } } +void HDF5NativeWriter::WriteScalar(const std::string &varName, const void *data, + hid_t h5Type) +{ + CheckWriteGroup(); + // scalar + hid_t filespaceID = H5Screate(H5S_SCALAR); + hid_t dsetID = H5Dcreate(m_GroupId, varName.c_str(), h5Type, filespaceID, + H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + herr_t status = + H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); + + H5Sclose(filespaceID); + H5Dclose(dsetID); +} -void HDF5NativeWriter::WriteScalar(const std::string& varName, const void *data, hid_t h5Type) +void HDF5NativeWriter::WriteSimple(const std::string &varName, int dimSize, + const void *data, hid_t h5Type, + const hsize_t *shape, const hsize_t *offset, + const hsize_t *count) { - CheckWriteGroup(); - // scalar - hid_t filespaceID = H5Screate(H5S_SCALAR); - hid_t dsetID = - H5Dcreate(m_GroupId, varName.c_str(), h5Type, - filespaceID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - herr_t status = - H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); - - H5Sclose(filespaceID); - H5Dclose(dsetID); + CheckWriteGroup(); + hid_t fileSpace = H5Screate_simple(dimSize, shape, NULL); -} + hid_t dsetID = H5Dcreate(m_GroupId, varName.c_str(), h5Type, fileSpace, + H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + + hid_t memSpace = H5Screate_simple(dimSize, count, NULL); + + // Select hyperslab + fileSpace = H5Dget_space(dsetID); + H5Sselect_hyperslab(fileSpace, H5S_SELECT_SET, offset, NULL, count, NULL); + + // Create property list for collective dataset write. -void HDF5NativeWriter::WriteSimple(const std::string& varName, int dimSize, const void *data, hid_t h5Type, const hsize_t* shape, const hsize_t* offset, const hsize_t* count) -{ - CheckWriteGroup(); - hid_t fileSpace = H5Screate_simple(dimSize, shape, NULL); - - hid_t dsetID = - H5Dcreate(m_GroupId, varName.c_str(), h5Type, - fileSpace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - - hid_t memSpace = H5Screate_simple(dimSize, count, NULL); - - // Select hyperslab - fileSpace = H5Dget_space(dsetID); - H5Sselect_hyperslab(fileSpace, H5S_SELECT_SET, offset, NULL, - count, NULL); - - // Create property list for collective dataset write. - - hid_t plistID = H5Pcreate(H5P_DATASET_XFER); + hid_t plistID = H5Pcreate(H5P_DATASET_XFER); #ifdef ADIOS2_HAVE_MPI - H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE); + H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE); #endif - herr_t status; - - status = H5Dwrite(dsetID, h5Type, memSpace, fileSpace, plistID, data); - - if (status < 0) + herr_t status; + + status = H5Dwrite(dsetID, h5Type, memSpace, fileSpace, plistID, data); + + if (status < 0) { - // error - std::cerr << " Write failed. " << std::endl; + // error + std::cerr << " Write failed. " << std::endl; } - - H5Dclose(dsetID); - H5Sclose(fileSpace); - H5Sclose(memSpace); - H5Pclose(plistID); + + H5Dclose(dsetID); + H5Sclose(fileSpace); + H5Sclose(memSpace); + H5Pclose(plistID); } // // std::shared_ptr<HDF5NativeWriter> h5writer; -//HDF5NativeWriter* h5writer; - +// HDF5NativeWriter* h5writer; IO::IO(const Settings &s, MPI_Comm comm) { @@ -208,17 +204,18 @@ IO::IO(const Settings &s, MPI_Comm comm) IO::~IO() { h5writer->Close(); - //delete h5writer; + // delete h5writer; } void IO::write(int step, const HeatTransfer &ht, const Settings &s, MPI_Comm comm) { - std::vector<hsize_t> dims = {s.gndx, s.gndy}; + std::vector<hsize_t> dims = {s.gndx, s.gndy}; std::vector<hsize_t> offset = {s.offsx, s.offsy}; - std::vector<hsize_t> count = {s.ndx, s.ndy}; + std::vector<hsize_t> count = {s.ndx, s.ndy}; - h5writer->WriteSimple("T", 2, ht.data_noghost().data(), H5T_NATIVE_DOUBLE, dims.data(), offset.data(), count.data()); + h5writer->WriteSimple("T", 2, ht.data_noghost().data(), H5T_NATIVE_DOUBLE, + dims.data(), offset.data(), count.data()); h5writer->WriteScalar("gndy", &(s.gndy), H5T_NATIVE_UINT); h5writer->Advance(); diff --git a/examples/heatTransfer/write/IO_ph5_adios2.cpp b/examples/heatTransfer/write/IO_ph5_adios2.cpp index abed29d13712e1da144744f01879724122f4d8c5..e694d9f291698cbb51f4bc8b0e304e32d6d90e1f 100644 --- a/examples/heatTransfer/write/IO_ph5_adios2.cpp +++ b/examples/heatTransfer/write/IO_ph5_adios2.cpp @@ -106,8 +106,8 @@ void IO::write(int step, const HeatTransfer &ht, const Settings &s, h5writer->Write<double>(*varT, ht.data_noghost().data()); // h5writer->Write(*varT, ht.data_noghost().data()); - h5writer->Write<unsigned int>(*varGndx, s.gndx); - h5writer->Write<unsigned int>("gndy", s.gndy); + h5writer->Write<unsigned int>(*varGndx, &(s.gndx)); + h5writer->Write("gndy", &(s.gndy)); h5writer->Advance(); diff --git a/source/adios2/engine/hdf5/HDF5WriterP.cpp b/source/adios2/engine/hdf5/HDF5WriterP.cpp index acd523feca71fa7e9c59e006178b7efdf73a05da..db9d6c4e6e0e1a7d31e54aa5055eb2a325b9deb3 100644 --- a/source/adios2/engine/hdf5/HDF5WriterP.cpp +++ b/source/adios2/engine/hdf5/HDF5WriterP.cpp @@ -134,105 +134,105 @@ void HDF5Writer::Write(Variable<std::complex<long double>> &variable, } // String version -void HDF5Writer::Write(const std::string variableName, const char *values) +void HDF5Writer::Write(const std::string &variableName, const char *values) { UseHDFWrite(m_ADIOS.GetVariable<char>(variableName), values, H5T_NATIVE_CHAR); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const unsigned char *values) { UseHDFWrite(m_ADIOS.GetVariable<unsigned char>(variableName), values, H5T_NATIVE_UCHAR); } -void HDF5Writer::Write(const std::string variableName, const short *values) +void HDF5Writer::Write(const std::string &variableName, const short *values) { UseHDFWrite(m_ADIOS.GetVariable<short>(variableName), values, H5T_NATIVE_SHORT); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const unsigned short *values) { UseHDFWrite(m_ADIOS.GetVariable<unsigned short>(variableName), values, H5T_NATIVE_USHORT); } -void HDF5Writer::Write(const std::string variableName, const int *values) +void HDF5Writer::Write(const std::string &variableName, const int *values) { UseHDFWrite(m_ADIOS.GetVariable<int>(variableName), values, H5T_NATIVE_INT); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const unsigned int *values) { UseHDFWrite(m_ADIOS.GetVariable<unsigned int>(variableName), values, H5T_NATIVE_UINT); } -void HDF5Writer::Write(const std::string variableName, const long int *values) +void HDF5Writer::Write(const std::string &variableName, const long int *values) { UseHDFWrite(m_ADIOS.GetVariable<long int>(variableName), values, H5T_NATIVE_LONG); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const unsigned long int *values) { UseHDFWrite(m_ADIOS.GetVariable<unsigned long int>(variableName), values, H5T_NATIVE_ULONG); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const long long int *values) { UseHDFWrite(m_ADIOS.GetVariable<long long int>(variableName), values, H5T_NATIVE_LLONG); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const unsigned long long int *values) { UseHDFWrite(m_ADIOS.GetVariable<unsigned long long int>(variableName), values, H5T_NATIVE_ULLONG); } -void HDF5Writer::Write(const std::string variableName, const float *values) +void HDF5Writer::Write(const std::string &variableName, const float *values) { UseHDFWrite(m_ADIOS.GetVariable<float>(variableName), values, H5T_NATIVE_FLOAT); } -void HDF5Writer::Write(const std::string variableName, const double *values) +void HDF5Writer::Write(const std::string &variableName, const double *values) { UseHDFWrite(m_ADIOS.GetVariable<double>(variableName), values, H5T_NATIVE_DOUBLE); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const long double *values) { UseHDFWrite(m_ADIOS.GetVariable<long double>(variableName), values, H5T_NATIVE_LDOUBLE); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const std::complex<float> *values) { UseHDFWrite(m_ADIOS.GetVariable<std::complex<float>>(variableName), values, m_H5File.m_DefH5TypeComplexFloat); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const std::complex<double> *values) { UseHDFWrite(m_ADIOS.GetVariable<std::complex<double>>(variableName), values, m_H5File.m_DefH5TypeComplexDouble); } -void HDF5Writer::Write(const std::string variableName, +void HDF5Writer::Write(const std::string &variableName, const std::complex<long double> *values) { UseHDFWrite(m_ADIOS.GetVariable<std::complex<long double>>(variableName), diff --git a/source/adios2/engine/hdf5/HDF5WriterP.h b/source/adios2/engine/hdf5/HDF5WriterP.h index 3ab1b970ec75231a04537a74d9ad97967af8dd75..22a1b8f57ef079c04f87680c57bb52fa340f74a0 100644 --- a/source/adios2/engine/hdf5/HDF5WriterP.h +++ b/source/adios2/engine/hdf5/HDF5WriterP.h @@ -64,25 +64,26 @@ public: void Write(Variable<std::complex<long double>> &variable, const std::complex<long double> *values); - void Write(const std::string variableName, const char *values); - void Write(const std::string variableName, const unsigned char *values); - void Write(const std::string variableName, const short *values); - void Write(const std::string variableName, const unsigned short *values); - void Write(const std::string variableName, const int *values); - void Write(const std::string variableName, const unsigned int *values); - void Write(const std::string variableName, const long int *values); - void Write(const std::string variableName, const unsigned long int *values); - void Write(const std::string variableName, const long long int *values); - void Write(const std::string variableName, + void Write(const std::string &variableName, const char *values); + void Write(const std::string &variableName, const unsigned char *values); + void Write(const std::string &variableName, const short *values); + void Write(const std::string &variableName, const unsigned short *values); + void Write(const std::string &variableName, const int *values); + void Write(const std::string &variableName, const unsigned int *values); + void Write(const std::string &variableName, const long int *values); + void Write(const std::string &variableName, + const unsigned long int *values); + void Write(const std::string &variableName, const long long int *values); + void Write(const std::string &variableName, const unsigned long long int *values); - void Write(const std::string variableName, const float *values); - void Write(const std::string variableName, const double *values); - void Write(const std::string variableName, const long double *values); - void Write(const std::string variableName, + void Write(const std::string &variableName, const float *values); + void Write(const std::string &variableName, const double *values); + void Write(const std::string &variableName, const long double *values); + void Write(const std::string &variableName, const std::complex<float> *values); - void Write(const std::string variableName, + void Write(const std::string &variableName, const std::complex<double> *values); - void Write(const std::string variableName, + void Write(const std::string &variableName, const std::complex<long double> *values); void Advance(float timeoutSec = 0.0);