1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-04 05:03:58 +01:00

first prototype of read/write string datasets

This commit is contained in:
q-posev 2021-05-31 13:26:52 +02:00
parent c18b75a11d
commit b5a6987758

View File

@ -289,8 +289,7 @@ trexio_exit_code trexio_hdf5_write_$group_dset$(trexio_t* const file, const $gro
#+begin_src c :tangle read_dset_data_hdf5.c #+begin_src c :tangle read_dset_data_hdf5.c
trexio_exit_code trexio_exit_code
trexio_hdf5_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, trexio_hdf5_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims)
const uint32_t rank, const uint64_t* dims)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -345,8 +344,7 @@ trexio_hdf5_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $
#+begin_src c :tangle write_dset_data_hdf5.c #+begin_src c :tangle write_dset_data_hdf5.c
trexio_exit_code trexio_exit_code
trexio_hdf5_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$, trexio_hdf5_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims)
const uint32_t rank, const uint64_t* dims)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -421,21 +419,121 @@ trexio_exit_code trexio_hdf5_write_$group_dset$(trexio_t* const file, const $gro
#+begin_src c :tangle read_dset_str_hdf5.c #+begin_src c :tangle read_dset_str_hdf5.c
trexio_exit_code trexio_exit_code
trexio_hdf5_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, trexio_hdf5_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $group_dset$, const uint32_t rank, const uint64_t* dims)
const uint32_t rank, const uint64_t* dims)
{ {
return TREXIO_SUCCESS; if (file == NULL) return TREXIO_INVALID_ARG_1;
if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2;
const trexio_hdf5_t* f = (const trexio_hdf5_t*) file;
herr_t status;
int rrank;
// get the rank of the dataset in a file
status = H5LTget_dataset_ndims (f->$group$_group, $GROUP_DSET$_NAME, &rrank);
if (status < 0) return TREXIO_FAILURE;
if (rrank != (int) rank) return TREXIO_INVALID_ARG_3;
// open the dataset to get its dimensions
hid_t dset_id = H5Dopen(f->$group$_group, $GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID;
// allocate space for the dimensions to be read
hsize_t* ddims = CALLOC( (int) rank, hsize_t);
if (ddims == NULL) return TREXIO_FAILURE;
// read dimensions from the existing dataset
status = H5LDget_dset_dims(dset_id, ddims);
H5Dclose(dset_id);
if (status < 0) {
free(ddims);
return TREXIO_FAILURE;
}
for (uint32_t i=0; i<rank; ++i){
if (ddims[i] != dims[i]) {
free(ddims);
return TREXIO_INVALID_ARG_4;
}
}
free(ddims);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_string(f->$group$_group,
$GROUP_DSET$_NAME,
$group_dset$);
if (status < 0) return TREXIO_FAILURE;
return TREXIO_SUCCESS;
} }
#+end_src #+end_src
#+begin_src c :tangle write_dset_str_hdf5.c #+begin_src c :tangle write_dset_str_hdf5.c
trexio_exit_code trexio_exit_code
trexio_hdf5_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$, trexio_hdf5_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims)
const uint32_t rank, const uint64_t* dims)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if ($group_dset$ == NULL) return TREXIO_INVALID_ARG_2;
trexio_exit_code rc;
uint64_t $group_dset_dim$;
// error handling for rc is added by the generator
rc = trexio_hdf5_read_$group_dset_dim$(file, &($group_dset_dim$));
if ($group_dset_dim$ == 0L) return TREXIO_INVALID_NUM;
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
herr_t status;
hid_t dset_id;
/* we are going to write variable-length strings */
hid_t memtype = H5Tcopy (H5T_C_S1);
status = H5Tset_size (memtype, H5T_VARIABLE);
if ( H5LTfind_dataset(f->$group$_group, $GROUP_DSET$_NAME) != 1 ) {
/* code to create dataset */
hid_t filetype = H5Tcopy (H5T_FORTRAN_S1);
status = H5Tset_size (filetype, H5T_VARIABLE);
hid_t dspace = H5Screate_simple( (int) rank, (const hsize_t*) dims, NULL);
dset_id = H5Dcreate (f->$group$_group, $GROUP_DSET$_NAME, filetype, dspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID;
status = H5Dwrite (dset_id, memtype,
H5S_ALL, H5S_ALL, H5P_DEFAULT,
$group_dset$);
H5Dclose (dset_id)
H5Sclose (dspace);
H5Tclose (filetype);
H5Tclose (memtype);
if (status < 0) return TREXIO_FAILURE;
} else {
dset_id = H5Dopen(f->$group$_group, $GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID;
/* code to write dataset */
status = H5Dwrite(dset_id, memtype,
H5S_ALL, H5S_ALL, H5P_DEFAULT,
$group_dset$);
H5Dclose(dset_id);
H5Tclose (memtype);
if (status < 0) return TREXIO_FAILURE;
}
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }