1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-22 18:57:39 +02:00

Refactoring by adding a custom garbage collector

This commit is contained in:
q-posev 2022-05-18 14:55:42 +02:00
parent 24b1ce6b35
commit c6b628e583

View File

@ -91,6 +91,11 @@ typedef struct $group$_s {
* Template for general structure in text back end
Polymorphism of the ~trexio_t~ type is handled by ensuring that the
corresponding types for all back ends can be safely casted to
~trexio_t~. This is done by making the back-end structs start with
~trexio_t parent~ attribute:
#+begin_src c :tangle struct_text_group.h
typedef struct trexio_text_s {
trexio_t parent ;
@ -333,12 +338,7 @@ trexio_text_read_$group$ (trexio_text_t* const file)
rc = fscanf(f, "%u", &($group$->rank_$group_dset$));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
@ -349,24 +349,14 @@ trexio_text_read_$group$ (trexio_text_t* const file)
uint32_t j=0;
rc = fscanf(f, "%1023s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_$group_dset$") != 0) || (j!=i)) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
rc = fscanf(f, "%" SCNu64 "\n", &($group$->dims_$group_dset$[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
@ -379,24 +369,14 @@ trexio_text_read_$group$ (trexio_text_t* const file)
/* Allocate arrays */
$group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$);
if ($group$->$group_dset$ == NULL) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
for (uint64_t i=0 ; i<size_$group_dset$ ; ++i) {
rc = fscanf(f, "%$group_dset_format_scanf$", &($group$->$group_dset$[i]));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
}
@ -405,39 +385,28 @@ trexio_text_read_$group$ (trexio_text_t* const file)
// START REPEAT GROUP_DSET_STR
} else if (strcmp(buffer, "$group_dset$") == 0) {
if(size_$group_dset$ != 0) {
if (size_$group_dset$ != 0) {
/* Allocate arrays */
$group$->$group_dset$ = CALLOC(size_$group_dset$, $group_dset_dtype$);
if ($group$->$group_dset$ == NULL) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $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.
,*/
* 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$;
tmp_$group_dset$ = CALLOC(size_$group_dset$*32, char);
for (uint64_t i=0 ; i<size_$group_dset$ ; ++i) {
$group$->$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 ? */
* to read string with spaces use "%[^\n]" possible with space before or after, i.e. " %[^\n]"
*/
rc = fscanf(f, " %1023[^\n]", tmp_$group_dset$);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
@ -454,41 +423,25 @@ trexio_text_read_$group$ (trexio_text_t* const file)
/* additional parameter $group_num$_isSet is needed to suppress warning when fscanf into bool variable using %u or %d */
rc = fscanf(f, "%u", &($group_num$_isSet));
$group$->$group_num$_isSet = (bool) $group_num$_isSet;
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
if ($group$->$group_num$_isSet == true) {
rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_num$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_num$") != 0)) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
rc = fscanf(f, "%$group_num_format_scanf$", &($group$->$group_num$));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
}
// END REPEAT GROUP_NUM
@ -496,52 +449,28 @@ trexio_text_read_$group$ (trexio_text_t* const file)
} else if (strcmp(buffer, "len_$group_str$") == 0) {
rc = fscanf(f, "%" SCNu64 "", &($group$->len_$group_str$));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
rc = fscanf(f, "%1023s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_str$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_str$") != 0)) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
if ($group$->len_$group_str$ != 0) {
$group$->$group_str$ = CALLOC($group$->len_$group_str$, char);
assert (!($group$->$group_str$ == NULL));
if ($group$->$group_str$ == NULL) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
rc = fscanf(f, " %1023[^\n]", $group$->$group_str$);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
fclose(f);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
file->$group$ = $group$;
rc_free = trexio_text_free_$group$(file);
assert(rc_free == TREXIO_SUCCESS);
trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL;
}
@ -561,7 +490,6 @@ trexio_text_read_$group$ (trexio_text_t* const file)
file->$group$ = $group$;
return $group$;
}
#+end_src
@ -659,7 +587,7 @@ trexio_text_free_$group$ (trexio_text_t* const file)
// START REPEAT GROUP_DSET_STR
if ($group$->$group_dset$ != NULL) {
if($group$->rank_$group_dset$ != 0) FREE ($group$->$group_dset$[0]);
if ($group$->rank_$group_dset$ != 0) FREE ($group$->$group_dset$[0]);
FREE ($group$->$group_dset$);
}
// END REPEAT GROUP_DSET_STR
@ -673,6 +601,25 @@ trexio_text_free_$group$ (trexio_text_t* const file)
return TREXIO_SUCCESS;
}
#+end_src
This function is called upon the non-successful exit from the ~trexio_text_read_group~ function.
#+begin_src c :tangle free_group_text.c
trexio_exit_code
trexio_text_free_read_$group$ (char* buffer, FILE* txt_file, trexio_text_t* trexio_file, $group$_t* $group$)
{
trexio_exit_code rc_free;
FREE(buffer);
fclose(txt_file);
/* Set pointer to the struct so that the garbage collector can do the job on file handle */
trexio_file->$group$ = $group$;
rc_free = trexio_text_free_$group$(trexio_file);
assert(rc_free == TREXIO_SUCCESS);
return TREXIO_SUCCESS;
}
#+end_src
@ -1053,8 +1000,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file,
/* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */
FILE* f = fopen(file_full_path, "a");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Specify the line length in order to offset properly. For example, for 4-index quantities
the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char.
@ -1089,7 +1035,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file,
rc = fprintf(f, format_str,
$group_dset_sparse_indices_printf$,
,*(value_sparse + i));
if(rc <= 0) {
if (rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}
@ -1150,7 +1096,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file,
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Specify the line length in order to offset properly. For example, for 4-index quantities
the line_length is 69 because 10 per index + 4 spaces + 24 for floating point value + 1 for the new line char
@ -1176,7 +1122,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file,
memset(buffer, 0, sizeof(buffer));
if(fgets(buffer, 1023, f) == NULL){
if (fgets(buffer, 1023, f) == NULL){
fclose(f);
,*eof_read_size = count;
@ -1187,7 +1133,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file,
rc = sscanf(buffer, "$group_dset_format_scanf$",
$group_dset_sparse_indices_scanf$,
value_sparse + i);
if(rc <= 0) {
if (rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}
@ -1198,7 +1144,7 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file,
/* Close the TXT file */
rc = fclose(f);
if(rc != 0) return TREXIO_FILE_ERROR;
if (rc != 0) return TREXIO_FILE_ERROR;
return TREXIO_SUCCESS;
}
@ -1225,7 +1171,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF */
@ -1246,7 +1192,7 @@ trexio_exit_code trexio_text_read_$group_dset$_size(trexio_t* const file, int64_
/* Close the TXT file */
rc = fclose(f);
if(rc != 0) return TREXIO_FILE_ERROR;
if (rc != 0) return TREXIO_FILE_ERROR;
/* Overwrite the value at the input address and return TREXIO_SUCCESS */
,*size_max = size_accum;
@ -1353,7 +1299,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Specify the line length in order to offset properly.
Each 64-bit integer takes at most 10 slots and requires one space,
@ -1381,7 +1327,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
accum = 0UL;
memset(buffer, 0, buf_size);
if(fgets(buffer, buf_size-1, f) == NULL){
if (fgets(buffer, buf_size-1, f) == NULL){
fclose(f);
,*eof_read_size = count;
@ -1394,7 +1340,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
,*/
for (uint32_t j=0; j < (uint32_t) dims[1]; ++j) {
rc = sscanf(buffer+accum, "%10" SCNd64, list + dims[1]*i + j);
if(rc <= 0) {
if (rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}
@ -1407,7 +1353,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
/* Close the TXT file */
rc = fclose(f);
if(rc != 0) return TREXIO_FILE_ERROR;
if (rc != 0) return TREXIO_FILE_ERROR;
return TREXIO_SUCCESS;
}
@ -1443,7 +1389,7 @@ trexio_exit_code trexio_text_read_determinant_coefficient(trexio_t* const file,
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Specify the line length in order to offset properly.
Each double value 24 elements + one newline char.
@ -1464,7 +1410,7 @@ trexio_exit_code trexio_text_read_determinant_coefficient(trexio_t* const file,
for (uint64_t i=0UL; i < dims[0]; ++i) {
memset(buffer, 0, buf_size);
if(fgets(buffer, buf_size-1, f) == NULL){
if (fgets(buffer, buf_size-1, f) == NULL){
fclose(f);
,*eof_read_size = count;
@ -1473,7 +1419,7 @@ trexio_exit_code trexio_text_read_determinant_coefficient(trexio_t* const file,
} else {
rc = sscanf(buffer, "%lf", coeff + i);
if(rc <= 0) {
if (rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}
@ -1484,7 +1430,7 @@ trexio_exit_code trexio_text_read_determinant_coefficient(trexio_t* const file,
/* Close the TXT file */
rc = fclose(f);
if(rc != 0) return TREXIO_FILE_ERROR;
if (rc != 0) return TREXIO_FILE_ERROR;
return TREXIO_SUCCESS;
}
@ -1515,7 +1461,7 @@ trexio_text_read_determinant_coefficient_size(trexio_t* const file, int64_t* con
/* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */
FILE* f = fopen(file_full_path, "r");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Read the data from the file and check the return code of fprintf to verify that > 0 bytes have been read or reached EOF */
int rc;
@ -1535,7 +1481,7 @@ trexio_text_read_determinant_coefficient_size(trexio_t* const file, int64_t* con
/* Close the TXT file */
rc = fclose(f);
if(rc != 0) return TREXIO_FILE_ERROR;
if (rc != 0) return TREXIO_FILE_ERROR;
/* Overwrite the value at the input address and return TREXIO_SUCCESS */
,*size_max = size_accum;
@ -1565,7 +1511,7 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file,
/* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */
FILE* f = fopen(file_full_path, "a");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written */
int rc;
@ -1574,7 +1520,7 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file,
/* The loop below is needed to write a line with int bit fields for alpha and beta electrons */
for (uint32_t j=0; j < (uint32_t) dims[1]; ++j) {
rc = fprintf(f, "%10" PRId64 " ", *(list + i*dims[1] + j));
if(rc <= 0) {
if (rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}
@ -1620,14 +1566,14 @@ trexio_exit_code trexio_text_write_determinant_coefficient(trexio_t* const file,
/* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */
FILE* f = fopen(file_full_path, "a");
if(f == NULL) return TREXIO_FILE_ERROR;
if (f == NULL) return TREXIO_FILE_ERROR;
/* Write the data in the file and check the return code of fprintf to verify that > 0 bytes have been written */
int rc;
for (uint64_t i=0UL; i < dims[0]; ++i) {
rc = fprintf(f, "%24.16e\n", *(coeff + i));
if(rc <= 0) {
if (rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}