在C ++中将HDF5文件读取到动态数组 [英] Reading hdf5 files to dynamic arrays in c++

查看:252
本文介绍了在C ++中将HDF5文件读取到动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于堆栈的大小限制,我正在尝试将大型3D hdf5文件读取到动态数组中.我尝试了几种不同的方法,但由于分段错误而失败.下面是显示我的问题的示例代码.我非常感谢您的帮助!

I am trying to read large 3D hdf5 files into a dynamic array due to size limitations on the stack. I have tried several different methods and have failed via segmentation fault. Below is example code to show my issue. I would very much appreciate some help!!

//This example was based on several examples which came in the c++ examples directory of the hdf5 package.


#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>
#include <new>
#include "hdf5.h"

#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
    using namespace H5;
#endif

const H5std_string  FILE_NAME( "Test.h5" );
const H5std_string  DATASET_NAME( "Identity" );
const int   NX = 3;                    // dataset dimensions
const int   NY = 3;
const int   RANK = 2;

using namespace std;

int main (void)
{

    int buffer[3][3];


//  Make Identity Matrix for fill.
    for(int i=0; i<NX; i++){
        for(int j=0; j<NY; j++){
            if(i==j) buffer[i][j] = 1;
            else buffer[i][j]=0;
        }
    }
cout << "Lets check we filled it correctly: " << endl;
//  First let's check we do have I matrix

    for(int i=0; i<NX; i++){
        for(int j=0;j<NY;j++){
            cout << buffer[i][j] << "  ";
        } cout << endl;
    }




//  Now let's write into a file
    H5File file( FILE_NAME, H5F_ACC_TRUNC );
    hsize_t     dimsf[2];
    dimsf[0] = NX;
    dimsf[1] = NY;
    DataSpace dataspace( RANK, dimsf );
    IntType datatype( PredType::NATIVE_INT );
    datatype.setOrder( H5T_ORDER_LE );
    DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
    dataset.write( buffer, PredType::NATIVE_INT );



//  Ok great, now let's try opening this array using both static and dynamic(new) type arrays:

    H5File file1( FILE_NAME, H5F_ACC_RDONLY );
    DataSet dataset1 = file1.openDataSet( DATASET_NAME );

    /*
     * Get filespace for rank and dimension
     */
    DataSpace filespace = dataset1.getSpace();

    /*
     * Get number of dimensions in the file dataspace
     */
    int rank = filespace.getSimpleExtentNdims();

    /*
     * Get and print the dimension sizes of the file dataspace
     */
    hsize_t dims[2];    // dataset dimensions
    rank = filespace.getSimpleExtentDims( dims );
    /*
     * Define the memory space to read dataset.
     */
    DataSpace mspace1(RANK, dims);


    int newbuffer[NX][NY]; //static array.
    int **p2DArray;

    p2DArray = new int*[NX];
    for (uint i = 0; i < NX; ++i) {
        p2DArray[i] = new int[NY];
    }

    dataset1.read( newbuffer, PredType::NATIVE_INT, mspace1, filespace );
    dataset1.read( p2DArray, PredType::NATIVE_INT, mspace1, filespace );

//  Lastly lets print the arrays to make sure we get the same thing:
    cout << "Ok and now for the static array:" << endl;

    for(uint i=0; i<NX; i++){
        for(uint j=0;j<NY;j++){
            cout << newbuffer[i][j] << "  ";
        } cout << endl;
    }
    cout << "Ok and now for the dynamic array:" << endl;

    for(uint i=0; i<NX; i++){
        for(uint j=0;j<NY;j++){
            cout << p2DArray[i][j] << "  ";
        } cout << endl;
    }

return 0;
}

推荐答案

您的p2DArray必须是int的普通(1D)数组,而不是数组.这就是read方法所期望的.

Your p2DArray must be a plain (1D) array of int, not an array of arrays. That's what a read method expects.

只需分配足够的空间来存储NX * NY号:

Just allocate enough space to store NX*NY numbers:

int *p2DArray = new int[NX*NY];

,然后使用以下公式访问数组:p2DArray [i] [j] = p2DArray [i * NY + j].

and then access the array using the following formula: p2DArray[i][j] = p2DArray[i*NY + j].

这篇关于在C ++中将HDF5文件读取到动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