Caffe源码中各种依赖库的作用及简单使用(5)

/*
 * Create a new file using H5F_ACC_TRUNC access,
 * default file creation properties, and default file
 * access properties.
 */
 file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

/*
 * Describe the size of the array and create the data space for fixed
 * size dataset.
 */
 dimsf[0] = NX;
 dimsf[1] = NY;
 dataspace = H5Screate_simple(RANK, dimsf, NULL);

/*
 * Define datatype for the data in the file.
 * We will store little endian INT numbers.
 */
 datatype = H5Tcopy(H5T_NATIVE_INT);
 status = H5Tset_order(datatype, H5T_ORDER_LE);

/*
 * Create a new dataset within the file using defined dataspace and
 * datatype and default dataset creation properties.
 */
 dataset = H5Dcreate2(file, DATASETNAME, datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

//Write the data to the dataset using default transfer properties.
 status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);

//Close/release resources.
 H5Sclose(dataspace);
 H5Tclose(datatype);
 H5Dclose(dataset);
 H5Fclose(file);

return 0;
}

#define NX_SUB   3 /* hyperslab dimensions */
#define NY_SUB   4
#define NX   7 /* output buffer dimensions */
#define NY   7
#define NZ   3
#define RANK   2
#define RANK_OUT  3

int test_HDF5_read_HDF5_data()
{
 hid_t      file, dataset;        /* handles */
 hid_t      datatype, dataspace;
 hid_t      memspace;
 H5T_class_t t_class;                /* data type class */
 H5T_order_t order;                /* data order */
 size_t      size;                  /*
        * size of the data element
        * stored in file
        */
 hsize_t    dimsm[3];              /* memory space dimensions */
 hsize_t    dims_out[2];          /* dataset dimensions */
 herr_t      status;

int        data_out[NX][NY][NZ]; /* output buffer */

hsize_t      count[2];              /* size of the hyperslab in the file */
 hsize_t      offset[2];            /* hyperslab offset in the file */
 hsize_t      count_out[3];          /* size of the hyperslab in memory */
 hsize_t      offset_out[3];        /* hyperslab offset in memory */
 int          i, j, k, status_n, rank;

for (j = 0; j < NX; j++) {
  for (i = 0; i < NY; i++) {
   for (k = 0; k < NZ; k++)
    data_out[j][i][k] = 0 - 1000;//changed
  }
 }

/*
 * Open the file and the dataset.
 */
 file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT);
 dataset = H5Dopen2(file, DATASETNAME, H5P_DEFAULT);

/*
 * Get datatype and dataspace handles and then query
 * dataset class, order, size, rank and dimensions.
 */
 datatype = H5Dget_type(dataset);    /* datatype handle */
 t_class = H5Tget_class(datatype);
 if (t_class == H5T_INTEGER) printf("Data set has INTEGER type \n");
 order = H5Tget_order(datatype);
 if (order == H5T_ORDER_LE) printf("Little endian order \n");

size = H5Tget_size(datatype);
 printf("Data size is %d \n", (int)size);

dataspace = H5Dget_space(dataset);    /* dataspace handle */
 rank = H5Sget_simple_extent_ndims(dataspace);
 status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL);
 printf("rank %d, dimensions %lu x %lu \n", rank,
  (unsigned long)(dims_out[0]), (unsigned long)(dims_out[1]));

/*
 * Define hyperslab in the dataset.
 */
 offset[0] = 1;
 offset[1] = 2;
 count[0] = NX_SUB;
 count[1] = NY_SUB;
 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL,
  count, NULL);

/*
 * Define the memory dataspace.
 */
 dimsm[0] = NX;
 dimsm[1] = NY;
 dimsm[2] = NZ;
 memspace = H5Screate_simple(RANK_OUT, dimsm, NULL);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/b4757544dc3cf42f4c437584fa8527cf.html