1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 12:56:53 +01:00

Merge pull request #26 from TREX-CoE/text

Improved checks in text back end
This commit is contained in:
Evgeny Posenitskiy 2021-03-13 20:14:55 +01:00 committed by GitHub
commit 8e8b2ba57f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 448 additions and 212 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 */ /* 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); strcpy(result->file_name, file_name);
result->back_end = back_end; result->back_end = back_end;
result->mode = mode; result->mode = mode;

View File

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

View File

@ -88,18 +88,19 @@
#+begin_src c :tangle trexio.h #+begin_src c :tangle trexio.h
typedef int32_t trexio_exit_code; typedef int32_t trexio_exit_code;
#define TREXIO_FAILURE ( (trexio_exit_code) -1 ) #define TREXIO_FAILURE ( (trexio_exit_code) -1 )
#define TREXIO_SUCCESS ( (trexio_exit_code) 0 ) #define TREXIO_SUCCESS ( (trexio_exit_code) 0 )
#define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 ) #define TREXIO_INVALID_ARG_1 ( (trexio_exit_code) 1 )
#define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 ) #define TREXIO_INVALID_ARG_2 ( (trexio_exit_code) 2 )
#define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 ) #define TREXIO_INVALID_ARG_3 ( (trexio_exit_code) 3 )
#define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 ) #define TREXIO_INVALID_ARG_4 ( (trexio_exit_code) 4 )
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 ) #define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 ) #define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 ) #define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 ) #define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 ) #define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 ) #define TREXIO_ALLOCATION_FAILED ( (trexio_exit_code) 21 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 )
#+end_src #+end_src
** Back ends ** 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 */ /* 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); strcpy(result->file_name, file_name);
result->back_end = back_end; result->back_end = back_end;
result->mode = mode; result->mode = mode;

View File

@ -12,6 +12,9 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
trexio_text_t* f = (trexio_text_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 */ /* If directory doesn't exist, create it in write mode */
struct stat st; struct stat st;
@ -27,20 +30,22 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
/* Create the lock file in the directory */ /* Create the lock file in the directory */
const char* lock_file_name = "/.lock"; const char* lock_file_name = "/.lock";
char* file_name = (char*) char* file_name =
calloc( strlen(file->file_name) + strlen(lock_file_name) + 1, CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char);
sizeof(char));
assert (file_name != NULL); if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED;
}
strcpy (file_name, file->file_name); strcpy (file_name, file->file_name);
strcat (file_name, lock_file_name); strcat (file_name, lock_file_name);
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
assert (f->lock_file > 0);
FREE(file_name); FREE(file_name);
f->nucleus = NULL; if (f->lock_file <= 0) {
f->electron= NULL; return TREXIO_FAILURE;
f->rdm = NULL; }
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
@ -96,29 +101,34 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) {
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__);
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) { nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
if (file == NULL) return NULL; 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 */ /* Allocate the data structure */
nucleus_t* nucleus = MALLOC(nucleus_t); nucleus_t* nucleus = MALLOC(nucleus_t);
assert (nucleus != NULL); if (nucleus == NULL) return NULL;
nucleus->file = NULL; memset(nucleus,0,sizeof(nucleus_t));
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->dims_coord = NULL;
nucleus->charge = NULL;
nucleus->dims_charge = NULL;
nucleus->to_flush = 0;
/* Try to open the file. If the file does not exist, return */ /* Build the file name */
const char* nucleus_file_name = "/nucleus.txt"; const char* nucleus_file_name = "/nucleus.txt";
char * file_name = (char*) char * file_name = (char*)
calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1, calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1,
sizeof(char)); sizeof(char));
assert (file_name != NULL);
if (file_name == NULL) {
FREE(nucleus);
DEBUG
return NULL;
}
strcpy (file_name, file->parent.file_name); strcpy (file_name, file->parent.file_name);
strcat (file_name, nucleus_file_name); strcat (file_name, nucleus_file_name);
@ -130,98 +140,219 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
fseek(f, 0L, SEEK_END); fseek(f, 0L, SEEK_END);
size_t sz = ftell(f); size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET); fseek(f, 0L, SEEK_SET);
char* buffer = CALLOC(sz,char); char* buffer = CALLOC(sz,char);
if (buffer == NULL) {
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read the dimensioning variables */ /* Read the dimensioning variables */
int rc; int rc;
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) {
assert (strcmp(buffer, "rank_charge") == 0); FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_charge)); rc = fscanf(f, "%u", &(nucleus->rank_charge));
assert (rc == 1); if (rc != 1) {
FREE(buffer);
nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t)); FREE(file_name);
assert (nucleus->dims_charge != NULL); FREE(nucleus);
DEBUG
return NULL;
}
uint64_t size_charge = 1; uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){ for (unsigned int i=0; i<nucleus->rank_charge; i++){
rc = fscanf(f, "%s", buffer); unsigned int j=-1;
assert (rc == 1); rc = fscanf(f, "%s %u", buffer, &j);
//assert (strcmp(buffer, "dims_charge") == 0); 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])); rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i]));
assert (rc == 1); assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_charge *= nucleus->dims_charge[i]; size_charge *= nucleus->dims_charge[i];
} }
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) {
assert (strcmp(buffer, "rank_coord") == 0); FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_coord)); rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert (rc == 1); assert(!(rc != 1));
if (rc != 1) {
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t)); FREE(buffer);
assert (nucleus->dims_coord != NULL); FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
uint64_t size_coord = 1; uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){ for (unsigned int i=0; i<nucleus->rank_coord; i++){
rc = fscanf(f, "%s", buffer); unsigned int j=-1;
assert (rc == 1); rc = fscanf(f, "%s %u", buffer, &j);
//assert (strcmp(buffer, "dims_coord") == 0); 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])); 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]; size_coord *= nucleus->dims_coord[i];
} }
/* Allocate arrays */ /* Allocate arrays */
nucleus->charge = (double*) calloc(size_charge, sizeof(double)); 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)); 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 */ /* Read data */
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); assert(!((rc != 1) || (strcmp(buffer, "num") != 0)));
assert (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)); rc = fscanf(f, "%lu", &(nucleus->num));
assert (rc == 1); assert(!(rc != 1));
if (rc != 1) {
rc = fscanf(f, "%s", buffer); FREE(buffer);
assert (rc == 1); FREE(file_name);
assert (strcmp(buffer, "charge") == 0); FREE(nucleus->charge);
FREE(nucleus);
for (uint64_t i=0 ; i<size_charge ; i++) { DEBUG
rc = fscanf(f, "%lf", &(nucleus->charge[i])); return NULL;
assert (rc == 1);
} }
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); assert(!((rc != 1) || (strcmp(buffer, "charge") != 0)));
assert (strcmp(buffer, "coord") == 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++) { for (uint64_t i=0 ; i<size_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->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); FREE(buffer);
fclose(f); fclose(f);
f = NULL; f = NULL;
} }
if (file->parent.mode == 'w') { if (file->parent.mode == 'w') {
nucleus->file = fopen(file_name,"a"); nucleus->file = fopen(file_name,"a");
} else { } else {
nucleus->file = fopen(file_name,"r"); nucleus->file = fopen(file_name,"r");
} }
FREE(file_name); 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; file->nucleus = nucleus;
return nucleus; return nucleus;
} }
@ -245,16 +376,16 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
fprintf(f, "rank_charge %d\n", nucleus->rank_charge); fprintf(f, "rank_charge %d\n", nucleus->rank_charge);
uint64_t size_charge = 1; uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){ for (unsigned int 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]; size_charge *= nucleus->dims_charge[i];
} }
fprintf(f, "rank_coord %d\n", nucleus->rank_coord); fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
uint64_t size_coord = 1; uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){ for (unsigned int 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]; size_coord *= nucleus->dims_coord[i];
} }
@ -292,18 +423,10 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL; nucleus->file = NULL;
} }
if (nucleus->dims_coord != NULL) {
FREE (nucleus->dims_coord);
}
if (nucleus->coord != NULL) { if (nucleus->coord != NULL) {
FREE (nucleus->coord); FREE (nucleus->coord);
} }
if (nucleus->dims_charge != NULL) {
FREE (nucleus->dims_charge);
}
if (nucleus->charge != NULL) { if (nucleus->charge != NULL) {
FREE (nucleus->charge); FREE (nucleus->charge);
} }
@ -352,7 +475,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co
if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3; if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<rank; i++){ for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4; if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i]; dim_size *= dims[i];
} }
@ -378,15 +501,10 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
FREE(nucleus->coord); FREE(nucleus->coord);
} }
if (nucleus->dims_coord != NULL) {
FREE(nucleus->dims_coord);
}
nucleus->rank_coord = rank; nucleus->rank_coord = rank;
nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_coord; i++){ for (unsigned int i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i]; nucleus->dims_coord[i] = dims[i];
dim_size *= dims[i]; dim_size *= dims[i];
} }
@ -412,7 +530,7 @@ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* c
if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3; if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<rank; i++){ for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_charge[i]) return TREXIO_INVALID_ARG_4; if (dims[i] != nucleus->dims_charge[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i]; dim_size *= dims[i];
} }
@ -438,16 +556,10 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
FREE(nucleus->charge); FREE(nucleus->charge);
} }
if (nucleus->dims_charge != NULL) {
FREE(nucleus->dims_charge);
}
nucleus->rank_charge = rank; nucleus->rank_charge = rank;
nucleus->dims_charge = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_charge; i++){ for (unsigned int i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i]; nucleus->dims_charge[i] = dims[i];
dim_size *= dims[i]; dim_size *= dims[i];
} }

