From 945c0c2a7d4842d04f270940793132e6c32ad1b1 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Fri, 12 Mar 2021 16:18:11 +0100 Subject: [PATCH 1/3] Added MALLOC and FREE macros --- src/trexio.c | 17 +++++++------- src/trexio.org | 36 ++++++++++++++++++++++-------- src/trexio_hdf5.c | 36 +++++++++++++++--------------- src/trexio_hdf5.h | 1 + src/trexio_hdf5.org | 29 ++++++++++++------------ src/trexio_s.h | 1 + src/trexio_text.c | 53 ++++++++++++++++++-------------------------- src/trexio_text.h | 1 + src/trexio_text.org | 54 ++++++++++++++++++--------------------------- 9 files changed, 114 insertions(+), 114 deletions(-) diff --git a/src/trexio.c b/src/trexio.c index 554d46f..a5c8c49 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -83,8 +83,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -109,8 +109,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -143,8 +143,8 @@ trexio_exit_code trexio_close(trexio_t* file) { } if (rc != TREXIO_SUCCESS) { - free(file->file_name); - free(file); + FREE(file->file_name); + FREE(file); return TREXIO_FAILURE; } @@ -170,12 +170,11 @@ trexio_exit_code trexio_close(trexio_t* file) { /* Terminate front end */ - free(file->file_name); - file->file_name = NULL; + FREE(file->file_name); int irc = pthread_mutex_destroy( &(file->thread_lock) ); - free(file); + FREE(file); if (irc != 0) return TREXIO_ERRNO; if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; diff --git a/src/trexio.org b/src/trexio.org index 3b58cf2..f464ed3 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -46,6 +46,7 @@ #define _TREXIO_S_H #include "trexio.h" +#include "trexio_private.h" #include #include #+end_src @@ -61,6 +62,24 @@ - structs are suffixed by ~_s~ - types are suffixed by ~_t~ +** Memory allocation + + Memory allocation of structures can be facilitated by using the + following macro, which ensures that the size of the allocated + object is the same as the size of the data type pointed by the pointer. + + #+begin_src c :tangle trexio_private.h +#define MALLOC(T) (T*) malloc (sizeof(T)); +#define CALLOC(N,T) (T*) calloc ((N),sizeof(T)); + #+end_src + + When a pointer is freed, it should be set to ~NULL~. + This can be facilitated by the use of the following macro: + + #+begin_src c :tangle trexio_private.h +#define FREE(X) { free(X) ; (X)=NULL; } + #+end_src + * Front end All calls to TREXIO are thread-safe. @@ -223,8 +242,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -249,8 +268,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) { - free(result->file_name); - free(result); + FREE(result->file_name); + FREE(result); return NULL; } @@ -291,8 +310,8 @@ trexio_exit_code trexio_close(trexio_t* file) { } if (rc != TREXIO_SUCCESS) { - free(file->file_name); - free(file); + FREE(file->file_name); + FREE(file); return TREXIO_FAILURE; } @@ -318,12 +337,11 @@ trexio_exit_code trexio_close(trexio_t* file) { /* Terminate front end */ - free(file->file_name); - file->file_name = NULL; + FREE(file->file_name); int irc = pthread_mutex_destroy( &(file->thread_lock) ); - free(file); + FREE(file); if (irc != 0) return TREXIO_ERRNO; if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; diff --git a/src/trexio_hdf5.c b/src/trexio_hdf5.c index 2cfcdc1..1e1b2dc 100644 --- a/src/trexio_hdf5.c +++ b/src/trexio_hdf5.c @@ -7,10 +7,10 @@ #include "trexio_hdf5.h" -#define NUCLEUS_GROUP_NAME "nucleus" -#define NUCLEUS_NUM_NAME "nucleus_num" -#define NUCLEUS_CHARGE_NAME "nucleus_charge" -#define NUCLEUS_COORD_NAME "nucleus_coord" + #define NUCLEUS_GROUP_NAME "nucleus" + #define NUCLEUS_NUM_NAME "nucleus_num" + #define NUCLEUS_CHARGE_NAME "nucleus_charge" + #define NUCLEUS_COORD_NAME "nucleus_coord" /* * Currently H5LTread_dataset_ is used instead of this function @@ -24,7 +24,7 @@ dset_t* trexio_hdf5_read_dset_low(const trexio_hdf5_t* file, const char *dset_na /* * Low-level implementation. Involves dealing with all HDF5 handles and dimensions */ - dset_t* dset = (dset_t*) malloc(sizeof(dset_t)); + dset_t* dset = MALLOC(dset_t); assert (dset != NULL); dset->dset_id = H5Dopen(file->nucleus_group, @@ -145,7 +145,7 @@ trexio_exit_code trexio_hdf5_finalize(trexio_t* file) { h5nucleus_t* trexio_hdf5_read_nucleus(const trexio_hdf5_t* file) { /* Allocate the data structure */ - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + h5nucleus_t* nucleus = MALLOC(h5nucleus_t); assert (nucleus != NULL); nucleus->num = 0; @@ -323,19 +323,19 @@ trexio_exit_code trexio_hdf5_free_nucleus(h5nucleus_t* nucleus) { if (nucleus == NULL) return TREXIO_FAILURE; - if (nucleus->coord != NULL) free (nucleus->coord); - nucleus->coord = NULL; + if (nucleus->coord != NULL) + FREE (nucleus->coord); - if (nucleus->charge != NULL) free (nucleus->charge); - nucleus->charge = NULL; + if (nucleus->charge != NULL) + FREE (nucleus->charge); - if (nucleus->h5_coord != NULL) free (nucleus->h5_coord); - nucleus->h5_coord = NULL; + if (nucleus->h5_coord != NULL) + FREE (nucleus->h5_coord); - if (nucleus->h5_charge != NULL) free (nucleus->h5_charge); - nucleus->h5_charge = NULL; + if (nucleus->h5_charge != NULL) + FREE (nucleus->h5_charge); - free (nucleus); + FREE (nucleus); return TREXIO_SUCCESS; } @@ -446,17 +446,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co H5Dclose(dset_id); if (status < 0) { - free(ddims); + FREE(ddims); return TREXIO_FAILURE; } for (uint32_t i=0; inucleus_group, diff --git a/src/trexio_hdf5.h b/src/trexio_hdf5.h index f71360c..89106aa 100644 --- a/src/trexio_hdf5.h +++ b/src/trexio_hdf5.h @@ -9,6 +9,7 @@ #define _TREXIO_HDF5_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index f0c30b5..90d3493 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -19,6 +19,7 @@ #define _TREXIO_HDF5_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include @@ -115,7 +116,7 @@ dset_t* trexio_hdf5_read_dset_low(const trexio_hdf5_t* file, const char *dset_na /* * Low-level implementation. Involves dealing with all HDF5 handles and dimensions */ - dset_t* dset = (dset_t*) malloc(sizeof(dset_t)); + dset_t* dset = MALLOC(dset_t); assert (dset != NULL); dset->dset_id = H5Dopen(file->nucleus_group, @@ -280,7 +281,7 @@ typedef struct four_index_s { h5nucleus_t* trexio_hdf5_read_nucleus(const trexio_hdf5_t* file) { /* Allocate the data structure */ - h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t)); + h5nucleus_t* nucleus = MALLOC(h5nucleus_t); assert (nucleus != NULL); nucleus->num = 0; @@ -465,19 +466,19 @@ trexio_exit_code trexio_hdf5_free_nucleus(h5nucleus_t* nucleus) { if (nucleus == NULL) return TREXIO_FAILURE; - if (nucleus->coord != NULL) free (nucleus->coord); - nucleus->coord = NULL; + if (nucleus->coord != NULL) + FREE (nucleus->coord); - if (nucleus->charge != NULL) free (nucleus->charge); - nucleus->charge = NULL; + if (nucleus->charge != NULL) + FREE (nucleus->charge); - if (nucleus->h5_coord != NULL) free (nucleus->h5_coord); - nucleus->h5_coord = NULL; + if (nucleus->h5_coord != NULL) + FREE (nucleus->h5_coord); - if (nucleus->h5_charge != NULL) free (nucleus->h5_charge); - nucleus->h5_charge = NULL; + if (nucleus->h5_charge != NULL) + FREE (nucleus->h5_charge); - free (nucleus); + FREE (nucleus); return TREXIO_SUCCESS; } @@ -607,17 +608,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co H5Dclose(dset_id); if (status < 0) { - free(ddims); + FREE(ddims); return TREXIO_FAILURE; } for (uint32_t i=0; inucleus_group, diff --git a/src/trexio_s.h b/src/trexio_s.h index 4810d10..315c90f 100644 --- a/src/trexio_s.h +++ b/src/trexio_s.h @@ -9,6 +9,7 @@ #define _TREXIO_S_H #include "trexio.h" +#include "trexio_private.h" #include #include diff --git a/src/trexio_text.c b/src/trexio_text.c index 6bd36c9..55019e5 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -36,7 +36,7 @@ trexio_exit_code trexio_text_init(trexio_t* file) { f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); assert (f->lock_file > 0); - free(file_name); + FREE(file_name); f->nucleus = NULL; f->electron= NULL; @@ -102,7 +102,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { if (file->nucleus != NULL) return file->nucleus; /* Allocate the data structure */ - nucleus_t* nucleus = (nucleus_t*) malloc(sizeof(nucleus_t)); + nucleus_t* nucleus = MALLOC(nucleus_t); assert (nucleus != NULL); nucleus->file = NULL; @@ -130,7 +130,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -212,7 +212,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { rc = fscanf(f, "%lf", &(nucleus->coord[i])); assert (rc == 1); } - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -221,7 +221,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { } else { nucleus->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->nucleus = nucleus; return nucleus; } @@ -293,27 +293,22 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { } if (nucleus->dims_coord != NULL) { - free (nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE (nucleus->dims_coord); } if (nucleus->coord != NULL) { - free (nucleus->coord); - nucleus->coord = NULL; + FREE (nucleus->coord); } if (nucleus->dims_charge != NULL) { - free (nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE (nucleus->dims_charge); } if (nucleus->charge != NULL) { - free (nucleus->charge); - nucleus->charge = NULL; + FREE (nucleus->charge); } - free (nucleus); - file->nucleus = NULL; + FREE (nucleus); return TREXIO_SUCCESS; } @@ -380,13 +375,11 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->coord != NULL) { - free(nucleus->coord); - nucleus->coord = NULL; + FREE(nucleus->coord); } if (nucleus->dims_coord != NULL) { - free(nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE(nucleus->dims_coord); } nucleus->rank_coord = rank; @@ -442,13 +435,11 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->charge != NULL) { - free(nucleus->charge); - nucleus->charge = NULL; + FREE(nucleus->charge); } if (nucleus->dims_charge != NULL) { - free(nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE(nucleus->dims_charge); } nucleus->rank_charge = rank; @@ -477,7 +468,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file->rdm != NULL) return file->rdm; /* Allocate the data structure */ - rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t)); + rdm_t* rdm = MALLOC(rdm_t); assert (rdm != NULL); rdm->one_e = NULL; @@ -502,7 +493,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -534,10 +525,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { rc = fscanf(f, "%s", buffer); assert (rc == 1); - rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char)); + rdm->two_e_file_name = CALLOC (strlen(buffer),char); strcpy(rdm->two_e_file_name, buffer); - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -546,7 +537,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { } else { rdm->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->rdm = rdm ; return rdm; } @@ -600,13 +591,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { } if (rdm->one_e != NULL) { - free (rdm->one_e); - rdm->one_e = NULL; + FREE (rdm->one_e); } if (rdm->two_e_file_name != NULL) { - free (rdm->two_e_file_name); - rdm->two_e_file_name = NULL; + FREE (rdm->two_e_file_name); } free (rdm); diff --git a/src/trexio_text.h b/src/trexio_text.h index 806d180..13117fa 100644 --- a/src/trexio_text.h +++ b/src/trexio_text.h @@ -9,6 +9,7 @@ #define _TREXIO_TEXT_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include diff --git a/src/trexio_text.org b/src/trexio_text.org index 5104e4c..08be523 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -19,6 +19,7 @@ #define _TREXIO_TEXT_H #include "trexio.h" +#include "trexio_private.h" #include "trexio_s.h" #include #include @@ -137,7 +138,7 @@ trexio_exit_code trexio_text_init(trexio_t* file) { f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); assert (f->lock_file > 0); - free(file_name); + FREE(file_name); f->nucleus = NULL; f->electron= NULL; @@ -234,7 +235,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { if (file->nucleus != NULL) return file->nucleus; /* Allocate the data structure */ - nucleus_t* nucleus = (nucleus_t*) malloc(sizeof(nucleus_t)); + nucleus_t* nucleus = MALLOC(nucleus_t); assert (nucleus != NULL); nucleus->file = NULL; @@ -262,7 +263,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -344,7 +345,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { rc = fscanf(f, "%lf", &(nucleus->coord[i])); assert (rc == 1); } - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -353,7 +354,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { } else { nucleus->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->nucleus = nucleus; return nucleus; } @@ -443,27 +444,22 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { } if (nucleus->dims_coord != NULL) { - free (nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE (nucleus->dims_coord); } if (nucleus->coord != NULL) { - free (nucleus->coord); - nucleus->coord = NULL; + FREE (nucleus->coord); } if (nucleus->dims_charge != NULL) { - free (nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE (nucleus->dims_charge); } if (nucleus->charge != NULL) { - free (nucleus->charge); - nucleus->charge = NULL; + FREE (nucleus->charge); } - free (nucleus); - file->nucleus = NULL; + FREE (nucleus); return TREXIO_SUCCESS; } #+end_src @@ -550,13 +546,11 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->coord != NULL) { - free(nucleus->coord); - nucleus->coord = NULL; + FREE(nucleus->coord); } if (nucleus->dims_coord != NULL) { - free(nucleus->dims_coord); - nucleus->dims_coord = NULL; + FREE(nucleus->dims_coord); } nucleus->rank_coord = rank; @@ -622,13 +616,11 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do if (nucleus == NULL) return TREXIO_FAILURE; if (nucleus->charge != NULL) { - free(nucleus->charge); - nucleus->charge = NULL; + FREE(nucleus->charge); } if (nucleus->dims_charge != NULL) { - free(nucleus->dims_charge); - nucleus->dims_charge = NULL; + FREE(nucleus->dims_charge); } nucleus->rank_charge = rank; @@ -666,7 +658,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file->rdm != NULL) return file->rdm; /* Allocate the data structure */ - rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t)); + rdm_t* rdm = MALLOC(rdm_t); assert (rdm != NULL); rdm->one_e = NULL; @@ -691,7 +683,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); - char* buffer = (char*) malloc(sz*sizeof(char)); + char* buffer = CALLOC(sz,char); /* Read the dimensioning variables */ int rc; @@ -723,10 +715,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { rc = fscanf(f, "%s", buffer); assert (rc == 1); - rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char)); + rdm->two_e_file_name = CALLOC (strlen(buffer),char); strcpy(rdm->two_e_file_name, buffer); - free(buffer); + FREE(buffer); fclose(f); f = NULL; } @@ -735,7 +727,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { } else { rdm->file = fopen(file_name,"r"); } - free(file_name); + FREE(file_name); file->rdm = rdm ; return rdm; } @@ -807,13 +799,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { } if (rdm->one_e != NULL) { - free (rdm->one_e); - rdm->one_e = NULL; + FREE (rdm->one_e); } if (rdm->two_e_file_name != NULL) { - free (rdm->two_e_file_name); - rdm->two_e_file_name = NULL; + FREE (rdm->two_e_file_name); } free (rdm); From e6a211127c3bda3d8628a1ac0bc0827fab7bad5f Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sat, 13 Mar 2021 00:19:39 +0100 Subject: [PATCH 2/3] Improved checks in text backend --- src/trexio.c | 2 +- src/trexio.h | 25 ++-- src/trexio.org | 27 ++-- src/trexio_text.c | 300 +++++++++++++++++++++++++++++----------- src/trexio_text.h | 102 +++++++++++++- src/trexio_text.org | 329 ++++++++++++++++++++++++++++++-------------- 6 files changed, 579 insertions(+), 206 deletions(-) diff --git a/src/trexio.c b/src/trexio.c index a5c8c49..9f86ab7 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -53,7 +53,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b /* Data for the parent type */ - result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char)); + result->file_name = CALLOC(strlen(file_name)+1,char); strcpy(result->file_name, file_name); result->back_end = back_end; result->mode = mode; diff --git a/src/trexio.h b/src/trexio.h index f36285f..79940e6 100644 --- a/src/trexio.h +++ b/src/trexio.h @@ -12,18 +12,19 @@ typedef int32_t trexio_exit_code; -#define TREXIO_FAILURE ( (trexio_exit_code) -1 ) -#define TREXIO_SUCCESS ( (trexio_exit_code) 0 ) -#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 ) -#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 ) -#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 ) -#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 ) -#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_ERRNO ( (trexio_exit_code) 12 ) -#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) -#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 ) +#define TREXIO_FAILURE ( (trexio_exit_code) -1 ) +#define TREXIO_SUCCESS ( (trexio_exit_code) 0 ) +#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 ) +#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 ) +#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 ) +#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 ) +#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_ERRNO ( (trexio_exit_code) 12 ) +#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) +#define TREXIO_ALLOCATION_FAILED ( (trexio_exit_code) 21 ) +#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 ) typedef int32_t back_end_t; diff --git a/src/trexio.org b/src/trexio.org index f464ed3..1d936b1 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -88,18 +88,19 @@ #+begin_src c :tangle trexio.h typedef int32_t trexio_exit_code; -#define TREXIO_FAILURE ( (trexio_exit_code) -1 ) -#define TREXIO_SUCCESS ( (trexio_exit_code) 0 ) -#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 ) -#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 ) -#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 ) -#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 ) -#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_ERRNO ( (trexio_exit_code) 12 ) -#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) -#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 ) +#define TREXIO_FAILURE ( (trexio_exit_code) -1 ) +#define TREXIO_SUCCESS ( (trexio_exit_code) 0 ) +#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 ) +#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 ) +#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 ) +#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 ) +#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_ERRNO ( (trexio_exit_code) 12 ) +#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) +#define TREXIO_ALLOCATION_FAILED ( (trexio_exit_code) 21 ) +#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 ) #+end_src ** Back ends @@ -212,7 +213,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b /* Data for the parent type */ - result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char)); + result->file_name = CALLOC(strlen(file_name)+1,char); strcpy(result->file_name, file_name); result->back_end = back_end; result->mode = mode; diff --git a/src/trexio_text.c b/src/trexio_text.c index 55019e5..50758e6 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -1,3 +1,4 @@ +/* [[file:trexio_text.org::*File prefixes][File prefixes:4]] */ /* This file was generated from the trexio.org org-mode file. To generate it, open trexio.org in Emacs and execute M-x org-babel-tangle @@ -6,12 +7,17 @@ #include "trexio_text.h" +/* File prefixes:4 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:2]] */ trexio_exit_code trexio_text_init(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; trexio_text_t* f = (trexio_text_t*) file; + /* Put all pointers to NULL but leave parent untouched */ + memset(&(f->parent)+1,0,sizeof(trexio_text_t)-sizeof(trexio_t)); + /* If directory doesn't exist, create it in write mode */ struct stat st; @@ -27,24 +33,28 @@ trexio_exit_code trexio_text_init(trexio_t* file) { /* Create the lock file in the directory */ const char* lock_file_name = "/.lock"; - char* file_name = (char*) - calloc( strlen(file->file_name) + strlen(lock_file_name) + 1, - sizeof(char)); - assert (file_name != NULL); + char* file_name = + CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char); + + if (file_name == NULL) { + return TREXIO_ALLOCATION_FAILED; + } + strcpy (file_name, file->file_name); strcat (file_name, lock_file_name); f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); - assert (f->lock_file > 0); FREE(file_name); - f->nucleus = NULL; - f->electron= NULL; - f->rdm = NULL; - + if (f->lock_file <= 0) { + return TREXIO_FAILURE; + } + return TREXIO_SUCCESS; } +/* Init/deinit functions:2 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:4]] */ trexio_exit_code trexio_text_lock(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -63,7 +73,9 @@ trexio_exit_code trexio_text_lock(trexio_t* file) { return TREXIO_SUCCESS; } +/* Init/deinit functions:4 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:6]] */ trexio_exit_code trexio_text_finalize(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -76,7 +88,9 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) { return TREXIO_SUCCESS; } +/* Init/deinit functions:6 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:8]] */ trexio_exit_code trexio_text_unlock(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -95,30 +109,37 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) { close(f->lock_file); return TREXIO_SUCCESS; } +/* Init/deinit functions:8 ends here */ + +/* [[file:trexio_text.org::*Read the struct][Read the struct:2]] */ +#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__); nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { if (file == NULL) return NULL; - if (file->nucleus != NULL) return file->nucleus; + /* If the data structure exists, return it */ + if (file->nucleus != NULL) { + return file->nucleus; + } /* Allocate the data structure */ nucleus_t* nucleus = MALLOC(nucleus_t); - assert (nucleus != NULL); + if (nucleus == NULL) return NULL; - nucleus->file = NULL; - nucleus->num = 0; - nucleus->coord = NULL; - nucleus->dims_coord = NULL; - nucleus->charge = NULL; - nucleus->dims_charge = NULL; - nucleus->to_flush = 0; + memset(nucleus,0,sizeof(nucleus_t)); - /* Try to open the file. If the file does not exist, return */ + /* Build the file name */ const char* nucleus_file_name = "/nucleus.txt"; char * file_name = (char*) calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1, sizeof(char)); - assert (file_name != NULL); + + if (file_name == NULL) { + FREE(nucleus); +DEBUG + return NULL; + } + strcpy (file_name, file->parent.file_name); strcat (file_name, nucleus_file_name); @@ -130,102 +151,225 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); + char* buffer = CALLOC(sz,char); + if (buffer == NULL) { + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } /* Read the dimensioning variables */ int rc; + rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "rank_charge") == 0); + if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%u", &(nucleus->rank_charge)); - assert (rc == 1); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } - nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t)); - assert (nucleus->dims_charge != NULL); - uint64_t size_charge = 1; for (uint i=0; irank_charge; i++){ - rc = fscanf(f, "%s", buffer); - assert (rc == 1); - //assert (strcmp(buffer, "dims_charge") == 0); + uint j=-1; + rc = fscanf(f, "%s %u", buffer, &j); + if ((rc != 2) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } - rc = fscanf(f, "%lu", &(nucleus->dims_charge[i])); - assert (rc == 1); + rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i])); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } size_charge *= nucleus->dims_charge[i]; } rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "rank_coord") == 0); + if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%u", &(nucleus->rank_coord)); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } - nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t)); - assert (nucleus->dims_coord != NULL); - uint64_t size_coord = 1; for (uint i=0; irank_coord; i++){ - rc = fscanf(f, "%s", buffer); - assert (rc == 1); - //assert (strcmp(buffer, "dims_coord") == 0); + uint j=-1; + rc = fscanf(f, "%s %u", buffer, &j); + if ((rc != 2) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%lu", &(nucleus->dims_coord[i])); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } size_coord *= nucleus->dims_coord[i]; } /* Allocate arrays */ nucleus->charge = (double*) calloc(size_charge, sizeof(double)); - assert (nucleus->charge != NULL); + assert (!(nucleus->charge == NULL)); + if (nucleus->charge == NULL) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } nucleus->coord = (double*) calloc(size_coord, sizeof(double)); - assert (nucleus->coord != NULL); + assert (!(nucleus->coord == NULL)); + if (nucleus->coord == NULL) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } /* Read data */ rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "num") == 0); + assert(!((rc != 1) || (strcmp(buffer, "num") != 0))); + if ((rc != 1) || (strcmp(buffer, "num") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%lu", &(nucleus->num)); - assert (rc == 1); - - rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "charge") == 0); - - for (uint64_t i=0 ; icharge[i])); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; } rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "coord") == 0); + assert(!((rc != 1) || (strcmp(buffer, "charge") != 0))); + if ((rc != 1) || (strcmp(buffer, "charge") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } + + for (uint64_t i=0 ; icharge[i])); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } + } + + rc = fscanf(f, "%s", buffer); + assert(!((rc != 1) || (strcmp(buffer, "coord") != 0))); + if ((rc != 1) || (strcmp(buffer, "coord") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } for (uint64_t i=0 ; icoord[i])); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } } FREE(buffer); fclose(f); f = NULL; } + if (file->parent.mode == 'w') { nucleus->file = fopen(file_name,"a"); } else { nucleus->file = fopen(file_name,"r"); } FREE(file_name); + assert (!(nucleus->file == NULL)); + if (nucleus->file == NULL) { + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } + + fseek(nucleus->file, 0L, SEEK_SET); file->nucleus = nucleus; return nucleus; } +/* Read the struct:2 ends here */ +/* [[file:trexio_text.org::*Flush the struct][Flush the struct:2]] */ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -246,7 +390,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { uint64_t size_charge = 1; for (uint i=0; irank_charge; i++){ - fprintf(f, "dims_charge[%d] %ld\n", i, nucleus->dims_charge[i]); + fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]); size_charge *= nucleus->dims_charge[i]; } @@ -254,7 +398,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { uint64_t size_coord = 1; for (uint i=0; irank_coord; i++){ - fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]); + fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]); size_coord *= nucleus->dims_coord[i]; } @@ -273,7 +417,9 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { file->nucleus->to_flush = 0; return TREXIO_SUCCESS; } +/* Flush the struct:2 ends here */ +/* [[file:trexio_text.org::*Free memory][Free memory:2]] */ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -292,18 +438,10 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { nucleus->file = NULL; } - if (nucleus->dims_coord != NULL) { - FREE (nucleus->dims_coord); - } - if (nucleus->coord != NULL) { FREE (nucleus->coord); } - if (nucleus->dims_charge != NULL) { - FREE (nucleus->dims_charge); - } - if (nucleus->charge != NULL) { FREE (nucleus->charge); } @@ -311,7 +449,9 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { FREE (nucleus); return TREXIO_SUCCESS; } +/* Free memory:2 ends here */ +/* [[file:trexio_text.org::*Read/Write the num attribute][Read/Write the num attribute:2]] */ trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -340,7 +480,9 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6 return TREXIO_SUCCESS; } +/* Read/Write the num attribute:2 ends here */ +/* [[file:trexio_text.org::*Read/Write the coord attribute][Read/Write the coord attribute:2]] */ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -378,12 +520,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou FREE(nucleus->coord); } - if (nucleus->dims_coord != NULL) { - FREE(nucleus->dims_coord); - } - nucleus->rank_coord = rank; - nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t)); uint64_t dim_size = 1; for (uint i=0; irank_coord; i++){ @@ -400,7 +537,9 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou nucleus->to_flush = 1; return TREXIO_SUCCESS; } +/* Read/Write the coord attribute:2 ends here */ +/* [[file:trexio_text.org::*Read/Write the charge attribute][Read/Write the charge attribute:2]] */ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -438,14 +577,8 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do FREE(nucleus->charge); } - if (nucleus->dims_charge != NULL) { - FREE(nucleus->dims_charge); - } - nucleus->rank_charge = rank; - nucleus->dims_charge = (uint64_t*) calloc(rank, sizeof(uint64_t)); - uint64_t dim_size = 1; for (uint i=0; irank_charge; i++){ nucleus->dims_charge[i] = dims[i]; @@ -461,7 +594,9 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do nucleus->to_flush = 1; return TREXIO_SUCCESS; } +/* Read/Write the charge attribute:2 ends here */ +/* [[file:trexio_text.org::*Read the complete struct][Read the complete struct:2]] */ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file == NULL) return NULL; @@ -541,7 +676,9 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { file->rdm = rdm ; return rdm; } +/* Read the complete struct:2 ends here */ +/* [[file:trexio_text.org::*Flush the complete struct][Flush the complete struct:2]] */ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -572,7 +709,9 @@ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) { file->rdm->to_flush = 0; return TREXIO_SUCCESS; } +/* Flush the complete struct:2 ends here */ +/* [[file:trexio_text.org::*Free memory][Free memory:2]] */ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -602,7 +741,9 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { file->rdm = NULL; return TREXIO_SUCCESS; } +/* Free memory:2 ends here */ +/* [[file:trexio_text.org::*Read/Write the one_e attribute][Read/Write the one_e attribute:2]] */ trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -637,7 +778,9 @@ trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* rdm->to_flush = 1; return TREXIO_SUCCESS; } +/* Read/Write the one_e attribute:2 ends here */ +/* [[file:trexio_text.org::*Read/Write the two_e attribute][Read/Write the two_e attribute:2]] */ trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -702,3 +845,4 @@ trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, cons return TREXIO_SUCCESS; } +/* Read/Write the two_e attribute:2 ends here */ diff --git a/src/trexio_text.h b/src/trexio_text.h index 13117fa..9f6f50d 100644 --- a/src/trexio_text.h +++ b/src/trexio_text.h @@ -1,3 +1,4 @@ +/* [[file:trexio_text.org::*File prefixes][File prefixes:3]] */ /* This file was generated from the trexio.org org-mode file. To generate it, open trexio.org in Emacs and execute M-x org-babel-tangle @@ -20,14 +21,18 @@ #include #include #include +/* File prefixes:3 ends here */ +/* Structs for blocks */ + +/* [[file:trexio_text.org::*Structs for blocks][Structs for blocks:1]] */ typedef struct nucleus_s { FILE* file; double* coord; double* charge; uint64_t num; - uint64_t* dims_charge; - uint64_t* dims_coord; + uint64_t dims_charge[16]; + uint64_t dims_coord[16]; uint32_t rank_charge; uint32_t rank_coord; int to_flush; @@ -47,7 +52,12 @@ typedef struct rdm_s { char* two_e_file_name; int to_flush; } rdm_t; +/* Structs for blocks:1 ends here */ +/* Structs for the text back end */ + + +/* [[file:trexio_text.org::*Structs for the text back end][Structs for the text back end:1]] */ typedef struct trexio_text_s { trexio_t parent ; int lock_file; @@ -56,40 +66,128 @@ typedef struct trexio_text_s { electron_t* electron; rdm_t* rdm; } trexio_text_t; +/* Structs for the text back end:1 ends here */ +/* Init/deinit functions */ + + +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:1]] */ trexio_exit_code trexio_text_init(trexio_t* file); +/* Init/deinit functions:1 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:3]] */ trexio_exit_code trexio_text_lock(trexio_t* file); +/* Init/deinit functions:3 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:5]] */ trexio_exit_code trexio_text_finalize(trexio_t* file); +/* Init/deinit functions:5 ends here */ +/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:7]] */ trexio_exit_code trexio_text_unlock(trexio_t* file); +/* Init/deinit functions:7 ends here */ +/* Read the struct */ + + +/* [[file:trexio_text.org::*Read the struct][Read the struct:1]] */ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file); +/* Read the struct:1 ends here */ +/* Flush the struct */ + + +/* [[file:trexio_text.org::*Flush the struct][Flush the struct:1]] */ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file); +/* Flush the struct:1 ends here */ +/* Free memory */ + +/* Memory is allocated when reading. The following function frees memory. */ + + +/* [[file:trexio_text.org::*Free memory][Free memory:1]] */ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file); +/* Free memory:1 ends here */ +/* Read/Write the num attribute */ + + +/* [[file:trexio_text.org::*Read/Write the num attribute][Read/Write the num attribute:1]] */ trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num); trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint64_t num); +/* Read/Write the num attribute:1 ends here */ +/* Read/Write the coord attribute */ + +/* The ~coord~ array is assumed allocated with the appropriate size. */ + + +/* [[file:trexio_text.org::*Read/Write the coord attribute][Read/Write the coord attribute:1]] */ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims); trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims); +/* Read/Write the coord attribute:1 ends here */ +/* Read/Write the charge attribute */ + +/* The ~charge~ array is assumed allocated with the appropriate size. */ + + +/* [[file:trexio_text.org::*Read/Write the charge attribute][Read/Write the charge attribute:1]] */ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims); trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint32_t rank, const uint64_t* dims); +/* Read/Write the charge attribute:1 ends here */ +/* Read the complete struct */ + + +/* [[file:trexio_text.org::*Read the complete struct][Read the complete struct:1]] */ rdm_t* trexio_text_read_rdm(trexio_text_t* file); +/* Read the complete struct:1 ends here */ +/* Flush the complete struct */ + + +/* [[file:trexio_text.org::*Flush the complete struct][Flush the complete struct:1]] */ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file); +/* Flush the complete struct:1 ends here */ +/* Free memory */ + +/* Memory is allocated when reading. The followig function frees memory. */ + + +/* [[file:trexio_text.org::*Free memory][Free memory:1]] */ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file); +/* Free memory:1 ends here */ +/* Read/Write the one_e attribute */ + +/* The ~one_e~ array is assumed allocated with the appropriate size. */ + + +/* [[file:trexio_text.org::*Read/Write the one_e attribute][Read/Write the one_e attribute:1]] */ trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e); trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* one_e, const uint64_t dim_one_e); +/* Read/Write the one_e attribute:1 ends here */ +/* Read/Write the two_e attribute */ + +/* ~two_e~ is a sparse data structure, which can be too large to fit */ +/* in memory. So we provide functions to read and write it by */ +/* chunks. */ +/* In the text back end, the easiest way to do it is to create a */ +/* file for each sparse float structure. */ + + +/* [[file:trexio_text.org::*Read/Write the two_e attribute][Read/Write the two_e attribute:1]] */ trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value); trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, const int64_t* index, const double* value); +/* Read/Write the two_e attribute:1 ends here */ +/* File suffixes :noxport: */ + + +/* [[file:trexio_text.org::*File suffixes][File suffixes:1]] */ #endif +/* File suffixes:1 ends here */ diff --git a/src/trexio_text.org b/src/trexio_text.org index 08be523..9de9e06 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -2,6 +2,19 @@ * File prefixes :noxport: + #+begin_src emacs-lisp + (setq-local org-babel-default-header-args:c '((:comments . "both"))) + org-babel-default-header-args:c +#+end_src + +#+RESULTS: +: ((:comments . both)) + +# Local Variables: +# eval: (setq-local org-babel-default-header-args:Python '((:session . "foo"))) +# End: + + #+NAME:header #+begin_src c /* This file was generated from the trexio.org org-mode file. @@ -34,7 +47,13 @@ #+end_src #+begin_src c :tangle trexio_text.c :noweb yes -<
> +/* This file was generated from the trexio.org org-mode file. + To generate it, open trexio.org in Emacs and execute + M-x org-babel-tangle +*/ + + + #include "trexio_text.h" #+end_src @@ -59,8 +78,8 @@ typedef struct nucleus_s { double* coord; double* charge; uint64_t num; - uint64_t* dims_charge; - uint64_t* dims_coord; + uint64_t dims_charge[16]; + uint64_t dims_coord[16]; uint32_t rank_charge; uint32_t rank_coord; int to_flush; @@ -114,6 +133,9 @@ trexio_exit_code trexio_text_init(trexio_t* file) { trexio_text_t* f = (trexio_text_t*) file; + /* Put all pointers to NULL but leave parent untouched */ + memset(&(f->parent)+1,0,sizeof(trexio_text_t)-sizeof(trexio_t)); + /* If directory doesn't exist, create it in write mode */ struct stat st; @@ -129,24 +151,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) { /* Create the lock file in the directory */ const char* lock_file_name = "/.lock"; - char* file_name = (char*) - calloc( strlen(file->file_name) + strlen(lock_file_name) + 1, - sizeof(char)); - assert (file_name != NULL); + char* file_name = + CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char); + + if (file_name == NULL) { + return TREXIO_ALLOCATION_FAILED; + } + strcpy (file_name, file->file_name); strcat (file_name, lock_file_name); f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); - assert (f->lock_file > 0); FREE(file_name); - f->nucleus = NULL; - f->electron= NULL; - f->rdm = NULL; - + if (f->lock_file <= 0) { + return TREXIO_FAILURE; + } + return TREXIO_SUCCESS; } - #+end_src #+begin_src c :tangle trexio_text.h @@ -172,7 +195,6 @@ trexio_exit_code trexio_text_lock(trexio_t* file) { return TREXIO_SUCCESS; } - #+end_src @@ -229,29 +251,34 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file); #+end_src #+begin_src c :tangle trexio_text.c +#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__); + nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { if (file == NULL) return NULL; - if (file->nucleus != NULL) return file->nucleus; + /* If the data structure exists, return it */ + if (file->nucleus != NULL) { + return file->nucleus; + } /* Allocate the data structure */ nucleus_t* nucleus = MALLOC(nucleus_t); - assert (nucleus != NULL); + if (nucleus == NULL) return NULL; - nucleus->file = NULL; - nucleus->num = 0; - nucleus->coord = NULL; - nucleus->dims_coord = NULL; - nucleus->charge = NULL; - nucleus->dims_charge = NULL; - nucleus->to_flush = 0; + memset(nucleus,0,sizeof(nucleus_t)); - /* Try to open the file. If the file does not exist, return */ + /* Build the file name */ const char* nucleus_file_name = "/nucleus.txt"; char * file_name = (char*) calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1, sizeof(char)); - assert (file_name != NULL); + + if (file_name == NULL) { + FREE(nucleus); +DEBUG + return NULL; + } + strcpy (file_name, file->parent.file_name); strcat (file_name, nucleus_file_name); @@ -263,98 +290,219 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { fseek(f, 0L, SEEK_END); size_t sz = ftell(f); fseek(f, 0L, SEEK_SET); + char* buffer = CALLOC(sz,char); + if (buffer == NULL) { + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } /* Read the dimensioning variables */ int rc; + rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "rank_charge") == 0); + if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%u", &(nucleus->rank_charge)); - assert (rc == 1); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } - nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t)); - assert (nucleus->dims_charge != NULL); - uint64_t size_charge = 1; for (uint i=0; irank_charge; i++){ - rc = fscanf(f, "%s", buffer); - assert (rc == 1); - //assert (strcmp(buffer, "dims_charge") == 0); + uint j=-1; + rc = fscanf(f, "%s %u", buffer, &j); + if ((rc != 1) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } - rc = fscanf(f, "%lu", &(nucleus->dims_charge[i])); - assert (rc == 1); + rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i])); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } size_charge *= nucleus->dims_charge[i]; } rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "rank_coord") == 0); + if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%u", &(nucleus->rank_coord)); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } - nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t)); - assert (nucleus->dims_coord != NULL); - uint64_t size_coord = 1; for (uint i=0; irank_coord; i++){ - rc = fscanf(f, "%s", buffer); - assert (rc == 1); - //assert (strcmp(buffer, "dims_coord") == 0); + uint j=-1; + rc = fscanf(f, "%s %u", buffer, &j); + if ((rc != 1) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%lu", &(nucleus->dims_coord[i])); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } size_coord *= nucleus->dims_coord[i]; } /* Allocate arrays */ nucleus->charge = (double*) calloc(size_charge, sizeof(double)); - assert (nucleus->charge != NULL); + assert (!(nucleus->charge == NULL)); + if (nucleus->charge == NULL) { + FREE(buffer); + FREE(file_name); + FREE(nucleus); +DEBUG + return NULL; + } nucleus->coord = (double*) calloc(size_coord, sizeof(double)); - assert (nucleus->coord != NULL); + assert (!(nucleus->coord == NULL)); + if (nucleus->coord == NULL) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } /* Read data */ rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "num") == 0); + assert(!((rc != 1) || (strcmp(buffer, "num") != 0))); + if ((rc != 1) || (strcmp(buffer, "num") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } rc = fscanf(f, "%lu", &(nucleus->num)); - assert (rc == 1); - - rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "charge") == 0); - - for (uint64_t i=0 ; icharge[i])); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; } rc = fscanf(f, "%s", buffer); - assert (rc == 1); - assert (strcmp(buffer, "coord") == 0); + assert(!((rc != 1) || (strcmp(buffer, "charge") != 0))); + if ((rc != 1) || (strcmp(buffer, "charge") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } + + for (uint64_t i=0 ; icharge[i])); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } + } + + rc = fscanf(f, "%s", buffer); + assert(!((rc != 1) || (strcmp(buffer, "coord") != 0))); + if ((rc != 1) || (strcmp(buffer, "coord") != 0)) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } for (uint64_t i=0 ; icoord[i])); - assert (rc == 1); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + FREE(file_name); + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } } FREE(buffer); fclose(f); f = NULL; } + if (file->parent.mode == 'w') { nucleus->file = fopen(file_name,"a"); } else { nucleus->file = fopen(file_name,"r"); } FREE(file_name); + assert (!(nucleus->file == NULL)); + if (nucleus->file == NULL) { + FREE(nucleus->charge); + FREE(nucleus); +DEBUG + return NULL; + } + + fseek(nucleus->file, 0L, SEEK_SET); file->nucleus = nucleus; return nucleus; } @@ -387,7 +535,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { uint64_t size_charge = 1; for (uint i=0; irank_charge; i++){ - fprintf(f, "dims_charge[%d] %ld\n", i, nucleus->dims_charge[i]); + fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]); size_charge *= nucleus->dims_charge[i]; } @@ -395,7 +543,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { uint64_t size_coord = 1; for (uint i=0; irank_coord; i++){ - fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]); + fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]); size_coord *= nucleus->dims_coord[i]; } @@ -425,41 +573,33 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file); #+end_src #+begin_src c :tangle trexio_text.c -trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { +trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; trexio_exit_code rc; - if (file->parent.mode != 'r') { - rc = trexio_text_flush_nucleus(file); + rc = trexio_text_flush_rdm(file); if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; } - nucleus_t* nucleus = file->nucleus; - if (nucleus == NULL) return TREXIO_SUCCESS; + rdm_t* rdm = file->rdm; + if (rdm == NULL) return TREXIO_SUCCESS; - if (nucleus->file != NULL) { - fclose(nucleus->file); - nucleus->file = NULL; - } - - if (nucleus->dims_coord != NULL) { - FREE (nucleus->dims_coord); - } - - if (nucleus->coord != NULL) { - FREE (nucleus->coord); - } - - if (nucleus->dims_charge != NULL) { - FREE (nucleus->dims_charge); - } - - if (nucleus->charge != NULL) { - FREE (nucleus->charge); + if (rdm->file != NULL) { + fclose(rdm->file); + rdm->file = NULL; } - FREE (nucleus); + if (rdm->one_e != NULL) { + FREE (rdm->one_e); + } + + if (rdm->two_e_file_name != NULL) { + FREE (rdm->two_e_file_name); + } + + free (rdm); + file->rdm = NULL; return TREXIO_SUCCESS; } #+end_src @@ -549,12 +689,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou FREE(nucleus->coord); } - if (nucleus->dims_coord != NULL) { - FREE(nucleus->dims_coord); - } - nucleus->rank_coord = rank; - nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t)); uint64_t dim_size = 1; for (uint i=0; irank_coord; i++){ @@ -619,14 +754,8 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do FREE(nucleus->charge); } - if (nucleus->dims_charge != NULL) { - FREE(nucleus->dims_charge); - } - nucleus->rank_charge = rank; - nucleus->dims_charge = (uint64_t*) calloc(rank, sizeof(uint64_t)); - uint64_t dim_size = 1; for (uint i=0; irank_charge; i++){ nucleus->dims_charge[i] = dims[i]; From 455168efc6e9ac27a3a0d1d8dba976aba0566db4 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sat, 13 Mar 2021 00:56:16 +0100 Subject: [PATCH 3/3] Fixed --- src/trexio_text.c | 52 +++++------------------- src/trexio_text.h | 98 --------------------------------------------- src/trexio_text.org | 65 ++++++++++++++---------------- 3 files changed, 39 insertions(+), 176 deletions(-) diff --git a/src/trexio_text.c b/src/trexio_text.c index 50758e6..d13eace 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -1,4 +1,3 @@ -/* [[file:trexio_text.org::*File prefixes][File prefixes:4]] */ /* This file was generated from the trexio.org org-mode file. To generate it, open trexio.org in Emacs and execute M-x org-babel-tangle @@ -7,9 +6,7 @@ #include "trexio_text.h" -/* File prefixes:4 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:2]] */ trexio_exit_code trexio_text_init(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -52,9 +49,7 @@ trexio_exit_code trexio_text_init(trexio_t* file) { return TREXIO_SUCCESS; } -/* Init/deinit functions:2 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:4]] */ trexio_exit_code trexio_text_lock(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -73,9 +68,7 @@ trexio_exit_code trexio_text_lock(trexio_t* file) { return TREXIO_SUCCESS; } -/* Init/deinit functions:4 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:6]] */ trexio_exit_code trexio_text_finalize(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -88,9 +81,7 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) { return TREXIO_SUCCESS; } -/* Init/deinit functions:6 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:8]] */ trexio_exit_code trexio_text_unlock(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -109,9 +100,7 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) { close(f->lock_file); return TREXIO_SUCCESS; } -/* Init/deinit functions:8 ends here */ -/* [[file:trexio_text.org::*Read the struct][Read the struct:2]] */ #define DEBUG printf("%s : line %d\n", __FILE__, __LINE__); nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { @@ -182,9 +171,9 @@ DEBUG } uint64_t size_charge = 1; - for (uint i=0; irank_charge; i++){ + for (unsigned int i=0; irank_charge; i++){ - uint j=-1; + unsigned int j=-1; rc = fscanf(f, "%s %u", buffer, &j); if ((rc != 2) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) { FREE(buffer); @@ -227,9 +216,9 @@ DEBUG } uint64_t size_coord = 1; - for (uint i=0; irank_coord; i++){ + for (unsigned int i=0; irank_coord; i++){ - uint j=-1; + unsigned int j=-1; rc = fscanf(f, "%s %u", buffer, &j); if ((rc != 2) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) { FREE(buffer); @@ -367,9 +356,7 @@ DEBUG file->nucleus = nucleus; return nucleus; } -/* Read the struct:2 ends here */ -/* [[file:trexio_text.org::*Flush the struct][Flush the struct:2]] */ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -389,7 +376,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { fprintf(f, "rank_charge %d\n", nucleus->rank_charge); uint64_t size_charge = 1; - for (uint i=0; irank_charge; i++){ + for (unsigned int i=0; irank_charge; i++){ fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]); size_charge *= nucleus->dims_charge[i]; } @@ -397,7 +384,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { fprintf(f, "rank_coord %d\n", nucleus->rank_coord); uint64_t size_coord = 1; - for (uint i=0; irank_coord; i++){ + for (unsigned int i=0; irank_coord; i++){ fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]); size_coord *= nucleus->dims_coord[i]; } @@ -417,9 +404,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { file->nucleus->to_flush = 0; return TREXIO_SUCCESS; } -/* Flush the struct:2 ends here */ -/* [[file:trexio_text.org::*Free memory][Free memory:2]] */ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -449,9 +434,7 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { FREE (nucleus); return TREXIO_SUCCESS; } -/* Free memory:2 ends here */ -/* [[file:trexio_text.org::*Read/Write the num attribute][Read/Write the num attribute:2]] */ trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -480,9 +463,7 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6 return TREXIO_SUCCESS; } -/* Read/Write the num attribute:2 ends here */ -/* [[file:trexio_text.org::*Read/Write the coord attribute][Read/Write the coord attribute:2]] */ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -494,7 +475,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3; uint64_t dim_size = 1; - for (uint i=0; idims_coord[i]) return TREXIO_INVALID_ARG_4; dim_size *= dims[i]; } @@ -523,7 +504,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou nucleus->rank_coord = rank; uint64_t dim_size = 1; - for (uint i=0; irank_coord; i++){ + for (unsigned int i=0; irank_coord; i++){ nucleus->dims_coord[i] = dims[i]; dim_size *= dims[i]; } @@ -537,9 +518,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou nucleus->to_flush = 1; return TREXIO_SUCCESS; } -/* Read/Write the coord attribute:2 ends here */ -/* [[file:trexio_text.org::*Read/Write the charge attribute][Read/Write the charge attribute:2]] */ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -551,7 +530,7 @@ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* c if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3; uint64_t dim_size = 1; - for (uint i=0; idims_charge[i]) return TREXIO_INVALID_ARG_4; dim_size *= dims[i]; } @@ -580,7 +559,7 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do nucleus->rank_charge = rank; uint64_t dim_size = 1; - for (uint i=0; irank_charge; i++){ + for (unsigned int i=0; irank_charge; i++){ nucleus->dims_charge[i] = dims[i]; dim_size *= dims[i]; } @@ -594,9 +573,7 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do nucleus->to_flush = 1; return TREXIO_SUCCESS; } -/* Read/Write the charge attribute:2 ends here */ -/* [[file:trexio_text.org::*Read the complete struct][Read the complete struct:2]] */ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { if (file == NULL) return NULL; @@ -676,9 +653,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) { file->rdm = rdm ; return rdm; } -/* Read the complete struct:2 ends here */ -/* [[file:trexio_text.org::*Flush the complete struct][Flush the complete struct:2]] */ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -709,9 +684,7 @@ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) { file->rdm->to_flush = 0; return TREXIO_SUCCESS; } -/* Flush the complete struct:2 ends here */ -/* [[file:trexio_text.org::*Free memory][Free memory:2]] */ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -741,9 +714,7 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { file->rdm = NULL; return TREXIO_SUCCESS; } -/* Free memory:2 ends here */ -/* [[file:trexio_text.org::*Read/Write the one_e attribute][Read/Write the one_e attribute:2]] */ trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -778,9 +749,7 @@ trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* rdm->to_flush = 1; return TREXIO_SUCCESS; } -/* Read/Write the one_e attribute:2 ends here */ -/* [[file:trexio_text.org::*Read/Write the two_e attribute][Read/Write the two_e attribute:2]] */ trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -845,4 +814,3 @@ trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, cons return TREXIO_SUCCESS; } -/* Read/Write the two_e attribute:2 ends here */ diff --git a/src/trexio_text.h b/src/trexio_text.h index 9f6f50d..abaaa43 100644 --- a/src/trexio_text.h +++ b/src/trexio_text.h @@ -1,4 +1,3 @@ -/* [[file:trexio_text.org::*File prefixes][File prefixes:3]] */ /* This file was generated from the trexio.org org-mode file. To generate it, open trexio.org in Emacs and execute M-x org-babel-tangle @@ -21,11 +20,7 @@ #include #include #include -/* File prefixes:3 ends here */ -/* Structs for blocks */ - -/* [[file:trexio_text.org::*Structs for blocks][Structs for blocks:1]] */ typedef struct nucleus_s { FILE* file; double* coord; @@ -52,12 +47,7 @@ typedef struct rdm_s { char* two_e_file_name; int to_flush; } rdm_t; -/* Structs for blocks:1 ends here */ -/* Structs for the text back end */ - - -/* [[file:trexio_text.org::*Structs for the text back end][Structs for the text back end:1]] */ typedef struct trexio_text_s { trexio_t parent ; int lock_file; @@ -66,128 +56,40 @@ typedef struct trexio_text_s { electron_t* electron; rdm_t* rdm; } trexio_text_t; -/* Structs for the text back end:1 ends here */ -/* Init/deinit functions */ - - -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:1]] */ trexio_exit_code trexio_text_init(trexio_t* file); -/* Init/deinit functions:1 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:3]] */ trexio_exit_code trexio_text_lock(trexio_t* file); -/* Init/deinit functions:3 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:5]] */ trexio_exit_code trexio_text_finalize(trexio_t* file); -/* Init/deinit functions:5 ends here */ -/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:7]] */ trexio_exit_code trexio_text_unlock(trexio_t* file); -/* Init/deinit functions:7 ends here */ -/* Read the struct */ - - -/* [[file:trexio_text.org::*Read the struct][Read the struct:1]] */ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file); -/* Read the struct:1 ends here */ -/* Flush the struct */ - - -/* [[file:trexio_text.org::*Flush the struct][Flush the struct:1]] */ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file); -/* Flush the struct:1 ends here */ -/* Free memory */ - -/* Memory is allocated when reading. The following function frees memory. */ - - -/* [[file:trexio_text.org::*Free memory][Free memory:1]] */ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file); -/* Free memory:1 ends here */ -/* Read/Write the num attribute */ - - -/* [[file:trexio_text.org::*Read/Write the num attribute][Read/Write the num attribute:1]] */ trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num); trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint64_t num); -/* Read/Write the num attribute:1 ends here */ -/* Read/Write the coord attribute */ - -/* The ~coord~ array is assumed allocated with the appropriate size. */ - - -/* [[file:trexio_text.org::*Read/Write the coord attribute][Read/Write the coord attribute:1]] */ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims); trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims); -/* Read/Write the coord attribute:1 ends here */ -/* Read/Write the charge attribute */ - -/* The ~charge~ array is assumed allocated with the appropriate size. */ - - -/* [[file:trexio_text.org::*Read/Write the charge attribute][Read/Write the charge attribute:1]] */ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims); trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint32_t rank, const uint64_t* dims); -/* Read/Write the charge attribute:1 ends here */ -/* Read the complete struct */ - - -/* [[file:trexio_text.org::*Read the complete struct][Read the complete struct:1]] */ rdm_t* trexio_text_read_rdm(trexio_text_t* file); -/* Read the complete struct:1 ends here */ -/* Flush the complete struct */ - - -/* [[file:trexio_text.org::*Flush the complete struct][Flush the complete struct:1]] */ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file); -/* Flush the complete struct:1 ends here */ -/* Free memory */ - -/* Memory is allocated when reading. The followig function frees memory. */ - - -/* [[file:trexio_text.org::*Free memory][Free memory:1]] */ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file); -/* Free memory:1 ends here */ -/* Read/Write the one_e attribute */ - -/* The ~one_e~ array is assumed allocated with the appropriate size. */ - - -/* [[file:trexio_text.org::*Read/Write the one_e attribute][Read/Write the one_e attribute:1]] */ trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e); trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* one_e, const uint64_t dim_one_e); -/* Read/Write the one_e attribute:1 ends here */ -/* Read/Write the two_e attribute */ - -/* ~two_e~ is a sparse data structure, which can be too large to fit */ -/* in memory. So we provide functions to read and write it by */ -/* chunks. */ -/* In the text back end, the easiest way to do it is to create a */ -/* file for each sparse float structure. */ - - -/* [[file:trexio_text.org::*Read/Write the two_e attribute][Read/Write the two_e attribute:1]] */ trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value); trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, const int64_t* index, const double* value); -/* Read/Write the two_e attribute:1 ends here */ -/* File suffixes :noxport: */ - - -/* [[file:trexio_text.org::*File suffixes][File suffixes:1]] */ #endif -/* File suffixes:1 ends here */ diff --git a/src/trexio_text.org b/src/trexio_text.org index 9de9e06..3fab80a 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -100,13 +100,6 @@ typedef struct rdm_s { int to_flush; } rdm_t; #+end_src - - -*** TO DO - - to_flush = 1 in write - - to_flush = 0 when flushed - - name - *** Structs for the text back end #+begin_src c :tangle trexio_text.h @@ -321,11 +314,11 @@ DEBUG } uint64_t size_charge = 1; - for (uint i=0; irank_charge; i++){ + for (unsigned int i=0; irank_charge; i++){ - uint j=-1; + unsigned int j=-1; rc = fscanf(f, "%s %u", buffer, &j); - if ((rc != 1) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) { + if ((rc != 2) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) { FREE(buffer); FREE(file_name); FREE(nucleus); @@ -366,11 +359,11 @@ DEBUG } uint64_t size_coord = 1; - for (uint i=0; irank_coord; i++){ + for (unsigned int i=0; irank_coord; i++){ - uint j=-1; + unsigned int j=-1; rc = fscanf(f, "%s %u", buffer, &j); - if ((rc != 1) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) { + if ((rc != 2) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) { FREE(buffer); FREE(file_name); FREE(nucleus); @@ -534,7 +527,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { fprintf(f, "rank_charge %d\n", nucleus->rank_charge); uint64_t size_charge = 1; - for (uint i=0; irank_charge; i++){ + for (unsigned int i=0; irank_charge; i++){ fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]); size_charge *= nucleus->dims_charge[i]; } @@ -542,7 +535,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { fprintf(f, "rank_coord %d\n", nucleus->rank_coord); uint64_t size_coord = 1; - for (uint i=0; irank_coord; i++){ + for (unsigned int i=0; irank_coord; i++){ fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]); size_coord *= nucleus->dims_coord[i]; } @@ -573,33 +566,33 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file); #+end_src #+begin_src c :tangle trexio_text.c -trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) { +trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; trexio_exit_code rc; + if (file->parent.mode != 'r') { - rc = trexio_text_flush_rdm(file); + rc = trexio_text_flush_nucleus(file); if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; } - rdm_t* rdm = file->rdm; - if (rdm == NULL) return TREXIO_SUCCESS; + nucleus_t* nucleus = file->nucleus; + if (nucleus == NULL) return TREXIO_SUCCESS; - if (rdm->file != NULL) { - fclose(rdm->file); - rdm->file = NULL; + if (nucleus->file != NULL) { + fclose(nucleus->file); + nucleus->file = NULL; + } + + if (nucleus->coord != NULL) { + FREE (nucleus->coord); + } + + if (nucleus->charge != NULL) { + FREE (nucleus->charge); } - if (rdm->one_e != NULL) { - FREE (rdm->one_e); - } - - if (rdm->two_e_file_name != NULL) { - FREE (rdm->two_e_file_name); - } - - free (rdm); - file->rdm = NULL; + FREE (nucleus); return TREXIO_SUCCESS; } #+end_src @@ -663,7 +656,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3; uint64_t dim_size = 1; - for (uint i=0; idims_coord[i]) return TREXIO_INVALID_ARG_4; dim_size *= dims[i]; } @@ -692,7 +685,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou nucleus->rank_coord = rank; uint64_t dim_size = 1; - for (uint i=0; irank_coord; i++){ + for (unsigned int i=0; irank_coord; i++){ nucleus->dims_coord[i] = dims[i]; dim_size *= dims[i]; } @@ -728,7 +721,7 @@ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* c if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3; uint64_t dim_size = 1; - for (uint i=0; idims_charge[i]) return TREXIO_INVALID_ARG_4; dim_size *= dims[i]; } @@ -757,7 +750,7 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do nucleus->rank_charge = rank; uint64_t dim_size = 1; - for (uint i=0; irank_charge; i++){ + for (unsigned int i=0; irank_charge; i++){ nucleus->dims_charge[i] = dims[i]; dim_size *= dims[i]; }