1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00

Improved checks in text backend

This commit is contained in:
Anthony Scemama 2021-03-13 00:19:39 +01:00
parent 945c0c2a7d
commit e6a211127c
6 changed files with 579 additions and 206 deletions

View File

@ -53,7 +53,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
/* Data for the parent type */
result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char));
result->file_name = CALLOC(strlen(file_name)+1,char);
strcpy(result->file_name, file_name);
result->back_end = back_end;
result->mode = mode;

View File

@ -12,18 +12,19 @@
typedef int32_t trexio_exit_code;
#define TREXIO_FAILURE ( (trexio_exit_code) -1 )
#define TREXIO_SUCCESS ( (trexio_exit_code) 0 )
#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 )
#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 )
#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 )
#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 )
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 )
#define TREXIO_FAILURE ( (trexio_exit_code) -1 )
#define TREXIO_SUCCESS ( (trexio_exit_code) 0 )
#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 )
#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 )
#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 )
#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 )
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_ALLOCATION_FAILED ( (trexio_exit_code) 21 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 )
typedef int32_t back_end_t;

View File

@ -88,18 +88,19 @@
#+begin_src c :tangle trexio.h
typedef int32_t trexio_exit_code;
#define TREXIO_FAILURE ( (trexio_exit_code) -1 )
#define TREXIO_SUCCESS ( (trexio_exit_code) 0 )
#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 )
#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 )
#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 )
#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 )
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 )
#define TREXIO_FAILURE ( (trexio_exit_code) -1 )
#define TREXIO_SUCCESS ( (trexio_exit_code) 0 )
#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 )
#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 )
#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 )
#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 )
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_ALLOCATION_FAILED ( (trexio_exit_code) 21 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 )
#+end_src
** Back ends
@ -212,7 +213,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
/* Data for the parent type */
result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char));
result->file_name = CALLOC(strlen(file_name)+1,char);
strcpy(result->file_name, file_name);
result->back_end = back_end;
result->mode = mode;

View File

