From d2efb67a9d7b102f6d7b65343da35ba3b81b71e5 Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 3 Mar 2021 15:59:30 +0100 Subject: [PATCH 1/8] stupid but working implementation of read/write_dset decoupled from read_group --- src/trexio_hdf5.c | 80 ++++++++++++++++++++++++++++++++++++++++++--- src/trexio_hdf5.org | 80 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 150 insertions(+), 10 deletions(-) diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index d99d341..dec5604 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -411,10 +411,28 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co assert (file != NULL); assert (coord != NULL); - h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - + /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); if (nucleus == NULL) return TREXIO_FAILURE; + assert (nucleus->coord != NULL);*/ + + /* Allocate and read nucleus_coord array */ + h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + assert (nucleus != NULL); + + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); + assert (rc == TREXIO_SUCCESS); + assert (nucleus->num > 0L); + + nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); assert (nucleus->coord != NULL); + + trexio_hdf5_t* f = (trexio_hdf5_t*) file; + herr_t status; + /* High-level H5LT API. No need to deal with dataspaces and datatypes */ + status = H5LTread_dataset_double(f->nucleus_group, + NUCLEUS_COORD_NAME, + nucleus->coord); + assert (status >= 0); for (size_t i=0 ; i<3*nucleus->num ; i++) { coord[i] = nucleus->coord[i]; @@ -430,17 +448,69 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou assert (file != NULL); assert (coord != NULL); - h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - + /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); if (nucleus == NULL) return TREXIO_FAILURE; - assert (nucleus->coord != NULL); + assert (nucleus->coord != NULL); for (size_t i=0 ; i<3*nucleus->num ; i++) { nucleus->coord[i] = coord[i]; } trexio_exit_code rc = trexio_hdf5_write_nucleus((trexio_hdf5_t*) file, nucleus); + assert (rc == TREXIO_SUCCESS);*/ + + h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + assert (nucleus != NULL); + + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); assert (rc == TREXIO_SUCCESS); + assert (nucleus->num > 0L); + + nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); + assert (nucleus->coord != NULL); + + for (size_t i=0 ; i<3*nucleus->num ; i++) { + nucleus->coord[i] = coord[i]; + } + + trexio_hdf5_t* f = (trexio_hdf5_t*) file; + + hid_t dset_id, dspace, dtype; + herr_t status; + int coord_rank = 2; + const hsize_t coord_dims[2] = {nucleus->num, 3}; + if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { + + status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, + coord_rank, coord_dims, nucleus->coord); + assert (status >= 0); + + } else { + + dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); + assert (dset_id > 0); + dspace = H5Dget_space(dset_id); + assert (dspace > 0); + dtype = H5Dget_type(dset_id); + assert (dtype > 0); + + int rrank; + hsize_t dims[2] = {0, 0}; + rrank = H5Sget_simple_extent_dims(dspace, + dims, NULL); + assert (rrank == coord_rank); + for (int i=0; icoord); + assert (status >= 0); + + H5Sclose(dspace); + H5Tclose(dtype); + H5Dclose(dset_id); + + } trexio_hdf5_free_nucleus(nucleus); diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index 97d2b91..a1d979a 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -574,10 +574,28 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co assert (file != NULL); assert (coord != NULL); - h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - + /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); if (nucleus == NULL) return TREXIO_FAILURE; + assert (nucleus->coord != NULL);*/ + + /* Allocate and read nucleus_coord array */ + h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + assert (nucleus != NULL); + + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); + assert (rc == TREXIO_SUCCESS); + assert (nucleus->num > 0L); + + nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); assert (nucleus->coord != NULL); + + trexio_hdf5_t* f = (trexio_hdf5_t*) file; + herr_t status; + /* High-level H5LT API. No need to deal with dataspaces and datatypes */ + status = H5LTread_dataset_double(f->nucleus_group, + NUCLEUS_COORD_NAME, + nucleus->coord); + assert (status >= 0); for (size_t i=0 ; i<3*nucleus->num ; i++) { coord[i] = nucleus->coord[i]; @@ -593,17 +611,69 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou assert (file != NULL); assert (coord != NULL); - h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - + /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); if (nucleus == NULL) return TREXIO_FAILURE; - assert (nucleus->coord != NULL); + assert (nucleus->coord != NULL); for (size_t i=0 ; i<3*nucleus->num ; i++) { nucleus->coord[i] = coord[i]; } trexio_exit_code rc = trexio_hdf5_write_nucleus((trexio_hdf5_t*) file, nucleus); + assert (rc == TREXIO_SUCCESS);*/ + + h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + assert (nucleus != NULL); + + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); assert (rc == TREXIO_SUCCESS); + assert (nucleus->num > 0L); + + nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); + assert (nucleus->coord != NULL); + + for (size_t i=0 ; i<3*nucleus->num ; i++) { + nucleus->coord[i] = coord[i]; + } + + trexio_hdf5_t* f = (trexio_hdf5_t*) file; + + hid_t dset_id, dspace, dtype; + herr_t status; + int coord_rank = 2; + const hsize_t coord_dims[2] = {nucleus->num, 3}; + if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { + + status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, + coord_rank, coord_dims, nucleus->coord); + assert (status >= 0); + + } else { + + dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); + assert (dset_id > 0); + dspace = H5Dget_space(dset_id); + assert (dspace > 0); + dtype = H5Dget_type(dset_id); + assert (dtype > 0); + + int rrank; + hsize_t dims[2] = {0, 0}; + rrank = H5Sget_simple_extent_dims(dspace, + dims, NULL); + assert (rrank == coord_rank); + for (int i=0; icoord); + assert (status >= 0); + + H5Sclose(dspace); + H5Tclose(dtype); + H5Dclose(dset_id); + + } trexio_hdf5_free_nucleus(nucleus); From 88d280820067d5983b8039a2c2c987d2e3d0d7cc Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 4 Mar 2021 14:00:56 +0100 Subject: [PATCH 2/8] missing mode in rdm --- src/trexio_text.c | 2 +- src/trexio_text.org | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/trexio_text.c b/src/trexio_text.c index 5b795f0..b27eef5 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -438,7 +438,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file->parent.mode == 'w') { rdm->file = fopen(file_name,"a"); } else { - rdm->file = fopen(file_name,""); + rdm->file = fopen(file_name,"r"); } free(file_name); file->rdm = rdm ; diff --git a/src/trexio_text.org b/src/trexio_text.org index 208cd5e..3a91064 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -99,6 +99,7 @@ typedef struct trexio_text_s { #+end_src +*** Init/deinit functions #+begin_src c :tangle trexio_text.h trexio_exit_code trexio_text_init(trexio_t* file); @@ -611,7 +612,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file->parent.mode == 'w') { rdm->file = fopen(file_name,"a"); } else { - rdm->file = fopen(file_name,""); + rdm->file = fopen(file_name,"r"); } free(file_name); file->rdm = rdm ; From aee737fa23753c8ef928ec6bc28ef31d287176e3 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 4 Mar 2021 14:03:34 +0100 Subject: [PATCH 3/8] provide dimensions and decouple dataset read/write from parent group --- src/trexio.c | 10 +++- src/trexio.org | 10 +++- src/trexio_hdf5.c | 111 +++++++++++++++--------------------------- src/trexio_hdf5.h | 4 +- src/trexio_hdf5.org | 115 ++++++++++++++++---------------------------- 5 files changed, 100 insertions(+), 150 deletions(-) diff --git a/src/trexio.c b/src/trexio.c index 627f8e6..afe98d1 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -183,6 +183,9 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) { int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; + int rank = 2; + uint64_t dims[2] = {nucleus_num, 3}; + switch (file->back_end) { case TREXIO_TEXT: @@ -190,7 +193,7 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) { break; case TREXIO_HDF5: - return trexio_hdf5_read_nucleus_coord(file, coord); + return trexio_hdf5_read_nucleus_coord(file, coord, rank, dims); break; /* case TREXIO_JSON: @@ -213,6 +216,9 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord) int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; + int rank = 2; + uint64_t dims[2] = {nucleus_num, 3}; + switch (file->back_end) { case TREXIO_TEXT: @@ -220,7 +226,7 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord) break; case TREXIO_HDF5: - return trexio_hdf5_write_nucleus_coord(file, coord); + return trexio_hdf5_write_nucleus_coord(file, coord, rank, dims); break; /* case TREXIO_JSON: diff --git a/src/trexio.org b/src/trexio.org index 050df1c..c0e35fb 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -345,6 +345,9 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) { int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; + int rank = 2; + uint64_t dims[2] = {nucleus_num, 3}; + switch (file->back_end) { case TREXIO_TEXT: @@ -352,7 +355,7 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) { break; case TREXIO_HDF5: - return trexio_hdf5_read_nucleus_coord(file, coord); + return trexio_hdf5_read_nucleus_coord(file, coord, rank, dims); break; /* case TREXIO_JSON: @@ -375,6 +378,9 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord) int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; + int rank = 2; + uint64_t dims[2] = {nucleus_num, 3}; + switch (file->back_end) { case TREXIO_TEXT: @@ -382,7 +388,7 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord) break; case TREXIO_HDF5: - return trexio_hdf5_write_nucleus_coord(file, coord); + return trexio_hdf5_write_nucleus_coord(file, coord, rank, dims); break; /* case TREXIO_JSON: diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index dec5604..b5e5bdd 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -406,113 +406,82 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6 return TREXIO_SUCCESS; } -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord) { +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); - - /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - if (nucleus == NULL) return TREXIO_FAILURE; - assert (nucleus->coord != NULL);*/ - - /* Allocate and read nucleus_coord array */ - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); - assert (nucleus != NULL); - - trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); - assert (rc == TREXIO_SUCCESS); - assert (nucleus->num > 0L); - - nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); - assert (nucleus->coord != NULL); - - trexio_hdf5_t* f = (trexio_hdf5_t*) file; - herr_t status; - /* High-level H5LT API. No need to deal with dataspaces and datatypes */ - status = H5LTread_dataset_double(f->nucleus_group, - NUCLEUS_COORD_NAME, - nucleus->coord); - assert (status >= 0); - for (size_t i=0 ; i<3*nucleus->num ; i++) { - coord[i] = nucleus->coord[i]; + uint64_t nucleus_num; + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); + assert (rc == TREXIO_SUCCESS); + assert (nucleus_num > 0L); + + uint64_t ddims[2] = {nucleus_num, 3}; + for (int i=0; inucleus_group, + NUCLEUS_COORD_NAME, + nucleus_coord); + + assert (status >= 0); + + for (size_t i=0 ; i<3*nucleus_num ; i++) { + coord[i] = nucleus_coord[i]; + } + + free(nucleus_coord); + return TREXIO_SUCCESS; } -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord) { +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); - - /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - if (nucleus == NULL) return TREXIO_FAILURE; - assert (nucleus->coord != NULL); - - for (size_t i=0 ; i<3*nucleus->num ; i++) { - nucleus->coord[i] = coord[i]; - } - - trexio_exit_code rc = trexio_hdf5_write_nucleus((trexio_hdf5_t*) file, nucleus); - assert (rc == TREXIO_SUCCESS);*/ - - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); - assert (nucleus != NULL); - - trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); + + uint64_t nucleus_num; + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); assert (rc == TREXIO_SUCCESS); - assert (nucleus->num > 0L); + assert (nucleus_num > 0L); - nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); - assert (nucleus->coord != NULL); + double* nucleus_coord = (double*) calloc(3 * nucleus_num, sizeof(double)); + assert (nucleus_coord != NULL); - for (size_t i=0 ; i<3*nucleus->num ; i++) { - nucleus->coord[i] = coord[i]; + for (size_t i=0 ; i<3*nucleus_num ; i++) { + nucleus_coord[i] = coord[i]; } trexio_hdf5_t* f = (trexio_hdf5_t*) file; - hid_t dset_id, dspace, dtype; herr_t status; - int coord_rank = 2; - const hsize_t coord_dims[2] = {nucleus->num, 3}; if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, - coord_rank, coord_dims, nucleus->coord); + rank, (hsize_t*) dims, nucleus_coord); assert (status >= 0); } else { - dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); + hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); assert (dset_id > 0); - dspace = H5Dget_space(dset_id); - assert (dspace > 0); - dtype = H5Dget_type(dset_id); - assert (dtype > 0); - int rrank; - hsize_t dims[2] = {0, 0}; - rrank = H5Sget_simple_extent_dims(dspace, - dims, NULL); - assert (rrank == coord_rank); - for (int i=0; icoord); + status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus_coord); assert (status >= 0); - H5Sclose(dspace); - H5Tclose(dtype); H5Dclose(dset_id); } - trexio_hdf5_free_nucleus(nucleus); + free(nucleus_coord); return TREXIO_SUCCESS; } diff --git a/src/trexio_hdf5.h b/src/trexio_hdf5.h index 1689cf8..596b486 100644 --- a/src/trexio_hdf5.h +++ b/src/trexio_hdf5.h @@ -91,7 +91,7 @@ typedef struct four_index_s { trexio_exit_code trexio_hdf5_read_nucleus_num(const trexio_t* file, uint64_t* num); trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint64_t num); -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord); -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord); +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims); +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims); #endif diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index a1d979a..0ad87a0 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -564,118 +564,87 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6 The ~coord~ array is assumed allocated with the appropriate size. #+begin_src c :tangle trexio_hdf5.h -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord); -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord); +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims); +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims); #+end_src #+begin_src c :tangle trexio_hdf5.c -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord) { +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); - - /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - if (nucleus == NULL) return TREXIO_FAILURE; - assert (nucleus->coord != NULL);*/ - - /* Allocate and read nucleus_coord array */ - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); - assert (nucleus != NULL); - - trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); - assert (rc == TREXIO_SUCCESS); - assert (nucleus->num > 0L); - - nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); - assert (nucleus->coord != NULL); - - trexio_hdf5_t* f = (trexio_hdf5_t*) file; - herr_t status; - /* High-level H5LT API. No need to deal with dataspaces and datatypes */ - status = H5LTread_dataset_double(f->nucleus_group, - NUCLEUS_COORD_NAME, - nucleus->coord); - assert (status >= 0); - for (size_t i=0 ; i<3*nucleus->num ; i++) { - coord[i] = nucleus->coord[i]; + uint64_t nucleus_num; + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); + assert (rc == TREXIO_SUCCESS); + assert (nucleus_num > 0L); + + uint64_t ddims[2] = {nucleus_num, 3}; + for (int i=0; inucleus_group, + NUCLEUS_COORD_NAME, + nucleus_coord); + + assert (status >= 0); + + for (size_t i=0 ; i<3*nucleus_num ; i++) { + coord[i] = nucleus_coord[i]; + } + + free(nucleus_coord); + return TREXIO_SUCCESS; } -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord) { +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); - - /*h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file); - if (nucleus == NULL) return TREXIO_FAILURE; - assert (nucleus->coord != NULL); - - for (size_t i=0 ; i<3*nucleus->num ; i++) { - nucleus->coord[i] = coord[i]; - } - - trexio_exit_code rc = trexio_hdf5_write_nucleus((trexio_hdf5_t*) file, nucleus); - assert (rc == TREXIO_SUCCESS);*/ - - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); - assert (nucleus != NULL); - - trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus->num)); + + uint64_t nucleus_num; + trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); assert (rc == TREXIO_SUCCESS); - assert (nucleus->num > 0L); + assert (nucleus_num > 0L); - nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double)); - assert (nucleus->coord != NULL); + double* nucleus_coord = (double*) calloc(3 * nucleus_num, sizeof(double)); + assert (nucleus_coord != NULL); - for (size_t i=0 ; i<3*nucleus->num ; i++) { - nucleus->coord[i] = coord[i]; + for (size_t i=0 ; i<3*nucleus_num ; i++) { + nucleus_coord[i] = coord[i]; } trexio_hdf5_t* f = (trexio_hdf5_t*) file; - hid_t dset_id, dspace, dtype; herr_t status; - int coord_rank = 2; - const hsize_t coord_dims[2] = {nucleus->num, 3}; if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, - coord_rank, coord_dims, nucleus->coord); + rank, (hsize_t*) dims, nucleus_coord); assert (status >= 0); } else { - dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); + hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); assert (dset_id > 0); - dspace = H5Dget_space(dset_id); - assert (dspace > 0); - dtype = H5Dget_type(dset_id); - assert (dtype > 0); - int rrank; - hsize_t dims[2] = {0, 0}; - rrank = H5Sget_simple_extent_dims(dspace, - dims, NULL); - assert (rrank == coord_rank); - for (int i=0; icoord); + status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus_coord); assert (status >= 0); - H5Sclose(dspace); - H5Tclose(dtype); H5Dclose(dset_id); } - trexio_hdf5_free_nucleus(nucleus); + free(nucleus_coord); return TREXIO_SUCCESS; } From e52cfbafea9865d1243c0f621e7364e6fdd88a6d Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 4 Mar 2021 15:02:23 +0100 Subject: [PATCH 4/8] dummy write and smart read --- src/trexio_hdf5.c | 52 +++++++++++++++++++------------------------ src/trexio_hdf5.org | 54 ++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 59 deletions(-) diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index b5e5bdd..586ab67 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -411,34 +411,37 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co assert (file != NULL); assert (coord != NULL); - uint64_t nucleus_num; - trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); - assert (rc == TREXIO_SUCCESS); - assert (nucleus_num > 0L); + trexio_hdf5_t* f = (trexio_hdf5_t*) file; - uint64_t ddims[2] = {nucleus_num, 3}; + herr_t status; + int rrank; + status = H5LTget_dataset_ndims (f->nucleus_group, NUCLEUS_COORD_NAME, + &rrank); + assert (status >= 0); + assert (rrank == rank); + + hsize_t* ddims = (hsize_t*) calloc(rrank, sizeof(hsize_t)); + + hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); + assert (dset_id > 0); + + status = H5LDget_dset_dims(dset_id, ddims); + assert (status >= 0); + + H5Dclose(dset_id); + + //uint64_t ddims[2] = {nucleus_num, 3}; for (int i=0; inucleus_group, + status = H5LTread_dataset_double(f->nucleus_group, NUCLEUS_COORD_NAME, - nucleus_coord); + coord); assert (status >= 0); - for (size_t i=0 ; i<3*nucleus_num ; i++) { - coord[i] = nucleus_coord[i]; - } - - free(nucleus_coord); - return TREXIO_SUCCESS; } @@ -453,20 +456,13 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou assert (rc == TREXIO_SUCCESS); assert (nucleus_num > 0L); - double* nucleus_coord = (double*) calloc(3 * nucleus_num, sizeof(double)); - assert (nucleus_coord != NULL); - - for (size_t i=0 ; i<3*nucleus_num ; i++) { - nucleus_coord[i] = coord[i]; - } - trexio_hdf5_t* f = (trexio_hdf5_t*) file; herr_t status; if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, - rank, (hsize_t*) dims, nucleus_coord); + rank, (hsize_t*) dims, coord); assert (status >= 0); } else { @@ -474,14 +470,12 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); assert (dset_id > 0); - status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus_coord); + status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, coord); assert (status >= 0); H5Dclose(dset_id); } - - free(nucleus_coord); return TREXIO_SUCCESS; } diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index 0ad87a0..382592f 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -36,7 +36,7 @@ #include "trexio_hdf5.h" #+end_src - + * HDF5 Back end ** HDF5 definitions @@ -574,34 +574,37 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co assert (file != NULL); assert (coord != NULL); - uint64_t nucleus_num; - trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); - assert (rc == TREXIO_SUCCESS); - assert (nucleus_num > 0L); + trexio_hdf5_t* f = (trexio_hdf5_t*) file; - uint64_t ddims[2] = {nucleus_num, 3}; + herr_t status; + int rrank; + status = H5LTget_dataset_ndims (f->nucleus_group, NUCLEUS_COORD_NAME, + &rrank); + assert (status >= 0); + assert (rrank == rank); + + hsize_t* ddims = (hsize_t*) calloc(rrank, sizeof(hsize_t)); + + hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); + assert (dset_id > 0); + + status = H5LDget_dset_dims(dset_id, ddims); + assert (status >= 0); + + H5Dclose(dset_id); + + //uint64_t ddims[2] = {nucleus_num, 3}; for (int i=0; inucleus_group, + status = H5LTread_dataset_double(f->nucleus_group, NUCLEUS_COORD_NAME, - nucleus_coord); + coord); assert (status >= 0); - for (size_t i=0 ; i<3*nucleus_num ; i++) { - coord[i] = nucleus_coord[i]; - } - - free(nucleus_coord); - return TREXIO_SUCCESS; } @@ -616,20 +619,13 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou assert (rc == TREXIO_SUCCESS); assert (nucleus_num > 0L); - double* nucleus_coord = (double*) calloc(3 * nucleus_num, sizeof(double)); - assert (nucleus_coord != NULL); - - for (size_t i=0 ; i<3*nucleus_num ; i++) { - nucleus_coord[i] = coord[i]; - } - trexio_hdf5_t* f = (trexio_hdf5_t*) file; herr_t status; if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, - rank, (hsize_t*) dims, nucleus_coord); + rank, (hsize_t*) dims, coord); assert (status >= 0); } else { @@ -637,14 +633,12 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); assert (dset_id > 0); - status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus_coord); + status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, coord); assert (status >= 0); H5Dclose(dset_id); } - - free(nucleus_coord); return TREXIO_SUCCESS; } From db1f1d69800ae83ce523615cc17c024cfd3567c5 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 4 Mar 2021 18:53:13 +0100 Subject: [PATCH 5/8] fix leaking memory --- src/trexio.c | 8 ++++++-- src/trexio_hdf5.c | 11 ++++------- src/trexio_hdf5.org | 11 ++++------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/trexio.c b/src/trexio.c index afe98d1..b5860fb 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -74,8 +74,12 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b break; */ } - if (rc != TREXIO_SUCCESS) return NULL; - + if (rc != TREXIO_SUCCESS) { + free(result->file_name); + free(result); + return NULL; + } + return result; } diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index 586ab67..9959deb 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -94,9 +94,9 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) { switch (file->mode) { case 'r': + case 'a': // reading non-existing file -> error return TREXIO_FAILURE; - case 'a': case 'w': // appending or writing non-existing file -> create it f->file_id = H5Fcreate(file->file_name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT); @@ -109,13 +109,8 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) { switch (file->mode) { case 'r': case 'a': - if (f_exists == 1) { - f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT); + f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT); //f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT); - } else { - f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - //f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - } break; case 'w': f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); @@ -442,6 +437,8 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co assert (status >= 0); + free(ddims); + return TREXIO_SUCCESS; } diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index 382592f..a31c709 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -185,9 +185,9 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) { switch (file->mode) { case 'r': + case 'a': // reading non-existing file -> error return TREXIO_FAILURE; - case 'a': case 'w': // appending or writing non-existing file -> create it f->file_id = H5Fcreate(file->file_name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT); @@ -200,13 +200,8 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) { switch (file->mode) { case 'r': case 'a': - if (f_exists == 1) { - f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT); + f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT); //f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT); - } else { - f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - //f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); - } break; case 'w': f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); @@ -605,6 +600,8 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co assert (status >= 0); + free(ddims); + return TREXIO_SUCCESS; } From 05d55028c4ea6be0ce976ee97b9098ffef004565 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 4 Mar 2021 19:36:32 +0100 Subject: [PATCH 6/8] add error codes for invalid returns --- src/trexio.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/trexio.h b/src/trexio.h index d00c1ff..fcf2de8 100644 --- a/src/trexio.h +++ b/src/trexio.h @@ -21,6 +21,8 @@ typedef int32_t trexio_exit_code; #define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 ) #define TREXIO_END ( (trexio_exit_code) 10 ) #define TREXIO_READONLY ( (trexio_exit_code) 11 ) +#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) +#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 ) typedef int32_t back_end_t; From 1df9cd7177e2a7534bf0d2599b84cb0e7a9925c5 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 4 Mar 2021 19:39:06 +0100 Subject: [PATCH 7/8] uint32 rank and better error handling --- src/trexio.c | 13 +++++---- src/trexio.org | 23 +++++++++------- src/trexio_hdf5.c | 60 ++++++++++++++++++++++++------------------ src/trexio_hdf5.h | 4 +-- src/trexio_hdf5.org | 64 ++++++++++++++++++++++++++------------------- 5 files changed, 94 insertions(+), 70 deletions(-) diff --git a/src/trexio.c b/src/trexio.c index b5860fb..9d5cdf2 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -74,6 +74,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b break; */ } + if (rc != TREXIO_SUCCESS) { free(result->file_name); free(result); @@ -107,17 +108,15 @@ trexio_exit_code trexio_close(trexio_t* file) { assert (1 == 0); /* Impossible case */ } - if (rc != TREXIO_SUCCESS) { - return TREXIO_FAILURE; - } - free(file->file_name); file->file_name = NULL; int irc = pthread_mutex_destroy( &(file->thread_lock) ); assert (irc == 0); - free(file); + free(file); + + if (rc != TREXIO_SUCCESS) return rc; return TREXIO_SUCCESS; } @@ -187,7 +186,7 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) { int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; - int rank = 2; + uint32_t rank = 2; uint64_t dims[2] = {nucleus_num, 3}; switch (file->back_end) { @@ -220,7 +219,7 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord) int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; - int rank = 2; + uint32_t rank = 2; uint64_t dims[2] = {nucleus_num, 3}; switch (file->back_end) { diff --git a/src/trexio.org b/src/trexio.org index c0e35fb..a1fe8fa 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -78,6 +78,8 @@ typedef int32_t trexio_exit_code; #define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 ) #define TREXIO_END ( (trexio_exit_code) 10 ) #define TREXIO_READONLY ( (trexio_exit_code) 11 ) +#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) +#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 ) #+end_src ** Back ends @@ -211,8 +213,13 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b break; ,*/ } - if (rc != TREXIO_SUCCESS) return NULL; - + + if (rc != TREXIO_SUCCESS) { + free(result->file_name); + free(result); + return NULL; + } + return result; } #+end_src @@ -248,17 +255,15 @@ trexio_exit_code trexio_close(trexio_t* file) { assert (1 == 0); /* Impossible case */ } - if (rc != TREXIO_SUCCESS) { - return TREXIO_FAILURE; - } - free(file->file_name); file->file_name = NULL; int irc = pthread_mutex_destroy( &(file->thread_lock) ); assert (irc == 0); - free(file); + free(file); + + if (rc != TREXIO_SUCCESS) return rc; return TREXIO_SUCCESS; } @@ -345,7 +350,7 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) { int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; - int rank = 2; + uint32_t rank = 2; uint64_t dims[2] = {nucleus_num, 3}; switch (file->back_end) { @@ -378,7 +383,7 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord) int64_t dim_coord = nucleus_num*3; if (dim_coord < 0) return TREXIO_FAILURE; - int rank = 2; + uint32_t rank = 2; uint64_t dims[2] = {nucleus_num, 3}; switch (file->back_end) { diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index 9959deb..4f54dd2 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -401,7 +401,7 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6 return TREXIO_SUCCESS; } -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims) { +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); @@ -410,48 +410,58 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co herr_t status; int rrank; + // get the rank of the dataset in a file status = H5LTget_dataset_ndims (f->nucleus_group, NUCLEUS_COORD_NAME, &rrank); - assert (status >= 0); - assert (rrank == rank); - hsize_t* ddims = (hsize_t*) calloc(rrank, sizeof(hsize_t)); - + 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->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); - assert (dset_id > 0); + if (dset_id <= 0) return TREXIO_INVALID_ID; + // allocate space for the dimensions to be read + hsize_t* ddims = (hsize_t*) calloc( (int) rank, sizeof(hsize_t)); + if (ddims == NULL) return TREXIO_FAILURE; + + // read dimensions from the existing dataset status = H5LDget_dset_dims(dset_id, ddims); - assert (status >= 0); - - H5Dclose(dset_id); - //uint64_t ddims[2] = {nucleus_num, 3}; - for (int i=0; inucleus_group, NUCLEUS_COORD_NAME, - coord); - - assert (status >= 0); - - free(ddims); + coord); + if (status < 0) return TREXIO_FAILURE; return TREXIO_SUCCESS; } -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims) { +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); uint64_t nucleus_num; trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); - assert (rc == TREXIO_SUCCESS); - assert (nucleus_num > 0L); + if (rc != TREXIO_SUCCESS) return rc; + if (nucleus_num <= 0L) return TREXIO_INVALID_NUM; trexio_hdf5_t* f = (trexio_hdf5_t*) file; @@ -459,18 +469,18 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, - rank, (hsize_t*) dims, coord); - assert (status >= 0); + (int) rank, (hsize_t*) dims, coord); + if (status < 0) return TREXIO_FAILURE; } else { hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); - assert (dset_id > 0); + if (dset_id <= 0) return TREXIO_INVALID_ID; status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, coord); - assert (status >= 0); - + H5Dclose(dset_id); + if (status < 0) return TREXIO_FAILURE; } diff --git a/src/trexio_hdf5.h b/src/trexio_hdf5.h index 596b486..f71360c 100644 --- a/src/trexio_hdf5.h +++ b/src/trexio_hdf5.h @@ -91,7 +91,7 @@ typedef struct four_index_s { trexio_exit_code trexio_hdf5_read_nucleus_num(const trexio_t* file, uint64_t* num); trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint64_t num); -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims); -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims); +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims); #endif diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index a31c709..1f57adf 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -559,12 +559,12 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6 The ~coord~ array is assumed allocated with the appropriate size. #+begin_src c :tangle trexio_hdf5.h -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims); -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims); +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims); #+end_src #+begin_src c :tangle trexio_hdf5.c -trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const int rank, const uint64_t* dims) { +trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); @@ -573,48 +573,58 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co herr_t status; int rrank; + // get the rank of the dataset in a file status = H5LTget_dataset_ndims (f->nucleus_group, NUCLEUS_COORD_NAME, &rrank); - assert (status >= 0); - assert (rrank == rank); - hsize_t* ddims = (hsize_t*) calloc(rrank, sizeof(hsize_t)); - + 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->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); - assert (dset_id > 0); + if (dset_id <= 0) return TREXIO_INVALID_ID; + // allocate space for the dimensions to be read + hsize_t* ddims = (hsize_t*) calloc( (int) rank, sizeof(hsize_t)); + if (ddims == NULL) return TREXIO_FAILURE; + + // read dimensions from the existing dataset status = H5LDget_dset_dims(dset_id, ddims); - assert (status >= 0); - - H5Dclose(dset_id); - //uint64_t ddims[2] = {nucleus_num, 3}; - for (int i=0; inucleus_group, NUCLEUS_COORD_NAME, - coord); - - assert (status >= 0); - - free(ddims); + coord); + if (status < 0) return TREXIO_FAILURE; return TREXIO_SUCCESS; } -trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const int rank, const uint64_t* dims) { +trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) { assert (file != NULL); assert (coord != NULL); uint64_t nucleus_num; trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num)); - assert (rc == TREXIO_SUCCESS); - assert (nucleus_num > 0L); + if (rc != TREXIO_SUCCESS) return rc; + if (nucleus_num <= 0L) return TREXIO_INVALID_NUM; trexio_hdf5_t* f = (trexio_hdf5_t*) file; @@ -622,18 +632,18 @@ trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const dou if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) { status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME, - rank, (hsize_t*) dims, coord); - assert (status >= 0); + (int) rank, (hsize_t*) dims, coord); + if (status < 0) return TREXIO_FAILURE; } else { hid_t dset_id = H5Dopen(f->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT); - assert (dset_id > 0); + if (dset_id <= 0) return TREXIO_INVALID_ID; status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, coord); - assert (status >= 0); - + H5Dclose(dset_id); + if (status < 0) return TREXIO_FAILURE; } From b964b73bbe6deacf230abe0b8a205b2e8d10a263 Mon Sep 17 00:00:00 2001 From: Evgeny Posenitskiy <45995097+q-posev@users.noreply.github.com> Date: Thu, 4 Mar 2021 20:43:44 +0100 Subject: [PATCH 8/8] removed bracket --- src/trexio.org | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/trexio.org b/src/trexio.org index d803907..874585f 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -329,8 +329,6 @@ trexio_exit_code trexio_close(trexio_t* file) { if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; return TREXIO_SUCCESS; -} - } #+end_src ** Reading/writing data