#include "hdf5.h" #define FILE "SDScompound.h5" #define DATASETNAME "ArrayOfStructures" #define LENGTH 1 #define RANK 1 #define ITER 100000 int main(void) { /* First structure and dataset*/ typedef struct s1_t { int a; float b; double c; } s1_t; s1_t s1[LENGTH]; hid_t s1_tid; /* File datatype identifier */ int i=0; hid_t file, dataset, space, filespace, cparms; /* Handles */ herr_t status; hsize_t dim[] = { LENGTH }; /* Dataspace dimensions */ hsize_t offset[LENGTH], size[LENGTH]; /* Dataspace dimensions */ hsize_t maxdims[] = { H5S_UNLIMITED }; hsize_t chunk_dims[] = { 1024 }; // what is the best number if I am reading/writing one row at a time /* * Initialize the data */ s1[i].a = 10; s1[i].b = 15; s1[i].c = 99.99; /* * Create the data space. */ space = H5Screate_simple (RANK, dim, maxdims); /* * Create the file. */ file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Modify dataset creation properties, i.e. enable chunking */ cparms = H5Pcreate (H5P_DATASET_CREATE); status = H5Pset_chunk ( cparms, RANK, chunk_dims); /* * Create the memory data type. */ s1_tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t)); H5Tinsert(s1_tid, "a_name", HOFFSET(s1_t, a), H5T_NATIVE_INT); H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE); H5Tinsert(s1_tid, "b_name", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT); /* * Create the dataset. */ dataset = H5Dcreate(file, DATASETNAME, s1_tid, space, H5P_DEFAULT,cparms, H5P_DEFAULT); /* Extend the dataset to the orig dimension */ size[0] = dim[0]; status = H5Dextend (dataset, size); /* Select a hyperslab */ filespace = H5Dget_space (dataset); offset[0] = 0; status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, dim, NULL); /* Write the data to the hyperslab */ status = H5Dwrite (dataset, s1_tid,space, filespace, H5P_DEFAULT, s1); for (i = 0; i < ITER; ++i) { /* Extend the dataset. Add one more row */ ++size[0]; // increase the row size by 1 status = H5Dextend (dataset, size); /* Select a hyperslab */ filespace = H5Dget_space (dataset); offset[0] = size[0] - 1; // offset starts at 0 status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, dim, NULL); space = H5Screate_simple (RANK, dim, NULL); status = H5Dwrite (dataset, s1_tid, space, filespace, H5P_DEFAULT, s1); H5Sclose(space); // status = H5Fflush(file, H5F_SCOPE_GLOBAL); // program still brings down the system w/ or w/o flush } /* * Release resources */ H5Tclose(s1_tid); H5Dclose(dataset); H5Fclose(file); return 0; }