1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

generator for text backend before test

This commit is contained in:
q-posev 2021-03-16 18:34:07 +01:00
parent 0da57b6b6c
commit aa67877948
3 changed files with 431 additions and 390 deletions

View File

@ -1,6 +1,6 @@
import json
from os import listdir
from os import listdir, scandir, remove
from os.path import isfile, join, dirname, abspath
fileDir = dirname(abspath(__file__))
@ -15,7 +15,8 @@ del config0['metadata']
config = {}
for k,v in config0.items():
if k == 'nucleus' or k == 'ecp':
#if k == 'nucleus' or k == 'mo':
if k == 'nucleus': #or k == 'electron':
config[k] = v
#print(config)
@ -74,52 +75,152 @@ for k,v in datasets.items():
datasets_nostr[k] = tmp_dict
print(datasets_nostr['nucleus_coord'])
#print(numbers)
#put also dimensioning variables in numbers
numbers.update(dim_variables)
print(numbers)
templ_path_text = join(fileDir,'templates_text')
templ_path_hdf5 = join(fileDir,'templates_hdf5')
templ_path_front = join(fileDir,'templates_front')
#clean the populated/ directory
for popdir in [templ_path_front, templ_path_front, templ_path_text]:
cleandir = join(popdir, 'populated')
for f in scandir(cleandir):
remove(f.path)
files_exclude = ['prefix_hdf5.c', 'prefix_hdf5.h', 'suffix_hdf5.h',
'prefix_text.c', 'prefix_text.h', 'suffix_text.h',
'prefix_front.c', 'prefix_front.h', 'suffix_front.h',
'prefix_s_front.h', 'suffix_s_front.h',
'templator_front.org', 'templator_hdf5.org']
'templator_front.org', 'templator_hdf5.org', 'templator_text.org']
files_text = [f for f in listdir(templ_path_text) if isfile(join(templ_path_text, f)) and f not in files_exclude]
files_hdf5 = [f for f in listdir(templ_path_hdf5) if isfile(join(templ_path_hdf5, f)) and f not in files_exclude]
files_front = [f for f in listdir(templ_path_front) if isfile(join(templ_path_front, f)) and f not in files_exclude]
files = files_hdf5 + files_front
files = files_text + files_hdf5 + files_front
files_funcs = [f for f in files if 'read_' in f or 'write_' in f or 'rw_' in f ]
files_funcs = [f for f in files if 'read_' in f or 'write_' in f or 'flush_' in f or 'free_' in f or 'rw_' in f ]
files_funcs_dsets = [f for f in files_funcs if 'dset' in f]
files_funcs_nums = [f for f in files_funcs if 'num' in f]
files_funcs_groups = [f for f in files_funcs if 'group' in f]
files_funcs_groups.append('struct_text_group_dset.h')
print(files_funcs_groups)
files_auxil = [f for f in files if not ('read_' in f or 'write_' in f or 'rw_' in f)]
# build files with functions
for fname in files_funcs_nums:
fname_new = join('populated',f'pop_{fname}')
if '_hdf5' in fname:
templ_path = templ_path_hdf5
if '_front' in fname:
templ_path = templ_path_front
for dim in dim_variables.keys():
# build files with functions for text groups
grname = dim.split('_')[0]
for fname in files_funcs_groups:
fname_new = join('populated',f'pop_{fname}')
if '_text' in fname:
templ_path = templ_path_text
groups_done = []
for group in config.keys():
grname = group.split('_')[0]
if grname in groups_done:
continue
else:
groups_done.append(grname)
subloop = False
do_dset = False
do_num = False
loop_body = ''
with open(join(templ_path,fname), 'r') as f_in :
with open(join(templ_path,fname_new), 'a') as f_out :
for line in f_in :
if '$' in line:
templine1 = line.replace('$GROUP_NUM$', dim.upper())
templine2 = templine1.replace('$group_num$', dim)
if 'END REPEAT' in line:
templine1 = templine2.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
if do_dset:
for dset,params in datasets_nostr.items():
dset_grname = dset.split('_')[0]
if dset_grname != grname:
continue
templine1 = loop_body.replace('$group_dset$', dset)
templine2 = templine1.replace('$group$', grname)
f_out.write(templine2)
elif do_num:
#for dim in dim_variables.keys():
for dim in numbers.keys():
num_grname = dim.split('_')[0]
if num_grname != grname:
continue
templine1 = loop_body.replace('$group_num$', dim)
templine2 = templine1.replace('$group$', grname)
f_out.write(templine2)
else:
print('fishy')
#print(loop_body)
loop_body = ''
subloop = False
do_dset = False
do_num = False
continue
if subloop:
loop_body += line
if 'START REPEAT' in line:
if 'GROUP_DSET' in line:
do_dset = True
if 'GROUP_NUM' in line:
do_num = True
subloop = True
if '$group_dset' in line and not subloop:
for dset,params in datasets_nostr.items():
f_out.write(templine2)
else:
dset_grname = dset.split('_')[0]
if dset_grname != grname:
continue
templine1 = line.replace('$group_dset$', dset)
templine2 = templine1
templine1 = templine2.replace('$group_dset_dtype$', params['dtype'])
templine2 = templine1
templine1 = templine2.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
f_out.write(templine2)
elif '$group_num' in line and not subloop:
#for dim in dim_variables.keys():
for dim in numbers.keys():
num_grname = dim.split('_')[0]
if num_grname != grname:
continue
templine1 = line.replace('$GROUP_NUM$', dim.upper())
templine2 = templine1.replace('$group_num$', dim)
templine1 = templine2.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
f_out.write(templine2)
elif '$group$' in line and not subloop:
templine1 = line.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
f_out.write(templine2)
elif not subloop:
f_out.write(line)
# build files with functions
for fname in files_funcs_dsets:
fname_new = join('populated',f'pop_{fname}')
@ -127,6 +228,9 @@ for fname in files_funcs_dsets:
templ_path = templ_path_hdf5
if '_front' in fname:
templ_path = templ_path_front
if '_text' in fname:
templ_path = templ_path_text
for dset,params in datasets_nostr.items():
grname = dset.split('_')[0]
@ -178,13 +282,43 @@ for fname in files_funcs_dsets:
else:
f_out.write(line)
# build files with $group$ and $group$-based
for fname in ['def_hdf5.c', 'basic_hdf5.c', 'struct_hdf5.h'] :
# build files with functions
for fname in files_funcs_nums:
fname_new = join('populated',f'pop_{fname}')
if '_hdf5' in fname:
templ_path = templ_path_hdf5
if '_front' in fname:
templ_path = templ_path_front
if '_text' in fname:
templ_path = templ_path_text
#for dim in dim_variables.keys():
for dim in numbers.keys():
grname = dim.split('_')[0]
with open(join(templ_path,fname), 'r') as f_in :
with open(join(templ_path,fname_new), 'a') as f_out :
for line in f_in :
if '$' in line:
templine1 = line.replace('$GROUP_NUM$', dim.upper())
templine2 = templine1.replace('$group_num$', dim)
templine1 = templine2.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
f_out.write(templine2)
else:
f_out.write(line)
# build files with $group$ and $group$-based
for fname in ['def_hdf5.c', 'basic_hdf5.c', 'basic_text_group.c',
'struct_hdf5.h', 'struct_text_group.h'] :
fname_new = join('populated',f'pop_{fname}')
if '_hdf5' in fname:
templ_path = templ_path_hdf5
if '_front' in fname:
templ_path = templ_path_front
if '_text' in fname:
templ_path = templ_path_text
with open(join(templ_path,fname), 'r') as f_in :
with open(join(templ_path,fname_new), 'a') as f_out :
@ -195,7 +329,8 @@ for fname in ['def_hdf5.c', 'basic_hdf5.c', 'struct_hdf5.h'] :
templine2 = templine1.replace('$group_dset$', dset)
f_out.write(templine2)
elif '$group_num$' in line or '$GROUP_NUM$' in line :
for num in dim_variables.keys():
#for num in dim_variables.keys():
for num in numbers.keys():
templine1 = line.replace('$GROUP_NUM$', num.upper())
templine2 = templine1.replace('$group_num$', num)
f_out.write(templine2)

