Commit 98d987d2 authored by guj's avatar guj
Browse files

added metadata info

parent 27ceea06
Loading
Loading
Loading
Loading
+54 −21
Original line number Diff line number Diff line
@@ -27,6 +27,21 @@ namespace interop

const std::string HDF5Common::ATTRNAME_NUM_STEPS = "NumSteps";
const std::string HDF5Common::ATTRNAME_GIVEN_ADIOSNAME = "ADIOSName";
const std::string HDF5Common::PREFIX_BLOCKINFO = "ADIOS_BLOCKINFO_";
const std::string HDF5Common::PREFIX_STAT = "ADIOS_STAT_";

/*
   //need to know ndim before defining this.
   //inconvenient
class HDF5BlockStat
{
public:
  size_t m_offset[ndim];
  size_t m_Count[ndim];
  double m_Min;
  double m_Max;
};
*/

HDF5Common::HDF5Common(const bool debugMode) : m_DebugMode(debugMode)
{
@@ -41,6 +56,11 @@ HDF5Common::HDF5Common(const bool debugMode) : m_DebugMode(debugMode)
    H5Tinsert(m_DefH5TypeComplexDouble, "dreal", 0, H5T_NATIVE_DOUBLE);
    H5Tinsert(m_DefH5TypeComplexDouble, "dimg", H5Tget_size(H5T_NATIVE_DOUBLE),
              H5T_NATIVE_DOUBLE);

    m_PropertyTxfID = H5Pcreate(H5P_DATASET_XFER);
#ifdef ADIOS2_HAVE_MPI
    H5Pset_dxpl_mpio(m_PropertyTxfID, H5FD_MPIO_COLLECTIVE);
#endif
}

void HDF5Common::Init(const std::string &name, MPI_Comm comm, bool toWrite)
@@ -50,6 +70,8 @@ void HDF5Common::Init(const std::string &name, MPI_Comm comm, bool toWrite)

#ifdef ADIOS2_HAVE_MPI
    H5Pset_fapl_mpio(m_PropertyListId, comm, MPI_INFO_NULL);
    MPI_Comm_rank(comm, &m_CommRank);
    MPI_Comm_size(comm, &m_CommSize);
#endif

    // std::string ts0 = "/AdiosStep0";
@@ -211,13 +233,16 @@ void HDF5Common::FindVarsFromH5(core::IO &io, hid_t top_id, const char *gname,
                int currType = H5Gget_objtype_by_idx(gid, k);
                if ((currType == H5G_DATASET) || (currType == H5G_TYPE))
                {
                    // std::cout<<" ... handling native: "<<name<<" from
                    // :"<<heritage<<"/"<<gname<<std::endl;
                    std::string nameStr = name;
                    if (!(0 == nameStr.find(PREFIX_BLOCKINFO)) &&
                        !(0 == nameStr.find(PREFIX_STAT)))
                    {
                        hid_t datasetId = H5Dopen(gid, name, H5P_DEFAULT);
                        HDF5TypeGuard d(datasetId, E_H5_DATASET);

                    char longName[std::strlen(heritage) + std::strlen(gname) +
                                  std::strlen(name) + 10];
                        char longName[std::strlen(heritage) +
                                      std::strlen(gname) + std::strlen(name) +
                                      10];

                        if (strcmp(gname, "/") == 0)
                        {
@@ -225,12 +250,14 @@ void HDF5Common::FindVarsFromH5(core::IO &io, hid_t top_id, const char *gname,
                        }
                        else
                        {
                        sprintf(longName, "%s/%s/%s", heritage, gname, name);
                            sprintf(longName, "%s/%s/%s", heritage, gname,
                                    name);
                        }
                        // CreateVar(io, datasetId, name);
                        ReadNativeAttrToIO(io, datasetId, longName);
                        CreateVar(io, datasetId, longName, ts);
                    }
                }
                else if (currType == H5G_GROUP)
                {
                    std::string heritageNext = heritage;
@@ -306,6 +333,10 @@ void HDF5Common::ReadVariables(unsigned int ts, core::IO &io)
                    FindVarsFromH5(io, gid, name, "", ts);
                }
                else if ((currType == H5G_DATASET) || (currType == H5G_TYPE))
                {
                    std::string nameStr = name;
                    if (!(0 == nameStr.find(PREFIX_BLOCKINFO)) &&
                        !(0 == nameStr.find(PREFIX_STAT)))
                    {
                        hid_t datasetId = H5Dopen(gid, name, H5P_DEFAULT);

@@ -316,6 +347,7 @@ void HDF5Common::ReadVariables(unsigned int ts, core::IO &io)
                }
            }
        }
    }
    /// H5Gclose(gid);
    ///}
}
@@ -505,6 +537,7 @@ void HDF5Common::Close()
        H5Gclose(m_GroupId);
    }

    H5Pclose(m_PropertyTxfID);
    H5Fclose(m_FileId);
    m_FileId = -1;
    m_GroupId = -1;
+13 −1
Original line number Diff line number Diff line
@@ -112,6 +112,8 @@ public:

    static const std::string ATTRNAME_NUM_STEPS;
    static const std::string ATTRNAME_GIVEN_ADIOSNAME;
    static const std::string PREFIX_BLOCKINFO;
    static const std::string PREFIX_STAT;

    void Init(const std::string &name, MPI_Comm comm, bool toWrite);

@@ -164,11 +166,13 @@ public:
    static void StaticGetAdiosStepString(std::string &adiosStepName, int ts);

    hid_t m_PropertyListId = -1;
    hid_t m_PropertyTxfID = -1;
    hid_t m_FileId = -1;
    hid_t m_GroupId = -1;

    hid_t m_DefH5TypeComplexDouble;
    hid_t m_DefH5TypeComplexFloat;
    hid_t m_DefH5TypeBlockStat;

    unsigned int m_CurrentAdiosStep = 0;

@@ -197,13 +201,21 @@ private:
    const bool m_DebugMode;
    bool m_WriteMode = false;
    unsigned int m_NumAdiosSteps = 0;

    int m_CommRank = 0;
    int m_CommSize = 1;

    template <class T>
    void AddBlockInfo(const core::Variable<T> &varaible, hid_t parentId);
    template <class T>
    void AddStats(const core::Variable<T> &variable, hid_t parentId,
                  std::vector<T> &stats);
};

// Explicit declaration of the public template methods
#define declare_template_instantiation(T)                                      \
    extern template void HDF5Common::Write(core::Variable<T> &variable,        \
                                           const T *value);

ADIOS2_FOREACH_TYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation

+106 −17
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
#include <iostream>
#include <vector>

#include "adios2/helper/adiosFunctions.h"

namespace adios2
{
namespace interop
@@ -46,23 +48,23 @@ void HDF5Common::Write(core::Variable<T> &variable, const T *values)
            H5Dcreate(m_GroupId, variable.m_Name.c_str(), h5Type, filespaceID,
                      H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
                      */

        /*
                hid_t plistID = H5Pcreate(H5P_DATASET_XFER);
        #ifdef ADIOS2_HAVE_MPI
                H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE);
        #endif

        */
        herr_t status;
        if (std::is_same<T, std::string>::value)
        {
            status = H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, plistID,
            status = H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, m_PropertyTxfID,
                              ((std::string *)values)->data());
            H5Tclose(h5Type);
        }
        else
        {
            status =
                H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, plistID, values);
            status = H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL, m_PropertyTxfID,
                              values);
        }
        H5Sclose(filespaceID);
        //	CloseDataset(dsetID);
