Search code examples
c++scientific-computinghdf5

How to split HDF5 compund datatype in C++


I have an HDF5 file with a compound type (int, double, double). Currently I am reading it in a single operation by storing its result in an array of structures, following the example in the documentation.

However I would prefer instead to save each of the three parts in a different array, so I would like to provide 3 pointers, (int*, double*, double*) and read the data directly in the 3 arrays without copying data.

Does anybody have a suggestion on how to do that?


Solution

  • I don't think it is possible directly.

    You can easily copy your array of structures into three arrays in a loop:

    int a[n];
    double b[n], c[n];
    for (int i = 0; i < n; i++)
    {
        a[i] = s[i].a;
        b[i] = s[i].b;
        c[i] = s[i].c;
    }
    

    assuming s is an array of struct { int a; double b; double c; } of (constant) size n, but I have a feeling that you are looking for a direct HDF5 way…

    Clearly, an hyperslab won't work since I believe that a compound type is atomic. Anyway a compound type implies that the data is stored compound in the file so it is probably more efficient to read it that way. The only limitation with the solution I gave you is memory if your array is really large. On a modern computer that should be ok though ;)