From cbddfa60102204c3a84253847b5e886a3f68b290 Mon Sep 17 00:00:00 2001 From: q-posev Date: Fri, 12 Mar 2021 11:56:03 +0100 Subject: [PATCH] works for nucleus_coord; text backend adapted --- src/trexio_text.c | 77 ++++++++++++++++++++++++++++++++-------- src/trexio_text.h | 9 ++--- src/trexio_text.org | 86 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 134 insertions(+), 38 deletions(-) diff --git a/src/trexio_text.c b/src/trexio_text.c index 3dd4529..9b14e30 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -108,6 +108,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { nucleus->file = NULL; nucleus->num = 0; nucleus->coord = NULL; + nucleus->dims_coord = NULL; nucleus->charge = NULL; nucleus->to_flush = 0; @@ -141,16 +142,32 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { rc = fscanf(f, "%s", buffer); assert (rc == 1); - assert (strcmp(buffer, "dim_coord") == 0); + assert (strcmp(buffer, "rank_coord") == 0); - rc = fscanf(f, "%lu", &(nucleus->dim_coord)); + rc = fscanf(f, "%u", &(nucleus->rank_coord)); assert (rc == 1); + + nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t)); + assert (nucleus->dims_coord != NULL); + + uint64_t dim_coord = 1; + for (uint i=0; irank_coord; i++){ + + rc = fscanf(f, "%s", buffer); + assert (rc == 1); + //assert (strcmp(buffer, "dims_coord") == 0); + + rc = fscanf(f, "%lu", &(nucleus->dims_coord[i])); + assert (rc == 1); + + dim_coord *= nucleus->dims_coord[i]; + } /* Allocate arrays */ nucleus->charge = (double*) calloc(nucleus->dim_charge, sizeof(double)); assert (nucleus->charge != NULL); - nucleus->coord = (double*) calloc(nucleus->dim_coord, sizeof(double)); + nucleus->coord = (double*) calloc(dim_coord, sizeof(double)); assert (nucleus->coord != NULL); /* Read data */ @@ -174,7 +191,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { assert (rc == 1); assert (strcmp(buffer, "coord") == 0); - for (uint64_t i=0 ; idim_coord ; i++) { + for (uint64_t i=0 ; icoord[i])); assert (rc == 1); } @@ -207,9 +224,15 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { assert (f != NULL); rewind(f); - /* Write the dimensioning variables */ - fprintf(f, "dim_charge %ld\n", nucleus->dim_charge); - fprintf(f, "dim_coord %ld\n", nucleus->dim_coord ); + /* Write the dimensioning variables */ + fprintf(f, "dim_charge %ld\n", nucleus->dim_charge ); + fprintf(f, "rank_coord %d\n", nucleus->rank_coord); + + uint64_t dim_coord = (nucleus->dims_coord[0])*(nucleus->dims_coord[1]); + for (uint i=0; irank_coord; i++){ + fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]); + //dim_coord *= nucleus->dims_coord[i]; + } /* Write arrays */ fprintf(f, "num %ld\n", nucleus->num); @@ -219,7 +242,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { } fprintf(f, "coord\n"); - for (uint64_t i=0 ; idim_coord ; i++) { + for (uint64_t i=0 ; icoord[i]); } fflush(f); @@ -245,6 +268,11 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { nucleus->file = NULL; } + if (nucleus->dims_coord != NULL) { + free (nucleus->dims_coord); + nucleus->dims_coord = NULL; + } + if (nucleus->coord != NULL) { free (nucleus->coord); nucleus->coord = NULL; @@ -289,7 +317,7 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6 return TREXIO_SUCCESS; } -trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord) { +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; if (coord == NULL) return TREXIO_INVALID_ARG_2; @@ -297,9 +325,15 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file); if (nucleus == NULL) return TREXIO_FAILURE; - if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3; + if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3; - for (uint64_t i=0 ; idims_coord[i]) return TREXIO_INVALID_ARG_4; + } + //if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3; + uint64_t dim_size = dims[0]*dims[1]; + + for (uint64_t i=0 ; icoord[i]; } @@ -307,7 +341,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co } -trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord) { +trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (coord == NULL) return TREXIO_INVALID_ARG_2; @@ -321,10 +355,23 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou nucleus->coord = NULL; } - nucleus->dim_coord = dim_coord; - nucleus->coord = (double*) calloc(dim_coord, sizeof(double)); + if (nucleus->dims_coord != NULL) { + free(nucleus->dims_coord); + nucleus->dims_coord = NULL; + } - for (uint64_t i=0 ; irank_coord = rank; + + nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t)); + + for (uint i=0; irank_coord; i++){ + nucleus->dims_coord[i] = dims[i]; + } + + uint64_t dim_size = dims[0]*dims[1]; + nucleus->coord = (double*) calloc(dim_size, sizeof(double)); + + for (uint64_t i=0 ; icoord[i] = coord[i]; } diff --git a/src/trexio_text.h b/src/trexio_text.h index ea8c1c4..a96068e 100644 --- a/src/trexio_text.h +++ b/src/trexio_text.h @@ -22,11 +22,12 @@ typedef struct nucleus_s { FILE* file; - uint64_t dim_coord; - uint64_t dim_charge; double* coord; double* charge; uint64_t num; + uint64_t dim_charge; + uint64_t* dims_coord; + uint32_t rank_coord; int to_flush; } nucleus_t; @@ -71,8 +72,8 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file); 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); -trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord); -trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord); +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); trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint64_t dim_charge); trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint64_t dim_charge); diff --git a/src/trexio_text.org b/src/trexio_text.org index 310fd88..ea18fff 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -55,11 +55,12 @@ #+begin_src c :tangle trexio_text.h typedef struct nucleus_s { FILE* file; - uint64_t dim_coord; - uint64_t dim_charge; double* coord; double* charge; uint64_t num; + uint64_t dim_charge; + uint64_t* dims_coord; + uint32_t rank_coord; int to_flush; } nucleus_t; @@ -238,6 +239,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { nucleus->file = NULL; nucleus->num = 0; nucleus->coord = NULL; + nucleus->dims_coord = NULL; nucleus->charge = NULL; nucleus->to_flush = 0; @@ -271,16 +273,32 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { rc = fscanf(f, "%s", buffer); assert (rc == 1); - assert (strcmp(buffer, "dim_coord") == 0); + assert (strcmp(buffer, "rank_coord") == 0); - rc = fscanf(f, "%lu", &(nucleus->dim_coord)); + rc = fscanf(f, "%u", &(nucleus->rank_coord)); assert (rc == 1); + + nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t)); + assert (nucleus->dims_coord != NULL); + + uint64_t dim_coord = 1; + for (uint i=0; irank_coord; i++){ + + rc = fscanf(f, "%s", buffer); + assert (rc == 1); + //assert (strcmp(buffer, "dims_coord") == 0); + + rc = fscanf(f, "%lu", &(nucleus->dims_coord[i])); + assert (rc == 1); + + dim_coord *= nucleus->dims_coord[i]; + } /* Allocate arrays */ nucleus->charge = (double*) calloc(nucleus->dim_charge, sizeof(double)); assert (nucleus->charge != NULL); - nucleus->coord = (double*) calloc(nucleus->dim_coord, sizeof(double)); + nucleus->coord = (double*) calloc(dim_coord, sizeof(double)); assert (nucleus->coord != NULL); /* Read data */ @@ -304,7 +322,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { assert (rc == 1); assert (strcmp(buffer, "coord") == 0); - for (uint64_t i=0 ; idim_coord ; i++) { + for (uint64_t i=0 ; icoord[i])); assert (rc == 1); } @@ -345,9 +363,15 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { assert (f != NULL); rewind(f); - /* Write the dimensioning variables */ - fprintf(f, "dim_charge %ld\n", nucleus->dim_charge); - fprintf(f, "dim_coord %ld\n", nucleus->dim_coord ); + /* Write the dimensioning variables */ + fprintf(f, "dim_charge %ld\n", nucleus->dim_charge ); + fprintf(f, "rank_coord %d\n", nucleus->rank_coord); + + uint64_t dim_coord = (nucleus->dims_coord[0])*(nucleus->dims_coord[1]); + for (uint i=0; irank_coord; i++){ + fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]); + //dim_coord *= nucleus->dims_coord[i]; + } /* Write arrays */ fprintf(f, "num %ld\n", nucleus->num); @@ -357,7 +381,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) { } fprintf(f, "coord\n"); - for (uint64_t i=0 ; idim_coord ; i++) { + for (uint64_t i=0 ; icoord[i]); } fflush(f); @@ -393,6 +417,11 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) { nucleus->file = NULL; } + if (nucleus->dims_coord != NULL) { + free (nucleus->dims_coord); + nucleus->dims_coord = NULL; + } + if (nucleus->coord != NULL) { free (nucleus->coord); nucleus->coord = NULL; @@ -452,12 +481,12 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6 The ~coord~ array is assumed allocated with the appropriate size. #+begin_src c :tangle trexio_text.h -trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord); -trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord); +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); #+end_src #+begin_src c :tangle trexio_text.c -trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord) { +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; if (coord == NULL) return TREXIO_INVALID_ARG_2; @@ -465,9 +494,15 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file); if (nucleus == NULL) return TREXIO_FAILURE; - if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3; + if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3; - for (uint64_t i=0 ; idims_coord[i]) return TREXIO_INVALID_ARG_4; + } + //if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3; + uint64_t dim_size = dims[0]*dims[1]; + + for (uint64_t i=0 ; icoord[i]; } @@ -475,7 +510,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co } -trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord) { +trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) { if (file == NULL) return TREXIO_INVALID_ARG_1; if (coord == NULL) return TREXIO_INVALID_ARG_2; @@ -489,10 +524,23 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou nucleus->coord = NULL; } - nucleus->dim_coord = dim_coord; - nucleus->coord = (double*) calloc(dim_coord, sizeof(double)); + if (nucleus->dims_coord != NULL) { + free(nucleus->dims_coord); + nucleus->dims_coord = NULL; + } - for (uint64_t i=0 ; irank_coord = rank; + + nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t)); + + for (uint i=0; irank_coord; i++){ + nucleus->dims_coord[i] = dims[i]; + } + + uint64_t dim_size = dims[0]*dims[1]; + nucleus->coord = (double*) calloc(dim_size, sizeof(double)); + + for (uint64_t i=0 ; icoord[i] = coord[i]; }