@@ -121,16 +123,18 @@ void HDF5Common::Write(core::Variable<T> &variable, const T *values)
    H5Sselect_hyperslab(fileSpace, H5S_SELECT_SET, offset.data(), NULL,
                        count.data(), NULL);

    /*
        //  Create property list for collective dataset write.

        hid_t plistID = H5Pcreate(H5P_DATASET_XFER);
    #ifdef ADIOS2_HAVE_MPI
        H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE);
    #endif
    */
    herr_t status;

    status = H5Dwrite(dsetID, h5Type, memSpace, fileSpace, plistID, values);

    status =
        H5Dwrite(dsetID, h5Type, memSpace, fileSpace, m_PropertyTxfID, values);
    if (status < 0)
    {
        if (m_DebugMode)
@@ -140,12 +144,97 @@ void HDF5Common::Write(core::Variable<T> &variable, const T *values)
        }
    }

    size_t valuesSize = adios2::helper::GetTotalSize(variable.m_Count);
    T min, max;
    adios2::helper::GetMinMaxThreads(values, valuesSize, min, max, 1);

    int chainSize = chain.size();
    hid_t parentId = m_GroupId;
    if (chainSize > 1)
    {
        parentId = chain[chainSize - 2];
    }

    AddBlockInfo(variable, parentId);

    std::vector<T> stats = {min, max};
    AddStats(variable, parentId, stats);

    //    CloseDataset(dsetID);
    H5Sclose(fileSpace);
    H5Sclose(memSpace);
    H5Pclose(plistID);
    //    H5Pclose(plistID);
}

template <class T>
void HDF5Common::AddStats(const core::Variable<T> &variable, hid_t parentId,
                          std::vector<T> &stats)
{

    hid_t h5Type = GetHDF5Type<T>();

    std::string statInfo_name = PREFIX_STAT + variable.m_Name;
    hsize_t numStat = stats.size(); // min, max etc
    hsize_t statDim[2] = {(hsize_t)m_CommSize, numStat};
    hid_t statSpace_id = H5Screate_simple(numStat, statDim, NULL);
    hid_t statId =
        H5Dcreate(parentId, statInfo_name.c_str(), h5Type, statSpace_id,
                  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
    hsize_t statLocalDim[1] = {numStat};
    hid_t statLocal_id = H5Screate_simple(1, statLocalDim, NULL);

    hsize_t statOffset[2] = {(hsize_t)m_CommRank, 0};
    hsize_t statCount[2] = {1, numStat};
    H5Sselect_hyperslab(statSpace_id, H5S_SELECT_SET, statOffset, NULL,
                        statCount, NULL);

    H5Dwrite(statId, h5Type, statLocal_id, statSpace_id, m_PropertyTxfID,
             stats.data());

    H5Sclose(statLocal_id);
    H5Sclose(statSpace_id);
    H5Dclose(statId);
    //     H5Pclose(plistID);
}

template <class T>
void HDF5Common::AddBlockInfo(const core::Variable<T> &variable, hid_t parentId)
{
    int dimSize = std::max(variable.m_Shape.size(), variable.m_Count.size());
    hsize_t metaDim[2];
    metaDim[1] = dimSize * 2;
    metaDim[0] = m_CommSize;
    hid_t metaSpace_id = H5Screate_simple(2, metaDim, NULL);
    std::string blockInfo_name = PREFIX_BLOCKINFO + variable.m_Name;
    hid_t metaId =
        H5Dcreate(parentId, blockInfo_name.c_str(), H5T_NATIVE_HSIZE,
                  metaSpace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

    size_t blocks[dimSize * 2];
    for (int i = 0; i < dimSize; i++)
    {
        blocks[i + dimSize] = variable.m_Count[i];
        blocks[i] = variable.m_Start[i];
    }
    hsize_t blockDim[1] = {(hsize_t)(dimSize * 2)};
    hid_t metaLocal_id = H5Screate_simple(1, blockDim, NULL);

    hsize_t metaOffset[2] = {(hsize_t)m_CommRank, 0};
    hsize_t metaCount[2] = {1, (hsize_t)(dimSize * 2)};
    H5Sselect_hyperslab(metaSpace_id, H5S_SELECT_SET, metaOffset, NULL,
                        metaCount, NULL);

    H5Dwrite(metaId, H5T_NATIVE_HSIZE, metaLocal_id, metaSpace_id,
             m_PropertyTxfID, blocks);

    H5Sclose(metaLocal_id);
    H5Sclose(metaSpace_id);
    H5Dclose(metaId);

    //      H5Pclose(plistID);
}
//
//
template <>
hid_t HDF5Common::GetHDF5Type<std::string>()
{