/* * Copyright (C) 1998 NCSA * All rights reserved. * * Programmer: Robb Matzke * Friday, January 23, 1998 */ #undef NDEBUG #include #include #include #include #include #define TEST_FILE_NAME "cmpd_dset.h5" /* The first dataset */ typedef struct s1_t { unsigned int a; unsigned int b; unsigned int c[4]; unsigned int d; unsigned int e; } s1_t; /* The second dataset (same as first) */ typedef s1_t s2_t; /* The third dataset (reversed fields of s1) */ typedef struct s3_t { unsigned int e; unsigned int d; unsigned int c[4]; unsigned int b; unsigned int a; } s3_t; /* The fourth dataset (a subset of s1) */ typedef struct s4_t { unsigned int b; unsigned int d; } s4_t; /* The fifth dataset (a superset of s1) */ typedef struct s5_t { unsigned int pre; unsigned int a; unsigned int b; unsigned int mid1; unsigned int c[4]; unsigned int mid2; unsigned int d; unsigned int e; unsigned int post; } s5_t; #if 1 # define NX 100u # define NY 2000u #else # define NX 12u # define NY 9u #endif /*------------------------------------------------------------------------- * Function: cleanup * * Purpose: Cleanup temporary test files * * Return: none * * Programmer: Albert Cheng * May 28, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static void cleanup(void) { if (!getenv ("HDF5_NOCLEANUP")) { remove(TEST_FILE_NAME); } } /*------------------------------------------------------------------------- * Function: main * * Purpose: Creates a simple dataset of a compound type and then reads * it back. The dataset is read back in various ways to * exercise the I/O pipeline and compound type conversion. * * Return: Success: 0 * * Failure: 1 * * Programmer: Robb Matzke * Friday, January 23, 1998 * * Modifications: * *------------------------------------------------------------------------- */ int main (void) { /* First dataset */ static s1_t s1[NX*NY]; hid_t s1_tid; /* Second dataset */ static s2_t s2[NX*NY]; hid_t s2_tid; /* Third dataset */ static s3_t s3[NX*NY]; hid_t s3_tid; /* Fourth dataset */ static s4_t s4[NX*NY]; hid_t s4_tid; /* Fifth dataset */ static s5_t s5[NX*NY]; hid_t s5_tid; /* Sixth dataset */ /* Seventh dataset */ hid_t s7_sid; /* Eighth dataset */ s1_t *s8 = NULL; hid_t s8_f_sid; /*file data space */ hid_t s8_m_sid; /*memory data space */ /* Ninth dataset */ /* Tenth dataset */ /* Eleventh dataset */ s4_t *s11 = NULL; /* Other variables */ unsigned int i, j; int ndims; hid_t file, dataset, space, PRESERVE; herr_t status; static hsize_t dim[] = {NX, NY}; hssize_t f_offset[2]; /*offset of hyperslab in file */ hsize_t h_size[2]; /*size of hyperslab */ hsize_t h_sample[2]; /*hyperslab sampling */ size_t memb_size[1] = {4}; /* Create the file */ file = H5Fcreate (TEST_FILE_NAME, H5F_ACC_TRUNC|H5F_ACC_DEBUG, H5P_DEFAULT, H5P_DEFAULT); assert (file>=0); /* Create the data space */ space = H5Screate_simple (2, dim, NULL); assert (space>=0); /* Create xfer properties to preserve initialized data */ PRESERVE = H5Pcreate (H5P_DATASET_XFER); assert (PRESERVE>=0); status = H5Pset_preserve (PRESERVE, 1); assert (status>=0); /* *###################################################################### * STEP 1: Save the original dataset natively. */ printf ("\ STEP 1: Initialize dataset `s1' and store it on disk in native order.\n"); fflush (stdout); /* Initialize the dataset */ for (i=0; i=0); /* Create the dataset */ dataset = H5Dcreate (file, "s1", s1_tid, space, H5P_DEFAULT); assert (dataset>=0); /* Write the data */ status = H5Dwrite (dataset, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s1); assert (status>=0); /* *###################################################################### * STEP 2: We create a new type ID for the second dataset even though * it's the same as the first just to test things better, but * in fact, we could have used s1_tid. */ printf ("\ STEP 2: Read the dataset from disk into a new memory buffer which has the\n\ same data type and space. This will be the typical case.\n"); fflush (stdout); /* Create a data type for s2 */ s2_tid = H5Tcreate (H5T_COMPOUND, sizeof(s2_t)); H5Tinsert (s2_tid, "a", HOFFSET(s2_t,a), H5T_NATIVE_INT); H5Tinsert (s2_tid, "b", HOFFSET(s2_t,b), H5T_NATIVE_INT); H5Tinsert_array (s2_tid, "c", HOFFSET(s2_t,c), 1, memb_size, NULL, H5T_NATIVE_INT); H5Tinsert (s2_tid, "d", HOFFSET(s2_t,d), H5T_NATIVE_INT); H5Tinsert (s2_tid, "e", HOFFSET(s2_t,e), H5T_NATIVE_INT); assert (s2_tid>=0); /* Read the data */ status = H5Dread (dataset, s2_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s2); assert (status>=0); /* Compare s2 with s1. They should be the same */ for (i=0; i=0); /* Read the data */ status = H5Dread (dataset, s3_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s3); assert (status>=0); /* Compare s3 with s1. They should be the same */ for (i=0; i members * stored on disk we'll read . */ printf ("\ STEP 4: Read a subset of the members.\n"); fflush (stdout); /* Create a datatype for s4 */ s4_tid = H5Tcreate (H5T_COMPOUND, sizeof(s4_t)); H5Tinsert (s4_tid, "b", HOFFSET(s4_t,b), H5T_NATIVE_INT); H5Tinsert (s4_tid, "d", HOFFSET(s4_t,d), H5T_NATIVE_INT); assert (s4_tid>=0); /* Read the data */ status = H5Dread (dataset, s4_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s4); assert (status>=0); /* Compare s4 with s1 */ for (i=0; i=0); /* Read the data */ status = H5Dread (dataset, s5_tid, H5S_ALL, H5S_ALL, PRESERVE, s5); assert (status>=0); /* Check that the data was read properly */ for (i=0; i=0); /* Read the data back */ status = H5Dread (dataset, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s1); assert (status>=0); /* Compare */ for (i=0; i=0); /* Read the dataset */ status = H5Dread (dataset, s2_tid, s7_sid, H5S_ALL, H5P_DEFAULT, s2); assert (status>=0); /* Compare */ for (i=0; i=0); f_offset[0] = NX/3; f_offset[1] = NY/3; h_size[0] = 2*NX/3 - f_offset[0]; h_size[1] = 2*NY/3 - f_offset[1]; status = H5Sselect_hyperslab (s8_f_sid, H5S_SELECT_SET, f_offset, NULL, h_size, NULL); assert (status>=0); /* Create memory data space */ s8_m_sid = H5Screate_simple (2, h_size, NULL); assert (s8_m_sid>=0); /* Read the dataset */ s8 = calloc ((size_t)(h_size[0]*h_size[1]), sizeof(s1_t)); assert (s8); status = H5Dread (dataset, s1_tid, s8_m_sid, s8_f_sid, H5P_DEFAULT, s8); assert (status>=0); /* Compare */ for (i=0; ia == ps1->a); assert (ps8->b == ps1->b); assert (ps8->c[0] == ps1->c[0]); assert (ps8->c[1] == ps1->c[1]); assert (ps8->c[2] == ps1->c[2]); assert (ps8->c[3] == ps1->c[3]); assert (ps8->d == ps1->d); assert (ps8->e == ps1->e); } } free (s8); s8 = NULL; /* *###################################################################### * STEP 9. Read a hyperslab of the file into a hyperslab of memory. The * part of memory not read is already initialized and must not change. */ printf ("\ STEP 9: Read middle third of hyperslab into middle third of memory array.\n"); fflush (stdout); /* Initialize */ for (i=0; i=0); /* Compare */ for (i=0; i=f_offset[0] && (hsize_t)i=f_offset[1] && (hsize_t)ja == ps1->a); assert (ps2->b == ps1->b); assert (ps2->c[0] == ps1->c[0]); assert (ps2->c[1] == ps1->c[1]); assert (ps2->c[2] == ps1->c[2]); assert (ps2->c[3] == ps1->c[3]); assert (ps2->d == ps1->d); assert (ps2->e == ps1->e); } else { assert (ps2->a == (unsigned)(-1)); assert (ps2->b == (unsigned)(-1)); assert (ps2->c[0] == (unsigned)(-1)); assert (ps2->c[1] == (unsigned)(-1)); assert (ps2->c[2] == (unsigned)(-1)); assert (ps2->c[3] == (unsigned)(-1)); assert (ps2->d == (unsigned)(-1)); assert (ps2->e == (unsigned)(-1)); } } } /* *###################################################################### * STEP 10. Same as step 9 except the memory array contains some members * which are already initialized, like step 5. */ printf ("\ STEP 10: Read middle third of hyperslab into middle third of memory array\n\ where some of the struct members are already initialized.\n"); fflush (stdout); /* Initialize */ for (i=0; i=0); /* Compare */ for (i=0; i=f_offset[0] && (hsize_t)i=f_offset[1] && (hsize_t)jpre == (unsigned)(-1)); assert (ps5->a == ps1->a); assert (ps5->b == ps1->b); assert (ps5->mid1 == (unsigned)(-1)); assert (ps5->c[0] == ps1->c[0]); assert (ps5->c[1] == ps1->c[1]); assert (ps5->c[2] == ps1->c[2]); assert (ps5->c[3] == ps1->c[3]); assert (ps5->mid2 == (unsigned)(-1)); assert (ps5->d == ps1->d); assert (ps5->e == ps1->e); assert (ps5->post == (unsigned)(-1)); } else { assert (ps5->pre == (unsigned)(-1)); assert (ps5->a == (unsigned)(-1)); assert (ps5->b == (unsigned)(-1)); assert (ps5->mid1 == (unsigned)(-1)); assert (ps5->c[0] == (unsigned)(-1)); assert (ps5->c[1] == (unsigned)(-1)); assert (ps5->c[2] == (unsigned)(-1)); assert (ps5->c[3] == (unsigned)(-1)); assert (ps5->mid2 == (unsigned)(-1)); assert (ps5->d == (unsigned)(-1)); assert (ps5->e == (unsigned)(-1)); assert (ps5->post == (unsigned)(-1)); } } } #ifdef OLD_WAY /* *###################################################################### * Step 11: Write an array into the middle third of the dataset * initializeing only members `b' and `d' to -1. */ printf ("\ STEP 11: Write an array back to the middle third of the dataset to\n\ initialize the `b' and `d' members to -1.\n"); fflush (stdout); /* Create the memory array and initialize all fields to zero */ ndims = H5Sget_hyperslab (s8_f_sid, f_offset, h_size, h_sample); assert (ndims==2); s11 = malloc ((size_t)h_size[0]*(size_t)h_size[1]*sizeof(s4_t)); assert (s11); /* Initialize */ for (i=0; i=0); free (s11); s11=NULL; /* Read the whole thing */ status = H5Dread (dataset, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s1); assert (status>=0); /* Compare */ for (i=0; ia == 8*(i*NY+j)+0); assert (ps1->c[0] == 8*(i*NY+j)+2); assert (ps1->c[1] == 8*(i*NY+j)+3); assert (ps1->c[2] == 8*(i*NY+j)+4); assert (ps1->c[3] == 8*(i*NY+j)+5); assert (ps1->e == 8*(i*NY+j)+7); if ((hssize_t)i>=f_offset[0] && (hsize_t)i=f_offset[1] && (hsize_t)jb == (unsigned)(-1)); assert (ps1->d == (unsigned)(-1)); } else { assert (ps1->b == 8*(i*NY+j)+1); assert (ps1->d == 8*(i*NY+j)+6); } } } #endif /* OLD_WAY */ /* * Release resources. */ H5Pclose (PRESERVE); H5Dclose (dataset); H5Fclose (file); cleanup(); return 0; }