From 13b8d2410825d443685b9b9a510e78d72ec4f3bd Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 10 Jun 2021 16:35:38 +0200 Subject: [PATCH] [WIP] prepare new subloop triggers for the generator to properly read/free groups [text] --- src/templates_text/templator_text.org | 102 +++++++++++++++++++------- 1 file changed, 76 insertions(+), 26 deletions(-) diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 4ac4817..abdcc64 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -298,7 +298,7 @@ trexio_text_read_$group$ (trexio_text_t* const file) /* Read the dimensioning variables */ int rc; - // START REPEAT GROUP_DSET + // START REPEAT GROUP_DSET_ALL rc = fscanf(f, "%1023s", buffer); if ((rc != 1) || (strcmp(buffer, "rank_$group_dset$") != 0)) { FREE(buffer); @@ -319,9 +319,9 @@ trexio_text_read_$group$ (trexio_text_t* const file) uint64_t size_$group_dset$ = 0; if ($group$->rank_$group_dset$ != 0) size_$group_dset$ = 1; - for (unsigned int i=0; i<$group$->rank_$group_dset$; ++i){ + for (uint32_t i=0; i<$group$->rank_$group_dset$; ++i){ - unsigned int j=0; + uint32_t j=0; rc = fscanf(f, "%1023s %u", buffer, &j); if ((rc != 2) || (strcmp(buffer, "dims_$group_dset$") != 0) || (j!=i)) { @@ -342,7 +342,7 @@ trexio_text_read_$group$ (trexio_text_t* const file) size_$group_dset$ *= $group$->dims_$group_dset$[i]; } - // END REPEAT GROUP_DSET + // END REPEAT GROUP_DSET_ALL // START REPEAT GROUP_NUM /* Read data */ @@ -365,13 +365,14 @@ trexio_text_read_$group$ (trexio_text_t* const file) } // END REPEAT GROUP_NUM - // START REPEAT GROUP_DSET + // START REPEAT GROUP_DSET_NUM /* Allocate arrays */ $group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$); assert (!($group$->$group_dset$ == NULL)); if ($group$->$group_dset$ == NULL) { FREE(buffer); fclose(f); + FREE($group$->$group_dset$); FREE($group$); return NULL; } @@ -397,7 +398,56 @@ trexio_text_read_$group$ (trexio_text_t* const file) return NULL; } } - // END REPEAT GROUP_DSET + // END REPEAT GROUP_DSET_NUM + + size_t tmp_len; + + // START REPEAT GROUP_DSET_STR + /* Allocate arrays */ + $group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$); + assert (!($group$->$group_dset$ == NULL)); + if ($group$->$group_dset$ == NULL) { + FREE(buffer); + fclose(f); + FREE($group$->$group_dset$); + FREE($group$); + return NULL; + } + + rc = fscanf(f, "%1023s", buffer); + assert(!((rc != 1) || (strcmp(buffer, "$group_dset$") != 0))); + if ((rc != 1) || (strcmp(buffer, "$group_dset$") != 0)) { + FREE(buffer); + fclose(f); + FREE($group$->$group_dset$); + FREE($group$); + return NULL; + } + + /* WARNING: this tmp array allows to avoid allocation of space for each element of array of string + , BUT it's size has to be number_of_str*max_len_str where max_len_str is somewhat arbitrary, e.g. 32. + ,*/ + char* tmp_$group_dset$ = CALLOC(size_$group_dset$*32 + 1); + tmp_len = 0; + for (uint64_t i=0 ; i$group_dset$[i] = tmp_$group_dset$; + /* conventional fcanf with "%s" only return the string before the first space character + ,* to read string with spaces use "%[^\n]" possible with space before or after, i.e. " %[^\n]" + ,* Q: depending on what ? */ + rc = fscanf(f, " %[^\n]", tmp_$group_dset$); + assert(!(rc != 1)); + if (rc != 1) { + FREE(buffer); + fclose(f); + FREE($group$->$group_dset$); + FREE($group$); + return NULL; + } + + tmp_len = strlen($group$->$group_dset$[i]); + tmp_$group_dset$ += tmp_len + 1; + } + // END REPEAT GROUP_DSET_STR FREE(buffer); fclose(f); @@ -437,7 +487,7 @@ trexio_text_flush_$group$ (trexio_text_t* const file) if (f == NULL) return TREXIO_INVALID_ARG_1; /* Write the dimensioning variables */ - // START REPEAT GROUP_DSET + // START REPEAT GROUP_DSET_ALL fprintf(f, "rank_$group_dset$ %u\n", $group$->rank_$group_dset$); // workaround for the case of missing blocks in the file uint64_t size_$group_dset$ = 0; @@ -448,20 +498,20 @@ trexio_text_flush_$group$ (trexio_text_t* const file) size_$group_dset$ *= $group$->dims_$group_dset$[i]; } - // END REPEAT GROUP_DSET + // END REPEAT GROUP_DSET_ALL // START REPEAT GROUP_NUM fprintf(f, "$group_num$ %" PRIu64 "\n", $group$->$group_num$); // END REPEAT GROUP_NUM /* Write arrays */ - // START REPEAT GROUP_DSET + // START REPEAT GROUP_DSET_ALL fprintf(f, "$group_dset$\n"); for (uint64_t i=0 ; i$group_dset$[i]); } - // END REPEAT GROUP_DSET + // END REPEAT GROUP_DSET_ALL fclose(f); $group$->to_flush = 0; @@ -493,11 +543,18 @@ trexio_text_free_$group$ (trexio_text_t* const file) $group$_t* $group$ = file->$group$; if ($group$ == NULL) return TREXIO_SUCCESS; - // START REPEAT GROUP_DSET + // START REPEAT GROUP_DSET_NUM if ($group$->$group_dset$ != NULL) { FREE ($group$->$group_dset$); } - // END REPEAT GROUP_DSET + // END REPEAT GROUP_DSET_NUM + + // START REPEAT GROUP_DSET_STR + if ($group$->$group_dset$ != NULL) { + if($group$->$group_dset$[0] != NULL) FREE ($group$->$group_dset$[0]); + FREE ($group$->$group_dset$); + } + // END REPEAT GROUP_DSET_STR FREE ($group$); return TREXIO_SUCCESS; @@ -593,7 +650,7 @@ trexio_text_read_$group_dset$ (trexio_t* const file, $group_dset_dtype$* const $ if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3; uint64_t dim_size = 1; - for (unsigned int i=0; idims_$group_dset$[i]) return TREXIO_INVALID_ARG_4; dim_size *= dims[i]; } @@ -628,7 +685,7 @@ trexio_text_write_$group_dset$ (trexio_t* const file, const $group_dset_dtype$* $group$->rank_$group_dset$ = rank; uint64_t dim_size = 1; - for (unsigned int i=0; i<$group$->rank_$group_dset$; ++i){ + for (uint32_t i=0; i<$group$->rank_$group_dset$; ++i){ $group$->dims_$group_dset$[i] = dims[i]; dim_size *= dims[i]; } @@ -669,13 +726,13 @@ trexio_text_has_$group_dset$ (trexio_t* const file) #+begin_src c :tangle hrw_dset_str_text.h :exports none trexio_exit_code trexio_text_has_$group_dset$ (trexio_t* const file); -trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims); +trexio_exit_code trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims, const uint32_t max_str_len); trexio_exit_code trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const uint32_t rank, const uint64_t* dims); #+end_src #+begin_src c :tangle read_dset_str_text.c trexio_exit_code -trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims) +trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uint32_t rank, const uint64_t* dims, const uint32_t max_str_len) { if (file == NULL) return TREXIO_INVALID_ARG_1; @@ -686,17 +743,15 @@ trexio_text_read_$group_dset$ (trexio_t* const file, char* const dset, const uin if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3; - for (unsigned int i=0 ; idims_$group_dset$[i]) return TREXIO_INVALID_ARG_4; } - /* REDO ! */ strcpy(dset, ""); for (uint64_t i=0 ; i$group_dset$[i]); + strncat(dset, $group$->$group_dset$[i], max_str_len); strcat(dset, TREXIO_DELIM); } - /* REDO ! */ return TREXIO_SUCCESS; @@ -723,13 +778,10 @@ trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const u $group$->rank_$group_dset$ = rank; - uint64_t dim_size = 1; - for (unsigned int i=0; i<$group$->rank_$group_dset$; ++i){ + for (uint32_t i=0; i<$group$->rank_$group_dset$; ++i){ $group$->dims_$group_dset$[i] = dims[i]; - dim_size *= dims[i]; } - /* REDO ! */ $group$->$group_dset$ = CALLOC(dims[0], char*); char* tmp_str = CALLOC(dims[0]*32 + 1, char); @@ -739,9 +791,7 @@ trexio_text_write_$group_dset$ (trexio_t* const file, const char** dset, const u $group$->$group_dset$[i] = tmp_str; strncpy(tmp_str, dset[i], tmp_len); tmp_str += tmp_len + 1; - printf("%s\n", $group$->$group_dset$[i]); } - /* REDO ! */ $group$->to_flush = 1;