@ -1,3 +1,4 @@
/* [[file:trexio_text.org::*File prefixes][File prefixes:4]] */
/* This file was generated from the trexio.org org-mode file.
To generate it, open trexio.org in Emacs and execute
M-x org-babel-tangle
@ -6,12 +7,17 @@
#include "trexio_text.h"
/* File prefixes:4 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:2]] */
trexio_exit_code trexio_text_init(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
/* Put all pointers to NULL but leave parent untouched */
memset(&(f->parent)+1,0,sizeof(trexio_text_t)-sizeof(trexio_t));
/* If directory doesn't exist, create it in write mode */
struct stat st;
@ -27,24 +33,28 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
/* Create the lock file in the directory */
const char* lock_file_name = "/.lock";
char* file_name = (char*)
calloc( strlen(file->file_name) + strlen(lock_file_name) + 1,
sizeof(char));
assert (file_name != NULL);
char* file_name =
CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char);
if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED;
}
strcpy (file_name, file->file_name);
strcat (file_name, lock_file_name);
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
assert (f->lock_file > 0);
FREE(file_name);
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
if (f->lock_file <= 0) {
return TREXIO_FAILURE;
}
return TREXIO_SUCCESS;
}
/* Init/deinit functions:2 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:4]] */
trexio_exit_code trexio_text_lock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -63,7 +73,9 @@ trexio_exit_code trexio_text_lock(trexio_t* file) {
return TREXIO_SUCCESS;
}
/* Init/deinit functions:4 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:6]] */
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -76,7 +88,9 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) {
return TREXIO_SUCCESS;
}
/* Init/deinit functions:6 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:8]] */
trexio_exit_code trexio_text_unlock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -95,30 +109,37 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) {
close(f->lock_file);
return TREXIO_SUCCESS;
}
/* Init/deinit functions:8 ends here */
/* [[file:trexio_text.org::*Read the struct][Read the struct:2]] */
#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__);
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
if (file == NULL) return NULL;
if (file->nucleus != NULL) return file->nucleus;
/* If the data structure exists, return it */
if (file->nucleus != NULL) {
return file->nucleus;
}
/* Allocate the data structure */
nucleus_t* nucleus = MALLOC(nucleus_t);
assert (nucleus != NULL);
if (nucleus == NULL) return NULL;
nucleus->file = NULL;
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->dims_coord = NULL;
nucleus->charge = NULL;
nucleus->dims_charge = NULL;
nucleus->to_flush = 0;
memset(nucleus,0,sizeof(nucleus_t));
/* Try to open the file. If the file does not exist, return */
/* Build the file name */
const char* nucleus_file_name = "/nucleus.txt";
char * file_name = (char*)
calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1,
sizeof(char));
assert (file_name != NULL);
if (file_name == NULL) {
FREE(nucleus);
DEBUG
return NULL;
}
strcpy (file_name, file->parent.file_name);
strcat (file_name, nucleus_file_name);
@ -130,102 +151,225 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char* buffer = CALLOC(sz,char);
if (buffer == NULL) {
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read the dimensioning variables */
int rc;
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "rank_charge") == 0);
if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_charge));
assert (rc == 1);
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t));
assert (nucleus->dims_charge != NULL);
uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
//assert (strcmp(buffer, "dims_charge") == 0);
uint j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dims_charge[i]));
assert (rc == 1);
rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_charge *= nucleus->dims_charge[i];
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "rank_coord") == 0);
if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t));
assert (nucleus->dims_coord != NULL);
uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
//assert (strcmp(buffer, "dims_coord") == 0);
uint j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dims_coord[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_coord *= nucleus->dims_coord[i];
}
/* Allocate arrays */
nucleus->charge = (double*) calloc(size_charge, sizeof(double));
assert (nucleus->charge != NULL);
assert (!(nucleus->charge == NULL));
if (nucleus->charge == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->coord = (double*) calloc(size_coord, sizeof(double));
assert (nucleus->coord != NULL);
assert (!(nucleus->coord == NULL));
if (nucleus->coord == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read data */
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "num") == 0);
assert(!((rc != 1) || (strcmp(buffer, "num") != 0)));
if ((rc != 1) || (strcmp(buffer, "num") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->num));
assert (rc == 1);
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "charge") == 0);
for (uint64_t i=0 ; i<size_charge ; i++) {
rc = fscanf(f, "%lf", &(nucleus->charge[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "coord") == 0);
assert(!((rc != 1) || (strcmp(buffer, "charge") != 0)));
if ((rc != 1) || (strcmp(buffer, "charge") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
for (uint64_t i=0 ; i<size_charge ; i++) {
rc = fscanf(f, "%lf", &(nucleus->charge[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
}
rc = fscanf(f, "%s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "coord") != 0)));
if ((rc != 1) || (strcmp(buffer, "coord") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
for (uint64_t i=0 ; i<size_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->coord[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
}
FREE(buffer);
fclose(f);
f = NULL;
}
if (file->parent.mode == 'w') {
nucleus->file = fopen(file_name,"a");
} else {
nucleus->file = fopen(file_name,"r");
}
FREE(file_name);
assert (!(nucleus->file == NULL));
if (nucleus->file == NULL) {
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
fseek(nucleus->file, 0L, SEEK_SET);
file->nucleus = nucleus;
return nucleus;
}
/* Read the struct:2 ends here */
/* [[file:trexio_text.org::*Flush the struct][Flush the struct:2]] */
trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -246,7 +390,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){
fprintf(f, "dims_charge[%d] %ld\n", i, nucleus->dims_charge[i]);
fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]);
size_charge *= nucleus->dims_charge[i];
}
@ -254,7 +398,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]);
fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]);
size_coord *= nucleus->dims_coord[i];
}
@ -273,7 +417,9 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
file->nucleus->to_flush = 0;
return TREXIO_SUCCESS;
}
/* Flush the struct:2 ends here */
/* [[file:trexio_text.org::*Free memory][Free memory:2]] */
trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -292,18 +438,10 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
}
if (nucleus->dims_coord != NULL) {
FREE (nucleus->dims_coord);
}
if (nucleus->coord != NULL) {
FREE (nucleus->coord);
}
if (nucleus->dims_charge != NULL) {
FREE (nucleus->dims_charge);
}
if (nucleus->charge != NULL) {
FREE (nucleus->charge);
}
@ -311,7 +449,9 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
FREE (nucleus);
return TREXIO_SUCCESS;
}
/* Free memory:2 ends here */
/* [[file:trexio_text.org::*Read/Write the num attribute][Read/Write the num attribute:2]] */
trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -340,7 +480,9 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6
return TREXIO_SUCCESS;
}
/* Read/Write the num attribute:2 ends here */
/* [[file:trexio_text.org::*Read/Write the coord attribute][Read/Write the coord attribute:2]] */
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;
@ -378,12 +520,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
FREE(nucleus->coord);
}
if (nucleus->dims_coord != NULL) {
FREE(nucleus->dims_coord);
}
nucleus->rank_coord = rank;
nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
@ -400,7 +537,9 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
nucleus->to_flush = 1;
return TREXIO_SUCCESS;
}
/* Read/Write the coord attribute:2 ends here */
/* [[file:trexio_text.org::*Read/Write the charge attribute][Read/Write the charge attribute:2]] */
trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -438,14 +577,8 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
FREE(nucleus->charge);
}
if (nucleus->dims_charge != NULL) {
FREE(nucleus->dims_charge);
}
nucleus->rank_charge = rank;
nucleus->dims_charge = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i];
@ -461,7 +594,9 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
nucleus->to_flush = 1;
return TREXIO_SUCCESS;
}
/* Read/Write the charge attribute:2 ends here */
/* [[file:trexio_text.org::*Read the complete struct][Read the complete struct:2]] */
rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
if (file == NULL) return NULL;
@ -541,7 +676,9 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
file->rdm = rdm ;
return rdm;
}
/* Read the complete struct:2 ends here */
/* [[file:trexio_text.org::*Flush the complete struct][Flush the complete struct:2]] */
trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -572,7 +709,9 @@ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) {
file->rdm->to_flush = 0;
return TREXIO_SUCCESS;
}
/* Flush the complete struct:2 ends here */
/* [[file:trexio_text.org::*Free memory][Free memory:2]] */
trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -602,7 +741,9 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
file->rdm = NULL;
return TREXIO_SUCCESS;
}
/* Free memory:2 ends here */
/* [[file:trexio_text.org::*Read/Write the one_e attribute][Read/Write the one_e attribute:2]] */
trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -637,7 +778,9 @@ trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double*
rdm->to_flush = 1;
return TREXIO_SUCCESS;
}
/* Read/Write the one_e attribute:2 ends here */
/* [[file:trexio_text.org::*Read/Write the two_e attribute][Read/Write the two_e attribute:2]] */
trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -702,3 +845,4 @@ trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, cons
return TREXIO_SUCCESS;
}
/* Read/Write the two_e attribute:2 ends here */

