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

Merge branch 'master' of github.com:TREX-CoE/trexio into generator-1

This commit is contained in:
q-posev 2021-03-13 20:16:42 +01:00
commit 6a2533be3b
10 changed files with 544 additions and 308 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;
@ -83,8 +83,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
}
if (rc != TREXIO_SUCCESS) {
free(result->file_name);
free(result);
FREE(result->file_name);
FREE(result);
return NULL;
}
@ -109,8 +109,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
}
if (rc != TREXIO_SUCCESS) {
free(result->file_name);
free(result);
FREE(result->file_name);
FREE(result);
return NULL;
}
@ -143,8 +143,8 @@ trexio_exit_code trexio_close(trexio_t* file) {
}
if (rc != TREXIO_SUCCESS) {
free(file->file_name);
free(file);
FREE(file->file_name);
FREE(file);
return TREXIO_FAILURE;
}
@ -170,12 +170,11 @@ trexio_exit_code trexio_close(trexio_t* file) {
/* Terminate front end */
free(file->file_name);
file->file_name = NULL;
FREE(file->file_name);
int irc = pthread_mutex_destroy( &(file->thread_lock) );
free(file);
FREE(file);
if (irc != 0) return TREXIO_ERRNO;
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;

View File

@ -23,7 +23,8 @@ typedef int32_t trexio_exit_code;
#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_ALLOCATION_FAILED ( (trexio_exit_code) 21 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 )
typedef int32_t back_end_t;

View File

@ -46,6 +46,7 @@
#define _TREXIO_S_H
#include "trexio.h"
#include "trexio_private.h"
#include <pthread.h>
#include <assert.h>
#+end_src
@ -61,6 +62,24 @@
- structs are suffixed by ~_s~
- types are suffixed by ~_t~
** Memory allocation
Memory allocation of structures can be facilitated by using the
following macro, which ensures that the size of the allocated
object is the same as the size of the data type pointed by the pointer.
#+begin_src c :tangle trexio_private.h
#define MALLOC(T) (T*) malloc (sizeof(T));
#define CALLOC(N,T) (T*) calloc ((N),sizeof(T));
#+end_src
When a pointer is freed, it should be set to ~NULL~.
This can be facilitated by the use of the following macro:
#+begin_src c :tangle trexio_private.h
#define FREE(X) { free(X) ; (X)=NULL; }
#+end_src
* Front end
All calls to TREXIO are thread-safe.
@ -80,7 +99,8 @@ typedef int32_t trexio_exit_code;
#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_ALLOCATION_FAILED ( (trexio_exit_code) 21 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 22 )
#+end_src
** Back ends
@ -193,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;
@ -223,8 +243,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
}
if (rc != TREXIO_SUCCESS) {
free(result->file_name);
free(result);
FREE(result->file_name);
FREE(result);
return NULL;
}
@ -249,8 +269,8 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
}
if (rc != TREXIO_SUCCESS) {
free(result->file_name);
free(result);
FREE(result->file_name);
FREE(result);
return NULL;
}
@ -291,8 +311,8 @@ trexio_exit_code trexio_close(trexio_t* file) {
}
if (rc != TREXIO_SUCCESS) {
free(file->file_name);
free(file);
FREE(file->file_name);
FREE(file);
return TREXIO_FAILURE;
}
@ -318,12 +338,11 @@ trexio_exit_code trexio_close(trexio_t* file) {
/* Terminate front end */
free(file->file_name);
file->file_name = NULL;
FREE(file->file_name);
int irc = pthread_mutex_destroy( &(file->thread_lock) );
free(file);
FREE(file);
if (irc != 0) return TREXIO_ERRNO;
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;

View File

@ -7,10 +7,10 @@
#include "trexio_hdf5.h"
#define NUCLEUS_GROUP_NAME "nucleus"
#define NUCLEUS_NUM_NAME "nucleus_num"
#define NUCLEUS_CHARGE_NAME "nucleus_charge"
#define NUCLEUS_COORD_NAME "nucleus_coord"
#define NUCLEUS_GROUP_NAME "nucleus"
#define NUCLEUS_NUM_NAME "nucleus_num"
#define NUCLEUS_CHARGE_NAME "nucleus_charge"
#define NUCLEUS_COORD_NAME "nucleus_coord"
/*
* Currently H5LTread_dataset_ is used instead of this function
@ -24,7 +24,7 @@ dset_t* trexio_hdf5_read_dset_low(const trexio_hdf5_t* file, const char *dset_na
/*
* Low-level implementation. Involves dealing with all HDF5 handles and dimensions
*/
dset_t* dset = (dset_t*) malloc(sizeof(dset_t));
dset_t* dset = MALLOC(dset_t);
assert (dset != NULL);
dset->dset_id = H5Dopen(file->nucleus_group,
@ -145,7 +145,7 @@ trexio_exit_code trexio_hdf5_finalize(trexio_t* file) {
h5nucleus_t* trexio_hdf5_read_nucleus(const trexio_hdf5_t* file) {
/* Allocate the data structure */
h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t));
h5nucleus_t* nucleus = MALLOC(h5nucleus_t);
assert (nucleus != NULL);
nucleus->num = 0;
@ -323,19 +323,19 @@ trexio_exit_code trexio_hdf5_free_nucleus(h5nucleus_t* nucleus) {
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->coord != NULL) free (nucleus->coord);
nucleus->coord = NULL;
if (nucleus->coord != NULL)
FREE (nucleus->coord);
if (nucleus->charge != NULL) free (nucleus->charge);
nucleus->charge = NULL;
if (nucleus->charge != NULL)
FREE (nucleus->charge);
if (nucleus->h5_coord != NULL) free (nucleus->h5_coord);
nucleus->h5_coord = NULL;
if (nucleus->h5_coord != NULL)
FREE (nucleus->h5_coord);
if (nucleus->h5_charge != NULL) free (nucleus->h5_charge);
nucleus->h5_charge = NULL;
if (nucleus->h5_charge != NULL)
FREE (nucleus->h5_charge);
free (nucleus);
FREE (nucleus);
return TREXIO_SUCCESS;
}
@ -446,17 +446,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co
H5Dclose(dset_id);
if (status < 0) {
free(ddims);
FREE(ddims);
return TREXIO_FAILURE;
}
for (uint32_t i=0; i<rank; i++){
if (ddims[i] != dims[i]) {
free(ddims);
FREE(ddims);
return TREXIO_INVALID_ARG_4;
}
}
free(ddims);
FREE(ddims);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(f->nucleus_group,

View File

@ -9,6 +9,7 @@
#define _TREXIO_HDF5_H
#include "trexio.h"
#include "trexio_private.h"
#include "trexio_s.h"
#include <stdint.h>
#include <stdio.h>

View File

@ -19,6 +19,7 @@
#define _TREXIO_HDF5_H
#include "trexio.h"
#include "trexio_private.h"
#include "trexio_s.h"
#include <stdint.h>
#include <stdio.h>
@ -115,7 +116,7 @@ dset_t* trexio_hdf5_read_dset_low(const trexio_hdf5_t* file, const char *dset_na
/*
* Low-level implementation. Involves dealing with all HDF5 handles and dimensions
*/
dset_t* dset = (dset_t*) malloc(sizeof(dset_t));
dset_t* dset = MALLOC(dset_t);
assert (dset != NULL);
dset->dset_id = H5Dopen(file->nucleus_group,
@ -280,7 +281,7 @@ typedef struct four_index_s {
h5nucleus_t* trexio_hdf5_read_nucleus(const trexio_hdf5_t* file) {
/* Allocate the data structure */
h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t));
h5nucleus_t* nucleus = MALLOC(h5nucleus_t);
assert (nucleus != NULL);
nucleus->num = 0;
@ -465,19 +466,19 @@ trexio_exit_code trexio_hdf5_free_nucleus(h5nucleus_t* nucleus) {
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->coord != NULL) free (nucleus->coord);
nucleus->coord = NULL;
if (nucleus->coord != NULL)
FREE (nucleus->coord);
if (nucleus->charge != NULL) free (nucleus->charge);
nucleus->charge = NULL;
if (nucleus->charge != NULL)
FREE (nucleus->charge);
if (nucleus->h5_coord != NULL) free (nucleus->h5_coord);
nucleus->h5_coord = NULL;
if (nucleus->h5_coord != NULL)
FREE (nucleus->h5_coord);
if (nucleus->h5_charge != NULL) free (nucleus->h5_charge);
nucleus->h5_charge = NULL;
if (nucleus->h5_charge != NULL)
FREE (nucleus->h5_charge);
free (nucleus);
FREE (nucleus);
return TREXIO_SUCCESS;
}
@ -607,17 +608,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* co
H5Dclose(dset_id);
if (status < 0) {
free(ddims);
FREE(ddims);
return TREXIO_FAILURE;
}
for (uint32_t i=0; i<rank; i++){
if (ddims[i] != dims[i]) {
free(ddims);
FREE(ddims);
return TREXIO_INVALID_ARG_4;
}
}
free(ddims);
FREE(ddims);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(f->nucleus_group,

View File

@ -9,6 +9,7 @@
#define _TREXIO_S_H
#include "trexio.h"
#include "trexio_private.h"
#include <pthread.h>
#include <assert.h>

View File

@ -12,6 +12,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;
@ -27,20 +30,22 @@ 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);
FREE(file_name);
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
if (f->lock_file <= 0) {
return TREXIO_FAILURE;
}
return TREXIO_SUCCESS;
}
@ -96,29 +101,34 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) {
return TREXIO_SUCCESS;
}
#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 = (nucleus_t*) malloc(sizeof(nucleus_t));
assert (nucleus != NULL);
nucleus_t* nucleus = MALLOC(nucleus_t);
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,98 +140,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 = (char*) malloc(sz*sizeof(char));
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);
nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t));
assert (nucleus->dims_charge != NULL);
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
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);
assert (rc == 1);
//assert (strcmp(buffer, "dims_charge") == 0);
unsigned int 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);
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t));
assert (nucleus->dims_coord != NULL);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
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);
assert (rc == 1);
//assert (strcmp(buffer, "dims_coord") == 0);
unsigned int 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);
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, "charge") == 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);
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, "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);
}
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);
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;
}
@ -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);
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]);
for (unsigned int i=0; i<nucleus->rank_charge; i++){
fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]);
size_charge *= nucleus->dims_charge[i];
}
fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
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]);
for (unsigned int i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]);
size_coord *= nucleus->dims_coord[i];
}
@ -292,28 +423,15 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
}
if (nucleus->dims_coord != NULL) {
free (nucleus->dims_coord);
nucleus->dims_coord = NULL;
}
if (nucleus->coord != NULL) {
free (nucleus->coord);
nucleus->coord = NULL;
}
if (nucleus->dims_charge != NULL) {
free (nucleus->dims_charge);
nucleus->dims_charge = NULL;
FREE (nucleus->coord);
}
if (nucleus->charge != NULL) {
free (nucleus->charge);
nucleus->charge = NULL;
FREE (nucleus->charge);
}
free (nucleus);
file->nucleus = NULL;
FREE (nucleus);
return TREXIO_SUCCESS;
}
@ -357,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;
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;
dim_size *= dims[i];
}
@ -380,20 +498,13 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->coord != NULL) {
free(nucleus->coord);
nucleus->coord = NULL;
}
if (nucleus->dims_coord != NULL) {
free(nucleus->dims_coord);
nucleus->dims_coord = NULL;
FREE(nucleus->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++){
for (unsigned int i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i];
dim_size *= dims[i];
}
@ -419,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;
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;
dim_size *= dims[i];
}
@ -442,21 +553,13 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->charge != NULL) {
free(nucleus->charge);
nucleus->charge = NULL;
}
if (nucleus->dims_charge != NULL) {
free(nucleus->dims_charge);
nucleus->dims_charge = NULL;
FREE(nucleus->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++){
for (unsigned int i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i];
dim_size *= dims[i];
}
@ -477,7 +580,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
if (file->rdm != NULL) return file->rdm;
/* Allocate the data structure */
rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t));
rdm_t* rdm = MALLOC(rdm_t);
assert (rdm != NULL);
rdm->one_e = NULL;
@ -502,7 +605,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char* buffer = (char*) malloc(sz*sizeof(char));
char* buffer = CALLOC(sz,char);
/* Read the dimensioning variables */
int rc;
@ -534,10 +637,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char));
rdm->two_e_file_name = CALLOC (strlen(buffer),char);
strcpy(rdm->two_e_file_name, buffer);
free(buffer);
FREE(buffer);
fclose(f);
f = NULL;
}
@ -546,7 +649,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
} else {
rdm->file = fopen(file_name,"r");
}
free(file_name);
FREE(file_name);
file->rdm = rdm ;
return rdm;
}
@ -600,13 +703,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
}
if (rdm->one_e != NULL) {
free (rdm->one_e);
rdm->one_e = NULL;
FREE (rdm->one_e);
}
if (rdm->two_e_file_name != NULL) {
free (rdm->two_e_file_name);
rdm->two_e_file_name = NULL;
FREE (rdm->two_e_file_name);
}
free (rdm);