View File

@ -0,0 +1,31 @@
#!/bin/sh
cat prefix_text.c > trexio_text.c
cat prefix_text.h > trexio_text.h
cat basic_text.c >> trexio_text.c
cat populated/pop_basic_text_group.c >> trexio_text.c
cat populated/pop_struct_text_group_dset.h >> trexio_text.h
cat populated/pop_struct_text_group.h >> trexio_text.h
cat basic_text.h >> trexio_text.h
cat populated/pop_free_group_text.c >> trexio_text.c
cat populated/pop_read_group_text.c >> trexio_text.c
cat populated/pop_flush_group_text.c >> trexio_text.c
cat populated/pop_free_group_text.h >> trexio_text.h
cat populated/pop_read_group_text.h >> trexio_text.h
cat populated/pop_flush_group_text.h >> trexio_text.h
cat populated/pop_read_dset_text.c >> trexio_text.c
cat populated/pop_read_num_text.c >> trexio_text.c
cat populated/pop_write_dset_text.c >> trexio_text.c
cat populated/pop_write_num_text.c >> trexio_text.c
cat populated/pop_rw_num_text.h >> trexio_text.h
cat populated/pop_rw_dset_text.h >> trexio_text.h
cat rdm_text.c >> trexio_text.c
cat rdm_text.h >> trexio_text.h
cat suffix_text.h >> trexio_text.h

