def prototype(params): s = Settings(params) sim_py = Simulation(s) sim_py.run() sim_st = Simulation(s) stella.wrap(sim_st.run)() assert id(sim_py.observations) != id(sim_st.observations) assert sim_py == sim_st
def prototype(params): s = Settings(params) sim_py = Simulation(s) sim_py.run() sim_st = Simulation(s) stella.wrap(sim_st.run)() assert id(sim_py.observations) != id(sim_st.observations) assert sim_py == sim_st
def prototype(params): prepare(params) run() py = np.array(observations) # save the global result variable prepare(params) stella.wrap(run)() assert id(py) != id(observations) st = observations assert all(py == st)
def test_exception(f_exc): """ Note: this isn't a real test. The NotImplementedError is thrown during _compile-time_, not _run-time_! """ f, exc = f_exc with raises(exc): f() with raises(NotImplementedError): wrap(f)()
def test_exception(f_exc): """ Note: this isn't a real test. The NotImplementedError is thrown during _compile-time_, not _run-time_! """ f, exc = f_exc with raises(exc): f() with raises(NotImplementedError): wrap(f)()
def prototype(params): s = Settings(params) py = np.zeros(shape=s['K'], dtype=int) sp_py = Spider(s, py) run(sp_py) st = np.zeros(shape=s['K'], dtype=int) sp_st = Spider(s, st) stella.wrap(run)(sp_st) assert id(sp_py.observations) != id(sp_st.observations) assert sp_py == sp_st
def make_exc_test(f, args, py_exc, stella_exc): with raises(py_exc): x = f(*args) # noqa with raises(stella_exc): y = wrap(f)(*args) # noqa assert True
def make_exc_test(f, args, py_exc, stella_exc): with raises(py_exc): x = f(*args) # noqa with raises(stella_exc): y = wrap(f)(*args) # noqa assert True
def test_mutation7(f): l1 = [E(4), E(1)] l2 = [E(4), E(1)] py = f(l1) st = stella.wrap(f)(l2) assert l1 == l2 and py == st
def test_no_mutation10(f, args): b1 = G(*args) b2 = G(*args) assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 == b2 and py == st
def test_no_mutation13(f, arg): b1 = H(1, 2, 3) b2 = H(1, 2, 3) assert b1 == b2 py = f(b1, arg) st = stella.wrap(f)(b2, arg) assert b1 == b2 and py == st
def test_no_mutation14(f, arg): b1 = J(arg) b2 = J(arg) assert b1 == b2 py = f(b1, arg) st = stella.wrap(f)(b2, arg) assert b1 == b2 and py == st
def test_mutation3(f): b1 = D() b2 = D() assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 == b2 and py == st
def test_no_mutation12(f): b1 = H(1, 2, 3) b2 = H(1, 2, 3) assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 == b2 and py == st
def test_mutation2_u(f, args): b1 = B(*args) b2 = B(*args) assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 == b2 and py == st
def test_no_mutation3(f, args): b1 = C(args) b2 = C(args) assert b1 == b2 py = f(b1, 1) st = stella.wrap(f)(b2, 1) assert b1 == b2 and py == st
def test_mutation(f): b1 = B() b2 = B() assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 != B() and b1 == b2 and py == st
def test_mutation5(f, arg): e1 = E() e2 = E() assert e1 == e2 py = f(e1, arg) st = stella.wrap(f)(e2, arg) assert e1 == e2 and py == st
def test1(): import stella sim1 = Sim() sim2 = Sim() settings = 'test1_settings.txt' process_config(sim1, settings) process_config(sim2, settings) sim1.run() stella.wrap(sim2.run)() print("Python:") format_result(sim1) print("Stella:") format_result(sim2) assert ((sim1.observations == sim2.observations).all())
def test_mutation4(f): e1 = E() e2 = E() assert e1 == e2 py = f(e1) st = stella.wrap(f)(e2) assert e1 == e2 and py == st
def test_mutation8(f): l1 = [E(2), E(5)] l2 = [E(2), E(5)] f1 = F(l1) f2 = F(l2) py = f(f1) st = stella.wrap(f)(f2) assert f1 == f2 and py == st
def test_mutation6(f): e1 = E() e2 = E() e3 = E() e4 = E() assert e1 == e2 and e3 == e4 py = f(e1, e3) st = stella.wrap(f)(e2, e4) assert e1 == e2 and e3 == e4 and py == st
def test_no_mutation9(f, args): b1 = B(*args) b2 = B(*args) b1.next = b1 b2.next = b2 assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 == b2 and py == st
def _test1(opt): import stella n = 5990 offset_momentum(BODIES['sun'], SYSTEM) s1 = copy.deepcopy(SYSTEM) s2 = copy.deepcopy(SYSTEM) r1 = advance(0.01, n, s1) r2 = stella.wrap(advance, opt=opt)(0.01, n, s2) for i, body in enumerate(s1): body.diff(s2[i]) assert r1 == r2 and abs(calculate_energy(s1) - calculate_energy(s2)) < DELTA
def test13b(): """Global scalars are currently not updated in Python when their value changes in Stella""" global some_global some_global = 0 py = use_global() assert some_global == 1 some_global = 0 st = stella.wrap(use_global)() assert some_global == 0 assert py == st
def test_mutation_f(f): """ The opposite, setting an int when the struct member is float does not raise a TypeError since the int will be promoted to a float. """ b1 = B() b2 = B() assert b1 == b2 py = f(b1) st = stella.wrap(f)(b2) assert b1 != B() and b1 == b2 and py == st
def make_eq_test(f, args): args1 = [] args2 = [] for a in args: if type(a) == np.ndarray: args1.append(np.copy(a)) args2.append(np.copy(a)) else: args1.append(a) args2.append(a) x = f(*args1) y = wrap(f)(*args2) # TODO: also check the types inside the array assert x == y and type(x) == type(y)
def make_eq_test(f, args): args1 = [] args2 = [] for a in args: if type(a) == np.ndarray: args1.append(np.copy(a)) args2.append(np.copy(a)) else: args1.append(a) args2.append(a) x = f(*args1) y = wrap(f)(*args2) # TODO: also check the types inside the array assert x == y and type(x) == type(y)
def test13(): global numpy_global_var orig = np.zeros(5, dtype=int) numpy_global_var = np.array(orig) py = numpy_global() py_res = numpy_global_var numpy_global_var = orig st = stella.wrap(numpy_global)() st_res = numpy_global_var assert py == st assert all(py_res == st_res)
def test13d(): """Defining a new (i.e. not in Python initialized) global variable and initialize it with another variable """ global prev_undefined assert 'prev_undefined' not in globals() py = new_global_var(42) assert 'prev_undefined' in globals() del prev_undefined assert 'prev_undefined' not in globals() st = stella.wrap(new_global_var)(42) # Note: currently no variable updates are transfered back to Python assert 'prev_undefined' not in globals() assert py == st
def make_numpy_eq_test(f, args): """ TODO stella right now won't return numpy types. This test will treat them as equal to the python counterparts. """ args1 = [] args2 = [] for a in args: if type(a) == np.ndarray: args1.append(np.copy(a)) args2.append(np.copy(a)) else: args1.append(a) args2.append(a) x = f(*args1) y = wrap(f)(*args2) type_x = type(x) for type_name in ('int', 'float'): if type(x).__name__.startswith(type_name): type_x = __builtins__[type_name] assert x == y and type_x == type(y)
def make_numpy_eq_test(f, args): """ TODO stella right now won't return numpy types. This test will treat them as equal to the python counterparts. """ args1 = [] args2 = [] for a in args: if type(a) == np.ndarray: args1.append(np.copy(a)) args2.append(np.copy(a)) else: args1.append(a) args2.append(a) x = f(*args1) y = wrap(f)(*args2) type_x = type(x) for type_name in ('int', 'float'): if type(x).__name__.startswith(type_name): type_x = __builtins__[type_name] assert x == y and type_x == type(y)
def current_work(run=False, **kwargs): if type(run) == bool: ir = not run else: ir = run print(stella.wrap(stella.test.objects.selfRef, ir=ir, **kwargs)(g))
def make_delta_test(f, args, delta=delta): x = f(*args) y = wrap(f)(*args) assert x - y < delta and type(x) == type(y)
def make_eq_kw_test(f, args): x = f(**args) y = wrap(f)(**args) assert x == y and type(x) == type(y)
def test_no_mutation15(f): py = f() st = stella.wrap(f)() assert b1 == b2 and py == st
def make_eq_kw_test(f, args): x = f(**args) y = wrap(f)(**args) assert x == y and type(x) == type(y)
def make_delta_test(f, args, delta=delta): x = f(*args) y = wrap(f)(*args) assert x - y < delta and type(x) == type(y)
def current_work(run=False, **kwargs): if type(run) == bool: ir = not run else: ir = run print(stella.wrap(stella.test.objects.selfRef, ir=ir, **kwargs)(g))
print("{} | {} : in={}, out={}".format( str(obj), repr(obj), len(gc.get_referrers(obj)), len(gc.get_referrers(obj)))) for r in gc.get_referrers(obj): print(" < {}".format(type(r))) if isinstance(r, list): if len(r) > 20: print(" ", len(r)) continue print(" ", r) # for rr in gc.get_referrers(r): # print(" < {}".format(type(r))) # for r in gc.get_referents(obj): # print(" > {}".format(type(r))) # with bug r = stella.wrap(langconstr.kwargs_call1)(1) print(r) # with bug #r = stella.wrap(langconstr.call_void)() #print(r) # no bug #r = stella.wrap(langconstr.array_alloc_use)() #print(r) print ('='*78) check() print ('='*78) print("Garbarge: ", len(gc.garbage), any([isinstance(x, stella.ir.Module) for x in gc.garbage])) for m in filter(lambda x: isinstance(x, stella.ir.Module), gc.garbage): import pdb; pdb.set_trace() # XXX BREAKPOINT
def bench_it(name, c_src, args, extended, parse_f, verify_f, stella_f=None, full_f=None, flags={}): """args = {k=v, ...} Args gets expanded to `k`_init: `k`=`v` for the C template """ if not stella_f and not full_f: raise Exception( "Either need to specify stella_f(*arg_value) or full_f(args, stats)") t_run = {} t_compile = {} c_args = {k+'_init': k+'='+str(v) for k, v in args.items()} print("Doing {0}({1})".format(name, args)) src = pystache.render(c_src, **c_args) if extended: CCs = ['gcc', 'clang'] else: CCs = ['gcc'] results = {} for cc in CCs: exe, elapsed_compile = ccompile(__file__ + "." + name + ".c", src, cc, flags) t_compile[cc] = elapsed_compile cmd = [exe] print("Running C/{}: {}".format(cc, " ".join(cmd))) time_start = time.time() out = check_output(cmd, universal_newlines=True) print(out) results[cc] = parse_f(out) elapsed_c = time.time() - time_start t_run[cc] = elapsed_c print("Running Stella:") stats = {} wrapper_opts = {'debug': False, 'opt': opt, 'stats': stats} if stella_f: arg_values = args.values() time_start = time.time() res = stella.wrap(stella_f, **wrapper_opts)(*arg_values) elapsed_stella = time.time() - time_start else: elapsed_stella, res = full_f(args, stella.wrap, wrapper_opts) results['stella'] = res t_run['stella'] = stats['elapsed'] # TODO no need to keep track of the combined time, is there? # t_run['stella+compile'] = elapsed_stella t_compile['stella'] = elapsed_stella - stats['elapsed'] if extended > 1: print("\nRunning Python:") if stella_f: time_start = time.time() res = stella_f(*[v for k, v in args.items()]) elapsed_py = time.time() - time_start else: elapsed_py, res = full_f(args, time_stats, wrapper_opts) t_run['python'] = elapsed_py results['python'] = res # verify results are identical it = iter(results.keys()) k1 = next(it) for k2 in it: print('Verify:', k1, '==', k2) verify_f(results[k1], results[k2]) k1 = k2 return {'run': t_run, 'compile': t_compile}
for r in gc.get_referrers(obj): print(" < {}".format(type(r))) if isinstance(r, list): if len(r) > 20: print(" ", len(r)) continue print(" ", r) # for rr in gc.get_referrers(r): # print(" < {}".format(type(r))) # for r in gc.get_referents(obj): # print(" > {}".format(type(r))) # with bug r = stella.wrap(langconstr.kwargs_call1)(1) print(r) # with bug #r = stella.wrap(langconstr.call_void)() #print(r) # no bug #r = stella.wrap(langconstr.array_alloc_use)() #print(r) print('=' * 78) check() print('=' * 78) print("Garbarge: ", len(gc.garbage), any([isinstance(x, stella.ir.Module) for x in gc.garbage])) for m in filter(lambda x: isinstance(x, stella.ir.Module), gc.garbage):