View File

@ -1,3 +1,4 @@
/* [[file:trexio_text.org::*File prefixes][File prefixes:3]] */
/* This file was generated from the trexio.org org-mode file.
To generate it, open trexio.org in Emacs and execute
M-x org-babel-tangle
@ -20,14 +21,18 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
/* File prefixes:3 ends here */
/* Structs for blocks */
/* [[file:trexio_text.org::*Structs for blocks][Structs for blocks:1]] */
typedef struct nucleus_s {
FILE* file;
double* coord;
double* charge;
uint64_t num;
uint64_t* dims_charge;
uint64_t* dims_coord;
uint64_t dims_charge[16];
uint64_t dims_coord[16];
uint32_t rank_charge;
uint32_t rank_coord;
int to_flush;
@ -47,7 +52,12 @@ typedef struct rdm_s {
char* two_e_file_name;
int to_flush;
} rdm_t;
/* Structs for blocks:1 ends here */
/* Structs for the text back end */
/* [[file:trexio_text.org::*Structs for the text back end][Structs for the text back end:1]] */
typedef struct trexio_text_s {
trexio_t parent ;
int lock_file;
@ -56,40 +66,128 @@ typedef struct trexio_text_s {
electron_t* electron;
rdm_t* rdm;
} trexio_text_t;
/* Structs for the text back end:1 ends here */
/* Init/deinit functions */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:1]] */
trexio_exit_code trexio_text_init(trexio_t* file);
/* Init/deinit functions:1 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:3]] */
trexio_exit_code trexio_text_lock(trexio_t* file);
/* Init/deinit functions:3 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:5]] */
trexio_exit_code trexio_text_finalize(trexio_t* file);
/* Init/deinit functions:5 ends here */
/* [[file:trexio_text.org::*Init/deinit functions][Init/deinit functions:7]] */
trexio_exit_code trexio_text_unlock(trexio_t* file);
/* Init/deinit functions:7 ends here */
/* Read the struct */
/* [[file:trexio_text.org::*Read the struct][Read the struct:1]] */
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
/* Read the struct:1 ends here */
/* Flush the struct */
/* [[file:trexio_text.org::*Flush the struct][Flush the struct:1]] */
trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file);
/* Flush the struct:1 ends here */
/* Free memory */
/* Memory is allocated when reading. The following function frees memory. */
/* [[file:trexio_text.org::*Free memory][Free memory:1]] */
trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file);
/* Free memory:1 ends here */
/* Read/Write the num attribute */
/* [[file:trexio_text.org::*Read/Write the num attribute][Read/Write the num attribute:1]] */
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);
/* Read/Write the num attribute:1 ends here */
/* Read/Write the coord attribute */
/* The ~coord~ array is assumed allocated with the appropriate size. */
/* [[file:trexio_text.org::*Read/Write the coord attribute][Read/Write the coord attribute:1]] */
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);
/* Read/Write the coord attribute:1 ends here */
/* Read/Write the charge attribute */
/* The ~charge~ array is assumed allocated with the appropriate size. */
/* [[file:trexio_text.org::*Read/Write the charge attribute][Read/Write the charge attribute:1]] */
trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint32_t rank, const uint64_t* dims);
/* Read/Write the charge attribute:1 ends here */
/* Read the complete struct */
/* [[file:trexio_text.org::*Read the complete struct][Read the complete struct:1]] */
rdm_t* trexio_text_read_rdm(trexio_text_t* file);
/* Read the complete struct:1 ends here */
/* Flush the complete struct */
/* [[file:trexio_text.org::*Flush the complete struct][Flush the complete struct:1]] */
trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file);
/* Flush the complete struct:1 ends here */
/* Free memory */
/* Memory is allocated when reading. The followig function frees memory. */
/* [[file:trexio_text.org::*Free memory][Free memory:1]] */
trexio_exit_code trexio_text_free_rdm(trexio_text_t* file);
/* Free memory:1 ends here */
/* Read/Write the one_e attribute */
/* The ~one_e~ array is assumed allocated with the appropriate size. */
/* [[file:trexio_text.org::*Read/Write the one_e attribute][Read/Write the one_e attribute:1]] */
trexio_exit_code trexio_text_read_rdm_one_e(const trexio_t* file, double* one_e, const uint64_t dim_one_e);
trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double* one_e, const uint64_t dim_one_e);
/* Read/Write the one_e attribute:1 ends here */
/* Read/Write the two_e attribute */
/* ~two_e~ is a sparse data structure, which can be too large to fit */
/* in memory. So we provide functions to read and write it by */
/* chunks. */
/* In the text back end, the easiest way to do it is to create a */
/* file for each sparse float structure. */
/* [[file:trexio_text.org::*Read/Write the two_e attribute][Read/Write the two_e attribute:1]] */
trexio_exit_code trexio_text_buffered_read_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, int64_t* index, double* value);
trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, const uint64_t offset, const uint64_t size, const int64_t* index, const double* value);
/* Read/Write the two_e attribute:1 ends here */
/* File suffixes :noxport: */
/* [[file:trexio_text.org::*File suffixes][File suffixes:1]] */
#endif
/* File suffixes:1 ends here */

