Commit fd526328 authored by John Zhang's avatar John Zhang

refactor, use loop over array of pointers to implement running multiple times within a timed run.

parent e905a7af
......@@ -46,97 +46,109 @@ def run_pypy(config):
out, _ = run([PYPY, py_file.strpath] + map(str, config['setup_args']))
return float(out)
rpy_wrapper = \
def get_wrapper_str(config, callee_str, target):
name = config['rpy_fnc'].__name__
cmmargs = ', '.join(['v%d' % i for i in range(len(config['llcmmarg_ts']))])
m = config['multiplicity']
if config['llmularg_t']:
measure_arg_ts = [rffi.CArrayPtr(config['llmularg_t'])] + config['llcmmarg_ts']
wrapper = \
"""
def rpy_measure_%(name)s_%(target)s(mulargs, %(cmmargs)s):
from time import time
t0 = time()
for i in range(%(m)s):
%(callee_str)s(mulargs[i], %(cmmargs)s)
t1 = time()
return (t1 - t0) / float(%(m)s)
"""
else:
measure_arg_ts = config['llcmmarg_ts']
wrapper = \
"""
def rpy_measure_%(name)s_%(target)s(%(args)s):
def rpy_measure_%(name)s_%(target)s(%(cmmargs)s):
from time import time
t0 = time()
%(rpy_fnc)s(%(args)s)
for i in range(%(m)s):
%(callee_str)s(%(cmmargs)s)
t1 = time()
return t1 - t0
return (t1 - t0) / float(%(m)s)
"""
return wrapper % locals(), 'rpy_measure_%(name)s_%(target)s' % locals(), measure_arg_ts
def compile_rpython_c(config):
target = 'rpy_c'
rpyfnc = config['rpy_fnc']
wrapper_config = {
'name': rpyfnc.__name__ + target,
'target': target,
'rpy_fnc': rpyfnc.__name__,
'args': ', '.join(['v%d' % i for i in range(len(config['llarg_ts']))])
}
tl_config = {'gc': 'none'}
wrapper = rpy_wrapper % wrapper_config
locals()[rpyfnc.__name__] = rpyfnc
exec wrapper in locals()
rpy_measure_fnc = locals()['rpy_measure_%(name)s_%(target)s' % wrapper_config]
t = Translation(rpy_measure_fnc, config['llarg_ts'], **tl_config)
wrapper, rpy_measure_fnc_name, measure_arg_ts = get_wrapper_str(config,
callee_str=config['rpy_fnc'].__name__,
target='rpy_c')
ns = {
config['rpy_fnc'].__name__: config['rpy_fnc'],
}
exec wrapper in ns
rpy_measure_fnc = ns[rpy_measure_fnc_name]
t = Translation(rpy_measure_fnc, measure_arg_ts, **tl_config)
set_opt_level(t.config, '3')
libpath = t.compile_c()
eci = rffi.ExternalCompilationInfo(libraries=[libpath.strpath])
fnp = rffi.llexternal('pypy_g_' + rpy_measure_fnc.__name__, config['llarg_ts'], rffi.DOUBLE,
fnp = rffi.llexternal('pypy_g_' + rpy_measure_fnc.__name__, measure_arg_ts, rffi.DOUBLE,
compilation_info=eci, _nowrapper=True)
return fnp
def compile_rpython_mu(config):
preload_libmu()
target = 'rpy_mu'
rpyfnc = config['rpy_fnc']
wrapper_config = {
'name': rpyfnc.__name__ + target,
'target': target,
'rpy_fnc': rpyfnc.__name__,
'args': ', '.join(['v%d' % i for i in range(len(config['llarg_ts']))])
}
tl_config = {'backend': 'mu', 'muimpl': 'fast', 'mucodegen': 'api', 'mutestjit': True}
wrapper = rpy_wrapper % wrapper_config
locals()[rpyfnc.__name__] = rpyfnc
exec wrapper in locals()
rpy_measure_fnc = locals()['rpy_measure_%(name)s_%(target)s' % wrapper_config]
t = Translation(rpy_measure_fnc, config['llarg_ts'], **tl_config)
wrapper, rpy_measure_fnc_name, measure_arg_ts = get_wrapper_str(config,
callee_str=config['rpy_fnc'].__name__,
target='rpy_mu')
ns = {
config['rpy_fnc'].__name__: config['rpy_fnc'],
}
exec wrapper in ns
rpy_measure_fnc = ns[rpy_measure_fnc_name]
t = Translation(rpy_measure_fnc, measure_arg_ts, **tl_config)
set_opt_level(t.config, '3')
db, bdlgen, fnc_name = t.compile_mu()
libpath = libpath = config['libpath_mu']
libpath = config['libpath_mu']
bdlgen.mu.compile_to_sharedlib(libpath.strpath, [])
eci = rffi.ExternalCompilationInfo(libraries=[libpath.strpath])
fnp = rffi.llexternal(fnc_name, config['llarg_ts'], rffi.DOUBLE,
fnp = rffi.llexternal(fnc_name, measure_arg_ts, rffi.DOUBLE,
compilation_info=eci, _nowrapper=True)
return fnp
def compile_c(config):
libpath = config['libpath_c']
c_fnc = rffi.llexternal(config['c_sym_name'], config['llarg_ts'], config['llres_t'],
c_fnc = rffi.llexternal(config['rpy_fnc'].__name__, # the C function should have the same name
config['llarg_ts'], config['llres_t'],
compilation_info=rffi.ExternalCompilationInfo(
includes=['quicksort.h'],
include_dirs=[perf_target_dir.strpath],
separate_module_sources=['#include "quicksort.c"']
includes=[config['c_hdr_file'].basename],
include_dirs=[config['c_hdr_file'].dirpath().strpath],
separate_module_sources=['#include "%s"' % config['c_src_file'].basename]
), _nowrapper=True)
target = 'c'
rpyfnc = config['rpy_fnc']
wrapper_config = {
'name': rpyfnc.__name__ + target,
'target': target,
'rpy_fnc': 'c_fnc',
'args': ', '.join(['v%d' % i for i in range(len(config['llarg_ts']))])
}
tl_config = {'gc': 'none'}
wrapper = rpy_wrapper % wrapper_config
exec wrapper in locals()
rpy_measure_fnc = locals()['rpy_measure_%(name)s_%(target)s' % wrapper_config]
t = Translation(rpy_measure_fnc, config['llarg_ts'], **tl_config)
wrapper, rpy_measure_fnc_name, measure_arg_ts = get_wrapper_str(config,
callee_str='c_fnp',
target='c')
ns = {
config['rpy_fnc'].__name__: config['rpy_fnc'],
'c_fnp': c_fnc,
}
exec wrapper in ns
rpy_measure_fnc = ns[rpy_measure_fnc_name]
t = Translation(rpy_measure_fnc, measure_arg_ts, **tl_config)
set_opt_level(t.config, '3')
libpath = t.compile_c()
eci = rffi.ExternalCompilationInfo(libraries=[libpath.strpath])
fnp = rffi.llexternal('pypy_g_' + rpy_measure_fnc.__name__, config['llarg_ts'], rffi.DOUBLE,
fnp = rffi.llexternal('pypy_g_' + rpy_measure_fnc.__name__, measure_arg_ts, rffi.DOUBLE,
compilation_info=eci, _nowrapper=True)
return fnp
......@@ -160,9 +172,20 @@ def get_stat(run_fnc, config, iterations=100):
def get_stat_compiled(compile_fnc, config, iterations=100):
def run_funcptr(fnp, config):
args = config['setup'](*config['setup_args'])
t = fnp(*args)
config['teardown'](*args)
setup_fnc = config['setup']
teardown_fnc = config['teardown']
if config['llmularg_t']:
m = config['multiplicity']
with lltype.scoped_alloc(rffi.CArray(config['llmularg_t']), m) as arrs:
for i in range(m):
tup = setup_fnc(*config['setup_args'])
arrs[i] = tup[0]
cmmargs = tup[1:]
t = fnp(arrs, *cmmargs)
else:
args = setup_fnc(*config['setup_args'])
t = fnp(*args)
teardown_fnc(*args)
return t
fnp = compile_fnc(config)
......@@ -182,9 +205,9 @@ def perf(config, iterations):
# 'cpython': get_stat(run_cpython, config, iterations=iterations),
# 'pypy_nojit': get_stat(run_pypy_nojit, config, iterations=iterations),
# 'pypy': get_stat(run_pypy, config, iterations=iterations),
'rpy_c': get_stat_compiled(compile_rpython_c, config, iterations=iterations),
# 'rpy_c': get_stat_compiled(compile_rpython_c, config, iterations=iterations),
'rpy_mu': get_stat_compiled(compile_rpython_mu, config, iterations=iterations),
'c': get_stat_compiled(compile_c, config, iterations=iterations),
# 'c': get_stat_compiled(compile_c, config, iterations=iterations),
}
for python, result in results.items():
......@@ -209,7 +232,7 @@ def perf_fibonacci(N, iterations):
config = {
'py_file': perf_target_dir.join('fibonacci.py'),
'c_file': perf_target_dir.join('fibonacci.c'),
'c_src_file': perf_target_dir.join('fibonacci.c'),
'rpy_fnc': rpy_entry,
'c_sym_name': 'fib',
'llarg_ts': [lltype.Signed],
......@@ -235,7 +258,7 @@ def perf_arraysum(N, iterations):
config = {
'py_file': perf_target_dir.join('arraysum.py'),
'c_file': perf_target_dir.join('arraysum.c'),
'c_src_file': perf_target_dir.join('arraysum.c'),
'rpy_fnc': arraysum,
'c_sym_name': 'arraysum',
'llarg_ts': [rffi.CArrayPtr(rffi.LONGLONG), rffi.SIZE_T],
......@@ -262,16 +285,18 @@ def perf_quicksort(N, iterations):
os.environ['LIBRARY_PATH'] = tmpdir.strpath
config = {
'py_file': perf_target_dir.join('quicksort.py'),
'c_file': perf_target_dir.join('quicksort.c'),
'c_src_file': perf_target_dir.join('quicksort.c'),
'c_hdr_file': perf_target_dir.join('quicksort.h'),
'rpy_fnc': quicksort,
'c_sym_name': 'quicksort',
'llmularg_t': rffi.CArrayPtr(rffi.LONGLONG),
'llcmmarg_ts': [lltype.Signed, lltype.Signed],
'multiplicity': 5,
'llarg_ts': [rffi.CArrayPtr(rffi.LONGLONG), lltype.Signed, lltype.Signed],
'llres_t': lltype.Void,
'setup_args': (N,),
'setup': setup,
'teardown': teardown,
'libpath_mu': tmpdir.join('libquicksort_mu' + libext),
'libpath_c': tmpdir.join('libquicksort_c' + libext)
}
results = perf(config, iterations)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment