From 1c16b75992fd6c5584de67dec6205e2daa392e29 Mon Sep 17 00:00:00 2001 From: q-posev Date: Fri, 10 Dec 2021 16:02:38 +0100 Subject: [PATCH] adapt HDF5 back end to be generated for an arbitrary number of indices --- src/templates_front/templator_front.org | 4 ++-- src/templates_hdf5/templator_hdf5.org | 29 +++++++++++-------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index e141e4f..4bbb6b0 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -2476,7 +2476,7 @@ trexio_read_$group_dset$(trexio_t* const file, // shift indices to be one-based if Fortran API is used if (file->one_based) { // if EOF is reached - shift only indices that have been read, not an entire buffer - uint64_t index_size = (rc == TREXIO_END) ? (4UL*eof_read_size) : (4UL*buffer_size) ; + uint64_t index_size = (rc == TREXIO_END) ? (rank*eof_read_size) : (rank*buffer_size) ; for (uint64_t i=0; ione_based) { - uint64_t index_size = 4UL*buffer_size; + uint64_t index_size = rank * buffer_size; index_sparse_p = CALLOC(index_size, int32_t); if (index_sparse_p == NULL) return TREXIO_ALLOCATION_FAILED; diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index 1e28a6a..72a91d6 100644 --- a/src/templates_hdf5/templator_hdf5.org +++ b/src/templates_hdf5/templator_hdf5.org @@ -399,16 +399,15 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, trexio_hdf5_t* f = (trexio_hdf5_t*) file; - const uint32_t rank = 1; // 4; - const hsize_t chunk_dims[1] = {size*4}; //[4] = {size, size, size, size}; - // TODO: generator - hsize_t maxdims[1] = {H5S_UNLIMITED}; // [4] = {H5S_UNLIMITED, H5S_UNLIMITED, H5S_UNLIMITED, H5S_UNLIMITED}; + const uint32_t h5_rank = 1; + const hsize_t chunk_dims[1] = {size * $group_dset_rank$}; + const hsize_t maxdims[1] = {H5S_UNLIMITED}; if ( H5LTfind_dataset(f->$group$_group, $GROUP_DSET$_NAME) != 1 ) { - hid_t dspace = H5Screate_simple(rank, chunk_dims, maxdims); + hid_t dspace = H5Screate_simple(h5_rank, chunk_dims, maxdims); hid_t prop = H5Pcreate(H5P_DATASET_CREATE); - herr_t status = H5Pset_chunk(prop, rank, chunk_dims); + herr_t status = H5Pset_chunk(prop, h5_rank, chunk_dims); hid_t dset_id = H5Dcreate(f->$group$_group, $GROUP_DSET$_NAME, @@ -436,9 +435,8 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, hid_t dset_id = H5Dopen(f->$group$_group, $GROUP_DSET$_NAME, H5P_DEFAULT); if (dset_id <= 0) return TREXIO_INVALID_ID; - hid_t fspace = H5Dget_space(dset_id); - hsize_t offset[1] = {(hsize_t) offset_file*4}; //[4] = {offset_file, offset_file, offset_file, offset_file}; + hsize_t offset[1] = {(hsize_t) offset_file * $group_dset_rank$}; // allocate space for the dimensions to be read hsize_t ddims[1] = {0}; @@ -455,7 +453,7 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, fspace = H5Dget_space(dset_id); status = H5Sselect_hyperslab(fspace, H5S_SELECT_SET, offset, NULL, chunk_dims, NULL); - hid_t dspace = H5Screate_simple(rank, chunk_dims, NULL); + hid_t dspace = H5Screate_simple(h5_rank, chunk_dims, NULL); status = H5Dwrite(dset_id, H5T_NATIVE_INT32, @@ -472,7 +470,6 @@ trexio_hdf5_write_$group_dset$ (trexio_t* const file, } return TREXIO_SUCCESS; - } #+end_src @@ -501,8 +498,8 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, hid_t fspace_id = H5Dget_space(dset_id); // TODO: check for possible overflow HERE ? - hsize_t offset[1] = {(hsize_t) offset_file*4}; - hsize_t count[1] = {(hsize_t) size*4}; + hsize_t offset[1] = {(hsize_t) offset_file * $group_dset_rank$}; + hsize_t count[1] = {(hsize_t) size * $group_dset_rank$}; /* get dimensions of the dataset in the file to check whether reading with user-provided chunk size will reach end of the dataset (i.e. EOF in TEXT back end) @@ -518,7 +515,7 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, // lower the value of count to reduce the number of elements which will be read count[0] -= max_offset - ddims[0]; // modify the eof_read_size accordingly - *eof_read_size = (uint64_t) (count[0] / 4UL); + *eof_read_size = (uint64_t) (count[0] / $group_dset_rank$UL); } herr_t status = H5Sselect_hyperslab(fspace_id, H5S_SELECT_SET, offset, NULL, count, NULL); @@ -562,11 +559,11 @@ trexio_hdf5_read_$group_dset$_size (trexio_t* const file, int64_t* const size_ma H5Dclose(dset_id); H5Sclose(fspace_id); - int mod_4 = (int) (ddims[0] % 4); + int mod_$group_dset_rank$ = (int) (ddims[0] % $group_dset_rank$); - if (mod_4 != 0) return TREXIO_FAILURE; + if (mod_$group_dset_rank$ != 0) return TREXIO_FAILURE; - *size_max = ((int64_t) ddims[0]) / 4L; + *size_max = ((int64_t) ddims[0]) / $group_dset_rank$L; return TREXIO_SUCCESS; }