View File

@ -1,6 +1,7 @@
#+Title: TEXT back end of the TREX Input/Ouput library (TREXIO)
* File prefixes :noxport:
#+Title: Templator for TEXT backend
* Constant file prefixes (not used by generator) :noxport:
#+begin_src emacs-lisp
(setq-local org-babel-default-header-args:c '((:comments . "both")))
@ -26,7 +27,7 @@
#+end_src
#+begin_src c :tangle trexio_text.h :noweb yes
#+begin_src c :tangle prefix_text.h :noweb yes
<<header>>
#ifndef _TREXIO_TEXT_H
#define _TREXIO_TEXT_H
@ -46,17 +47,20 @@
#+end_src
#+begin_src c :tangle trexio_text.c :noweb yes
#+begin_src c :tangle prefix_text.c :noweb yes
/* 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"
#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__);
#+end_src
* TEXT Back end
The "file" produced by the text back end is a directory with one
@ -71,26 +75,24 @@
The file is written when closed, or when the flush function is called.
*** Structs for blocks
#+begin_src c :tangle trexio_text.h
typedef struct nucleus_s {
FILE* file;
double* coord;
double* charge;
uint64_t num;
uint64_t dims_charge[16];
uint64_t dims_coord[16];
uint32_t rank_charge;
uint32_t rank_coord;
int to_flush;
} nucleus_t;
*** Template for group-related structures in text back end
typedef struct electron_s {
#+begin_src c :tangle struct_text_group_dset.h
typedef struct $group$_s {
FILE* file;
uint64_t alpha_num;
uint64_t beta_num;
int to_flush;
} electron_t;
$group_dset_dtype$* $group_dset$;
uint64_t $group_num$;
uint64_t dims_$group_dset$[16];
uint32_t rank_$group_dset$;
int to_flush;
} $group$_t;
#+end_src
*** Template for general structure in text back end
#+begin_src c :tangle struct_text_group.h
typedef struct rdm_s {
FILE* file;
@ -99,28 +101,23 @@ typedef struct rdm_s {
char* two_e_file_name;
int to_flush;
} rdm_t;
#+end_src
*** Structs for the text back end
#+begin_src c :tangle trexio_text.h
typedef struct trexio_text_s {
trexio_t parent ;
int lock_file;
nucleus_t* nucleus;
electron_t* electron;
$group$_t* $group$;
rdm_t* rdm;
} trexio_text_t;
#+end_src
*** Init/deinit functions
*** Init/deinit functions (constant part)
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_init(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle basic_text.c
trexio_exit_code trexio_text_init(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -165,11 +162,11 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
}
#+end_src
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_lock(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle basic_text.c
trexio_exit_code trexio_text_lock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -191,30 +188,15 @@ trexio_exit_code trexio_text_lock(trexio_t* file) {
#+end_src
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_finalize(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_exit_code rc;
rc = trexio_text_free_nucleus( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle basic_text.h
trexio_exit_code trexio_text_unlock(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle basic_text.c
trexio_exit_code trexio_text_unlock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -235,45 +217,62 @@ trexio_exit_code trexio_text_unlock(trexio_t* file) {
}
#+end_src
*** Nucleus struct
*** Init/deinit functions (templated part)
#+begin_src c :tangle basic_text_group.c
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
**** Read the struct
trexio_exit_code rc;
/*rc = trexio_text_free_$group$( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);*/
#+begin_src c :tangle trexio_text.h
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
assert (trexio_text_free_$group$( (trexio_text_t*) file) == TREXIO_SUCCESS);
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.c
#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__);
*** Template for text read struct
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
#+begin_src c :tangle read_group_text.h
$group$_t* trexio_text_read_$group$(trexio_text_t* file);
#+end_src
#+begin_src c :tangle read_group_text.c
$group$_t* trexio_text_read_$group$(trexio_text_t* file) {
if (file == NULL) return NULL;
/* If the data structure exists, return it */
if (file->nucleus != NULL) {
return file->nucleus;
if (file->$group$ != NULL) {
return file->$group$;
}
/* Allocate the data structure */
nucleus_t* nucleus = MALLOC(nucleus_t);
if (nucleus == NULL) return NULL;
$group$_t* $group$ = MALLOC($group$_t);
if ($group$ == NULL) return NULL;
memset(nucleus,0,sizeof(nucleus_t));
memset($group$,0,sizeof($group$_t));
/* Build the file name */
const char* nucleus_file_name = "/nucleus.txt";
const char* $group$_file_name = "/$group$.txt";
char * file_name = (char*)
calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1,
calloc( strlen(file->parent.file_name) + strlen($group$_file_name) + 1,
sizeof(char));
if (file_name == NULL) {
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
strcpy (file_name, file->parent.file_name);
strcat (file_name, nucleus_file_name);
strcat (file_name, $group$_file_name);
/* If the file exists, read it */
FILE* f = fopen(file_name,"r");
@ -287,7 +286,7 @@ DEBUG
char* buffer = CALLOC(sz,char);
if (buffer == NULL) {
FREE(file_name);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
@ -295,485 +294,359 @@ DEBUG
/* Read the dimensioning variables */
int rc;
// START REPEAT GROUP_DSET
rc = fscanf(f, "%s", buffer);
if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) {
if ((rc != 1) || (strcmp(buffer, "rank_$group_dset$") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_charge));
rc = fscanf(f, "%u", &($group$->rank_$group_dset$));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
uint64_t size_charge = 1;
for (unsigned int i=0; i<nucleus->rank_charge; i++){
uint64_t size_$group_dset$ = 1;
for (unsigned int i=0; i<$group$->rank_$group_dset$; i++){
unsigned int j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) {
if ((rc != 2) || (strcmp(buffer, "dims_$group_dset$") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i]));
rc = fscanf(f, "%lu\n", &($group$->dims_$group_dset$[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
size_charge *= nucleus->dims_charge[i];
size_$group_dset$ *= $group$->dims_$group_dset$[i];
}
// END REPEAT GROUP_DSET
// START REPEAT GROUP_NUM
/* Read data */
rc = fscanf(f, "%s", buffer);
if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) {
assert(!((rc != 1) || (strcmp(buffer, "$group_num$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_num$") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
uint64_t size_coord = 1;
for (unsigned int i=0; i<nucleus->rank_coord; i++){
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));
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));
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));
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) || (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", &($group$->$group_num$));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
// END REPEAT GROUP_NUM
rc = fscanf(f, "%s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "charge") != 0)));
if ((rc != 1) || (strcmp(buffer, "charge") != 0)) {
// START REPEAT GROUP_DSET
/* Allocate arrays */
$group$->$group_dset$ = (double*) calloc(size_$group_dset$, sizeof(double));
assert (!($group$->$group_dset$ == NULL));
if ($group$->$group_dset$ == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
FREE($group$);
DEBUG
return NULL;
}
rc = fscanf(f, "%s", buffer);
assert(!((rc != 1) || (strcmp(buffer, "$group_dset$") != 0)));
if ((rc != 1) || (strcmp(buffer, "$group_dset$") != 0)) {
FREE(buffer);
FREE(file_name);
// TODO: free all dsets
FREE($group$->$group_dset$);
FREE($group$);
DEBUG
return NULL;
}
for (uint64_t i=0 ; i<size_charge ; i++) {
rc = fscanf(f, "%lf", &(nucleus->charge[i]));
for (uint64_t i=0 ; i<size_$group_dset$ ; i++) {
rc = fscanf(f, "%lf", &($group$->$group_dset$[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
// TODO: free all dsets
FREE($group$->$group_dset$);
FREE($group$);
DEBUG
return NULL;
}
}
// END REPEAT GROUP_DSET
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));
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");
$group$->file = fopen(file_name,"a");
} else {
nucleus->file = fopen(file_name,"r");
$group$->file = fopen(file_name,"r");
}
FREE(file_name);
assert (!(nucleus->file == NULL));
if (nucleus->file == NULL) {
FREE(nucleus->charge);
FREE(nucleus);
assert (!($group$->file == NULL));
if ($group$->file == NULL) {
// TODO: free all dsets
FREE($group$->$group_dset$);
FREE($group$);
DEBUG
return NULL;
}
fseek(nucleus->file, 0L, SEEK_SET);
file->nucleus = nucleus;
return nucleus;
fseek($group$->file, 0L, SEEK_SET);
file->$group$ = $group$;
return $group$;
}
#+end_src
*** Template for text flush struct
**** Flush the struct
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file);
#+begin_src c :tangle flush_group_text.h
trexio_exit_code trexio_text_flush_$group$(const trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
#+begin_src c :tangle flush_group_text.c
trexio_exit_code trexio_text_flush_$group$(const trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (file->parent.mode == 'r') return TREXIO_READONLY;
nucleus_t* nucleus = file->nucleus;
$group$_t* $group$ = file->$group$;
if (nucleus == NULL) return TREXIO_SUCCESS;
if ($group$ == NULL) return TREXIO_SUCCESS;
if (nucleus->to_flush == 0) return TREXIO_SUCCESS;
if ($group$->to_flush == 0) return TREXIO_SUCCESS;
FILE* f = nucleus->file;
FILE* f = $group$->file;
assert (f != NULL);
rewind(f);
/* Write the dimensioning variables */
fprintf(f, "rank_charge %d\n", nucleus->rank_charge);
// START REPEAT GROUP_DSET
fprintf(f, "rank_$group_dset$ %d\n", $group$->rank_$group_dset$);
uint64_t size_charge = 1;
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];
uint64_t size_$group_dset$ = 1;
for (unsigned int i=0; i<$group$->rank_$group_dset$; i++){
fprintf(f, "dims_$group_dset$ %d %ld\n", i, $group$->dims_$group_dset$[i]);
size_$group_dset$ *= $group$->dims_$group_dset$[i];
}
fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
// END REPEAT GROUP_DSET
uint64_t size_coord = 1;
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];
}
// START REPEAT GROUP_NUM
fprintf(f, "$group_num$ %ld\n", $group$->$group_num$);
// END REPEAT GROUP_NUM
// START REPEAT GROUP_DSET
/* Write arrays */
fprintf(f, "num %ld\n", nucleus->num);
fprintf(f, "charge\n");
for (uint64_t i=0 ; i<size_charge ; i++) {
fprintf(f, "%lf\n", nucleus->charge[i]);
fprintf(f, "$group_dset$\n");
for (uint64_t i=0 ; i<size_$group_dset$ ; i++) {
fprintf(f, "%lf\n", $group$->$group_dset$[i]);
}
fprintf(f, "coord\n");
for (uint64_t i=0 ; i<size_coord ; i++) {
fprintf(f, "%lf\n", nucleus->coord[i]);
}
// END REPEAT GROUP_DSET
fflush(f);
file->nucleus->to_flush = 0;
file->$group$->to_flush = 0;
return TREXIO_SUCCESS;
}
#+end_src
**** Free memory
*** Template for text free memory
Memory is allocated when reading. The following function frees memory.
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file);
#+begin_src c :tangle free_group_text.h
trexio_exit_code trexio_text_free_$group$(trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
#+begin_src c :tangle free_group_text.c
trexio_exit_code trexio_text_free_$group$(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_$group$(file);
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
}
nucleus_t* nucleus = file->nucleus;
if (nucleus == NULL) return TREXIO_SUCCESS;
$group$_t* $group$ = file->$group$;
if ($group$ == NULL) return TREXIO_SUCCESS;
if (nucleus->file != NULL) {
fclose(nucleus->file);
nucleus->file = NULL;
}
if (nucleus->coord != NULL) {
FREE (nucleus->coord);
if ($group$->file != NULL) {
fclose($group$->file);
$group$->file = NULL;
}
if (nucleus->charge != NULL) {
FREE (nucleus->charge);
// START REPEAT GROUP_DSET
if ($group$->$group_dset$ != NULL) {
FREE ($group$->$group_dset$);
}
FREE (nucleus);
// END REPEAT GROUP_DSET
FREE ($group$);
return TREXIO_SUCCESS;
}
#+end_src
**** Read/Write the num attribute
*** Template for read/write the $group_num$ attribute
#+begin_src c :tangle trexio_text.h
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);
#+begin_src c :tangle rw_num_text.h
trexio_exit_code trexio_text_read_$group_num$(const trexio_t* file, uint64_t* num);
trexio_exit_code trexio_text_write_$group_num$(const trexio_t* file, const uint64_t num);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num) {
#+begin_src c :tangle read_num_text.c
trexio_exit_code trexio_text_read_$group_num$(const trexio_t* file, uint64_t* num) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
$group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
/**/ *num = nucleus->num;
/**/ *num = $group$->$group_num$;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle write_num_text.c
trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint64_t num) {
trexio_exit_code trexio_text_write_$group_num$(const trexio_t* file, const uint64_t num) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (file->mode == 'r') return TREXIO_READONLY;
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
$group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
nucleus->num = num;
nucleus->to_flush = 1;
$group$->$group_num$ = num;
$group$->to_flush = 1;
return TREXIO_SUCCESS;
}
#+end_src
**** Read/Write the coord attribute
*** Template for read/write the $group_dset$ dataset
The ~coord~ array is assumed allocated with the appropriate size.
The ~dset~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle trexio_text.h
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);
#+begin_src c :tangle rw_dset_text.h
trexio_exit_code trexio_text_read_$group_dset$(const trexio_t* file, $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_$group_dset$(const trexio_t* file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) {
#+begin_src c :tangle read_dset_text.c
trexio_exit_code trexio_text_read_$group_dset$(const trexio_t* file, $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coord == NULL) return TREXIO_INVALID_ARG_2;
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
$group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3;
if (rank != $group$->rank_$group_dset$) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1;
for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4;
if (dims[i] != $group$->dims_$group_dset$[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i];
}
for (uint64_t i=0 ; i<dim_size ; i++) {
coord[i] = nucleus->coord[i];
$group_dset$[i] = $group$->$group_dset$[i];
}
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle write_dset_text.c
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) {
trexio_exit_code trexio_text_write_$group_dset$(const trexio_t* file, const $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coord == NULL) return TREXIO_INVALID_ARG_2;
if (file->mode == 'r') return TREXIO_READONLY;
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
$group$_t* $group$ = trexio_text_read_$group$((trexio_text_t*) file);
if ($group$ == NULL) return TREXIO_FAILURE;
if (nucleus->coord != NULL) {
FREE(nucleus->coord);
if ($group$->$group_dset$ != NULL) {
FREE($group$->$group_dset$);
}
nucleus->rank_coord = rank;
$group$->rank_$group_dset$ = rank;
uint64_t dim_size = 1;
for (unsigned int i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i];
for (unsigned int i=0; i<$group$->rank_$group_dset$; i++){
$group$->dims_$group_dset$[i] = dims[i];
dim_size *= dims[i];
}
nucleus->coord = (double*) calloc(dim_size, sizeof(double));
$group$->$group_dset$ = (double*) calloc(dim_size, sizeof(double));
for (uint64_t i=0 ; i<dim_size ; i++) {
nucleus->coord[i] = coord[i];
$group$->$group_dset$[i] = $group_dset$[i];
}
nucleus->to_flush = 1;
return TREXIO_SUCCESS;
}
#+end_src
**** Read/Write the charge attribute
The ~charge~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle trexio_text.h
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);
#+end_src
#+begin_src c :tangle trexio_text.c
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;
if (charge == NULL) return TREXIO_INVALID_ARG_2;
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = 1;
for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_charge[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i];
}
for (uint64_t i=0 ; i<dim_size ; i++) {
charge[i] = nucleus->charge[i];
}
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (charge == NULL) return TREXIO_INVALID_ARG_2;
if (file->mode == 'r') return TREXIO_READONLY;
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->charge != NULL) {
FREE(nucleus->charge);
}
nucleus->rank_charge = rank;
uint64_t dim_size = 1;
for (unsigned int i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i];
dim_size *= dims[i];
}
nucleus->charge = (double*) calloc(dim_size, sizeof(double));
for (uint64_t i=0 ; i<dim_size ; i++) {
nucleus->charge[i] = charge[i];
}
nucleus->to_flush = 1;
$group$->to_flush = 1;
return TREXIO_SUCCESS;
}
#+end_src
*** RDM struct
**** Read the complete struct
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle rdm_text.h
rdm_t* trexio_text_read_rdm(trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle rdm_text.c
rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
if (file == NULL) return NULL;
@ -857,11 +730,11 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
**** Flush the complete struct
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle rdm_text.h
trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle rdm_text.c
trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -898,11 +771,11 @@ trexio_exit_code trexio_text_flush_rdm(const trexio_text_t* file) {
Memory is allocated when reading. The followig function frees memory.
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle rdm_text.h
trexio_exit_code trexio_text_free_rdm(trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle rdm_text.c
trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
@ -938,12 +811,12 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
The ~one_e~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle rdm_text.h
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);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle rdm_text.c
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;
@ -988,12 +861,12 @@ trexio_exit_code trexio_text_write_rdm_one_e(const trexio_t* file, const double*
In the text back end, the easiest way to do it is to create a
file for each sparse float structure.
#+begin_src c :tangle trexio_text.h
#+begin_src c :tangle rdm_text.h
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);
#+end_src
#+begin_src c :tangle trexio_text.c
#+begin_src c :tangle rdm_text.c
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;
@ -1060,9 +933,11 @@ trexio_exit_code trexio_text_buffered_write_rdm_two_e(const trexio_t* file, cons
}
#+end_src
* File suffixes :noxport:
:noexport:
#+begin_src c :tangle trexio_text.h
* Constant file suffixes :noxport:
#+begin_src c :tangle suffix_text.h
#endif
#+end_src