1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2025-04-25 09:54:44 +02:00

Add coefficients to text back end and test

This commit is contained in:
q-posev 2022-04-14 16:35:32 +02:00
parent 2f6366412f
commit 42ae31a4d1
2 changed files with 211 additions and 3 deletions

View File

@ -1323,6 +1323,9 @@ trexio_text_delete_$group$ (trexio_t* const file)
trexio_exit_code trexio_text_has_determinant_list(trexio_t* const file);
trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file, const int64_t offset_file, const uint32_t rank, const uint64_t* dims, int64_t* const eof_read_size, int64_t* const list);
trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file, const int64_t offset_file, const uint32_t rank, const uint64_t* dims, const int64_t* list);
trexio_exit_code trexio_text_has_determinant_coefficient(trexio_t* const file);
trexio_exit_code trexio_text_read_determinant_coefficient(trexio_t* const file, const int64_t offset_file, const uint32_t rank, const uint64_t* dims, int64_t* const eof_read_size, double* const coeff);
trexio_exit_code trexio_text_write_determinant_coefficient(trexio_t* const file, const int64_t offset_file, const uint32_t rank, const uint64_t* dims, const double* coeff);
#+end_src
#+begin_src c :tangle read_determinant_text.c
@ -1335,6 +1338,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (eof_read_size == NULL) return TREXIO_INVALID_ARG_5;
if (list == NULL) return TREXIO_INVALID_ARG_6;
const char determinant_list_file_name[256] = "/determinant_list.txt";
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
@ -1379,7 +1383,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
if(fgets(buffer, buf_size-1, f) == NULL){
fclose(f);
*eof_read_size = count;
,*eof_read_size = count;
return TREXIO_END;
} else {
@ -1406,6 +1410,83 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file,
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_read_determinant_coefficient(trexio_t* const file,
const int64_t offset_file,
const uint32_t rank,
const uint64_t* dims,
int64_t* const eof_read_size,
double* const coeff)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (eof_read_size == NULL) return TREXIO_INVALID_ARG_5;
if (coeff == NULL) return TREXIO_INVALID_ARG_6;
char coeff_file_name[256];
memset(coeff_file_name, 0, sizeof(coeff_file_name));
const int32_t trexio_state = file->state;
if (trexio_state != 0) {
sprintf(coeff_file_name, "/determinant_coefficient_state_%" PRId32 ".txt", trexio_state);
} else {
strncpy(coeff_file_name, "/determinant_coefficient.txt", 28);
}
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
char file_full_path[TREXIO_MAX_FILENAME_LENGTH];
/* Copy directory name in file_full_path */
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* 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;
/* Specify the line length in order to offset properly.
Each double value 24 elements + one newline char.
,*/
uint64_t line_length = 25UL;
/* Offset in the file according to the provided value of offset_file and optimal line_length */
fseek(f, (long) offset_file * line_length, SEEK_SET);
/* 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;
/* Declare fixed buffer which will be used to read the determinant string <a1 a2 ... a/\ b1 b2 ... b\/> */
char buffer[64];
uint32_t buf_size = sizeof(buffer);
/* Counter for number of elements beind processed */
uint64_t count = 0UL;
for (uint64_t i=0UL; i < dims[0]; ++i) {
memset(buffer, 0, buf_size);
if(fgets(buffer, buf_size-1, f) == NULL){
fclose(f);
,*eof_read_size = count;
return TREXIO_END;
} else {
rc = sscanf(buffer, "%lf", coeff + i);
if(rc <= 0) {
fclose(f);
return TREXIO_FAILURE;
}
count += 1UL;
}
}
/* Close the TXT file */
rc = fclose(f);
if(rc != 0) return TREXIO_FILE_ERROR;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle write_determinant_text.c
@ -1416,6 +1497,7 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file,
const int64_t* list)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (list == NULL) return TREXIO_INVALID_ARG_5;
const char determinant_list_file_name[256] = "/determinant_list.txt";
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
@ -1454,6 +1536,58 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file,
/* Exit upon success */
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_write_determinant_coefficient(trexio_t* const file,
const int64_t offset_file,
const uint32_t rank,
const uint64_t* dims,
const double* coeff)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coeff == NULL) return TREXIO_INVALID_ARG_5;
char coeff_file_name[256];
memset(coeff_file_name, 0, sizeof(coeff_file_name));
const int32_t trexio_state = file->state;
if (trexio_state != 0) {
sprintf(coeff_file_name, "/determinant_coefficient_state_%" PRId32 ".txt", trexio_state);
} else {
strncpy(coeff_file_name, "/determinant_coefficient.txt", 28);
}
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
char file_full_path[TREXIO_MAX_FILENAME_LENGTH];
/* Copy directory name in file_full_path */
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* 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;
/* 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) {
fclose(f);
return TREXIO_FAILURE;
}
}
/* Close the TXT file */
rc = fclose(f);
if (rc != 0) return TREXIO_FILE_ERROR;
/* Exit upon success */
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle has_determinant_text.c
@ -1478,6 +1612,36 @@ trexio_exit_code trexio_text_has_determinant_list(trexio_t* const file)
return TREXIO_HAS_NOT;
}
}
trexio_exit_code trexio_text_has_determinant_coefficient(trexio_t* const file)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
char coeff_file_name[256];
memset(coeff_file_name, 0, sizeof(coeff_file_name));
const int32_t trexio_state = file->state;
if (trexio_state != 0) {
sprintf(coeff_file_name, "/determinant_coefficient_state_%" PRId32 ".txt", trexio_state);
} else {
strncpy(coeff_file_name, "/determinant_coefficient.txt", 28);
}
/* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */
char file_full_path[TREXIO_MAX_FILENAME_LENGTH];
/* Copy directory name in file_full_path */
strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH);
/* Append name of the file with sparse data */
strncat (file_full_path, coeff_file_name,
TREXIO_MAX_FILENAME_LENGTH-strlen(coeff_file_name));
/* Check the return code of access function to determine whether the file with data exists or not */
if (access(file_full_path, F_OK) == 0){
return TREXIO_SUCCESS;
} else {
return TREXIO_HAS_NOT;
}
}
#+end_src
* Constant file suffixes (not used by the generator) :noexport:

