GitLab will continue to be upgraded from 11.4.5-ce.0 on November 25th 2019 at 4.00pm (AEDT) to 5.00pm (AEDT) due to Critical Security Patch Availability. During the update, GitLab and Mattermost services will not be available.

Commit 371fb8c7 authored by John Zhang's avatar John Zhang

refactor; fix spawn_proc mistake on new and new_cmp_eq tests

parent 61d3bf99
Pipeline #155 failed with stage
in 14 minutes and 49 seconds
......@@ -10,13 +10,15 @@ import ctypes
from rpython.translator.interactive import Translation
from rpython.config.translationoption import set_opt_level
from util import libmu_path, libext, preload_libmu
from util import libext, preload_libmu
CPYTHON = os.environ.get('CPYTHON', 'python')
PYPY = os.environ.get('PYPY', 'pypy')
RPYTHON = os.environ.get('RPYTHON', None)
CC = os.environ.get('CC', 'clang')
perf_target_dir = py.path.local(__file__).join('perf_target')
def run(cmd):
# print ' '.join(cmd)
p = subp.Popen(cmd, stdout=subp.PIPE, stderr=subp.PIPE)
......@@ -31,80 +33,8 @@ def perf_fibonacci():
tmpdir = py.path.local(mkdtemp())
print tmpdir
py_code_str = \
"""
from time import time
from rpython.rlib import jit
driver = jit.JitDriver(greens = [], reds = 'auto')
def fib(n):
if n in (0, 1):
return n
k = 2
fib_k_2 = 0
fib_k_1 = 1
while k < n:
driver.jit_merge_point()
fib_k = fib_k_2 + fib_k_1
fib_k_2 = fib_k_1
fib_k_1 = fib_k
k += 1
return fib_k_2 + fib_k_1
def measure(N):
t0 = time()
fib(N)
t1 = time()
return t0, t1
def rpy_entry(N):
t0, t1 = measure(N)
# from rpython.rlib import rfloat
# print rfloat.double_to_string(t1 - t0, 'e', %(fprec)d, rfloat.DTSF_ADD_DOT_0)
return t1 - t0
if __name__ == '__main__':
import sys
t0, t1 = measure(int(sys.argv[1]))
print '%%.%(fprec)df' %% (t1 - t0)
def target(*args):
from rpython.rlib.entrypoint import export_symbol
export_symbol(rpy_entry)
return rpy_entry, [int]
""" % {'fprec': 10}
c_code_str = \
"""
#include <stdint.h>
uint64_t fib(uint64_t n) {
uint64_t k, fib_k, fib_k_2, fib_k_1;
if(n <= 1) return n;
k = 2;
fib_k_2 = 0;
fib_k_1 = 1;
while(k < n) {
fib_k = fib_k_2 + fib_k_1;
fib_k_2 = fib_k_1;
fib_k_1 = fib_k;
k += 1;
}
return fib_k_2 + fib_k_1;
}
"""
py_file = tmpdir.join('fibonacci.py')
with py_file.open('w') as fp:
fp.write(py_code_str)
c_file = tmpdir.join('fibonacci.c')
with c_file.open('w') as fp:
fp.write(c_code_str)
py_file = perf_target_dir.join('fibonacci.py')
c_file = perf_target_dir.join('fibonacci.c')
def run_cpython(N):
out, _ = run([CPYTHON, py_file.strpath, str(N)])
......@@ -119,27 +49,23 @@ uint64_t fib(uint64_t n) {
return float(out)
def compile_rpython_c():
mod = {}
exec(py_code_str, mod)
rpy_fnc = mod['fib']
t = Translation(rpy_fnc, [int],
from perf_target.fibonacci import fib
t = Translation(fib, [int],
gc='none')
set_opt_level(t.config, '3')
t.ensure_opt('gc', 'none')
libpath = t.compile_c()
fnp = getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + rpy_fnc.__name__)
fnp = getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + fib.__name__)
return fnp
def compile_rpython_c_jit():
mod = {}
exec (py_code_str, mod)
rpy_fnc = mod['fib']
t = Translation(rpy_fnc, [int],
from perf_target.fibonacci import fib
t = Translation(fib, [int],
gc='none')
set_opt_level(t.config, 'jit')
t.ensure_opt('gc', 'none')
libpath = t.compile_c()
fnp = getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + rpy_fnc.__name__)
fnp = getattr(ctypes.CDLL(libpath.strpath), 'pypy_g_' + fib.__name__)
return fnp
def compile_c():
......@@ -149,13 +75,10 @@ uint64_t fib(uint64_t n) {
return lib.fib
def compile_rpython_mu():
mod = {}
exec (py_code_str, mod)
rpy_fnc = mod['fib']
preload_libmu()
t = Translation(rpy_fnc, [int],
from perf_target.fibonacci import fib
t = Translation(fib, [int],
backend='mu', muimpl='fast', mucodegen='api', mutestjit=True)
set_opt_level(t.config, '3')
db, bdlgen, fnc_name = t.compile_mu()
......@@ -184,8 +107,8 @@ uint64_t fib(uint64_t n) {
fnp = compile_fnc()
return get_average_time(lambda *a: run_funcptr(fnp, *a), args, warmup, iterations)
N = 100000
iterations = 20
N = 30
iterations = 10
t_cpython = get_average_time(run_cpython, [N], iterations=iterations)
t_pypy_nojit = get_average_time(run_pypy_nojit, [N], iterations=iterations)
......
#include <stdint.h>
uint64_t fib(uint64_t n) {
if(n <= 1)
return n;
return fib(n - 2) + fib(n - 1);
}
\ No newline at end of file
def fib(n):
if n <= 1:
return n
return fib(n - 2) + fib(n - 1)
def measure(N):
from time import time
t0 = time()
fib(N)
t1 = time()
return t0, t1
def rpy_entry(N):
t0, t1 = measure(N)
# from rpython.rlib import rfloat
# print rfloat.double_to_string(t1 - t0, 'e', %(fprec)d, rfloat.DTSF_ADD_DOT_0)
return t1 - t0
if __name__ == '__main__':
import sys
t0, t1 = measure(int(sys.argv[1]))
print '%.15df' % (t1 - t0)
def target(*args):
from rpython.rlib.entrypoint import export_symbol
export_symbol(rpy_entry)
return rpy_entry, [int]
\ No newline at end of file
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rmu_fast as rmu
from util import fncptr_from_rpy_func, fncptr_from_py_script, proc_call
from util import fncptr_from_rpy_func, fncptr_from_py_script, proc_call, call_and_check
import ctypes, os
spawn_proc = bool(int(os.environ.get('SPAWN_PROC', '1')))
......@@ -18,14 +18,6 @@ def rand_list_of(n):
return lst
def call_and_check(fnc, args, check_fnc):
res = fnc(*args)
if res is None:
check_fnc()
else:
check_fnc(res)
# --------------------------
# tests
def test_add():
......@@ -383,7 +375,7 @@ def test_new():
"@i64": i64
}
fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'test_fnc', spawn_proc)
fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'test_fnc', spawn_proc=spawn_proc)
mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
def check(res):
......@@ -453,7 +445,7 @@ def test_new_cmpeq():
"@i64": i64
}
fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'test_fnc', spawn_proc)
fnp, (mu, ctx, bldr) = fncptr_from_py_script(build_test_bundle, 'test_fnc', spawn_proc=spawn_proc)
mu.current_thread_as_mu_thread(rmu.null(rmu.MuCPtr))
def check(res):
......
......@@ -116,6 +116,14 @@ def proc_call(fnc, args):
return rtn
def call_and_check(fnc, args, check_fnc):
res = fnc(*args)
if res is None:
check_fnc()
else:
check_fnc(res)
def fncptr_from_rpy_func(rpy_fnc, llargtypes, llrestype, spawn_proc=True, **kwargs):
# NOTE: requires mu-client-pypy
from rpython.rtyper.lltypesystem import rffi
......
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