From fafb1552282da19a0434769de3ea13b37c277010 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 16 Dec 2021 11:05:57 +0100 Subject: [PATCH] return the number of read elements if read_sparse reached EOF --- src/templates_front/templator_front.org | 28 +++++++++++++------------ tests/io_dset_sparse_hdf5.c | 16 +++++++++++--- tests/io_dset_sparse_text.c | 9 ++++++-- tests/test_f.f90 | 10 +++++++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index e7a2d3e..aa98d8c 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -2413,7 +2413,7 @@ def has_$group_dset$(trexio_file) -> bool: #+begin_src c :tangle hrw_dset_sparse_front.h :exports none trexio_exit_code trexio_has_$group_dset$(trexio_t* const file); -trexio_exit_code trexio_read_$group_dset$(trexio_t* const file, const int64_t offset_file, const int64_t buffer_size, int32_t* const index_sparse, double* const value_sparse); +trexio_exit_code trexio_read_$group_dset$(trexio_t* const file, const int64_t offset_file, int64_t* const buffer_size, int32_t* const index_sparse, double* const value_sparse); trexio_exit_code trexio_read_$group_dset$_size(trexio_t* const file, int64_t* const size_max); trexio_exit_code trexio_write_$group_dset$(trexio_t* const file, const int64_t offset_file, const int64_t buffer_size, const int32_t* index_sparse, const double* value_sparse); #+end_src @@ -2424,14 +2424,14 @@ trexio_exit_code trexio_write_$group_dset$(trexio_t* const file, const int64_t o trexio_exit_code trexio_read_$group_dset$(trexio_t* const file, const int64_t offset_file, - const int64_t buffer_size, + int64_t* const buffer_size, int32_t* const index_sparse, double* const value_sparse ) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (offset_file < 0L) return TREXIO_INVALID_ARG_2; - if (buffer_size <= 0L) return TREXIO_INVALID_ARG_3; + if (*buffer_size <= 0L) return TREXIO_INVALID_ARG_3; if (index_sparse == NULL) return TREXIO_INVALID_ARG_4; if (value_sparse == NULL) return TREXIO_INVALID_ARG_5; if (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) return TREXIO_DSET_MISSING; @@ -2455,12 +2455,12 @@ trexio_read_$group_dset$(trexio_t* const file, switch (file->back_end) { case TREXIO_TEXT: - rc = trexio_text_read_$group_dset$(file, offset_file, buffer_size, size_max, &eof_read_size, index_sparse, value_sparse); + rc = trexio_text_read_$group_dset$(file, offset_file, *buffer_size, size_max, &eof_read_size, index_sparse, value_sparse); break; case TREXIO_HDF5: #ifdef HAVE_HDF5 - rc = trexio_hdf5_read_$group_dset$(file, offset_file, buffer_size, num, &eof_read_size, index_sparse, value_sparse); + rc = trexio_hdf5_read_$group_dset$(file, offset_file, *buffer_size, num, &eof_read_size, index_sparse, value_sparse); break; #else rc = TREXIO_BACK_END_MISSING; @@ -2476,10 +2476,12 @@ trexio_read_$group_dset$(trexio_t* const file, if (rc != TREXIO_SUCCESS && rc != TREXIO_END) return rc; + if (rc == TREXIO_END) *buffer_size = eof_read_size; + // 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) ? (rank*eof_read_size) : (rank*buffer_size) ; + uint64_t index_size = rank*(*buffer_size) ; for (uint64_t i=0; i size_max) - offset_file_read = 97L; + offset_file_read = 97; offset_data_read = 1; + int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100 if (offset != 0L) offset_file_read += offset; // read one chunk that will reach EOF and return TREXIO_END code - rc = trexio_read_mo_2e_int_eri(file, offset_file_read, chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]); + rc = trexio_read_mo_2e_int_eri(file, offset_file_read, &chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]); assert(rc == TREXIO_END); + assert(chunk_read == eof_read_size_check); assert(index_read[4*size_r-1] == 0); assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset)); + /* + for(int i=0; i size_max) offset_file_read = 97L; offset_data_read = 1; + int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100 if (offset != 0L) offset_file_read += offset; // read one chunk that will reach EOF and return TREXIO_END code - rc = trexio_read_mo_2e_int_eri(file, offset_file_read, chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]); + rc = trexio_read_mo_2e_int_eri(file, offset_file_read, &chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]); assert(rc == TREXIO_END); + assert(chunk_read == eof_read_size_check); assert(index_read[4*size_r-1] == 0); assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset)); diff --git a/tests/test_f.f90 b/tests/test_f.f90 index 01fc89a..dcadec8 100644 --- a/tests/test_f.f90 +++ b/tests/test_f.f90 @@ -200,6 +200,7 @@ subroutine test_read(file_name, back_end) integer(4) :: index_sparse_mo_2e_int_eri(4,20) double precision :: value_sparse_mo_2e_int_eri(20) integer(8) :: read_buf_size = 10 + integer(8) :: read_buf_size_save = 10 integer(8) :: offset_read = 40 integer(8) :: offset_data_read = 5 integer(8) :: offset_eof = 97 @@ -282,6 +283,9 @@ subroutine test_read(file_name, back_end) rc = trexio_read_mo_2e_int_eri(trex_file, offset_read, read_buf_size, & index_sparse_mo_2e_int_eri(1, offset_data_read + 1), & value_sparse_mo_2e_int_eri(offset_data_read + 1)) + !do i = 1,20 + ! write(*,*) index_sparse_mo_2e_int_eri(1,i) + !enddo call trexio_assert(rc, TREXIO_SUCCESS) if (index_sparse_mo_2e_int_eri(1, 1) == 0 .and. & index_sparse_mo_2e_int_eri(1, offset_data_read + 1) == offset_read*4 + 1) then @@ -297,14 +301,16 @@ subroutine test_read(file_name, back_end) rc = trexio_read_mo_2e_int_eri(trex_file, offset_eof, read_buf_size, & index_sparse_mo_2e_int_eri(1, offset_data_eof + 1), & value_sparse_mo_2e_int_eri(offset_data_eof + 1)) - call trexio_assert(rc, TREXIO_END) !do i = 1,20 ! write(*,*) index_sparse_mo_2e_int_eri(1,i) !enddo - if (index_sparse_mo_2e_int_eri(1, 1) == 0 .and. & + call trexio_assert(rc, TREXIO_END) + if (read_buf_size == 3 .and. & + index_sparse_mo_2e_int_eri(1, 1) == 0 .and. & index_sparse_mo_2e_int_eri(1, offset_data_read + 1) == offset_read*4 + 1 .and. & index_sparse_mo_2e_int_eri(1, offset_data_eof + 1) == offset_eof*4 + 1) then write(*,*) 'SUCCESS READ SPARSE DATA EOF' + read_buf_size = read_buf_size_save else print *, 'FAILURE SPARSE DATA EOF CHECK' call exit(-1)