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

346 lines
13 KiB
Python
Raw Normal View History

2021-03-05 16:50:44 +01:00
import json
2021-03-16 18:34:07 +01:00
from os import listdir, scandir, remove
2021-03-10 10:27:44 +01:00
from os.path import isfile, join, dirname, abspath
2021-03-09 11:58:47 +01:00
2021-03-10 10:27:44 +01:00
fileDir = dirname(abspath(__file__))
parentDir = dirname(fileDir)
with open(join(parentDir,'trex.json'), 'r') as f:
2021-03-05 16:50:44 +01:00
config0 = json.load(f)
print('Metadata I/O currently not supported')
# TODO, for now remove metadata-related stuff
del config0['metadata']
config = {}
for k,v in config0.items():
2021-03-17 14:50:02 +01:00
#if k == 'nucleus' or k == 'ecp':
if k == 'nucleus':
2021-03-05 16:50:44 +01:00
config[k] = v
2021-03-09 11:58:47 +01:00
#print(config)
2021-03-05 16:50:44 +01:00
groups = [group for group in config.keys()]
dim_variables = {}
dim_list = []
2021-03-09 11:58:47 +01:00
dim_dict = {}
2021-03-05 16:50:44 +01:00
for k1,v1 in config.items():
2021-03-09 11:58:47 +01:00
grname = k1
2021-03-05 16:50:44 +01:00
for v2 in v1.values():
for dim in v2[1]:
if not dim.isdigit():
tmp = dim.replace('.','_')
dim_variables[tmp] = 0
if dim not in dim_list:
2021-03-09 11:58:47 +01:00
dim_list.append(tmp)
dim_dict[grname] = dim_list
dim_list = []
2021-03-10 10:27:44 +01:00
#print(dim_variables)
2021-03-09 11:58:47 +01:00
#print(dim_dict)
2021-03-05 16:50:44 +01:00
datasets = {}
numbers = {}
for k1,v1 in config.items():
for k2,v2 in v1.items():
if len(v2[1]) > 0:
datasets[f'{k1}_{k2}'] = v2
else:
var_name = f'{k1}_{k2}'
if var_name not in dim_variables.keys():
numbers[var_name] = v2[0]
print('Strings I/O currently not supported')
# TODO, for now remove char-related stuff
datasets_nostr = {}
for k,v in datasets.items():
tmp_dict = {}
2021-03-05 16:50:44 +01:00
if 'char' not in v[0]:
if v[0] == 'float':
datatype = 'double'
elif v[0] == 'int':
datatype = 'int64_t'
tmp_dict['dtype'] = datatype
tmp_dict['dims'] = [dim.replace('.','_') for dim in v[1]]
2021-03-10 14:50:51 +01:00
tmp_dict['rank'] = len(v[1])
dim_str = tmp_dict['dims'][0]
if tmp_dict['rank'] > 1:
for i in range(1, tmp_dict['rank']):
dim_toadd = tmp_dict['dims'][i]
dim_str += f', {dim_toadd}'
tmp_dict['dim_list'] = dim_str
datasets_nostr[k] = tmp_dict
2021-03-05 16:50:44 +01:00
2021-03-17 14:50:02 +01:00
#print(datasets_nostr['nucleus_coord'])
2021-03-05 16:50:44 +01:00
2021-03-16 18:34:07 +01:00
#put also dimensioning variables in numbers
numbers.update(dim_variables)
print(numbers)
templ_path_text = join(fileDir,'templates_text')
2021-03-10 14:50:51 +01:00
templ_path_hdf5 = join(fileDir,'templates_hdf5')
templ_path_front = join(fileDir,'templates_front')
2021-03-16 18:34:07 +01:00
#clean the populated/ directory
2021-03-16 19:05:05 +01:00
#for popdir in [templ_path_front, templ_path_front, templ_path_text]:
# cleandir = join(popdir, 'populated')
# for f in scandir(cleandir):
# remove(f.path)
2021-03-16 18:34:07 +01:00
2021-03-10 14:50:51 +01:00
files_exclude = ['prefix_hdf5.c', 'prefix_hdf5.h', 'suffix_hdf5.h',
2021-03-16 18:34:07 +01:00
'prefix_text.c', 'prefix_text.h', 'suffix_text.h',
2021-03-10 14:50:51 +01:00
'prefix_front.c', 'prefix_front.h', 'suffix_front.h',
'prefix_s_front.h', 'suffix_s_front.h',
2021-03-16 18:34:07 +01:00
'templator_front.org', 'templator_hdf5.org', 'templator_text.org']
2021-03-09 11:58:47 +01:00
2021-03-16 18:34:07 +01:00
files_text = [f for f in listdir(templ_path_text) if isfile(join(templ_path_text, f)) and f not in files_exclude]
2021-03-10 14:50:51 +01:00
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]
2021-03-05 16:50:44 +01:00
2021-03-16 18:34:07 +01:00
files = files_text + files_hdf5 + files_front
2021-03-09 11:58:47 +01:00
2021-03-16 18:34:07 +01:00
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]
2021-03-16 18:34:07 +01:00
files_funcs_groups = [f for f in files_funcs if 'group' in f]
files_funcs_groups.append('struct_text_group_dset.h')
2021-03-17 14:50:02 +01:00
#print(files_funcs_groups)
2021-03-09 11:58:47 +01:00
files_auxil = [f for f in files if not ('read_' in f or 'write_' in f or 'rw_' in f)]
2021-03-16 18:34:07 +01:00
# build files with functions for text groups
for fname in files_funcs_groups:
2021-03-10 11:54:23 +01:00
fname_new = join('populated',f'pop_{fname}')
2021-03-16 18:34:07 +01:00
if '_text' in fname:
templ_path = templ_path_text
2021-03-16 18:34:07 +01:00
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 = ''
2021-03-10 14:50:51 +01:00
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 :
2021-03-16 18:34:07 +01:00
if 'END REPEAT' in line:
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():
dset_grname = dset.split('_')[0]
if dset_grname != grname:
continue
2021-03-16 18:34:07 +01:00
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())
2021-03-16 18:34:07 +01:00
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)
2021-03-16 18:34:07 +01:00
# build files with functions
for fname in files_funcs_dsets:
2021-03-10 11:54:23 +01:00
fname_new = join('populated',f'pop_{fname}')
2021-03-10 14:50:51 +01:00
if '_hdf5' in fname:
templ_path = templ_path_hdf5
if '_front' in fname:
templ_path = templ_path_front
2021-03-16 18:34:07 +01:00
if '_text' in fname:
templ_path = templ_path_text
for dset,params in datasets_nostr.items():
grname = dset.split('_')[0]
2021-03-10 14:50:51 +01:00
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:
if '$group_dset_dim$' in line:
rc_line = ' if (rc != TREXIO_SUCCESS) return rc;\n'
for dim in params['dims']:
if not dim.isdigit():
templine1 = line.replace('$group_dset_dim$', dim)
templine2 = templine1
2021-03-17 14:33:32 +01:00
if '_read' in templine2 and 'hdf5' in fname:
templine1 = rc_line
templine2 += templine1
f_out.write(templine2)
continue
templine1 = line.replace('$GROUP$_$GROUP_DSET$', dset.upper())
templine2 = templine1.replace('$group$_$group_dset$', dset)
templine1 = templine2.replace('$group_dset$', dset)
templine2 = templine1
2021-03-09 11:58:47 +01:00
templine1 = templine2.replace('$group_dset_dtype$', params['dtype'])
templine2 = templine1
if params['dtype'] == 'double':
h5_dtype = 'double'
elif params['dtype'] == 'int64_t':
h5_dtype = 'long'
templine1 = templine2.replace('$group_dset_h5_dtype$', h5_dtype)
templine2 = templine1.replace('$group_dset_h5_dtype$'.upper(), h5_dtype.upper())
2021-03-10 14:50:51 +01:00
templine1 = templine2.replace('$group_dset_rank$', str(params['rank']))
templine2 = templine1
templine1 = templine2.replace('$group_dset_dim_list$', params['dim_list'])
templine2 = templine1
templine1 = templine2.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
f_out.write(templine2)
else:
f_out.write(line)
2021-03-05 16:50:44 +01:00
2021-03-16 18:34:07 +01:00
# 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
2021-03-17 14:33:32 +01:00
for dim in dim_variables.keys():
#for dim in numbers.keys():
2021-03-16 18:34:07 +01:00
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)
2021-03-09 11:58:47 +01:00
# build files with $group$ and $group$-based
2021-03-16 18:34:07 +01:00
for fname in ['def_hdf5.c', 'basic_hdf5.c', 'basic_text_group.c',
'struct_hdf5.h', 'struct_text_group.h'] :
2021-03-10 11:54:23 +01:00
fname_new = join('populated',f'pop_{fname}')
2021-03-10 14:50:51 +01:00
if '_hdf5' in fname:
templ_path = templ_path_hdf5
if '_front' in fname:
templ_path = templ_path_front
2021-03-16 18:34:07 +01:00
if '_text' in fname:
templ_path = templ_path_text
2021-03-10 14:50:51 +01:00
with open(join(templ_path,fname), 'r') as f_in :
with open(join(templ_path,fname_new), 'a') as f_out :
2021-03-09 11:58:47 +01:00
for line in f_in :
if '$group_dset$' in line or '$GROUP_DSET$' in line :
for dset in datasets_nostr.keys():
templine1 = line.replace('$GROUP$_$GROUP_DSET$', dset.upper())
templine2 = templine1.replace('$group_dset$', dset)
f_out.write(templine2)
elif '$group_num$' in line or '$GROUP_NUM$' in line :
2021-03-16 19:05:05 +01:00
for num in dim_variables.keys():
#for num in numbers.keys():
2021-03-09 11:58:47 +01:00
templine1 = line.replace('$GROUP_NUM$', num.upper())
templine2 = templine1.replace('$group_num$', num)
f_out.write(templine2)
elif '$group$' in line or '$GROUP$' in line :
for grname in config.keys():
templine1 = line.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
f_out.write(templine2)
2021-03-05 16:50:44 +01:00
else:
f_out.write(line)