Skip to content

Commit

Permalink
Merge pull request #62 from sfiligoi/codegen_250121
Browse files Browse the repository at this point in the history
Dynamically generate wrappers to minimize code repetition
  • Loading branch information
sfiligoi authored Jan 22, 2025
2 parents db45e1d + 87bfe80 commit d4cbb7d
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 524 deletions.
14 changes: 10 additions & 4 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,20 @@ lib$(SSU).so: tree.o biom.o biom_inmem.o biom_subsampled.o tsv.o unifrac.o cmd.o
api.o: api.cpp api.hpp unifrac.hpp skbio_alt.hpp biom.hpp biom_inmem.hpp biom_subsampled.hpp tree.hpp tsv.hpp
$(CXX) $(CPPFLAGS) api.cpp -c -o api.o -fPIC

unifrac_task_cpu.o: unifrac_task_noclass.cpp unifrac_task_noclass.hpp

unifrac_task_noclass.cpp: unifrac_task_impl.hpp
python3 generate_unifrac_task_noclass.py >unifrac_task_noclass.cpp

unifrac_task_cpu.o: unifrac_task_noclass.cpp unifrac_task_noclass.hpp unifrac_task_impl.hpp
$(CXX) $(CPPFLAGS) -DSUCMP_NM=su_cpu -c $< -o $@

unifrac_task_acc.o: unifrac_task_noclass.cpp unifrac_task_noclass.hpp
unifrac_task_acc.o: unifrac_task_noclass.cpp unifrac_task_noclass.hpp unifrac_task_impl.hpp
$(CXX) $(CPPFLAGS) $(ACCCPPFLAGS) -DSUCMP_NM=su_acc -c $< -o $@
unifrac_task_ompgpu.o: unifrac_task_noclass.cpp unifrac_task_noclass.hpp

unifrac_task_ompgpu.o: unifrac_task_noclass.cpp unifrac_task_noclass.hpp unifrac_task_impl.hpp
$(CXX) $(CPPFLAGS) $(OMPGPUCPPFLAGS) -DSUCMP_NM=su_ompgpu -c $< -o $@


unifrac_cmp_cpu.o: unifrac_cmp.cpp unifrac_cmp.hpp unifrac_internal.hpp unifrac.hpp unifrac_task.hpp unifrac_task_noclass.hpp biom_interface.hpp tree.hpp
$(CXX) $(CPPFLAGS) -DSUCMP_NM=su_cpu -c $< -o $@

Expand Down Expand Up @@ -217,5 +223,5 @@ rapi_test: main
rm -f *.o

clean:
-rm -f *.o $(SSU) $(FPD) test_su test_ska test_api lib$(SSU).so
-rm -f *.o $(SSU) $(FPD) test_su test_ska test_api lib$(SSU).so unifrac_task_noclass.cpp

93 changes: 93 additions & 0 deletions src/generate_unifrac_task_noclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# Parse unifrac_task_impl.hpp
# and generate concrete implementations
# for all inline functions ending in _T
#

# replace template name with concrete type
def patch_type(s,t):
return s.replace('TFloat',t).replace('TNum',t)

# extract argument name from the arg definition
def get_arg_name(s):
return s.split()[-1].split('*')[-1]

# print out the header
print('// Generated from unifrac_task_impl.hpp');
print('// Do not edit by hand');
print('');
print('#include "unifrac_task_noclass.hpp"');
print('#include "unifrac_task_impl.hpp"');
print('');


with open('unifrac_task_impl.hpp','r') as fd:
lines=fd.readlines()

i=0
while (i<len(lines)):
line = lines[i]
i+=1
if not line.startswith('static inline '):
continue # not the beginning of an intersting function
if line.find('_T(')<0:
continue # not the beginning of an intersting function

larr=line.split();
ftype = larr[2];
fname = larr[3].split('_T(')[0]

print('// ==================================');
if line.find('_T()')>=0:
# special case, no arguments
print('%s SUCMP_NM::%s() {'%(ftype,fname))
if ftype=='void':
print(' %s_T();'%fname);
else:
print(' return %s_T();'%fname)
print('}');
print('');
continue

# assuming ftype == void simplifies the code
if ftype!='void':
raise "Unsupported templated void found!"

ftypes = set()
fargs = []
line = lines[i]
i+=1
while True:
# get all the arguments up to the optional close
larr=line.split(') ')[0].strip().split(',')
for el in larr:
el = el.strip()
if len(el)>0:
fargs.append(el)
if el.find('TFloat')>=0:
ftypes |= {'float','double'}
elif el.find('TNum')>=0:
ftypes |= {'float','double','uint64_t','uint32_t','bool'}


if line.find(') ')>=0:
break # found end of args, exit the loop
line = lines[i]
i+=1

for ft in ftypes:
print('template<>')
print('%s SUCMP_NM::%s('%(ftype,fname))
for el in fargs[:-1]:
print('\t\t\t%s,'%patch_type(el,ft))
print('\t\t\t%s) {'%patch_type(fargs[-1],ft))

print(' %s_T('%fname)
for el in fargs[:-1]:
print('\t%s,'%get_arg_name(el))
print('\t%s);'%get_arg_name(fargs[-1]))

print('}');
print('');


Loading

0 comments on commit d4cbb7d

Please sign in to comment.