mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-07-22 10:47:43 +02:00
uint32 rank and better error handling
This commit is contained in:
parent
05d55028c4
commit
1df9cd7177
13
src/trexio.c
13
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) {
|
||||
|
@ -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) {
|
||||
|
@ -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; i<rank; i++){
|
||||
assert (ddims[i] == dims[i]);
|
||||
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_double(f->nucleus_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;
|
||||
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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; i<rank; i++){
|
||||
assert (ddims[i] == dims[i]);
|
||||
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_double(f->nucleus_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;
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user