1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

more portable HDF5 back end

This commit is contained in:
q-posev 2021-05-04 20:22:58 +02:00
parent 5f4f94646f
commit 9c2b240418
2 changed files with 24 additions and 21 deletions

View File

@ -6,7 +6,7 @@
* Constant file prefixes (not used by the generator) :noexport: * Constant file prefixes (not used by the generator) :noexport:
#+begin_src emacs-lisp #+begin_src emacs-lisp
(setq-local org-babel-default-header-args:c '((:comments . "both"))) (setq-local org-babel-default-header-args:c '((:comments . "both")))
org-babel-default-header-args:c org-babel-default-header-args:c
#+end_src #+end_src
@ -22,8 +22,6 @@
#+end_src #+end_src
DOCUMENT HDF5 BACK END HERE
#+begin_src c :tangle prefix_hdf5.h :noweb yes #+begin_src c :tangle prefix_hdf5.h :noweb yes
<<header>> <<header>>
@ -304,15 +302,14 @@ trexio_hdf5_read_$group$_$group_dset$ (trexio_t* const file, $group_dset_dtype$*
herr_t status; herr_t status;
int rrank; int rrank;
// get the rank of the dataset in a file // get the rank of the dataset in a file
status = H5LTget_dataset_ndims (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, status = H5LTget_dataset_ndims (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, &rrank);
&rrank);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
if (rrank != (int) rank) return TREXIO_INVALID_ARG_3; if (rrank != (int) rank) return TREXIO_INVALID_ARG_3;
// open the dataset to get its dimensions // open the dataset to get its dimensions
hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT); hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID; if (dset_id <= 0) return TREXIO_INVALID_ID;
// allocate space for the dimensions to be read // allocate space for the dimensions to be read
@ -329,17 +326,18 @@ trexio_hdf5_read_$group$_$group_dset$ (trexio_t* const file, $group_dset_dtype$*
} }
for (uint32_t i=0; i<rank; ++i){ for (uint32_t i=0; i<rank; ++i){
if (ddims[i] != dims[i]) { if (ddims[i] != dims[i]) {
free(ddims); free(ddims);
return TREXIO_INVALID_ARG_4; return TREXIO_INVALID_ARG_4;
} }
} }
free(ddims); free(ddims);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */ /* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_$group_dset_h5_dtype$(f->$group$_group, status = H5LTread_dataset(f->$group$_group,
$GROUP$_$GROUP_DSET$_NAME, $GROUP$_$GROUP_DSET$_NAME,
$group_dset$); H5T_NATIVE_$GROUP_DSET_H5_DTYPE$,
$group_dset$);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
@ -363,11 +361,13 @@ trexio_hdf5_write_$group$_$group_dset$ (trexio_t* const file, const $group_dset_
trexio_hdf5_t* f = (trexio_hdf5_t*) file; trexio_hdf5_t* f = (trexio_hdf5_t*) file;
if ( H5LTfind_dataset(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME) != 1) { if ( H5LTfind_dataset(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME) != 1 ) {
const herr_t status = const herr_t status = H5LTmake_dataset(f->$group$_group,
H5LTmake_dataset_$group_dset_h5_dtype$ (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, $GROUP$_$GROUP_DSET$_NAME,
(int) rank, (const hsize_t*) dims, $group_dset$); (int) rank, (const hsize_t*) dims,
H5T_NATIVE_$GROUP_DSET_H5_DTYPE$,
$group_dset$);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
} else { } else {
@ -375,9 +375,10 @@ trexio_hdf5_write_$group$_$group_dset$ (trexio_t* const file, const $group_dset_
hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT); hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID; if (dset_id <= 0) return TREXIO_INVALID_ID;
const herr_t status = const herr_t status = H5Dwrite(dset_id,
H5Dwrite(dset_id, H5T_NATIVE_$GROUP_DSET_H5_DTYPE$, H5S_ALL, H5S_ALL, H5T_NATIVE_$GROUP_DSET_H5_DTYPE$,
H5P_DEFAULT, $group_dset$); H5S_ALL, H5S_ALL, H5P_DEFAULT,
$group_dset$);
H5Dclose(dset_id); H5Dclose(dset_id);
if (status < 0) return TREXIO_FAILURE; if (status < 0) return TREXIO_FAILURE;
@ -417,3 +418,5 @@ trexio_hdf5_has_$group$_$group_dset$ (trexio_t* const file)
#endif #endif
#+end_src #+end_src

View File

@ -288,7 +288,7 @@ for fname in files_funcs_dsets:
c_dtype_single = 'float' c_dtype_single = 'float'
elif params['dtype'] == 'int64_t': elif params['dtype'] == 'int64_t':
h5_dtype = 'long' h5_dtype = 'int64'
f_dtype_double = 'integer(8)' f_dtype_double = 'integer(8)'
f_dtype_single = 'integer(4)' f_dtype_single = 'integer(4)'
c_dtype_double = 'int64_t' c_dtype_double = 'int64_t'