View File

@ -26,8 +26,8 @@ typedef struct nucleus_s {
double* coord; double* coord;
double* charge; double* charge;
uint64_t num; uint64_t num;
uint64_t* dims_charge; uint64_t dims_charge[16];
uint64_t* dims_coord; uint64_t dims_coord[16];
uint32_t rank_charge; uint32_t rank_charge;
uint32_t rank_coord; uint32_t rank_coord;
int to_flush; int to_flush;

View File

@ -2,6 +2,19 @@
* File prefixes :noxport: * 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 #+NAME:header
#+begin_src c #+begin_src c
/* This file was generated from the trexio.org org-mode file. /* This file was generated from the trexio.org org-mode file.
@ -34,7 +47,13 @@
#+end_src #+end_src
#+begin_src c :tangle trexio_text.c :noweb yes #+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" #include "trexio_text.h"
#+end_src #+end_src
@ -59,8 +78,8 @@ typedef struct nucleus_s {
double* coord; double* coord;
double* charge; double* charge;
uint64_t num; uint64_t num;
uint64_t* dims_charge; uint64_t dims_charge[16];
uint64_t* dims_coord; uint64_t dims_coord[16];
uint32_t rank_charge; uint32_t rank_charge;
uint32_t rank_coord; uint32_t rank_coord;
int to_flush; int to_flush;
@ -81,13 +100,6 @@ typedef struct rdm_s {
int to_flush; int to_flush;
} rdm_t; } rdm_t;
#+end_src #+end_src
*** TO DO
- to_flush = 1 in write
- to_flush = 0 when flushed
- name
*** Structs for the text back end *** Structs for the text back end
#+begin_src c :tangle trexio_text.h #+begin_src c :tangle trexio_text.h
@ -114,6 +126,9 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
trexio_text_t* f = (trexio_text_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 */ /* If directory doesn't exist, create it in write mode */
struct stat st; struct stat st;
@ -129,24 +144,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
/* Create the lock file in the directory */ /* Create the lock file in the directory */
const char* lock_file_name = "/.lock"; const char* lock_file_name = "/.lock";
char* file_name = (char*) char* file_name =
calloc( strlen(file->file_name) + strlen(lock_file_name) + 1, CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char);
sizeof(char));
assert (file_name != NULL); if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED;
}
strcpy (file_name, file->file_name); strcpy (file_name, file->file_name);
strcat (file_name, lock_file_name); strcat (file_name, lock_file_name);
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644); f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
assert (f->lock_file > 0);
FREE(file_name); FREE(file_name);
f->nucleus = NULL; if (f->lock_file <= 0) {
f->electron= NULL; return TREXIO_FAILURE;
f->rdm = NULL; }
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
#+end_src #+end_src
#+begin_src c :tangle trexio_text.h #+begin_src c :tangle trexio_text.h
@ -172,7 +188,6 @@ trexio_exit_code trexio_text_lock(trexio_t* file) {
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
#+end_src #+end_src
@ -229,29 +244,34 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
#+end_src #+end_src
#+begin_src c :tangle trexio_text.c #+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) { nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
if (file == NULL) return NULL; 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 */ /* Allocate the data structure */
nucleus_t* nucleus = MALLOC(nucleus_t); nucleus_t* nucleus = MALLOC(nucleus_t);
assert (nucleus != NULL); if (nucleus == NULL) return NULL;
nucleus->file = NULL; memset(nucleus,0,sizeof(nucleus_t));
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->dims_coord = NULL;
nucleus->charge = NULL;
nucleus->dims_charge = NULL;
nucleus->to_flush = 0;
/* Try to open the file. If the file does not exist, return */ /* Build the file name */
const char* nucleus_file_name = "/nucleus.txt"; const char* nucleus_file_name = "/nucleus.txt";
char * file_name = (char*) char * file_name = (char*)
calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1, calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1,
sizeof(char)); sizeof(char));
assert (file_name != NULL);
if (file_name == NULL) {
FREE(nucleus);
DEBUG
return NULL;
}
strcpy (file_name, file->parent.file_name); strcpy (file_name, file->parent.file_name);
strcat (file_name, nucleus_file_name); strcat (file_name, nucleus_file_name);
@ -263,98 +283,219 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
fseek(f, 0L, SEEK_END); fseek(f, 0L, SEEK_END);
size_t sz = ftell(f); size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET); fseek(f, 0L, SEEK_SET);
char* buffer = CALLOC(sz,char); char* buffer = CALLOC(sz,char);
if (buffer == NULL) {
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read the dimensioning variables */ /* Read the dimensioning variables */
int rc; int rc;
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) {
assert (strcmp(buffer, "rank_charge") == 0); FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_charge)); rc = fscanf(f, "%u", &(nucleus->rank_charge));
assert (rc == 1); if (rc != 1) {
FREE(buffer);
nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t)); FREE(file_name);
assert (nucleus->dims_charge != NULL); FREE(nucleus);
DEBUG
return NULL;
}
uint64_t size_charge = 1; uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){ for (unsigned int i=0; i<nucleus->rank_charge; i++){
rc = fscanf(f, "%s", buffer); unsigned int j=-1;
assert (rc == 1); rc = fscanf(f, "%s %u", buffer, &j);
//assert (strcmp(buffer, "dims_charge") == 0); 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])); rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i]));
assert (rc == 1); assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_charge *= nucleus->dims_charge[i]; size_charge *= nucleus->dims_charge[i];
} }
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) {
assert (strcmp(buffer, "rank_coord") == 0); FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_coord)); rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert (rc == 1); assert(!(rc != 1));
if (rc != 1) {
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t)); FREE(buffer);
assert (nucleus->dims_coord != NULL); FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
uint64_t size_coord = 1; uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){ for (unsigned int i=0; i<nucleus->rank_coord; i++){
rc = fscanf(f, "%s", buffer); unsigned int j=-1;
assert (rc == 1); rc = fscanf(f, "%s %u", buffer, &j);
//assert (strcmp(buffer, "dims_coord") == 0); 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])); 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]; size_coord *= nucleus->dims_coord[i];
} }
/* Allocate arrays */ /* Allocate arrays */
nucleus->charge = (double*) calloc(size_charge, sizeof(double)); 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)); 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 */ /* Read data */
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); assert(!((rc != 1) || (strcmp(buffer, "num") != 0)));
assert (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)); rc = fscanf(f, "%lu", &(nucleus->num));
assert (rc == 1); assert(!(rc != 1));
if (rc != 1) {
rc = fscanf(f, "%s", buffer); FREE(buffer);
assert (rc == 1); FREE(file_name);
assert (strcmp(buffer, "charge") == 0); FREE(nucleus->charge);
FREE(nucleus);
for (uint64_t i=0 ; i<size_charge ; i++) { DEBUG
rc = fscanf(f, "%lf", &(nucleus->charge[i])); return NULL;
assert (rc == 1);
} }
rc = fscanf(f, "%s", buffer); rc = fscanf(f, "%s", buffer);
assert (rc == 1); assert(!((rc != 1) || (strcmp(buffer, "charge") != 0)));
assert (strcmp(buffer, "coord") == 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++) { for (uint64_t i=0 ; i<size_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->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); FREE(buffer);
fclose(f); fclose(f);
f = NULL; f = NULL;
} }
if (file->parent.mode == 'w') { if (file->parent.mode == 'w') {
nucleus->file = fopen(file_name,"a"); nucleus->file = fopen(file_name,"a");
} else { } else {
nucleus->file = fopen(file_name,"r"); nucleus->file = fopen(file_name,"r");
} }
FREE(file_name); 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; file->nucleus = nucleus;
return nucleus; return nucleus;
} }
@ -386,16 +527,16 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
fprintf(f, "rank_charge %d\n", nucleus->rank_charge); fprintf(f, "rank_charge %d\n", nucleus->rank_charge);
uint64_t size_charge = 1; uint64_t size_charge = 1;
for (uint i=0; i<nucleus->rank_charge; i++){ for (unsigned int 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]; size_charge *= nucleus->dims_charge[i];
} }
fprintf(f, "rank_coord %d\n", nucleus->rank_coord); fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
uint64_t size_coord = 1; uint64_t size_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){ for (unsigned int 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]; size_coord *= nucleus->dims_coord[i];
} }
@ -443,18 +584,10 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL; nucleus->file = NULL;
} }
if (nucleus->dims_coord != NULL) {
FREE (nucleus->dims_coord);
}
if (nucleus->coord != NULL) { if (nucleus->coord != NULL) {
FREE (nucleus->coord); FREE (nucleus->coord);
} }
if (nucleus->dims_charge != NULL) {
FREE (nucleus->dims_charge);
}
if (nucleus->charge != NULL) { if (nucleus->charge != NULL) {
FREE (nucleus->charge); FREE (nucleus->charge);
} }
@ -523,7 +656,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co
if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3; if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<rank; i++){ for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4; if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i]; dim_size *= dims[i];
} }
@ -549,15 +682,10 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
FREE(nucleus->coord); FREE(nucleus->coord);
} }
if (nucleus->dims_coord != NULL) {
FREE(nucleus->dims_coord);
}
nucleus->rank_coord = rank; nucleus->rank_coord = rank;
nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_coord; i++){ for (unsigned int i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i]; nucleus->dims_coord[i] = dims[i];
dim_size *= dims[i]; dim_size *= dims[i];
} }
@ -593,7 +721,7 @@ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* c
if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3; if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<rank; i++){ for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_charge[i]) return TREXIO_INVALID_ARG_4; if (dims[i] != nucleus->dims_charge[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i]; dim_size *= dims[i];
} }
@ -619,16 +747,10 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
FREE(nucleus->charge); FREE(nucleus->charge);
} }
if (nucleus->dims_charge != NULL) {
FREE(nucleus->dims_charge);
}
nucleus->rank_charge = rank; nucleus->rank_charge = rank;
nucleus->dims_charge = (uint64_t*) calloc(rank, sizeof(uint64_t));
uint64_t dim_size = 1; uint64_t dim_size = 1;
for (uint i=0; i<nucleus->rank_charge; i++){ for (unsigned int i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i]; nucleus->dims_charge[i] = dims[i];
dim_size *= dims[i]; dim_size *= dims[i];
} }