/*------------------------------------------------------------------------- * Function: test_value_dsnt_exist * * Purpose: Create an enumeration datatype with "gaps in values" * and then request a name of non-existing value within * an existing range by calling H5Tenum_nameof function. * Function should fail instead of succeeding and returning * a name of one of the existing values. * Request a value by supplying non-existing name by calling * H5Tenum_nameof function. Function should fail. * * * Return: Success: 0 * * Failure: number of errors * * Programmer: Elena Pourmal * Wednesday, June 7, 2002 * * Modifications: * *------------------------------------------------------------------------- */ static int test_value_dsnt_exist(void) { hid_t datatype_id=(-1); /* identifiers */ int val; char nam[100]; size_t size = 100; TESTING("for non-existing name and value"); /* Turn off error reporting since we expect failure in this test */ if (H5Eset_auto(NULL, NULL) < 0) goto error; if ((datatype_id = H5Tenum_create(H5T_NATIVE_INT))< 0) goto error; /* These calls should fail, since no memebrs exist yet */ if (H5Tenum_valueof(datatype_id, "SAX", &val) >= 0) goto error; val = 3; if (H5Tenum_nameof(datatype_id, &val, nam, size) >= 0) goto error; val = 2; if (H5Tenum_insert(datatype_id, "TWO", (int *)&val) < 0) goto error; val = 6; if (H5Tenum_insert(datatype_id, "SIX", (int *)&val) < 0) goto error; val = 10; if (H5Tenum_insert(datatype_id, "TEN", (int *)&val) < 0) goto error; /* This call should fail since we did not create a member with value = 3*/ val = 3; if (H5Tenum_nameof(datatype_id, &val, nam, size) >= 0) goto error; /* This call should fail since we did not create a member with value = 11*/ val = 11; if (H5Tenum_nameof(datatype_id, &val, nam, size) >= 0) goto error; /* This call should fail since we did not create a member with value = 0*/ val = 0; if (H5Tenum_nameof(datatype_id, &val, nam, size) >= 0) goto error; /* This call should fail since we do not have SAX name in the type */ if (H5Tenum_valueof(datatype_id, "SAX", &val) >= 0) goto error; /* This call should fail since we do not have TEEN name in the type */ if (H5Tenum_valueof(datatype_id, "TEEN", &val) >= 0) goto error; /* This call should fail since we do not have A name in the type */ if (H5Tenum_valueof(datatype_id, "A", &val) >= 0) goto error; if (H5Tclose(datatype_id) < 0) goto error; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Tclose(datatype_id); } H5E_END_TRY; return 1; }
/*------------------------------------------------------------------------- * Function: test_noconv * * Purpose: Tests creation of datasets when no conversion is present. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Monday, January 4, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static int test_noconv(hid_t file) { hid_t cwg=-1, type=-1, space=-1, dset=-1; c_e1 val; static c_e1 data1[]={E1_RED, E1_GREEN, E1_BLUE, E1_GREEN, E1_WHITE, E1_WHITE, E1_BLACK, E1_GREEN, E1_BLUE, E1_RED, E1_RED, E1_BLUE, E1_GREEN, E1_BLACK, E1_WHITE, E1_RED, E1_WHITE, E1_GREEN, E1_GREEN, E1_BLUE}; c_e1 data2[NELMTS(data1)]; hsize_t ds_size[1]={NELMTS(data1)}; size_t i; TESTING("no-conversion datasets"); if ((cwg=H5Gcreate(file, "test_noconv", 0))<0) goto error; if ((type = H5Tcreate(H5T_ENUM, sizeof(c_e1)))<0) goto error; if (H5Tenum_insert(type, "RED", CPTR(val, E1_RED ))<0) goto error; if (H5Tenum_insert(type, "GREEN", CPTR(val, E1_GREEN))<0) goto error; if (H5Tenum_insert(type, "BLUE", CPTR(val, E1_BLUE ))<0) goto error; if (H5Tenum_insert(type, "WHITE", CPTR(val, E1_WHITE))<0) goto error; if (H5Tenum_insert(type, "BLACK", CPTR(val, E1_BLACK))<0) goto error; if ((space=H5Screate_simple(1, ds_size, NULL))<0) goto error; if ((dset=H5Dcreate(cwg, "color_table", type, space, H5P_DEFAULT))<0) goto error; if (H5Dwrite(dset, type, space, space, H5P_DEFAULT, data1)<0) goto error; if (H5Dread(dset, type, space, space, H5P_DEFAULT, data2)<0) goto error; for (i=0; i<ds_size[0]; i++) { if (data1[i]!=data2[i]) { H5_FAILED(); printf(" data1[%lu]=%d, data2[%lu]=%d (should be same)\n", (unsigned long)i, (int)(data1[i]), (unsigned long)i, (int)(data2[i])); goto error; } } if (H5Dclose(dset)<0) goto error; if (H5Sclose(space)<0) goto error; if (H5Tclose(type)<0) goto error; if (H5Gclose(cwg)<0) goto error; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Dclose(dset); H5Sclose(space); H5Tclose(type); H5Gclose(cwg); } H5E_END_TRY; return 1; }
//-------------------------------------------------------------------------- // Function: EnumType::insert ///\brief Inserts a new member to this enumeration datatype. ///\param name - IN: Name of the new member ///\param value - IN: Pointer to the value of the new member ///\exception H5::DataTypeIException // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void EnumType::insert( const char* name, void *value ) const { // Calls C routine H5Tenum_insert to insert the new enum datatype member. herr_t ret_value = H5Tenum_insert( id, name, value ); if( ret_value < 0 ) { throw DataTypeIException("EnumType::insert", "H5Tenum_insert failed"); } }
/* * test_enum * Test that enumerated datatypes can have UTF-8 member names. */ void test_enum(hid_t UNUSED fid, const char * string) { /* Define an enumerated type */ typedef enum { E1_RED, E1_GREEN, E1_BLUE, E1_WHITE } c_e1; /* Variable declarations */ c_e1 val; herr_t ret; hid_t type_id; char readbuf[MAX_STRING_LENGTH]; /* Create an enumerated datatype in HDF5 with a UTF-8 member name*/ type_id = H5Tcreate(H5T_ENUM, sizeof(c_e1)); CHECK(type_id, FAIL, "H5Tcreate"); val = E1_RED; ret = H5Tenum_insert(type_id, "RED", &val); CHECK(ret, FAIL, "H5Tenum_insert"); val = E1_GREEN; ret = H5Tenum_insert(type_id, "GREEN", &val); CHECK(ret, FAIL, "H5Tenum_insert"); val = E1_BLUE; ret = H5Tenum_insert(type_id, "BLUE", &val); CHECK(ret, FAIL, "H5Tenum_insert"); val = E1_WHITE; ret = H5Tenum_insert(type_id, string, &val); CHECK(ret, FAIL, "H5Tenum_insert"); /* Ensure that UTF-8 member name gives the right value and vice versa. */ ret = H5Tenum_valueof(type_id, string, &val); CHECK(ret, FAIL, "H5Tenum_valueof"); VERIFY(val, E1_WHITE, "H5Tenum_valueof"); ret = H5Tenum_nameof(type_id, &val, readbuf, (size_t)MAX_STRING_LENGTH); CHECK(ret, FAIL, "H5Tenum_nameof"); ret = strcmp(readbuf, string); VERIFY(ret, 0, "strcmp"); /* Close the datatype */ ret = H5Tclose(type_id); CHECK(ret, FAIL, "H5Tclose"); }
int main (void) { hid_t file, filetype, memtype, space, dset; /* Handles */ herr_t status; hsize_t dims[2] = {DIM0, DIM1}; phase_t wdata[DIM0][DIM1], /* Write buffer */ **rdata, /* Read buffer */ val; char *names[4] = {"SOLID", "LIQUID", "GAS", "PLASMA"}, name[NAME_BUF_SIZE]; int ndims, i, j; /* * Initialize data. */ for (i=0; i<DIM0; i++) for (j=0; j<DIM1; j++) wdata[i][j] = (phase_t) ( (i + 1) * j - j) % (int) (PLASMA + 1); /* * Create a new file using the default properties. */ file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create the enumerated datatypes for file and memory. This * process is simplified if native types are used for the file, * as only one type must be defined. */ filetype = H5Tenum_create (F_BASET); memtype = H5Tenum_create (M_BASET); for (i = (int) SOLID; i <= (int) PLASMA; i++) { /* * Insert enumerated value for memtype. */ val = (phase_t) i; status = H5Tenum_insert (memtype, names[i], &val); /* * Insert enumerated value for filetype. We must first convert * the numerical value val to the base type of the destination. */ status = H5Tconvert (M_BASET, F_BASET, 1, &val, NULL, H5P_DEFAULT); status = H5Tenum_insert (filetype, names[i], &val); } /* * 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 and write the enumerated data to it. */ dset = H5Dcreate (file, DATASET, filetype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); status = H5Dwrite (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); /* * Close and release resources. */ status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (filetype); status = H5Fclose (file); /* * Now we begin the read section of this example. Here we assume * the dataset has the same name and rank, but can have any size. * Therefore we must allocate a new array to read in data using * malloc(). For simplicity, we do not rebuild memtype. */ /* * Open file and dataset. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen (file, DATASET, H5P_DEFAULT); /* * Get dataspace and allocate memory for read buffer. This is a * two dimensional dataset so the dynamic allocation must be done * in steps. */ space = H5Dget_space (dset); ndims = H5Sget_simple_extent_dims (space, dims, NULL); /* * Allocate array of pointers to rows. */ rdata = (phase_t **) malloc (dims[0] * sizeof (phase_t *)); /* * Allocate space for enumerated data. */ rdata[0] = (phase_t *) malloc (dims[0] * dims[1] * sizeof (phase_t)); /* * Set the rest of the pointers to rows to the correct addresses. */ for (i=1; i<dims[0]; i++) rdata[i] = rdata[0] + i * dims[1]; /* * Read the data. */ status = H5Dread (dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); /* * Output the data to the screen. */ printf ("%s:\n", DATASET); for (i=0; i<dims[0]; i++) { printf (" ["); for (j=0; j<dims[1]; j++) { /* * Get the name of the enumeration member. */ status = H5Tenum_nameof (memtype, &rdata[i][j], name, NAME_BUF_SIZE); printf (" %-6s", name); } printf ("]\n"); } /* * Close and release resources. */ free (rdata[0]); free (rdata); status = H5Dclose (dset); status = H5Sclose (space); status = H5Tclose (memtype); status = H5Fclose (file); return 0; }
/***************************************************************************** This function generates attributes, groups, and datasets of many types. Parameters: fname: file_name. ngrps: number of top level groups. ndsets: number of datasets. attrs: number of attributes. nrow: number of rows in a dataset. chunk: chunk size (single number). vlen: max vlen size. comp: use latest format. latest: use gzip comnpression. Return: Non-negative on success/Negative on failure Programmer: Peter Cao <*****@*****.**>, Jan. 2013 ****************************************************************************/ herr_t create_perf_test_file(const char *fname, int ngrps, int ndsets, int nattrs, hsize_t nrows, hsize_t dim0, hsize_t chunk, int vlen, int compressed, int latest) { int i, j, k; hid_t fid, sid_null, sid_scalar, sid_1d, sid_2d, did, aid, sid_2, sid_large, fapl=H5P_DEFAULT, dcpl=H5P_DEFAULT, gid1, gid2, cmp_tid, tid_str, tid_enum, tid_array_f, tid_vlen_i, tid_vlen_s; char name[32], tmp_name1[32], tmp_name2[32], tmp_name3[32]; hsize_t dims[1]={dim0}, dims2d[2]={dim0, (dim0/4+1)}, dims_array[1]={FIXED_LEN}, dim1[1]={2}; char *enum_names[4] = {"SOLID", "LIQUID", "GAS", "PLASMA"}; test_comp_t *buf_comp=NULL, *buf_comp_large=NULL; int *buf_int=NULL; float (*buf_float_a)[FIXED_LEN]=NULL; double **buf_double2d=NULL; hvl_t *buf_vlen_i=NULL; char (*buf_str)[FIXED_LEN]; char **buf_vlen_s=NULL; hobj_ref_t buf_ref[2]; hdset_reg_ref_t buf_reg_ref[2]; size_t offset, len; herr_t status; char *names[NTYPES] = { "int", "ulong", "float", "double", "fixed string", "enum", "fixed float array", "vlen int array", "vlen strings"}; hid_t types[NTYPES] = { H5T_NATIVE_INT, H5T_NATIVE_UINT64, H5T_NATIVE_FLOAT, H5T_NATIVE_DOUBLE, tid_str, tid_enum, tid_array_f, tid_vlen_i, tid_vlen_s}; hsize_t coords[4][2] = { {0, 1}, {3, 5}, {1, 0}, {2, 4}}, start=0, stride=1, count=1; if (nrows < NROWS) nrows = NROWS; if (ngrps<NGROUPS) ngrps=NGROUPS; if (ndsets<NDSETS) ndsets=NDSETS; if (nattrs<NATTRS) nattrs=NATTRS; if (dim0<DIM0) dim0=DIM0; if (chunk>dim0) chunk=dim0/4; if (chunk<1) chunk = 1; if (vlen<1) vlen = MAXVLEN; /* create fixed string datatype */ types[4] = tid_str = H5Tcopy (H5T_C_S1); H5Tset_size (tid_str, FIXED_LEN); /* create enum datatype */ types[5] = tid_enum = H5Tenum_create(H5T_NATIVE_INT); for (i = (int) SOLID; i <= (int) PLASMA; i++) { phase_t val = (phase_t) i; status = H5Tenum_insert (tid_enum, enum_names[i], &val); } /* create float array datatype */ types[6] = tid_array_f = H5Tarray_create (H5T_NATIVE_FLOAT, 1, dims_array); /* create variable length integer datatypes */ types[7] = tid_vlen_i = H5Tvlen_create (H5T_NATIVE_INT); /* create variable length string datatype */ types[8] = tid_vlen_s = H5Tcopy (H5T_C_S1); H5Tset_size (tid_vlen_s, H5T_VARIABLE); /* create compound datatypes */ cmp_tid = H5Tcreate (H5T_COMPOUND, sizeof (test_comp_t)); offset = 0; for (i=0; i<NTYPES-2; i++) { H5Tinsert(cmp_tid, names[i], offset, types[i]); offset += H5Tget_size(types[i]); } H5Tinsert(cmp_tid, names[7], offset, types[7]); offset += sizeof (hvl_t); H5Tinsert(cmp_tid, names[8], offset, types[8]); /* create dataspace */ sid_1d = H5Screate_simple (1, dims, NULL); sid_2d = H5Screate_simple (2, dims2d, NULL); sid_2 = H5Screate_simple (1, dim1, NULL); sid_large = H5Screate_simple (1, &nrows, NULL); sid_null = H5Screate (H5S_NULL); sid_scalar = H5Screate (H5S_SCALAR); /* create fid access property */ fapl = H5Pcreate (H5P_FILE_ACCESS); H5Pset_libver_bounds (fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST); /* create dataset creation property */ dcpl = H5Pcreate (H5P_DATASET_CREATE); /* set dataset chunk */ if (chunk>0) { H5Pset_chunk (dcpl, 1, &chunk); } /* set dataset compression */ if (compressed) { if (chunk<=0) { chunk = dim0/10+1;; H5Pset_chunk (dcpl, 1, &chunk); } H5Pset_shuffle (dcpl); H5Pset_deflate (dcpl, 6); } /* allocate buffers */ buf_comp = (test_comp_t *)calloc(dim0, sizeof(test_comp_t)); buf_comp_large = (test_comp_t *)calloc(nrows, sizeof(test_comp_t)); buf_int = (int *)calloc(dim0, sizeof(int)); buf_float_a = malloc(dim0*sizeof(*buf_float_a)); buf_vlen_i = (hvl_t *)calloc(dim0, sizeof (hvl_t)); buf_vlen_s = (char **)calloc(dim0, sizeof(char *)); buf_str = malloc(dim0*sizeof (*buf_str)); /* allocate array of doulbe pointers */ buf_double2d = (double **)calloc(dims2d[0],sizeof(double *)); /* allocate a contigous chunk of memory for the data */ buf_double2d[0] = (double *)calloc( dims2d[0]*dims2d[1],sizeof(double) ); /* assign memory city to pointer array */ for (i=1; i <dims2d[0]; i++) buf_double2d[i] = buf_double2d[0]+i*dims2d[1]; /* fill buffer values */ len = 1; for (i=0; i<dims[0]; i++) { buf_comp[i].i = buf_int[i] = i-2147483648; buf_comp[i].l = 0xffffffffffffffff-i; buf_comp[i].f = 1.0/(i+1.0); buf_comp[i].d = 987654321.0*i+1.0/(i+1.0); buf_comp[i].e = (phase_t) (i % (int) (PLASMA + 1)); for (j=0; j<FIXED_LEN; j++) { buf_comp[i].f_array[j] = buf_float_a[i][j] = i*100+j; buf_str[i][j] = 'a' + (i%26); } buf_str[i][FIXED_LEN-1] = 0; strcpy(buf_comp[i].s, buf_str[i]); len = (1-cos(i/8.0))/2*vlen+1; if (!i) len = vlen; buf_vlen_i[i].len = len; buf_vlen_i[i].p = (int *)calloc(len, sizeof(int)); for (j=0; j<len; j++) ((int*)(buf_vlen_i[i].p))[j] = i*100+j; buf_comp[i].i_vlen = buf_vlen_i[i]; buf_vlen_s[i] = (char *)calloc(len, sizeof(char)); for (j=0; j<len-1; j++) buf_vlen_s[i][j] = j%26+'A'; buf_comp[i].s_vlen = buf_vlen_s[i]; for (j=0; j<dims2d[1]; j++) buf_double2d[i][j] = i+j/10000.0; } for (i=0; i<nrows; i++) { buf_comp_large[i].i = i-2147483648; buf_comp_large[i].l = 0xffffffffffffffff-i; buf_comp_large[i].f = 1.0/(i+1.0); buf_comp_large[i].d = 987654321.0*i+1.0/(i+1.0); buf_comp_large[i].e = (phase_t) (i % (int) (PLASMA + 1)); for (j=0; j<FIXED_LEN-1; j++) { buf_comp_large[i].f_array[j] = i*100+j; buf_comp_large[i].s[j] = 'a' + (i%26); } len = i%vlen+1; buf_comp_large[i].i_vlen.len = len; buf_comp_large[i].i_vlen.p = (int *)calloc(len, sizeof(int)); for (j=0; j<len; j++) ((int*)(buf_comp_large[i].i_vlen.p))[j] = i*100+j; buf_comp_large[i].s_vlen = (char *)calloc(i+2, sizeof(char)); for (j=0; j<i+1; j++) (buf_comp_large[i].s_vlen)[j] = j%26+'A'; } /* create file */ if (latest) fid = H5Fcreate (fname, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); else fid = H5Fcreate (fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); add_attrs(fid, 0); sprintf(name, "a cmp ds of %d rows", nrows); did = H5Dcreate (fid, name, cmp_tid, sid_large, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, cmp_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_comp_large); add_attrs(did, 0); H5Dclose(did); // /* add attributes*/ gid1 = H5Gcreate (fid, "attributes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (nattrs<1) nattrs = 1; i=0; while (i<nattrs) i += add_attrs(gid1, i); H5Gclose(gid1); /* add many sub groups to a group*/ gid1 = H5Gcreate (fid, "groups", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); add_attrs(gid1, 0); for (i=0; i<ngrps; i++) { /* create sub groups */ sprintf(name, "g%02d", i); gid2 = H5Gcreate (gid1, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (i<10) add_attrs(gid2, 0); H5Gclose(gid2); } H5Gclose(gid1); /* add many datasets to a group */ gid1 = H5Gcreate (fid, "datasets", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); add_attrs(gid1, 0); for (j=0; j<ndsets; j+=12) { /* 1 add a null dataset */ sprintf(name, "%05d null dataset", j); did = H5Dcreate (gid1, name, H5T_STD_I32LE, sid_null, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (!j) add_attrs(did, j); H5Dclose(did); /* 2 add scalar int point */ sprintf(name, "%05d scalar int point", j); did = H5Dcreate (gid1, name, H5T_NATIVE_INT, sid_scalar, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &j); if (!j) add_attrs(did, j); H5Dclose(did); /* 3 scalar vlen string */ sprintf(name, "%05d scalar vlen string", j); did = H5Dcreate (gid1, name, tid_vlen_s, sid_scalar, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, tid_vlen_s, H5S_ALL, H5S_ALL, H5P_DEFAULT, &buf_vlen_s[0]); if (!j) add_attrs(did, j); H5Dclose(did); /* 4 add fixed-length float array */ sprintf(name, "%05d fixed-length float array", j); did = H5Dcreate (gid1, name, tid_array_f, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_array_f, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_float_a); if (!j) add_attrs(did, j); H5Dclose(did); /* 5 add fixed-length strings */ sprintf(name, "%05d fixed-length strings", j); did = H5Dcreate (gid1, name, tid_str, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_str, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_str); if (!j) add_attrs(did, j); H5Dclose(did); /* 6 add compound data */ sprintf(name, "%05d compund data", j); did = H5Dcreate (gid1, name, cmp_tid, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, cmp_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_comp); if (!j) add_attrs(did, j); H5Dclose(did); /* 7 add 2D double */ sprintf(name, "%05d 2D double", j); strcpy (tmp_name1, name); did = H5Dcreate (gid1, name, H5T_NATIVE_DOUBLE, sid_2d, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_double2d[0]); if (!j) add_attrs(did, j); H5Dclose(did); /* 8 add 1D int array */ sprintf(name, "%05d 1D int array", j); did = H5Dcreate (gid1, name, H5T_NATIVE_INT, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_int); if (!j) add_attrs(did, j); H5Dclose(did); /* 9 add vlen int array */ sprintf(name, "%05d vlen int array", j); strcpy (tmp_name2, name); did = H5Dcreate (gid1, name, tid_vlen_i, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_vlen_i, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_vlen_i); if (!j) add_attrs(did, j); H5Dclose(did); /* 10 add vlen strings */ sprintf(name, "%05d vlen strings", j); strcpy (tmp_name3, name); did = H5Dcreate (gid1, name, tid_vlen_s, sid_1d, H5P_DEFAULT, dcpl, H5P_DEFAULT); H5Dwrite (did, tid_vlen_s, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_vlen_s); if (!j) add_attrs(did, j); H5Dclose(did); /* 11 add object refs */ H5Rcreate(&buf_ref[0],gid1, ".", H5R_OBJECT, -1); H5Rcreate(&buf_ref[1],gid1, tmp_name3, H5R_OBJECT, -1); sprintf(name, "%05d obj refs", j); did = H5Dcreate (gid1, name, H5T_STD_REF_OBJ, sid_2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_ref); if (!j) add_attrs(did, j); H5Dclose(did); /* 12 add region refs */ H5Sselect_elements (sid_2d, H5S_SELECT_SET, 4, coords[0]); H5Rcreate(&buf_reg_ref[0],gid1, tmp_name1, H5R_DATASET_REGION, sid_2d); H5Sselect_none(sid_2d); count = dims[0]/2+1; H5Sselect_hyperslab (sid_1d, H5S_SELECT_SET, &start, &stride, &count,NULL); H5Rcreate(&buf_reg_ref[1],gid1, tmp_name2, H5R_DATASET_REGION, sid_1d); H5Sselect_none(sid_1d); sprintf(name, "%05d region refs", j); did = H5Dcreate (gid1, name, H5T_STD_REF_DSETREG, sid_2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite (did, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf_reg_ref); if (!j) add_attrs(did, j); H5Dclose(did); } H5Gclose(gid1); H5Tclose (tid_array_f); H5Tclose (tid_vlen_i); H5Tclose (tid_vlen_s); H5Tclose (tid_enum); H5Tclose (tid_str); H5Tclose (cmp_tid); H5Pclose (dcpl); H5Pclose (fapl); H5Sclose (sid_1d); H5Sclose (sid_2d); H5Sclose (sid_2); H5Sclose (sid_large); H5Sclose (sid_null); H5Sclose (sid_scalar); H5Fclose (fid); for (i=0; i<dims[0]; i++) { if (buf_vlen_i[i].p) free(buf_vlen_i[i].p); if (buf_vlen_s[i]) free(buf_vlen_s[i]); } for (i=0; i<nrows; i++) { if (buf_comp_large[i].i_vlen.p) free(buf_comp_large[i].i_vlen.p); if (buf_comp_large[i].s_vlen) free(buf_comp_large[i].s_vlen); } free (buf_comp); free (buf_comp_large); free (buf_int); free (buf_float_a); free (buf_double2d[0]); free (buf_double2d); free (buf_str); free(buf_vlen_i); free(buf_vlen_s); return 0; }
/*------------------------------------------------------------------------- * Function: test_named * * Purpose: Create an enumeration data type and store it in the file. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Wednesday, December 23, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int test_named(hid_t file) { hid_t type=-1, cwg=-1; c_e1 val; signed char val8; TESTING("named enumeration types"); if ((cwg=H5Gcreate(file, "test_named", 0))<0) goto error; /* A native integer */ if ((type = H5Tcreate(H5T_ENUM, sizeof(c_e1)))<0) goto error; if (H5Tenum_insert(type, "RED", CPTR(val, E1_RED ))<0) goto error; if (H5Tenum_insert(type, "GREEN", CPTR(val, E1_GREEN))<0) goto error; if (H5Tenum_insert(type, "BLUE", CPTR(val, E1_BLUE ))<0) goto error; if (H5Tenum_insert(type, "WHITE", CPTR(val, E1_WHITE))<0) goto error; if (H5Tenum_insert(type, "BLACK", CPTR(val, E1_BLACK))<0) goto error; if (H5Tcommit(cwg, "e1_a", type)<0) goto error; if (H5Tclose(type)<0) goto error; /* A smaller type */ if ((type = H5Tcreate(H5T_ENUM, 1))<0) goto error; if (H5Tenum_insert(type, "RED", CPTR(val8, E1_RED ))<0) goto error; if (H5Tenum_insert(type, "GREEN", CPTR(val8, E1_GREEN))<0) goto error; if (H5Tenum_insert(type, "BLUE", CPTR(val8, E1_BLUE ))<0) goto error; if (H5Tenum_insert(type, "WHITE", CPTR(val8, E1_WHITE))<0) goto error; if (H5Tenum_insert(type, "BLACK", CPTR(val8, E1_BLACK))<0) goto error; if (H5Tcommit(cwg, "e1_b", type)<0) goto error; if (H5Tclose(type)<0) goto error; /* A non-native type */ if (H5T_ORDER_BE==H5Tget_order(H5T_NATIVE_INT)) { if ((type = H5Tenum_create(H5T_STD_U8LE))<0) goto error; } else { if ((type = H5Tenum_create(H5T_STD_U8BE))<0) goto error; } if (H5Tenum_insert(type, "RED", CPTR(val8, E1_RED ))<0) goto error; if (H5Tenum_insert(type, "GREEN", CPTR(val8, E1_GREEN))<0) goto error; if (H5Tenum_insert(type, "BLUE", CPTR(val8, E1_BLUE ))<0) goto error; if (H5Tenum_insert(type, "WHITE", CPTR(val8, E1_WHITE))<0) goto error; if (H5Tenum_insert(type, "BLACK", CPTR(val8, E1_BLACK))<0) goto error; if (H5Tcommit(cwg, "e1_c", type)<0) goto error; if (H5Tclose(type)<0) goto error; if (H5Gclose(cwg)<0) goto error; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Tclose(type); H5Gclose(cwg); } H5E_END_TRY; return 1; }
/*------------------------------------------------------------------------- * Function: test_tr2 * * Purpose: Tests conversions that use the O(log N) lookup function. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Tuesday, January 5, 1999 * * Modifications: * *------------------------------------------------------------------------- */ static int test_tr2(hid_t file) { hid_t cwg=-1, m_type=-1, f_type=-1, space=-1, dset=-1; hsize_t ds_size[1]={10}; size_t i; c_e1 val1; int val2; static c_e1 data1[10]={E1_RED, E1_GREEN, E1_BLUE, E1_GREEN, E1_WHITE, E1_WHITE, E1_BLACK, E1_GREEN, E1_BLUE, E1_RED}; c_e1 data2[10]; TESTING("O(log N) converions"); if ((cwg=H5Gcreate(file, "test_tr2", 0))<0) goto error; if ((m_type = H5Tcreate(H5T_ENUM, sizeof(c_e1)))<0) goto error; if (H5Tenum_insert(m_type, "RED", CPTR(val1, E1_RED ))<0) goto error; if (H5Tenum_insert(m_type, "GREEN", CPTR(val1, E1_GREEN))<0) goto error; if (H5Tenum_insert(m_type, "BLUE", CPTR(val1, E1_BLUE ))<0) goto error; if (H5Tenum_insert(m_type, "WHITE", CPTR(val1, E1_WHITE))<0) goto error; if (H5Tenum_insert(m_type, "BLACK", CPTR(val1, E1_BLACK))<0) goto error; if ((f_type = H5Tcreate(H5T_ENUM, sizeof(int)))<0) goto error; if (H5Tenum_insert(f_type, "RED", CPTR(val2, 1050))<0) goto error; if (H5Tenum_insert(f_type, "GREEN", CPTR(val2, 1040))<0) goto error; if (H5Tenum_insert(f_type, "BLUE", CPTR(val2, 1030))<0) goto error; if (H5Tenum_insert(f_type, "WHITE", CPTR(val2, 1020))<0) goto error; if (H5Tenum_insert(f_type, "BLACK", CPTR(val2, 1010))<0) goto error; if ((space=H5Screate_simple(1, ds_size, NULL))<0) goto error; if ((dset=H5Dcreate(cwg, "color_table", f_type, space, H5P_DEFAULT))<0) goto error; if (H5Dwrite(dset, m_type, space, space, H5P_DEFAULT, data1)<0) goto error; if (H5Dread(dset, m_type, space, space, H5P_DEFAULT, data2)<0) goto error; for (i=0; i<ds_size[0]; i++) { if (data1[i]!=data2[i]) { H5_FAILED(); printf(" data1[%lu]=%d, data2[%lu]=%d (should be same)\n", (unsigned long)i, (int)(data1[i]), (unsigned long)i, (int)(data2[i])); goto error; } } if (H5Dclose(dset)<0) goto error; if (H5Sclose(space)<0) goto error; if (H5Tclose(m_type)<0) goto error; if (H5Tclose(f_type)<0) goto error; if (H5Gclose(cwg)<0) goto error; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Dclose(dset); H5Sclose(space); H5Tclose(m_type); H5Tclose(f_type); H5Gclose(cwg); } H5E_END_TRY; return 1; }
mhdf_FileHandle mhdf_createFile( const char* filename, int overwrite, const char** elem_type_list, size_t elem_list_len, hid_t id_type, mhdf_Status* status ) { FileHandle* file_ptr; unsigned int flags; unsigned char idx; size_t i; hid_t enum_id, group_id; int rval; API_BEGIN; if (elem_list_len > 255) { mhdf_setFail( status, "Element type list too long." ); return NULL; } mhdf_setOkay( status ); /* Create struct to hold working data */ file_ptr = mhdf_alloc_FileHandle( 0, id_type, status ); if (!file_ptr) return NULL; /* Create the file */ flags = overwrite ? H5F_ACC_TRUNC : H5F_ACC_EXCL; file_ptr->hdf_handle = H5Fcreate( filename, flags, H5P_DEFAULT, H5P_DEFAULT ); if (file_ptr->hdf_handle < 0) { mhdf_setFail( status, "Failed to create file \"%s\"", filename ); free( file_ptr ); return NULL; } /* Create file structure */ if (!make_hdf_group( ROOT_GROUP, file_ptr->hdf_handle, 6, status ) || !make_hdf_group( TAG_GROUP, file_ptr->hdf_handle, 0, status ) || !make_hdf_group( ELEMENT_GROUP, file_ptr->hdf_handle, 8, status ) || !make_hdf_group( NODE_GROUP, file_ptr->hdf_handle, 3, status ) || !make_hdf_group( SET_GROUP, file_ptr->hdf_handle, 5, status ) || !make_hdf_group( NODE_TAG_GROUP, file_ptr->hdf_handle, 0, status ) || !make_hdf_group( SET_TAG_GROUP, file_ptr->hdf_handle, 0, status )) { H5Fclose( file_ptr->hdf_handle ); free( file_ptr ); return NULL; } /* Store the max ID as an attribite on the /tstt/ group */ #if defined(H5Gopen_vers) && H5Gopen_vers > 1 group_id = H5Gopen2( file_ptr->hdf_handle, ROOT_GROUP, H5P_DEFAULT ); #else group_id = H5Gopen( file_ptr->hdf_handle, ROOT_GROUP ); #endif rval = mhdf_create_scalar_attrib( group_id, MAX_ID_ATTRIB, H5T_NATIVE_ULONG, &file_ptr->max_id, status ); H5Gclose( group_id ); if (!rval) { H5Fclose( file_ptr->hdf_handle ); free( file_ptr ); return NULL; } /* Create the type name list in file */ enum_id = H5Tenum_create( H5T_NATIVE_UCHAR ); if (enum_id < 0) { mhdf_setFail( status, "Failed to store elem type list." ); H5Fclose( file_ptr->hdf_handle ); free( file_ptr ); return NULL; } for (i = 0; i < elem_list_len; ++i) { if (!elem_type_list[i] || !*elem_type_list[i]) continue; idx = (unsigned char)i; if ( H5Tenum_insert( enum_id, elem_type_list[i], &idx ) < 0) { mhdf_setFail( status, "Failed to store elem type list." ); H5Fclose( file_ptr->hdf_handle ); free( file_ptr ); return NULL; } } #if defined(H5Tcommit_vers) && H5Tcommit_vers > 1 if (H5Tcommit2( file_ptr->hdf_handle, TYPE_ENUM_PATH, enum_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ) < 0) #else if (H5Tcommit( file_ptr->hdf_handle, TYPE_ENUM_PATH, enum_id ) < 0) #endif { mhdf_setFail( status, "Failed to store elem type list." ); H5Fclose( file_ptr->hdf_handle ); free( file_ptr ); return NULL; } H5Tclose( enum_id ); API_END_H( 1 ); return file_ptr; }
AccessTraceWriter::AccessTraceWriter(g_string _fname, uint32_t numChildren) : fname(_fname) { // Create record structure hid_t accType = H5Tenum_create(H5T_NATIVE_USHORT); uint16_t val; H5Tenum_insert(accType, "GETS", (val=GETS,&val)); H5Tenum_insert(accType, "GETX", (val=GETX,&val)); H5Tenum_insert(accType, "PUTS", (val=PUTS,&val)); H5Tenum_insert(accType, "PUTX", (val=PUTX,&val)); size_t offset = 0; size_t size = H5Tget_size(H5T_NATIVE_ULONG)*2 + H5Tget_size(H5T_NATIVE_UINT) + H5Tget_size(H5T_NATIVE_USHORT) + H5Tget_size(accType); hid_t recType = H5Tcreate(H5T_COMPOUND, size); auto insertType = [&](const char* name, hid_t type) { H5Tinsert(recType, name, offset, type); offset += H5Tget_size(type); }; insertType("lineAddr", H5T_NATIVE_ULONG); insertType("cycle", H5T_NATIVE_ULONG); insertType("lat", H5T_NATIVE_UINT); insertType("childId", H5T_NATIVE_USHORT); insertType("accType", accType); hid_t fid = H5Fcreate(fname.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); if (fid == H5I_INVALID_HID) panic("Could not create HDF5 file %s", fname.c_str()); // HACK: We want to use the SHUF filter... create the raw dataset instead of the packet table // hid_t table = H5PTcreate_fl(fid, "accs", recType, PT_CHUNKSIZE, 9); // if (table == H5I_INVALID_HID) panic("Could not create HDF5 packet table"); hsize_t dims[1] = {0}; hsize_t dims_chunk[1] = {PT_CHUNKSIZE}; hsize_t maxdims[1] = {H5S_UNLIMITED}; hid_t space_id = H5Screate_simple(1, dims, maxdims); hid_t plist_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(plist_id, 1, dims_chunk); H5Pset_shuffle(plist_id); H5Pset_deflate(plist_id, 9); hid_t table = H5Dcreate2(fid, "accs", recType, space_id, H5P_DEFAULT, plist_id, H5P_DEFAULT); if (table == H5I_INVALID_HID) panic("Could not create HDF5 dataset"); H5Dclose(table); // info("%ld %ld %ld %ld", sizeof(PackedAccessRecord), size, offset, H5Tget_size(recType)); assert(offset == size); assert(size == sizeof(PackedAccessRecord)); hid_t ncAttr = H5Acreate2(fid, "numChildren", H5T_NATIVE_UINT, H5Screate(H5S_SCALAR), H5P_DEFAULT, H5P_DEFAULT); H5Awrite(ncAttr, H5T_NATIVE_UINT, &numChildren); H5Aclose(ncAttr); hid_t fAttr = H5Acreate2(fid, "finished", H5T_NATIVE_UINT, H5Screate(H5S_SCALAR), H5P_DEFAULT, H5P_DEFAULT); uint32_t finished = 0; H5Awrite(fAttr, H5T_NATIVE_UINT, &finished); H5Aclose(fAttr); H5Fclose(fid); // Initialize buffer buf = gm_calloc<PackedAccessRecord>(PT_CHUNKSIZE); cur = 0; max = PT_CHUNKSIZE; assert((uint32_t)(((char*) &buf[1]) - ((char*) &buf[0])) == sizeof(PackedAccessRecord)); }