Help a newbie with hyperslabs

Dear all,

I am sorry if I am posing a stupid question, but it seems I cannot really see how I can solve this on my own.

The problem is understanding HDF5's possibility of creating datasets with slabs. For instance, write a 5x2 matrix with only one row set to { 42, 42 }.

As far as I've learned reading the docs, I create the file, the dataspace with properties (5x2 of integers, for instance), and finally the dataspace itself. Then I can select the hyperslab and write to it.

What I'm getting is junk. I suspect I'm writing what I'm not supposed to write, or reading where I've not intended to read, or both. Here's the dump:

HDF5 "test.h5" {
GROUP "/" {
    DATASET "dset" {
       DATATYPE H5T_STD_I32BE
       DATASPACE SIMPLE { ( 5, 2 ) / ( 5, 2 ) }
       DATA {
       (0,0): 704643072, 704643072,
       (1,0): 0, 0,
       (2,0): 16777216, 0,
       (3,0): 16777216, 0,
       (4,0): 16777216, 0
       }
    }
}

I've played with my values, with count, offset and block, but I cannot grasp how it works.

The code is below, it is commented with what I think each instruction does, and what each variable means.

I really hope you can help me.

Thank you!

#include "hdf5.h"

int main()
{
   hid_t file_id, dataspace_id, dataset_id;
   herr_t status;
   int rank = 2; /* rank 2 tensor */
   hsize_t dims[2] = { 5, 2 }; /* 5 rows, 2 columns */

   hsize_t offset[2], stride[2], count[2], block[2];

   int newdata[3] = { 42, 42 };

   /* create file */
   file_id = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

   /* create dataspace */
   dataspace_id = H5Screate_simple(rank, dims, NULL);

   /* create dataset: the ACTUAL data */
   dataset_id = H5Dcreate(file_id, "/dset", H5T_STD_I32BE, dataspace_id,
       H5P_DEFAULT);

   /* slab properties */
   offset[0] = 1; /* row offset, select second row */
   offset[1] = 0; /* col offset, first column*/

   stride[0] = 1; /* select every 1 element, don't skip any row*/
   stride[1] = 1; /* and don't skip any elements on columns */

   count[0] = 1; /* select only one block per dimension */
   count[1] = 1; /* supposing 1 block means one entire row with this */

   block[0] = 1; /* size of the row block */
   block[1] = 3; /* column block size */

   /* select a slab */
   status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET,
             offset, stride, count, block);

   /* write! */
   status = H5Dwrite(dataset_id, H5T_STD_I32BE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
        newdata);

   /* close dataset */
   status = H5Dclose (dataset_id);

   /* close dataspace */
   status = H5Sclose(dataspace_id);

   /* close file */
   status = H5Fclose(file_id);

   return 0;
}

Is H5T_STD_I32BE the native int datatype for your platform? If no, you may want
to replace it by H5T_NATIVE_INT that will handle the proper int type.

Pierre

···

On Tue, Feb 10, 2015 at 04:29:35PM +0100, Sensei wrote:

Dear all,

I am sorry if I am posing a stupid question, but it seems I cannot really
see how I can solve this on my own.

The problem is understanding HDF5's possibility of creating datasets with
slabs. For instance, write a 5x2 matrix with only one row set to { 42, 42 }.

As far as I've learned reading the docs, I create the file, the dataspace
with properties (5x2 of integers, for instance), and finally the dataspace
itself. Then I can select the hyperslab and write to it.

What I'm getting is junk. I suspect I'm writing what I'm not supposed to
write, or reading where I've not intended to read, or both. Here's the dump:

HDF5 "test.h5" {
GROUP "/" {
   DATASET "dset" {
      DATATYPE H5T_STD_I32BE
      DATASPACE SIMPLE { ( 5, 2 ) / ( 5, 2 ) }
      DATA {
      (0,0): 704643072, 704643072,
      (1,0): 0, 0,
      (2,0): 16777216, 0,
      (3,0): 16777216, 0,
      (4,0): 16777216, 0
      }
   }
}
}

I've played with my values, with count, offset and block, but I cannot grasp
how it works.

The code is below, it is commented with what I think each instruction does,
and what each variable means.

I really hope you can help me.

Thank you!

#include "hdf5.h"

int main()
{
  hid_t file_id, dataspace_id, dataset_id;
  herr_t status;
  int rank = 2; /* rank 2 tensor */
  hsize_t dims[2] = { 5, 2 }; /* 5 rows, 2 columns */

  hsize_t offset[2], stride[2], count[2], block[2];

  int newdata[3] = { 42, 42 };

  /* create file */
  file_id = H5Fcreate("test.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

  /* create dataspace */
  dataspace_id = H5Screate_simple(rank, dims, NULL);

  /* create dataset: the ACTUAL data */
  dataset_id = H5Dcreate(file_id, "/dset", H5T_STD_I32BE, dataspace_id,
       H5P_DEFAULT);

  /* slab properties */
  offset[0] = 1; /* row offset, select second row */
  offset[1] = 0; /* col offset, first column*/

  stride[0] = 1; /* select every 1 element, don't skip any row*/
  stride[1] = 1; /* and don't skip any elements on columns */

  count[0] = 1; /* select only one block per dimension */
  count[1] = 1; /* supposing 1 block means one entire row with this */

  block[0] = 1; /* size of the row block */
  block[1] = 3; /* column block size */

  /* select a slab */
  status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET,
             offset, stride, count, block);

  /* write! */
  status = H5Dwrite(dataset_id, H5T_STD_I32BE, H5S_ALL, H5S_ALL,
H5P_DEFAULT,
        newdata);

  /* close dataset */
  status = H5Dclose (dataset_id);

  /* close dataspace */
  status = H5Sclose(dataspace_id);

  /* close file */
  status = H5Fclose(file_id);

  return 0;
}

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@lists.hdfgroup.org
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5

--
-----------------------------------------------------------
Pierre de Buyl
KU Leuven - Institute for Theoretical Physics
T +32 16 3 27355
W http://pdebuyl.be/
-----------------------------------------------------------