Dataspaces and changing from row-major to column-major

Hi,

I am clearly misunderstanding something about reading datasets and
converting their write order in a buffer using dataspaces. I am
attempting to convert between a row-major memory layout to a column
major memory layout and am incapable. In the documentation here:

http://www.hdfgroup.org/HDF5/doc/UG/12_Dataspaces.html

"Figure 4a shows a simple example of a read operation in which the data
is stored as a 3 by 4 array in the file (Figure 4b), but the program
wants it to be a 4 by 3 array in memory. This is accomplished by setting
the memory dataspace to describe the desired memory layout, as in Figure
4c. The HDF5 Library will transform the data to the correct arrangement
during the read operation"

As an example, I took the program: h5ex_d_rdwr.c from:

http://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5D/h5ex_d_rdwr.c

I modified it to include the following lines (fully modified program is
attached).

hid_t memspace = H5Screate_simple(2, rdims, NULL);
hid_t filespace = H5Dget_space(dset);
H5Dread(dset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT,
  rdata[0]);

Here rdims is simply the original dims in reverse. When I do this,
nothing changes in the way that the matrix is printed. I feel I must be
missing something, but I can't find any examples doing this
transposition. I am using hdf 1.8.4 on linux.

Full program attached, Thanks again.

jim

h5_ex_d_rdwr.c (3.18 KB)

Hi Jim,

Hi,

I am clearly misunderstanding something about reading datasets and
converting their write order in a buffer using dataspaces. I am
attempting to convert between a row-major memory layout to a column
major memory layout and am incapable. In the documentation here:

http://www.hdfgroup.org/HDF5/doc/UG/12_Dataspaces.html

"Figure 4a shows a simple example of a read operation in which the data
is stored as a 3 by 4 array in the file (Figure 4b), but the program
wants it to be a 4 by 3 array in memory. This is accomplished by setting
the memory dataspace to describe the desired memory layout, as in Figure
4c. The HDF5 Library will transform the data to the correct arrangement
during the read operation"

As an example, I took the program: h5ex_d_rdwr.c from:

http://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5D/h5ex_d_rdwr.c

I modified it to include the following lines (fully modified program is
attached).

hid_t memspace = H5Screate_simple(2, rdims, NULL);
hid_t filespace = H5Dget_space(dset);
H5Dread(dset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT,
  rdata[0]);

Here rdims is simply the original dims in reverse. When I do this,
nothing changes in the way that the matrix is printed. I feel I must be
missing something, but I can't find any examples doing this
transposition. I am using hdf 1.8.4 on linux.

Full program attached, Thanks again.

  The HDF5 library won't transpose data during reads/writes. What your program is doing is telling the library that the array is different shapes, not transposed.

  Quincey

···

On Apr 22, 2010, at 1:02 PM, James Bullard wrote:

jim
#include "hdf5.h"
#include <stdio.h>
#include <stdlib.h>

#define FILE "h5ex_d_rdwr.h5"
#define DATASET "DS1"
#define DIM0 4
#define DIM1 7

int
main (void)
{
   hid_t file, space, dset; /* Handles */
   herr_t status;
   hsize_t dims[2] = {DIM0, DIM1};
   hsize_t rdims[2] = {DIM1, DIM0};
   int wdata[DIM0][DIM1], /* Write buffer */
               rdata[DIM0][DIM1], /* Read buffer */
               i, j;

   int* xdata = (int *) calloc(DIM0*DIM1, sizeof(int));

   /*
    * Initialize data.
    */
   for (i=0; i<DIM0; i++)
       for (j=0; j<DIM1; j++)
           wdata[i][j] = i * j - j;

   /*
    * Create a new file using the default properties.
    */
   file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Create dataspace. Setting maximum size to NULL sets the maximum
    * size to be the current size.
    */
   space = H5Screate_simple (2, dims, NULL);

   /*
    * Create the dataset. We will use all default properties for this
    * example.
    */
   dset = H5Dcreate (file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT,
          H5P_DEFAULT, H5P_DEFAULT);

   /*
    * Write the data to the dataset.
    */
   status = H5Dwrite (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
           wdata[0]);

   /*
    * Close and release resources.
    */
   status = H5Dclose (dset);
   status = H5Sclose (space);
   status = H5Fclose (file);

   /*
    * Now we begin the read section of this example.
    */

   /*
    * Open file and dataset using the default properties.
    */
   file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
   dset = H5Dopen (file, DATASET, H5P_DEFAULT);

   /*
    * Read the data using the default properties.
    */
   status = H5Dread (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
          rdata[0]);
   status = H5Dread (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
          xdata);
   /*
    * Output the data to the screen.
    */
   printf ("%s:\n", DATASET);
   for (i=0; i<DIM0; i++) {
       printf (" [");
       for (j=0; j<DIM1; j++)
           printf (" %3d", rdata[i][j]);
       printf ("]\n");
   }

   for (i = 0; i < DIM0*DIM1; i++) {
  printf(" %d", xdata[i]);
   }
   printf ("\n");

   hid_t memspace = H5Screate_simple(2, rdims, NULL);
   hid_t filespace = H5Dget_space(dset);
   H5Dread(dset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT,
      rdata[0]);
   H5Dread(dset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT,
      xdata);

   /*
    * Output the data to the screen.
    */
   printf ("Thought it would be reversed dataset %s:\n", DATASET);
   for (i=0; i<DIM0; i++) {
       printf (" [");
       for (j=0; j<DIM1; j++)
           printf (" %3d", rdata[i][j]);
       printf ("]\n");
   }

   for (i = 0; i < DIM0*DIM1; i++) {
  printf(" %d", xdata[i]);
   }
   printf ("\n");

   /*
    * Close and release resources.
    */
   status = H5Dclose (dset);
   status = H5Fclose (file);

   return 0;
}
_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org