View File

@ -9,6 +9,7 @@
#define _TREXIO_TEXT_H
#include "trexio.h"
#include "trexio_private.h"
#include "trexio_s.h"
#include <errno.h>
#include <stdint.h>
@ -25,8 +26,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;

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.
@ -19,6 +32,7 @@
#define _TREXIO_TEXT_H
#include "trexio.h"
#include "trexio_private.h"
#include "trexio_s.h"
#include <errno.h>
#include <stdint.h>
@ -33,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
@ -58,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;
@ -80,13 +100,6 @@ typedef struct rdm_s {
int to_flush;
} rdm_t;
#+end_src
*** TO DO
- to_flush = 1 in write
- to_flush = 0 when flushed
- name
*** Structs for the text back end
#+begin_src c :tangle trexio_text.h
@ -113,6 +126,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;
@ -128,24 +144,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);
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
@ -171,7 +188,6 @@ trexio_exit_code trexio_text_lock(trexio_t* file) {
return TREXIO_SUCCESS;
}
#+end_src
@ -228,29 +244,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 = (nucleus_t*) malloc(sizeof(nucleus_t));
assert (nucleus != NULL);
nucleus_t* nucleus = MALLOC(nucleus_t);
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);
@ -262,98 +283,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 = (char*) malloc(sz*sizeof(char));
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);
nucleus->dims_charge = (uint64_t*) calloc(nucleus->rank_charge, sizeof(uint64_t));
assert (nucleus->dims_charge != NULL);
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
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);
assert (rc == 1);
//assert (strcmp(buffer, "dims_charge") == 0);
unsigned int 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);
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t));
assert (nucleus->dims_coord != NULL);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
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);
assert (rc == 1);
//assert (strcmp(buffer, "dims_coord") == 0);
unsigned int 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);
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, "charge") == 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);
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, "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);
}
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);
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;
}
@ -385,16 +527,16 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
fprintf(f, "rank_charge %d\n", nucleus->rank_charge);
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]);
for (unsigned int i=0; i<nucleus->rank_charge; i++){
fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]);
size_charge *= nucleus->dims_charge[i];
}
fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
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]);
for (unsigned int i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]);
size_coord *= nucleus->dims_coord[i];
}
@ -442,28 +584,15 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
}
if (nucleus->dims_coord != NULL) {
free (nucleus->dims_coord);
nucleus->dims_coord = NULL;
}
if (nucleus->coord != NULL) {
free (nucleus->coord);
nucleus->coord = NULL;
}
if (nucleus->dims_charge != NULL) {
free (nucleus->dims_charge);
nucleus->dims_charge = NULL;
FREE (nucleus->coord);
}
if (nucleus->charge != NULL) {
free (nucleus->charge);
nucleus->charge = NULL;
FREE (nucleus->charge);
}
free (nucleus);
file->nucleus = NULL;
FREE (nucleus);
return TREXIO_SUCCESS;
}
#+end_src
@ -527,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;
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;
dim_size *= dims[i];
}
@ -550,20 +679,13 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->coord != NULL) {
free(nucleus->coord);
nucleus->coord = NULL;
}
if (nucleus->dims_coord != NULL) {
free(nucleus->dims_coord);
nucleus->dims_coord = NULL;
FREE(nucleus->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++){
for (unsigned int i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i];
dim_size *= dims[i];
}
@ -599,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;
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;
dim_size *= dims[i];
}
@ -622,21 +744,13 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->charge != NULL) {
free(nucleus->charge);
nucleus->charge = NULL;
}
if (nucleus->dims_charge != NULL) {
free(nucleus->dims_charge);
nucleus->dims_charge = NULL;
FREE(nucleus->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++){
for (unsigned int i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i];
dim_size *= dims[i];
}
@ -666,7 +780,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
if (file->rdm != NULL) return file->rdm;
/* Allocate the data structure */
rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t));
rdm_t* rdm = MALLOC(rdm_t);
assert (rdm != NULL);
rdm->one_e = NULL;
@ -691,7 +805,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char* buffer = (char*) malloc(sz*sizeof(char));
char* buffer = CALLOC(sz,char);
/* Read the dimensioning variables */
int rc;
@ -723,10 +837,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char));
rdm->two_e_file_name = CALLOC (strlen(buffer),char);
strcpy(rdm->two_e_file_name, buffer);
free(buffer);
FREE(buffer);
fclose(f);
f = NULL;
}
@ -735,7 +849,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
} else {
rdm->file = fopen(file_name,"r");
}
free(file_name);
FREE(file_name);
file->rdm = rdm ;
return rdm;
}
@ -807,13 +921,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
}
if (rdm->one_e != NULL) {
free (rdm->one_e);
rdm->one_e = NULL;
FREE (rdm->one_e);
}
if (rdm->two_e_file_name != NULL) {
free (rdm->two_e_file_name);
rdm->two_e_file_name = NULL;
FREE (rdm->two_e_file_name);
}
free (rdm);