View File

@ -26,10 +26,12 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
// parameters to be written
int64_t* det_list;
double* det_coef;
int mo_num = 150;
det_list = (int64_t*) calloc(2*3*SIZE, sizeof(int64_t));
det_coef = (double*) calloc(SIZE, sizeof(double));
for(int i=0; i<SIZE; i++){
det_list[6*i] = 6*i;
@ -38,6 +40,7 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
det_list[6*i+3] = 6*i+3;
det_list[6*i+4] = 6*i+4;
det_list[6*i+5] = 6*i+5;
det_coef[i] = 3.14 + (double) i;
}
// write mo_num which will be used to determine the optimal size of int indices
@ -54,8 +57,13 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
rc = trexio_write_determinant_list(file, offset_f, chunk_size, &det_list[6*offset_d]);
rc = trexio_write_determinant_list(file, offset_f, chunk_size, &det_list[6*offset_d]);
assert(rc == TREXIO_SUCCESS);
rc = trexio_write_determinant_coefficient(file, offset_f, chunk_size, &det_coef[offset_d]);
assert(rc == TREXIO_SUCCESS);
offset_d += chunk_size;
offset_f += chunk_size;
}
@ -66,6 +74,7 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
// free the allocated memeory
free(det_list);
free(det_coef);
/*================= END OF TEST ==================*/
@ -90,6 +99,10 @@ static int test_has_determinant(const char* file_name, const back_end_t backend)
rc = trexio_has_determinant_list(file);
assert(rc==TREXIO_SUCCESS);
// now check that previously written determinant_coefficient exists
rc = trexio_has_determinant_coefficient(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
@ -115,9 +128,12 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
// define arrays to read into
int64_t* det_list_read;
double* det_coef_read;
double check_diff;
uint64_t size_r = 40L;
det_list_read = (int64_t*) calloc(2*3*size_r,sizeof(int64_t));
det_coef_read = (double*) calloc(size_r,sizeof(double));
// specify the read parameters, here:
// 1 chunk of 10 elements using offset of 40 (i.e. lines No. 40--59) into elements of the array starting from 5
@ -136,6 +152,17 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
assert(det_list_read[0] == 0);
assert(det_list_read[6*offset_data_read] == 6 * (int64_t) (offset_file_read-offset));
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
check_diff = det_coef_read[0] - 0.;
assert(check_diff*check_diff < 1e-14);
check_diff = det_coef_read[offset_data_read] - (3.14 + (double) (offset_file_read-offset));
//printf("%lf %lf\n", check_diff, det_coef_read[offset_data_read]);
assert(check_diff*check_diff < 1e-14);
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
offset_data_read = 1;
@ -143,12 +170,13 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
if (offset != 0L) offset_file_read += offset;
chunk_read = read_size_check;
// read one chunk that will reach EOF and return TREXIO_END code
rc = trexio_read_determinant_list(file, offset_file_read, &chunk_read, &det_list_read[6*offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lld %lld\n", det_list_read[6*i], det_list_read[6*i+5]);
printf("%lld %lld\n", det_list_read[6*i], det_list_read[6*i+5]);
}
*/
assert(rc == TREXIO_END);
@ -156,6 +184,21 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
assert(det_list_read[6*size_r-1] == 0);
assert(det_list_read[6*offset_data_read] == 6 * (int64_t) (offset_file_read-offset));
chunk_read = read_size_check;
rc = trexio_read_determinant_coefficient(file, offset_file_read, &chunk_read, &det_coef_read[offset_data_read]);
/*
printf("%s\n", trexio_string_of_error(rc));
for (int i=0; i<size_r; i++) {
printf("%lf\n", det_coef_read[i]);
}
*/
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
check_diff= det_coef_read[size_r-1] - 0.;
//printf("%lf %lf\n", check_diff, det_coef_read[size_r-1]);
assert(check_diff*check_diff < 1e-14);
// check the value of determinant_num
int32_t det_num = 0;
int32_t size_check = SIZE;
@ -171,6 +214,7 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
// free the memory
free(det_list_read);
free(det_coef_read);
/*================= END OF TEST ==================*/