1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-04 21:24:08 +01:00
trexio/tests/io_dset_sparse_text.c

239 lines
7.0 KiB
C
Raw Normal View History

#include "trexio.h"
#include <assert.h>
2021-12-03 11:06:31 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define TEST_BACKEND TREXIO_TEXT
#define TREXIO_FILE "test_dset_sparse.dir"
#define RM_COMMAND "rm -f -- " TREXIO_FILE "/*.txt " TREXIO_FILE "/*.txt.size " TREXIO_FILE "/.lock && rm -fd -- " TREXIO_FILE
2021-12-03 11:06:31 +01:00
#define SIZE 100
#define N_CHUNKS 5
2021-12-14 17:46:04 +01:00
static int test_write_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
2021-12-03 11:06:31 +01:00
/* Try to write an array of sparse data into the TREXIO file */
2021-12-03 11:06:31 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
2021-12-03 11:06:31 +01:00
// open file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// parameters to be written
int32_t* index;
double* value;
2021-12-03 11:06:31 +01:00
index = calloc(4L*SIZE, sizeof(int32_t));
value = calloc(SIZE, sizeof(double));
2021-12-03 11:06:31 +01:00
for(int i=0; i<SIZE; i++){
index[4*i] = 4*i;
index[4*i+1] = 4*i+1;
index[4*i+2] = 4*i+2;
index[4*i+3] = 4*i+3;
value[i] = 3.14 + (double) i;
}
2021-12-14 17:46:04 +01:00
// write mo_num which will be used to determine the optimal size of int indices
if (trexio_has_mo_num(file) == TREXIO_HAS_NOT) {
rc = trexio_write_mo_num(file, 1000);
assert(rc == TREXIO_SUCCESS);
}
2021-12-03 11:06:31 +01:00
// write dataset chunks of sparse data in the file (including FAKE statements)
uint64_t chunk_size = (uint64_t) SIZE/N_CHUNKS;
2021-12-14 17:46:04 +01:00
uint64_t offset_f = 0UL;
uint64_t offset_d = 0UL;
if (offset != 0L) offset_f += offset;
2021-12-03 11:06:31 +01:00
// write n_chunks times using write_sparse
for(int i=0; i<N_CHUNKS; ++i){
2021-12-14 17:46:04 +01:00
rc = trexio_write_mo_2e_int_eri(file, offset_f, chunk_size, &index[4*offset_d], &value[offset_d]);
2021-12-03 11:06:31 +01:00
assert(rc == TREXIO_SUCCESS);
2021-12-14 17:46:04 +01:00
offset_d += chunk_size;
2021-12-03 11:06:31 +01:00
offset_f += chunk_size;
}
2021-12-01 13:36:29 +01:00
2021-12-03 11:06:31 +01:00
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-12-03 11:06:31 +01:00
// free the allocated memeory
free(index);
free(value);
2021-12-03 11:06:31 +01:00
/*================= END OF TEST ==================*/
2021-12-03 11:06:31 +01:00
return 0;
}
2021-12-03 11:06:31 +01:00
static int test_has_dset_sparse (const char* file_name, const back_end_t backend) {
2021-12-03 11:06:31 +01:00
/* Try to check the existence of a dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
2022-07-04 11:23:47 +02:00
// check that the group exists
rc = trexio_has_mo_2e_int(file);
assert(rc==TREXIO_SUCCESS);
// check that the group does not exist
rc = trexio_has_rdm(file);
assert(rc==TREXIO_HAS_NOT);
2021-12-03 11:06:31 +01:00
// first check that mo_2e_int_eri_lr (we only write non-lr component in this unit test)
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// check that previous call to has_sparse did not create a file/dset
rc = trexio_has_mo_2e_int_eri_lr(file);
assert(rc==TREXIO_HAS_NOT);
// now check that previously written mo_2e_int_eri exists
rc = trexio_has_mo_2e_int_eri(file);
assert(rc==TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
2021-12-14 17:46:04 +01:00
static int test_read_dset_sparse (const char* file_name, const back_end_t backend, const int64_t offset) {
2021-12-03 11:06:31 +01:00
/* Try to read one chunk of dataset of sparse data in the TREXIO file */
2021-12-03 11:06:31 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// define arrays to read into
int32_t* index_read;
double* value_read;
2021-12-14 17:46:04 +01:00
uint64_t size_r = 40L;
index_read = (int32_t*) calloc(4L*size_r,sizeof(int32_t));
value_read = (double*) calloc(size_r,sizeof(double));
2021-12-03 11:06:31 +01:00
// 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
int64_t chunk_read = 10L;
int64_t offset_file_read = 40L;
int offset_data_read = 5;
int64_t read_size_check;
read_size_check = chunk_read;
2021-12-14 17:46:04 +01:00
if (offset != 0L) offset_file_read += offset;
2021-12-03 11:06:31 +01:00
// read one chunk using the aforementioned parameters
rc = trexio_read_mo_2e_int_eri(file, offset_file_read, &chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]);
assert(rc == TREXIO_SUCCESS);
assert(chunk_read == read_size_check);
2021-12-03 11:06:31 +01:00
assert(index_read[0] == 0);
2021-12-14 17:46:04 +01:00
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
2021-12-03 11:06:31 +01:00
// 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;
int64_t eof_read_size_check = SIZE - offset_file_read; // if offset_file_read=97 => only 3 integrals will be read out of total of 100
2021-12-14 17:46:04 +01:00
if (offset != 0L) offset_file_read += offset;
// read one chunk that will reach EOF and return TREXIO_END code
rc = trexio_read_mo_2e_int_eri(file, offset_file_read, &chunk_read, &index_read[4*offset_data_read], &value_read[offset_data_read]);
assert(rc == TREXIO_END);
assert(chunk_read == eof_read_size_check);
assert(index_read[4*size_r-1] == 0);
2021-12-14 17:46:04 +01:00
assert(index_read[4*offset_data_read] == 4 * (int32_t) (offset_file_read-offset));
2021-12-03 11:06:31 +01:00
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// free the memory
free(index_read);
free(value_read);
2021-12-03 11:06:31 +01:00
/*================= END OF TEST ==================*/
return 0;
}
static int test_read_dset_sparse_size (const char* file_name, const back_end_t backend, const int64_t size_check) {
/* Try to read a size of the dataset of sparse data in the TREXIO file */
trexio_t* file = NULL;
trexio_exit_code rc;
/*================= START OF TEST ==================*/
// open file
file = trexio_open(file_name, 'r', backend, &rc);
assert (file != NULL);
assert (rc == TREXIO_SUCCESS);
// define the variable to read into
int64_t size_written;
// read one chunk using the aforementioned parameters
rc = trexio_read_mo_2e_int_eri_size(file, &size_written);
assert(rc == TREXIO_SUCCESS);
assert(size_written == size_check);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
int main(){
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);
// check the first write attempt (SIZE elements written in N_CHUNKS chunks)
2021-12-14 17:46:04 +01:00
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND, 0);
2021-12-03 11:06:31 +01:00
test_has_dset_sparse (TREXIO_FILE, TEST_BACKEND);
2021-12-14 17:46:04 +01:00
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, 0);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE);
2021-12-03 11:06:31 +01:00
// check the second write attempt (SIZE elements written in N_CHUNKS chunks)
2021-12-14 17:46:04 +01:00
test_write_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse (TREXIO_FILE, TEST_BACKEND, SIZE);
test_read_dset_sparse_size(TREXIO_FILE, TEST_BACKEND, SIZE*2);
2021-12-03 11:06:31 +01:00
rc = system(RM_COMMAND);
assert (rc == 0);
return 0;
}