vendredi 2 juin 2017

Writting from std::vector

I want use hdf5 in my C++ application, I use H5Cpp library.

I have a structure which contains doubles.

I am trying to write a vector of pointer (void*) of my structure in a HDF5 file.

When I try with a small size of elements in my vector, execution finish but I have several zero (values arround 10-316) in my file and sometimes true values expected but not in the right position.

result file HDF5 example

If I try to write bigger than 16381 elements in my vector, I have a segmentation fault.

Below my test code.

#include <vector>
#include <string>
#include <iostream>
#include <H5Cpp.h>

using namespace H5;
using namespace std;


const hsize_t CHUNK_DIM[2] = {1, 365};
const int COMPRESS_RATE = 6;

// definition structure of data
struct s1_t {
    double a;
    double b;
};

void write_hdf5(std::vector<void *> & data)
{
    // open file
    H5File *file = new H5File("test.h5", H5F_ACC_TRUNC);

    // Data column names
    vector<string> names;
    names.push_back("a");
    names.push_back("b");

    // nbr_samples
    hsize_t nbr_samples = data.size();

    // set dim of data in hdf5 file
    hsize_t dim[] = {nbr_samples};

    // set space for data in hdf5 file
    hsize_t maxdim[] = {H5S_UNLIMITED, H5S_UNLIMITED};
    int rank = 1;
    DataSpace space(rank, dim, maxdim);

    // define comptype
    CompType mtype1(sizeof(s1_t));
    int count = 0;
    for (auto a = names.cbegin(); a < names.cend(); ++a)
        {
            mtype1.insertMember(*a, sizeof(double)*count, PredType::NATIVE_DOUBLE);
            count++;
        }

    // Set chunked data
    DSetCreatPropList creatplist;
    creatplist.setChunk(1, CHUNK_DIM);
    creatplist.setDeflate(COMPRESS_RATE);

    // Write datas
    DataSet *dataset2;
    dataset2 = new DataSet(file->createDataSet("table_test_vector", mtype1, space, creatplist));
    dataset2->write(data.data(), mtype1);

    // Free memory and close file
    delete dataset2;
    delete file;
}

int main(void)
{
    try
        {
            // nbr_samples
            int nbr_samples = 20;

            // create data vector
            std::vector<void *> s2(nbr_samples);
            for (int i = 0; i< nbr_samples; i++)
                {
                    s1_t * s = new s1_t;
                    s->a=i;
                    s->b=2*i;
                    s2[i]=static_cast<void*>(s);
                }

            // write data in hdf5 file
            write_hdf5(s2);
        }
    catch(std::exception & e)
        {
            std::cout << e.what() << std::endl;
        }
}

Is it possible in HDF5 to write a vector of pointer i.e. std::vector? I have try several ways but I can't find a solution.

Here is a cmakefile

cmake_minimum_required(VERSION 2.8.7)
find_package(HDF5 COMPONENTS CXX)

set(CMAKE_CXX_FLAGS "-std=c++11")

include_directories(${HDF5_INCLUDE_DIRS})
add_executable(main3 main3.cpp)
target_link_libraries(main3 ${HDF5_LIBRARIES})
target_link_libraries(main3 ${HDF5_HL_LIBRARIES})

Aucun commentaire:

Enregistrer un commentaire