1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2025-01-09 12:44:11 +01:00

wip: generate read/write_dset functions

This commit is contained in:
q-posev 2021-03-09 15:48:32 +01:00
parent ebc7aa5d40
commit 50841e1937
2 changed files with 58 additions and 24 deletions

View File

@ -53,8 +53,15 @@ print('Strings I/O currently not supported')
# TODO, for now remove char-related stuff
datasets_nostr = {}
for k,v in datasets.items():
tmp_dict = {}
if 'char' not in v[0]:
datasets_nostr[k] = v
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]]
datasets_nostr[k] = tmp_dict
#print(datasets_nostr)
#print(numbers)
@ -73,30 +80,57 @@ files = [f for f in listdir(temp_path) if isfile(join(temp_path, f)) and f not i
#print(files)
files_funcs = [f for f in files if 'read_' in f or 'write_' 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_auxil = [f for f in files if not ('read_' in f or 'write_' in f or 'rw_' in f)]
print(files_auxil)
# build files with $group$ only
for fname in ['basic_hdf5.c', 'struct_hdf5.h']:
print(files_funcs_dsets)
# build files with functions
for fname in ['write_dset_hdf5.c']:
fname_new = 'populated/pop_' + fname
with open(f'{temp_path}/{fname}', 'r') as f_in:
with open(f'{temp_path}/{fname_new}', 'w') as f_out:
for line in f_in:
if '$group$' in line or '$GROUP$' in line:
for grname in config.keys():
templine1 = line.replace('$group$', grname)
templine2 = templine1.replace('$GROUP$', grname.upper())
#templine1 = templine2.replace('$GROUP_NUM$', dim.upper())
#templine2 = templine1.replace('$group_num$', dim)
#templine1 = templine2.replace('$GROUP_DSET$', '')
#templine2 = templine1.replace('$group_dset$', '')
for dset,params in datasets_nostr.items():
f_out.write(templine2)
else:
f_out.write(line)
grname = dset.split('_')[0]
with open(f'{temp_path}/{fname}', 'r') as f_in :
with open(f'{temp_path}/{fname_new}', 'a') as f_out :
for line in f_in :
if '$' in line:
templine1 = line.replace('$GROUP$_$GROUP_DSET$', dset.upper())
templine2 = templine1.replace('$group$_$group_dset$', dset)
templine1 = templine2.replace('$group_dset$', dset)
templine2 = templine1
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())
for dim in params['dims']:
if dim.isdigit():
continue
else:
templine1 = templine2.replace('$group_dset_dim$', dim)
templine2 = templine1
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'] :
for fname in ['def_hdf5.c', 'basic_hdf5.c', 'struct_hdf5.h'] :
fname_new = 'populated/pop_' + fname
with open(f'{temp_path}/{fname}', 'r') as f_in :
with open(f'{temp_path}/{fname_new}', 'w') as f_out :

View File

@ -242,8 +242,8 @@ trexio_exit_code trexio_hdf5_write_$group_num$(const trexio_t* file, const uint6
#+begin_src c :tangle rw_dset_hdf5.h
trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(const trexio_t* file, double* $group_dset$, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_hdf5_write_$group$_$group_dset$(const trexio_t* file, const double* $group_dset$, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(const trexio_t* file, $group_dset_dtype$* $group_dset$, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_hdf5_write_$group$_$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 read_dset_hdf5.c
@ -290,7 +290,7 @@ trexio_exit_code trexio_hdf5_read_$group$_$group_dset$(const trexio_t* file, $gr
free(ddims);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(f->$group$_group,
status = H5LTread_dataset_$group_dset_h5_dtype$(f->$group$_group,
$GROUP$_$GROUP_DSET$_NAME,
$group_dset$);
if (status < 0) return TREXIO_FAILURE;
@ -316,7 +316,7 @@ trexio_exit_code trexio_hdf5_write_$group$_$group_dset$(const trexio_t* file, co
herr_t status;
if ( H5LTfind_dataset(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME) != 1) {
status = H5LTmake_dataset_$group_dset_dtype$ (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME,
status = H5LTmake_dataset_$group_dset_h5_dtype$ (f->$group$_group, $GROUP$_$GROUP_DSET$_NAME,
(int) rank, (hsize_t*) dims, $group_dset$);
if (status < 0) return TREXIO_FAILURE;
@ -325,7 +325,7 @@ trexio_exit_code trexio_hdf5_write_$group$_$group_dset$(const trexio_t* file, co
hid_t dset_id = H5Dopen(f->$group$_group, $GROUP$_$GROUP_DSET$_NAME, H5P_DEFAULT);
if (dset_id <= 0) return TREXIO_INVALID_ID;
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, $group_dset$);
status = H5Dwrite(dset_id, H5T_NATIVE_$GROUP_DSET_H5_DTYPE$, H5S_ALL, H5S_ALL, H5P_DEFAULT, $group_dset$);
H5Dclose(dset_id);
if (status < 0) return TREXIO_FAILURE;