Re: compound types on different machines - is this expected?

Hi Ed

for a correct use of the creation of portable compound types you might take a 
look at the code of the high level Table API. 
see the files /hl/src/H5TB.c and /hl/test/test_table.c
and the function H5TBmake_table

the test_table.c test reads files saved in little and big endian machines 
Pedro



At 10:39 AM 4/28/2005, you wrote:
>Howdy HDF5 People!
>
>I have a program called tst_h_wrt_cmp, which write a little array of a
>compound type.
>
>Then I have another program called tst_h_rd_cmp.c, which reads them
>back in and checks the value. This works on any one machine, but when
>I run tst_h_wrt_cmp on a Sun, and try to read the resulting file on a
>Linux machine, I get garbled data because the two machines use
>different padding for the struct.
>
>Is this expected behavior?
>
>tst_h_wrt_cmp.c:
>
>
>#include "tests.h"
>
>#define FILE_NAME "tst_h_wrt_cmp.h5"
>#define DIM1_LEN 3
>#define COMPOUND_NAME "cmp"
>#define VAR_NAME "var"
>
>int
>main()
>{
>   hid_t fileid, access_plist, spaceid, typeid;
>   hid_t datasetid, datasetid1, typeid1;
>   int bool_out[DIM1_LEN] = {0, 1, 0}, bool_in[DIM1_LEN];
>   hsize_t dims[1];
>   struct s1 {
>        unsigned char c1;
>        double d;
>   } data[DIM1_LEN];
>   int i;
>
>   for (i=0; i<DIM1_LEN; i++)
>   {
>      data[i].c1 = 126;
>      data[i].d = -9999999;
>   }
>
>   printf("*** Checking packing of HDF5 compound types...");
>   
>   /* Open file and create group. */
>   if ((access_plist = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
>   if (H5Pset_fclose_degree(access_plist, H5F_CLOSE_STRONG)) ERR;
>   if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, 
>                           access_plist)) < 0) ERR;
>
>   /* Create a simple compound type. */
>   if ((typeid = H5Tcreate(H5T_COMPOUND, sizeof(struct s1))) < 0) ERR;
>   if (H5Tinsert(typeid, "c1", HOFFSET(struct s1, c1), H5T_NATIVE_UCHAR) < 0) 
> ERR;
>   if (H5Tinsert(typeid, "d", HOFFSET(struct s1, d), H5T_NATIVE_DOUBLE) < 0) 
> ERR;
>   if (H5Tcommit(fileid, COMPOUND_NAME, typeid) < 0) ERR;
>
>   /* Create a space. */
>   dims[0] = DIM1_LEN;
>   if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR;
>
>   /* Create a dataset of this compound type. */
>   if ((datasetid = H5Dcreate(fileid, VAR_NAME, typeid, spaceid, 
>                              H5P_DEFAULT)) < 0) ERR;
>
>   /* Write some data. */
>   if (H5Dwrite(datasetid, typeid, H5S_ALL, H5S_ALL, 
>                H5P_DEFAULT, data) < 0) ERR;
>
>   /* Release all resources. */
>   if (H5Fclose(fileid) < 0) ERR;
>
>   SUMMARIZE_ERR;
>
>   /* Print out our number of errors, if any, and exit badly. */
>   if (total_err)
>   {
>      printf("%d errors detected! Sorry!\n", total_err);
>      return 2;
>   }
>   
>   printf("*** Tests successful!\n");
>   return 0;
>}
>
>
>tst_h_rd_cmp.c:
>
>#include "tests.h"
>
>#define FILE_NAME "tst_h_wrt_cmp.h5"
>#define DIM1_LEN 3
>#define COMPOUND_NAME "cmp"
>#define VAR_NAME "var"
>
>int
>main()
>{
>   hid_t fileid, access_plist, spaceid, typeid;
>   hid_t datasetid, datasetid1, typeid1;
>   int bool_out[DIM1_LEN] = {0, 1, 0}, bool_in[DIM1_LEN];
>   hsize_t dims[1];
>   struct s1 {
>        unsigned char c1;
>        double d;
>   } data[DIM1_LEN];
>   int i;
>
>   printf("*** Checking packing of HDF5 compound types...");
>   
>   /* Open file. */
>   if ((access_plist = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
>   if (H5Pset_fclose_degree(access_plist, H5F_CLOSE_STRONG)) ERR;
>   if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDONLY, access_plist)) < 0) ERR;
>
>   /* Open dataset. */
>   if ((datasetid = H5Dopen(fileid, VAR_NAME)) < 0) ERR;
>
>   /* Check space. */
>   if ((spaceid = H5Dget_space(datasetid)) < 0) ERR;
>   if (H5Sget_simple_extent_ndims(spaceid) != 1) ERR;
>   if (H5Sget_simple_extent_npoints(spaceid) != DIM1_LEN) ERR;
>
>   /* Get type. */
>   if ((typeid = H5Dget_type(datasetid)) < 0) ERR;
>
>   /* Read the data. */
>   if (H5Dread(datasetid, typeid, H5S_ALL, H5S_ALL, 
>               H5P_DEFAULT, data) < 0) ERR;
>
>   /* Check the data. */
>   for (i=0; i<DIM1_LEN; i++)
>      if (data[i].c1 != 126 || data[i].d != -9999999) ERR;
>
>   /* Release all resources. */
>   if (H5Fclose(fileid) < 0) ERR;
>
>   SUMMARIZE_ERR;
>
>   /* Print out our number of errors, if any, and exit badly. */
>   if (total_err)
>   {
>      printf("%d errors detected! Sorry!\n", total_err);
>      return 2;
>   }
>   
>   printf("*** Tests successful!\n");
>   return 0;
>}
>
>-- 
>Ed Hartnett  -- ed@xxxxxxxxxxxxxxxx

</Pedro Vicente Nunes>
--------------------------------------------------------------
hdf.ncsa.uiuc.edu
Tel. 1-217-265 0311