diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index 0295cd0..2d37b9e 100644 --- a/src/templates_hdf5/templator_hdf5.org +++ b/src/templates_hdf5/templator_hdf5.org @@ -498,12 +498,26 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, // get the dataspace of the dataset hid_t fspace_id = H5Dget_space(dset_id); - // possible overflow HERE ? + // TODO: check for possible overflow HERE ? hsize_t offset[1] = {(hsize_t) offset_file*4}; hsize_t count[1] = {(hsize_t) size*4}; - herr_t status = H5Sselect_hyperslab(fspace_id, H5S_SELECT_SET, offset, NULL, count, NULL); + /* 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) + ,*/ + hsize_t ddims[1] = {0}; + int rrank = H5Sget_simple_extent_dims(fspace_id, ddims, NULL); + hsize_t max_offset = offset[0] + count[0]; + + int eof_reachable = 0; + if (max_offset > ddims[0]) { + eof_reachable = 1; + // lower the value of count to reduce the number of elements which will be read + count[0] -= max_offset - ddims[0]; + } + + herr_t status = H5Sselect_hyperslab(fspace_id, H5S_SELECT_SET, offset, NULL, count, NULL); hid_t memspace_id = H5Screate_simple(1, count, NULL); status = H5Dread(dset_id, H5T_NATIVE_INT32, memspace_id, fspace_id, H5P_DEFAULT, index_read); @@ -514,6 +528,8 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, assert (status >= 0); + if (eof_reachable == 1) return TREXIO_END; + return TREXIO_SUCCESS; } #+end_src diff --git a/tests/io_dset_sparse_hdf5.c b/tests/io_dset_sparse_hdf5.c index 402a8de..53ef81d 100644 --- a/tests/io_dset_sparse_hdf5.c +++ b/tests/io_dset_sparse_hdf5.c @@ -122,7 +122,7 @@ static int test_read_dset_sparse (const char* file_name, const back_end_t backen // specify the read parameters, here: // 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5 - int64_t chunk_read = 30L; + int64_t chunk_read = 10L; int64_t offset_file_read = 40L; int offset_data_read = 5; @@ -133,7 +133,6 @@ static int test_read_dset_sparse (const char* file_name, const back_end_t backen assert(index_read[4*offset_data_read] == offset_file_read*4); // now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max) - /* offset_file_read = 97L; offset_data_read = 1; @@ -142,11 +141,6 @@ static int test_read_dset_sparse (const char* file_name, const back_end_t backen assert(rc == TREXIO_END); assert(index_read[4*size_r-1] == 0); assert(index_read[4*offset_data_read] == 4 * (int32_t) offset_file_read); - */ - - for (int i=0; i