View File

@ -2,6 +2,19 @@
* File prefixes :noxport:
#+begin_src emacs-lisp
(setq-local org-babel-default-header-args:c '((:comments . "both")))
org-babel-default-header-args:c
#+end_src
#+RESULTS:
: ((:comments . both))
# Local Variables:
# eval: (setq-local org-babel-default-header-args:Python '((:session . "foo")))
# End:
#+NAME:header
#+begin_src c
/* This file was generated from the trexio.org org-mode file.
@ -34,7 +47,13 @@
#+end_src
#+begin_src c :tangle trexio_text.c :noweb yes
<<header>>
/* This file was generated from the trexio.org org-mode file.
To generate it, open trexio.org in Emacs and execute
M-x org-babel-tangle
*/
#include "trexio_text.h"
#+end_src
@ -59,8 +78,8 @@ typedef struct nucleus_s {
double* coord;
double* charge;
uint64_t num;
uint64_t* dims_charge;
uint64_t* dims_coord;
uint64_t dims_charge[16];
uint64_t dims_coord[16];
uint32_t rank_charge;
uint32_t rank_coord;
int to_flush;
@ -114,6 +133,9 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
trexio_text_t* f = (trexio_text_t*) file;
/* Put all pointers to NULL but leave parent untouched */
memset(&(f->parent)+1,0,sizeof(trexio_text_t)-sizeof(trexio_t));
/* If directory doesn't exist, create it in write mode */
struct stat st;
@ -129,24 +151,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
/* Create the lock file in the directory */
const char* lock_file_name = "/.lock";
char* file_name = (char*)
calloc( strlen(file->file_name) + strlen(lock_file_name) + 1,
sizeof(char));
assert (file_name != NULL);
char* file_name =
CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char);
if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED;
}
strcpy (file_name, file->file_name);
strcat (file_name, lock_file_name);
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
assert (f->lock_file > 0);
FREE(file_name);
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
if (f->lock_file <= 0) {
return TREXIO_FAILURE;
}
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
@ -172,7 +195,6 @@ trexio_exit_code trexio_text_lock(trexio_t* file) {
return TREXIO_SUCCESS;
}
#+end_src
@ -229,29 +251,34 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__);
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
if (file == NULL) return NULL;
if (file->nucleus != NULL) return file->nucleus;
/* If the data structure exists, return it */
if (file->nucleus != NULL) {
return file->nucleus;
}
/* Allocate the data structure */
nucleus_t* nucleus = MALLOC(nucleus_t);
assert (nucleus != NULL);
if (nucleus == NULL) return NULL;
nucleus->file = NULL;
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->dims_coord = NULL;
nucleus->charge = NULL;
nucleus->dims_charge = NULL;
nucleus->to_flush = 0;
memset(nucleus,0,sizeof(nucleus_t));
/* Try to open the file. If the file does not exist, return */
/* Build the file name */
const char* nucleus_file_name = "/nucleus.txt";
char * file_name = (char*)
calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1,
sizeof(char));
assert (file_name != NULL);
if (file_name == NULL) {
FREE(nucleus);
DEBUG
return NULL;
}
strcpy (file_name, file->parent.file_name);
strcat (file_name, nucleus_file_name);
@ -263,98 +290,219 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char* buffer = CALLOC(sz,char);
if (buffer == NULL) {
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read the dimensioning variables */
int rc;
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "rank_charge") == 0);
if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_charge));
assert (rc == 1);
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t));
assert (nucleus->dims_charge != NULL);
uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
//assert (strcmp(buffer, "dims_charge") == 0);
uint j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 1) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dims_charge[i]));
assert (rc == 1);
rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_charge *= nucleus->dims_charge[i];
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "rank_coord") == 0);
if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t));
assert (nucleus->dims_coord != NULL);
uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
//assert (strcmp(buffer, "dims_coord") == 0);
uint j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 1) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dims_coord[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_coord *= nucleus->dims_coord[i];
}
/* Allocate arrays */
nucleus->charge = (double*) calloc(size_charge, sizeof(double));
assert (nucleus->charge != NULL);
assert (!(nucleus->charge == NULL));
if (nucleus->charge == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->coord = (double*) calloc(size_coord, sizeof(double));
assert (nucleus->coord != NULL);
assert (!(nucleus->coord == NULL));
if (nucleus->coord == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read data */
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "num") == 0);
assert(!((rc != 1) || (strcmp(buffer, "num") != 0)));
if ((rc != 1) || (strcmp(buffer, "num") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->num));
assert (rc == 1);
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "charge") == 0);
for (uint64_t i=0 ; i<size_charge ; i++) {
rc = fscanf(f, "%lf", &(nucleus->charge[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "coord") == 0);
assert(!((rc != 1) || (strcmp(buffer, "charge") != 0)));
if ((rc != 1) || (strcmp(buffer, "charge") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
for (uint64_t i=0 ; i<size_charge ; i++) {
rc = fscanf(f, "%lf", &(nucleus->charge[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
}
rc = fscanf(f, "%s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "coord") != 0)));
if ((rc != 1) || (strcmp(buffer, "coord") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
for (uint64_t i=0 ; i<size_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->coord[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
}
FREE(buffer);
fclose(f);
f = NULL;
}
if (file->parent.mode == 'w') {
nucleus->file = fopen(file_name,"a");
} else {
nucleus->file = fopen(file_name,"r");
}
FREE(file_name);
assert (!(nucleus->file == NULL));
if (nucleus->file == NULL) {
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
fseek(nucleus->file, 0L, SEEK_SET);
file->nucleus = nucleus;
return nucleus;
}
@ -387,7 +535,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){
fprintf(f, "dims_charge[%d] %ld\n", i, nucleus->dims_charge[i]);
fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]);
size_charge *= nucleus->dims_charge[i];
}
@ -395,7 +543,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]);
fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]);
size_coord *= nucleus->dims_coord[i];
}
@ -425,41 +573,33 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_exit_code rc;
if (file->parent.mode != 'r') {
rc = trexio_text_flush_nucleus(file);
rc = trexio_text_flush_rdm(file);
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
}
nucleus_t* nucleus = file->nucleus;
if (nucleus == NULL) return TREXIO_SUCCESS;
rdm_t* rdm = file->rdm;
if (rdm == NULL) return TREXIO_SUCCESS;
if (nucleus->file != NULL) {
fclose(nucleus->file);
nucleus->file = NULL;
}
if (nucleus->dims_coord != NULL) {
FREE (nucleus->dims_coord);
}
if (nucleus->coord != NULL) {
FREE (nucleus->coord);
}
if (nucleus->dims_charge != NULL) {
FREE (nucleus->dims_charge);
}
if (nucleus->charge != NULL) {
FREE (nucleus->charge);
if (rdm->file != NULL) {
fclose(rdm->file);
rdm->file = NULL;
}
FREE (nucleus);
if (rdm->one_e != NULL) {
FREE (rdm->one_e);
}
if (rdm->two_e_file_name != NULL) {
FREE (rdm->two_e_file_name);
}
free (rdm);
file->rdm = NULL;
return TREXIO_SUCCESS;
}
#+end_src
@ -549,12 +689,7 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
FREE(nucleus->coord);
}
if (nucleus->dims_coord != NULL) {
FREE(nucleus->dims_coord);
}
nucleus->rank_coord = rank;
nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
@ -619,14 +754,8 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
FREE(nucleus->charge);
}
if (nucleus->dims_charge != NULL) {
FREE(nucleus->dims_charge);
}
nucleus->rank_charge = rank;
nucleus->dims_charge = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i];