mirror of
https://github.com/TREX-CoE/trexio.git
synced 2025-04-26 10:24:44 +02:00
Merge pull request #22 from TREX-CoE/change_h5rw_datasets
hdf5_read/write_dset decoupled from the parent group
This commit is contained in:
commit
e88a228bb5
38
src/trexio.c
38
src/trexio.c
@ -81,8 +81,12 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
|
|||||||
break;
|
break;
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
if (rc != TREXIO_SUCCESS) return NULL;
|
|
||||||
|
|
||||||
|
if (rc != TREXIO_SUCCESS) {
|
||||||
|
free(result->file_name);
|
||||||
|
free(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* File locking */
|
/* File locking */
|
||||||
|
|
||||||
@ -96,17 +100,19 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
|
|||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
rc = TREXIO_SUCCESS;
|
rc = TREXIO_SUCCESS;
|
||||||
/*
|
|
||||||
rc = trexio_hdf5_lock(result);
|
|
||||||
break;
|
break;
|
||||||
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
rc = trexio_json_lock(result);
|
rc = trexio_json_lock(result);
|
||||||
break;
|
break;
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
if (rc != TREXIO_SUCCESS) return NULL;
|
|
||||||
|
|
||||||
|
if (rc != TREXIO_SUCCESS) {
|
||||||
|
free(result->file_name);
|
||||||
|
free(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -137,10 +143,11 @@ trexio_exit_code trexio_close(trexio_t* file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rc != TREXIO_SUCCESS) {
|
if (rc != TREXIO_SUCCESS) {
|
||||||
|
free(file->file_name);
|
||||||
|
free(file);
|
||||||
return TREXIO_FAILURE;
|
return TREXIO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* File unlocking */
|
/* File unlocking */
|
||||||
|
|
||||||
rc = TREXIO_FAILURE;
|
rc = TREXIO_FAILURE;
|
||||||
@ -153,17 +160,13 @@ trexio_exit_code trexio_close(trexio_t* file) {
|
|||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
rc = TREXIO_SUCCESS;
|
rc = TREXIO_SUCCESS;
|
||||||
/*
|
|
||||||
rc = trexio_hdf5_unlock(file);
|
|
||||||
break;
|
break;
|
||||||
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
rc = trexio_json_unlock(file);
|
rc = trexio_json_unlock(file);
|
||||||
break;
|
break;
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
|
|
||||||
|
|
||||||
|
|
||||||
/* Terminate front end */
|
/* Terminate front end */
|
||||||
|
|
||||||
@ -171,10 +174,11 @@ trexio_exit_code trexio_close(trexio_t* file) {
|
|||||||
file->file_name = NULL;
|
file->file_name = NULL;
|
||||||
|
|
||||||
int irc = pthread_mutex_destroy( &(file->thread_lock) );
|
int irc = pthread_mutex_destroy( &(file->thread_lock) );
|
||||||
|
|
||||||
free(file);
|
free(file);
|
||||||
|
|
||||||
if (irc != 0) return TREXIO_ERRNO;
|
if (irc != 0) return TREXIO_ERRNO;
|
||||||
|
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
|
||||||
|
|
||||||
return TREXIO_SUCCESS;
|
return TREXIO_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -244,6 +248,9 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) {
|
|||||||
int64_t dim_coord = nucleus_num*3;
|
int64_t dim_coord = nucleus_num*3;
|
||||||
if (dim_coord < 0) return TREXIO_FAILURE;
|
if (dim_coord < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
|
uint32_t rank = 2;
|
||||||
|
uint64_t dims[2] = {nucleus_num, 3};
|
||||||
|
|
||||||
switch (file->back_end) {
|
switch (file->back_end) {
|
||||||
|
|
||||||
case TREXIO_TEXT:
|
case TREXIO_TEXT:
|
||||||
@ -251,7 +258,7 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
return trexio_hdf5_read_nucleus_coord(file, coord);
|
return trexio_hdf5_read_nucleus_coord(file, coord, rank, dims);
|
||||||
break;
|
break;
|
||||||
/*
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
@ -274,6 +281,9 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord)
|
|||||||
int64_t dim_coord = nucleus_num*3;
|
int64_t dim_coord = nucleus_num*3;
|
||||||
if (dim_coord < 0) return TREXIO_FAILURE;
|
if (dim_coord < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
|
uint32_t rank = 2;
|
||||||
|
uint64_t dims[2] = {nucleus_num, 3};
|
||||||
|
|
||||||
switch (file->back_end) {
|
switch (file->back_end) {
|
||||||
|
|
||||||
case TREXIO_TEXT:
|
case TREXIO_TEXT:
|
||||||
@ -281,7 +291,7 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
return trexio_hdf5_write_nucleus_coord(file, coord);
|
return trexio_hdf5_write_nucleus_coord(file, coord, rank, dims);
|
||||||
break;
|
break;
|
||||||
/*
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
|
@ -22,6 +22,8 @@ typedef int32_t trexio_exit_code;
|
|||||||
#define TREXIO_END ( (trexio_exit_code) 10 )
|
#define TREXIO_END ( (trexio_exit_code) 10 )
|
||||||
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
|
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
|
||||||
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
|
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
|
||||||
|
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
|
||||||
|
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 )
|
||||||
|
|
||||||
typedef int32_t back_end_t;
|
typedef int32_t back_end_t;
|
||||||
|
|
||||||
|
@ -79,6 +79,8 @@ typedef int32_t trexio_exit_code;
|
|||||||
#define TREXIO_END ( (trexio_exit_code) 10 )
|
#define TREXIO_END ( (trexio_exit_code) 10 )
|
||||||
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
|
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
|
||||||
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
|
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
|
||||||
|
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
|
||||||
|
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 )
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
** Back ends
|
** Back ends
|
||||||
@ -219,8 +221,12 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
|
|||||||
break;
|
break;
|
||||||
,*/
|
,*/
|
||||||
}
|
}
|
||||||
if (rc != TREXIO_SUCCESS) return NULL;
|
|
||||||
|
|
||||||
|
if (rc != TREXIO_SUCCESS) {
|
||||||
|
free(result->file_name);
|
||||||
|
free(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
/* File locking */
|
/* File locking */
|
||||||
|
|
||||||
@ -234,17 +240,19 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
|
|||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
rc = TREXIO_SUCCESS;
|
rc = TREXIO_SUCCESS;
|
||||||
/*
|
|
||||||
rc = trexio_hdf5_lock(result);
|
|
||||||
break;
|
break;
|
||||||
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
rc = trexio_json_lock(result);
|
rc = trexio_json_lock(result);
|
||||||
break;
|
break;
|
||||||
,*/
|
*/
|
||||||
}
|
}
|
||||||
if (rc != TREXIO_SUCCESS) return NULL;
|
|
||||||
|
|
||||||
|
if (rc != TREXIO_SUCCESS) {
|
||||||
|
free(result->file_name);
|
||||||
|
free(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -283,10 +291,11 @@ trexio_exit_code trexio_close(trexio_t* file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rc != TREXIO_SUCCESS) {
|
if (rc != TREXIO_SUCCESS) {
|
||||||
|
free(file->file_name);
|
||||||
|
free(file);
|
||||||
return TREXIO_FAILURE;
|
return TREXIO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* File unlocking */
|
/* File unlocking */
|
||||||
|
|
||||||
rc = TREXIO_FAILURE;
|
rc = TREXIO_FAILURE;
|
||||||
@ -299,17 +308,13 @@ trexio_exit_code trexio_close(trexio_t* file) {
|
|||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
rc = TREXIO_SUCCESS;
|
rc = TREXIO_SUCCESS;
|
||||||
/*
|
|
||||||
rc = trexio_hdf5_unlock(file);
|
|
||||||
break;
|
break;
|
||||||
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
rc = trexio_json_unlock(file);
|
rc = trexio_json_unlock(file);
|
||||||
break;
|
break;
|
||||||
,*/
|
*/
|
||||||
}
|
}
|
||||||
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
|
|
||||||
|
|
||||||
|
|
||||||
/* Terminate front end */
|
/* Terminate front end */
|
||||||
|
|
||||||
@ -317,10 +322,11 @@ trexio_exit_code trexio_close(trexio_t* file) {
|
|||||||
file->file_name = NULL;
|
file->file_name = NULL;
|
||||||
|
|
||||||
int irc = pthread_mutex_destroy( &(file->thread_lock) );
|
int irc = pthread_mutex_destroy( &(file->thread_lock) );
|
||||||
|
|
||||||
free(file);
|
free(file);
|
||||||
|
|
||||||
if (irc != 0) return TREXIO_ERRNO;
|
if (irc != 0) return TREXIO_ERRNO;
|
||||||
|
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
|
||||||
|
|
||||||
return TREXIO_SUCCESS;
|
return TREXIO_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -407,6 +413,9 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) {
|
|||||||
int64_t dim_coord = nucleus_num*3;
|
int64_t dim_coord = nucleus_num*3;
|
||||||
if (dim_coord < 0) return TREXIO_FAILURE;
|
if (dim_coord < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
|
uint32_t rank = 2;
|
||||||
|
uint64_t dims[2] = {nucleus_num, 3};
|
||||||
|
|
||||||
switch (file->back_end) {
|
switch (file->back_end) {
|
||||||
|
|
||||||
case TREXIO_TEXT:
|
case TREXIO_TEXT:
|
||||||
@ -414,7 +423,7 @@ trexio_exit_code trexio_read_nucleus_coord(trexio_t* file, double* coord) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
return trexio_hdf5_read_nucleus_coord(file, coord);
|
return trexio_hdf5_read_nucleus_coord(file, coord, rank, dims);
|
||||||
break;
|
break;
|
||||||
/*
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
@ -437,6 +446,9 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord)
|
|||||||
int64_t dim_coord = nucleus_num*3;
|
int64_t dim_coord = nucleus_num*3;
|
||||||
if (dim_coord < 0) return TREXIO_FAILURE;
|
if (dim_coord < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
|
uint32_t rank = 2;
|
||||||
|
uint64_t dims[2] = {nucleus_num, 3};
|
||||||
|
|
||||||
switch (file->back_end) {
|
switch (file->back_end) {
|
||||||
|
|
||||||
case TREXIO_TEXT:
|
case TREXIO_TEXT:
|
||||||
@ -444,7 +456,7 @@ trexio_exit_code trexio_write_nucleus_coord(trexio_t* file, const double* coord)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TREXIO_HDF5:
|
case TREXIO_HDF5:
|
||||||
return trexio_hdf5_write_nucleus_coord(file, coord);
|
return trexio_hdf5_write_nucleus_coord(file, coord, rank, dims);
|
||||||
break;
|
break;
|
||||||
/*
|
/*
|
||||||
case TREXIO_JSON:
|
case TREXIO_JSON:
|
||||||
|
@ -402,43 +402,88 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
|
|||||||
return TREXIO_SUCCESS;
|
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 uint32_t rank, const uint64_t* dims) {
|
||||||
|
|
||||||
assert (file != NULL);
|
assert (file != NULL);
|
||||||
assert (coord != NULL);
|
assert (coord != NULL);
|
||||||
|
|
||||||
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
|
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
|
||||||
|
|
||||||
if (nucleus == NULL) return TREXIO_FAILURE;
|
herr_t status;
|
||||||
assert (nucleus->coord != NULL);
|
int rrank;
|
||||||
|
// get the rank of the dataset in a file
|
||||||
|
status = H5LTget_dataset_ndims (f->nucleus_group, NUCLEUS_COORD_NAME,
|
||||||
|
&rrank);
|
||||||
|
|
||||||
for (size_t i=0 ; i<3*nucleus->num ; i++) {
|
if (status < 0) return TREXIO_FAILURE;
|
||||||
coord[i] = nucleus->coord[i];
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
|
||||||
|
H5Dclose(dset_id);
|
||||||
|
if (status < 0) {
|
||||||
|
free(ddims);
|
||||||
|
return TREXIO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
trexio_hdf5_free_nucleus(nucleus);
|
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);
|
||||||
|
if (status < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
return TREXIO_SUCCESS;
|
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 uint32_t rank, const uint64_t* dims) {
|
||||||
|
|
||||||
assert (file != NULL);
|
assert (file != NULL);
|
||||||
assert (coord != NULL);
|
assert (coord != NULL);
|
||||||
|
|
||||||
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
|
uint64_t nucleus_num;
|
||||||
|
trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num));
|
||||||
|
if (rc != TREXIO_SUCCESS) return rc;
|
||||||
|
if (nucleus_num <= 0L) return TREXIO_INVALID_NUM;
|
||||||
|
|
||||||
if (nucleus == NULL) return TREXIO_FAILURE;
|
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
|
||||||
assert (nucleus->coord != NULL);
|
|
||||||
|
herr_t status;
|
||||||
|
if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) {
|
||||||
|
|
||||||
|
status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME,
|
||||||
|
(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);
|
||||||
|
if (dset_id <= 0) return TREXIO_INVALID_ID;
|
||||||
|
|
||||||
|
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, coord);
|
||||||
|
|
||||||
|
H5Dclose(dset_id);
|
||||||
|
if (status < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
trexio_hdf5_free_nucleus(nucleus);
|
|
||||||
|
|
||||||
return TREXIO_SUCCESS;
|
return TREXIO_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -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_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_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_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);
|
trexio_exit_code trexio_hdf5_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -560,49 +560,94 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
|
|||||||
The ~coord~ array is assumed allocated with the appropriate size.
|
The ~coord~ array is assumed allocated with the appropriate size.
|
||||||
|
|
||||||
#+begin_src c :tangle trexio_hdf5.h
|
#+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_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);
|
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
|
#+end_src
|
||||||
|
|
||||||
#+begin_src c :tangle trexio_hdf5.c
|
#+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 uint32_t rank, const uint64_t* dims) {
|
||||||
|
|
||||||
assert (file != NULL);
|
assert (file != NULL);
|
||||||
assert (coord != NULL);
|
assert (coord != NULL);
|
||||||
|
|
||||||
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
|
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
|
||||||
|
|
||||||
if (nucleus == NULL) return TREXIO_FAILURE;
|
herr_t status;
|
||||||
assert (nucleus->coord != NULL);
|
int rrank;
|
||||||
|
// get the rank of the dataset in a file
|
||||||
|
status = H5LTget_dataset_ndims (f->nucleus_group, NUCLEUS_COORD_NAME,
|
||||||
|
&rrank);
|
||||||
|
|
||||||
for (size_t i=0 ; i<3*nucleus->num ; i++) {
|
if (status < 0) return TREXIO_FAILURE;
|
||||||
coord[i] = nucleus->coord[i];
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
|
||||||
|
H5Dclose(dset_id);
|
||||||
|
if (status < 0) {
|
||||||
|
free(ddims);
|
||||||
|
return TREXIO_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
trexio_hdf5_free_nucleus(nucleus);
|
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);
|
||||||
|
if (status < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
return TREXIO_SUCCESS;
|
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 uint32_t rank, const uint64_t* dims) {
|
||||||
|
|
||||||
assert (file != NULL);
|
assert (file != NULL);
|
||||||
assert (coord != NULL);
|
assert (coord != NULL);
|
||||||
|
|
||||||
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
|
uint64_t nucleus_num;
|
||||||
|
trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num));
|
||||||
|
if (rc != TREXIO_SUCCESS) return rc;
|
||||||
|
if (nucleus_num <= 0L) return TREXIO_INVALID_NUM;
|
||||||
|
|
||||||
if (nucleus == NULL) return TREXIO_FAILURE;
|
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
|
||||||
assert (nucleus->coord != NULL);
|
|
||||||
|
herr_t status;
|
||||||
|
if ( H5LTfind_dataset(f->nucleus_group, NUCLEUS_COORD_NAME) != 1) {
|
||||||
|
|
||||||
|
status = H5LTmake_dataset_double (f->nucleus_group, NUCLEUS_COORD_NAME,
|
||||||
|
(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);
|
||||||
|
if (dset_id <= 0) return TREXIO_INVALID_ID;
|
||||||
|
|
||||||
|
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, coord);
|
||||||
|
|
||||||
|
H5Dclose(dset_id);
|
||||||
|
if (status < 0) return TREXIO_FAILURE;
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
trexio_hdf5_free_nucleus(nucleus);
|
|
||||||
|
|
||||||
return TREXIO_SUCCESS;
|
return TREXIO_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,7 +448,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
|
|||||||
if (file->parent.mode == 'w') {
|
if (file->parent.mode == 'w') {
|
||||||
rdm->file = fopen(file_name,"a");
|
rdm->file = fopen(file_name,"a");
|
||||||
} else {
|
} else {
|
||||||
rdm->file = fopen(file_name,"");
|
rdm->file = fopen(file_name,"r");
|
||||||
}
|
}
|
||||||
free(file_name);
|
free(file_name);
|
||||||
file->rdm = rdm ;
|
file->rdm = rdm ;
|
||||||
|
@ -99,6 +99,7 @@ typedef struct trexio_text_s {
|
|||||||
|
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
*** Init/deinit functions
|
||||||
|
|
||||||
#+begin_src c :tangle trexio_text.h
|
#+begin_src c :tangle trexio_text.h
|
||||||
trexio_exit_code trexio_text_init(trexio_t* file);
|
trexio_exit_code trexio_text_init(trexio_t* file);
|
||||||
@ -634,7 +635,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
|
|||||||
if (file->parent.mode == 'w') {
|
if (file->parent.mode == 'w') {
|
||||||
rdm->file = fopen(file_name,"a");
|
rdm->file = fopen(file_name,"a");
|
||||||
} else {
|
} else {
|
||||||
rdm->file = fopen(file_name,"");
|
rdm->file = fopen(file_name,"r");
|
||||||
}
|
}
|
||||||
free(file_name);
|
free(file_name);
|
||||||
file->rdm = rdm ;
|
file->rdm = rdm ;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user