def compile_storagetank(): import cea.technologies.storage_tank reload(cea.technologies.storage_tank) cc = CC('storagetank_cc') cc.export('ode', "f8(f8[:], f8, f8, f8, f8, f8, f8, f8)")(cea.technologies.storage_tank.ode_hot_water_tank) cc.compile()
def compile_radiators(): import cea.technologies.radiators reload(cea.technologies.radiators) cc = CC('calc_radiator') cc.export('fh', "f8(f8, f8, f8, f8, f8, f8, f8)")(cea.technologies.radiators.fh) cc.export('lmrt', "f8(f8, f8, f8)")(cea.technologies.radiators.lmrt) cc.compile()
def compile_modules(): from numba.pycc import CC cc = CC("aot") # collect AOT-compiled modules directory = os.path.dirname(os.path.abspath(__file__)) for importer, module_name, _ in pkgutil.walk_packages([directory]): if module_name not in {"cc", "aot"}: module = importer.find_module(module_name).load_module(module_name) for function_name, (function, signature) in module.exports.items(): cc.export(function_name, signature)(function) cc.compile()
def aot_compile(file_name): """ :param file_name: :return: """ cc = CC(config['module_name']) func = [] signatures = [] if os.path.exists(config['example']): f = os.open(config['example'], os.O_RDONLY) stdin_bk = os.dup(0) os.dup2(f, 0) try: module = runpy.run_module(file_name, run_name="__main__") for k in module: e = module[k] if type(e) == numba.targets.registry.CPUDispatcher \ and e.nopython_signatures: cc.export(k, e.nopython_signatures[0])(e) func.append(k) signatures.append(str(e.nopython_signatures[0])) auto_jit = True finally: os.dup2(stdin_bk, 0) os.close(f) else: module = importlib.import_module(file_name) for dir_ in dir(module): e = eval('module.' + dir_) if type(e) == numba.targets.registry.CPUDispatcher \ and e.nopython_signatures: cc.export(dir_, e.nopython_signatures[0])(e) func.append(dir_) signatures.append(str(e.nopython_signatures[0])) auto_jit = False cc.output_dir = os.curdir if func: cc.compile() return cc, func, signatures, auto_jit
def numba_compile(numba_config): import os, sys if sys.argv[-1] == "ONLINE_JUDGE": from numba import njit from numba.pycc import CC cc = CC("my_module") for func, signature in numba_config: globals()[func.__name__] = njit(signature)(func) cc.export(func.__name__, signature)(func) cc.compile() exit() elif os.name == "posix": exec( f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}" ) for func, _ in numba_config: globals()[func.__name__] = vars()[func.__name__] else: from numba import njit for func, signature in numba_config: globals()[func.__name__] = njit(signature, cache=True)(func) print("compiled!", file=sys.stderr)
else: # no update on max_ps pass heappush(k_to_ps[dst], (-rateC, C)) minvalue = old_min() answers[t] = minvalue return answers USE_NUMBA = False if USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE' or sys.argv[-1] == '-c': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', 'void(i8,i8)')(main) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() else: if USE_NUMBA and sys.argv[-1] != '-p': # -p: pure python mode # if not -p, import compiled module from my_module import main # pylint: disable=all elif sys.argv[-1] == "-t": _test() exit() elif len(sys.argv) == 2: # input given as file input_as_file = open(sys.argv[1]) input = input_as_file.buffer.readline
# plan to visit neighbors for i in range(4): if not ng_edges[pos][i]: next = pos + direction[i] if visited[next]: continue to_visit.append(next) else: print(total_area) if sys.argv[-1] == 'ONLINE_JUDGE' or sys.argv[-1] == '-c': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', "void(i4, i4, i4[:])")(main) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() else: if sys.argv[-1] != '-p': # -p: pure python mode # if not -p, import compiled module from my_module import main # pylint: disable=all # read parameter import numba N, M = map(int, input().split()) data = np.int32(sys.stdin.read().split()) main(N, M, data)
def vectorized_function(arg1, arg2, arg3): output = np.empty((3, len(arg1)), dtype=np.float64) for i in range(len(arg1)): output[:, i] = function(arg1[i], arg2[i], arg3[i]) return output return jit(nopython=True, fastmath=True)(vectorized_function) out_type = typeof(np.empty((3, 9), dtype=np.float64)) arg_type = typeof(np.empty((9, ), dtype=np.float64)) if __name__ == "__main__": cc = CC("drive_1d_distance") cc.export("state_at_distance", "UniTuple(f8, 3)(f8, f8, f8)")(state_at_distance) cc.export("state_at_distance_vectorized", out_type(arg_type, arg_type, arg_type))(state_vectorize(state_at_distance)) cc.compile() cc = CC("drive_1d_time") cc.export("state_at_time", "UniTuple(f8, 3)(f8, f8, f8)")(state_at_time) cc.export("state_at_time_vectorized", out_type(arg_type, arg_type, arg_type))(state_vectorize(state_at_time)) cc.compile() cc = CC("drive_1d_velocity") cc.export("state_at_velocity", "UniTuple(f8, 3)(f8, f8, f8)")(state_at_velocity)
for _ in range(k%t+t): s=p[s]-1 a+=c[s] if a>m:m=a else: a=0 s=i for _ in range(min(k,t)): s=p[s]-1 a+=c[s] if a>m:m=a return m if sys.argv[-1]=='ONLINE_JUDGE': from numba.pycc import CC cc=CC('my_module') cc.export('main','i8(i8,i8,i8[:],i8[:])')(main) cc.compile() exit() from my_module import main (n,k),p,c=[int_(t.split())for t in open(0)] import os import sys #import numpy as np def solve(N, X, D): return # >>> numba compile >>> numba_config = [ [solve, "i8(i8,i8,i8)"],
def cc_export(): from numba.pycc import CC cc = CC('my_module') cc.export('main', 'i8(i8[:])')(main) cc.compile()
q, r = divmod(P, i) INV[i] = -INV[r] * q % P invf *= INV[i] invf %= P INVF[i] = invf def comb_rep(n, r): return (F[n + r - 1] * INVF[r] % P) * INVF[n - 1] % P ret = 0 for i in range(K + 1): ret += (P25[i] * P26[K - i] % P * comb_rep(N, i) % P) return (ret % P) if sys.argv[-1] == 'ONLINE_JUDGE': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', 'i8(i8,i8)')(main) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() else: # read parameter from my_module import main # K = int(input()) # S = input() # print(main(K, len(S))) print(main(10**6, 10**6))
def compile_rc_model_sia(): import cea.demand.rc_model_SIA reload(cea.demand.rc_model_SIA) cc = CC('rc_model_sia_cc') # cc.export('calc_h_ec', "f8(f8)")(cea.demand.rc_model_SIA.calc_h_ec) # cc.export('calc_h_ac', "f8(f8)")(cea.demand.rc_model_SIA.calc_h_ac) # cc.export('calc_h_ea', "f8(f8, f8, f8)")(cea.demand.rc_model_SIA.calc_h_ea) # cc.export('calc_f_sc', "f8(f8, f8, f8, f8)")(cea.demand.rc_model_SIA.calc_f_sc) cc.export('calc_phi_m', "f8(f8, f8, f8, f8, f8, f8, f8)")( cea.demand.rc_model_SIA.calc_phi_m) cc.export('calc_phi_c', "f8(f8, f8, f8, f8, f8, f8, f8)")( cea.demand.rc_model_SIA.calc_phi_c) cc.export('calc_theta_c', "f8(f8, f8, f8, f8, f8, f8, f8, f8, f8)")( cea.demand.rc_model_SIA.calc_theta_c) cc.export('calc_phi_m_tot', "f8(f8, f8, f8, f8, f8, f8, f8, f8, f8, f8, f8, f8)")( cea.demand.rc_model_SIA.calc_phi_m_tot) cc.export('calc_phi_a', "f8(f8, f8, f8, f8, f8)")(cea.demand.rc_model_SIA.calc_phi_a) cc.export('calc_theta_m', "f8(f8, f8)")(cea.demand.rc_model_SIA.calc_theta_m) cc.export('calc_h_ea', "f8(f8, f8, f8)")(cea.demand.rc_model_SIA.calc_h_ea) cc.export('calc_theta_m_t', "f8(f8, f8, f8, f8, f8)")(cea.demand.rc_model_SIA.calc_theta_m_t) cc.export('calc_theta_ea', "f8(f8, f8, f8, f8, f8)")(cea.demand.rc_model_SIA.calc_theta_ea) cc.export('calc_h_em', "f8(f8, f8)")(cea.demand.rc_model_SIA.calc_h_em) cc.export('calc_h_3', "f8(f8, f8)")(cea.demand.rc_model_SIA.calc_h_3) cc.compile()
A = np.zeros(n, dtype=np.int64) tmp = 1 for i in range(n - 1, -1, -1): A[i] = (tmp << 32) + tmp tmp *= 10 tmp %= MOD build(A) ans = np.zeros(q, dtype=np.int64) for i in range(q): range_apply(X[i] - 1, Y[i], REP[i]) ans[i] = all_prod() >> 32 return ans if sys.argv[-1] == 'ONLINE_JUDGE': from numba.pycc import CC cc = CC('my_module') cc.export('solve', '(i8[:],)')(solve) cc.compile() exit() from my_module import solve inp = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ') ans = solve(inp) print('\n'.join(map(str, ans)))
import numba def foo(x): for k in x: print("k:", k) for a, b in x[k]: print(a, b) if sys.argv[-1] == "-c": # numba compile print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export( 'foo', 'void(DictType(int64,ListType(UniTuple(int64,2))))')(foo) cc.compile() exit() else: x = numba.typed.Dict() x[1] = numba.typed.List([(1, 2), (3, 4)]) x[100] = numba.typed.List([(100, 200)]) if sys.argv[-1] == "-p": print(numba.typeof(x)) # => DictType[int64,ListType[UniTuple(int64 x 2)]] else: from my_module import foo foo(x)
import sys import numba import numpy as np def main(xs): ret = 0 a = {} for x in xs: a[x] = ret ret += x return ret if sys.argv[-1] == "-c": # numba compile print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', 'i8(i8[:])')(main) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() else: from my_module import main print(main(np.array(range(10))))
import sys import os import numpy as np def solve(n): su = 0 for i in range(1, n+1): m = n//i su += m*(2*i + (m-1)*i)//2 return su SIGNATURE = 'i8(i8)' if sys.argv[-1] == 'ONLINE_JUDGE': from numba.pycc import CC cc = CC('my_module') cc.export('solve', SIGNATURE)(solve) cc.compile() exit() if os.name == 'posix': # noinspection PyUnresolvedReferences from my_module import solve else: from numba import njit solve = njit(SIGNATURE, cache=True)(solve) print('compiled', file=sys.stderr) n=int(input()) print(solve(n))
def compile_sensible_loads(): import cea.demand.sensible_loads reload(cea.demand.sensible_loads) cc = CC('calc_tm') cc.export('calc_tm', "UniTuple(f8, 2)(f8, f8, f8, f8, f8)")(cea.demand.sensible_loads.calc_tm) cc.export('calc_ts', "f8(f8, f8, f8, f8, i4, f8, f8, f8, f8)")(cea.demand.sensible_loads.calc_ts) cc.export('calc_ts_tabs', "f8(f8, f8, f8, f8, i4, f8, f8, f8, f8)")(cea.demand.sensible_loads.calc_ts_tabs) cc.export('calc_ta', "f8(f8, f8, i4, f8, f8, f8)")(cea.demand.sensible_loads.calc_ta) cc.export('calc_ta_tabs', "f8(f8, f8, i4, f8, f8, f8)")(cea.demand.sensible_loads.calc_ta_tabs) cc.export('calc_top', "f8(f8, f8)")(cea.demand.sensible_loads.calc_top) cc.export('calc_Im_tot', "f8(f8, f8, f8, f8, f8, f8, f8, f8, i4, f8, f8)")(cea.demand.sensible_loads.calc_Im_tot) cc.export('calc_Im_tot_tabs', "f8(f8, f8, f8, f8, f8, f8, f8, f8, i4, f8, f8)")(cea.demand.sensible_loads.calc_Im_tot_tabs) cc.compile()
answers[t] = get_min(0) # print(t) # for i in range(21): # xs = node_to_list(roots[i]) # if len(xs): # print(f"{i}: ", xs) # print("answer", answers[t]) return np.array(answers) USE_NUMBA = True if USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE' or sys.argv[-1] == '-c': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', 'i8[:](i8,i8,i8[::1])')(main) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() else: input = sys.stdin.buffer.readline read = sys.stdin.buffer.read if USE_NUMBA and sys.argv[-1] != '-p': # -p: pure python mode # if not -p, import compiled module from my_module import main # pylint: disable=all elif sys.argv[-1] == "-t": _test() exit() elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
tt = tx ^ (tx << 11) t[0] = ty t[1] = tz t[2] = tw t[3] = tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)) return tw if __name__ == "__main__": import sys if sys.argv[-1] == "-c": # numba compile print("compiling") from numba.pycc import CC cc = CC('numba_rbst') cc.export('randInt', 'i8(i8[:])')(randInt) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() from numba_rbst import randInt # pylint: disable=all _test() r = RBST() if 1: t = time.perf_counter() for i in range(100000): r.insert(0) t = time.perf_counter() - t print(t) # 100000 => 3.55sec # with lprof 22.91sec
USE_NUMBA = True if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('solve', solve.__doc__.strip().split()[0])(solve) cc.compile() exit() else: input = sys.stdin.buffer.readline read = sys.stdin.buffer.read if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba": # -p: pure python mode # if not -p, import compiled module from my_module import solve # pylint: disable=all elif sys.argv[-1] == "-t": print("testing") _test() sys.exit() elif sys.argv[-1] != '-p' and len(sys.argv) == 2: # input given as file input_as_file = open(sys.argv[1]) input = input_as_file.buffer.readline read = input_as_file.buffer.read main()
def cc_export(): from numba.pycc import CC cc = CC("my_module") cc.export("solve", "(i8[:],)")(solve) cc.compile()
from numba.pycc import CC def main(): ds = {1: (2, 3)} # (A) x = ds.get(1, (4, 5)) # (B) # x = ds[1] # (B2) y = x[1] # (C) cc = CC('my_module') cc.export('main', '()')(main) cc.compile()
from numba.pycc import CC from base_filter import naive_filter import numpy as np cc = CC('filters_aot') DTYPE = np.float32 # Normally, you would use cc.export as a decorator before function definition: #@cc.export('numba_filter_aot', 'f4[:](f4[:], f4)') # But I define the function only once this way numba_filter_aot = cc.export('numba_filter_aot', 'f4[:](f4[:], f4)')(naive_filter) def compile_aot(): cc.compile()
def cc_export(): from numba.pycc import CC cc = CC('my_module') cc.export('solve', '(i8[:],)')(solve) cc.compile()
tt = tx ^ (tx << 11) t[0] = ty t[1] = tz t[2] = tw t[3] = tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)) return tw if __name__ == "__main__": import sys if sys.argv[-1] == "-c": # numba compile print("compiling") from numba.pycc import CC cc = CC('numba_rbst') cc.export('randInt', 'i8(i8[:])')(randInt) cc.export("lower_bound", "i8(i8[:],i8[:],i8[:],i8[:],i8,i8)")(lower_bound) cc.export("update", "i8(i8[:],i8[:],i8[:],i8[:],i8[:],i8)")(update) cc.export("split", "void(i8[:],i8[:],i8[:],i8[:],i8[:],i8[:],i8,i8)")(split) cc.export("merge", "i8(i8[:],i8[:],i8[:],i8[:],i8[:],i8,i8,i8[:])")(merge) cc.export("push", "void(i8)")(push) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() if sys.argv[-1] != "-p": # mean: pure python mode from numba_rbst import randInt, lower_bound, update, split, merge, push
def as_input(s): "use in test, use given string as input file" import io global read, input f = io.StringIO(s.strip()) input = f.readline read = f.read USE_NUMBA = False if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c': print("compiling") from numba.pycc import CC import numba cc = CC('my_module') cc.export('solve', "i4(i4,i4,i4[:])")(solve) cc.export('get_longest', "i4(i4,i4[:],i4[:],i4[:],i4[:])")(get_longest) cc.compile() exit() else: input = sys.stdin.buffer.readline read = sys.stdin.buffer.read if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba": # -p: pure python mode # if not -p, import compiled module from my_module import solve # pylint: disable=all elif sys.argv[-1] == "-t": _test() sys.exit() elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
shortest_path[to] = pos_dir result = min(distances[goal * NUM_DIR:goal * NUM_DIR + NUM_DIR]) if result[0] == INF: # unreachable return -1 else: return result[0] # print(sys.argv) if sys.argv[-1] == 'ONLINE_JUDGE': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', 'i8(i8,i8,i8,i8,i8,i8,i8,b1[:])')(main) cc.compile() exit() else: H, W, K = map(int, input().split()) x1, y1, x2, y2 = map(int, input().split()) from my_module import main import numpy as np C = np.zeros((H + 2, W + 2), np.bool_) data = np.frombuffer(sys.stdin.buffer.read(), 'S1') # print(data) data = data.reshape(H, -1) # print(data) data = data[:, :W] == b'.' # print(data)
n = len(x) for i in reversed(range(n // 2)): _siftup_max(x, i) def _test(): import doctest doctest.testmod() USE_NUMBA = False if USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE' or sys.argv[-1] == '-c': print("compiling") from numba.pycc import CC cc = CC('my_module') cc.export('main', 'void()')(main) # b1: bool, i4: int32, i8: int64, double: f8, [:], [:, :] cc.compile() exit() else: if USE_NUMBA and sys.argv[-1] != '-p': # -p: pure python mode # if not -p, import compiled module from my_module import main # pylint: disable=all elif sys.argv[-1] == "-t": _test() exit() elif len(sys.argv) == 2: # input given as file input_as_file = open(sys.argv[1]) input = input_as_file.buffer.readline
# 止まって向きを変える権利を得る y = 5 * v dist_y = dist_x + (-dist_x) % K if dist_y < dist[y]: dist[y] = dist_y heappush(q, B * dist_y + y) x = dist[5 * T] if x == INF: return -1 return dist[5 * T] // K if sys.argv[-1] == 'ONLINE_JUDGE': from numba.pycc import CC cc = CC('my_module') cc.export('main', '(b1[:],i8,i8,i8,i8,i8)')(main) cc.compile() exit() from my_module import main H, W, K = map(int, readline().split()) x1, y1, x2, y2 = map(int, readline().split()) C = np.zeros((H + 2, W + 2), np.bool_) C[1:-1, 1:-1] = np.frombuffer(read(), 'S1').reshape(H, -1)[:, :W] == b'.' C = C.ravel() H += 2 W += 2 S = W * x1 + y1 T = W * x2 + y2
def compile_sensible_loads(): import cea.demand.sensible_loads reload(cea.demand.sensible_loads) cc = CC('calc_tm') cc.export('calc_tm', "UniTuple(f8, 2)(f8, f8, f8, f8, f8)")( cea.demand.sensible_loads.calc_tm) cc.export('calc_ts', "f8(f8, f8, f8, f8, i4, f8, f8, f8, f8)")( cea.demand.sensible_loads.calc_ts) cc.export('calc_ts_tabs', "f8(f8, f8, f8, f8, i4, f8, f8, f8, f8)")( cea.demand.sensible_loads.calc_ts_tabs) cc.export('calc_ta', "f8(f8, f8, i4, f8, f8, f8)")(cea.demand.sensible_loads.calc_ta) cc.export('calc_ta_tabs', "f8(f8, f8, i4, f8, f8, f8)")( cea.demand.sensible_loads.calc_ta_tabs) cc.export('calc_top', "f8(f8, f8)")(cea.demand.sensible_loads.calc_top) cc.export('calc_Im_tot', "f8(f8, f8, f8, f8, f8, f8, f8, f8, i4, f8, f8)")( cea.demand.sensible_loads.calc_Im_tot) cc.export('calc_Im_tot_tabs', "f8(f8, f8, f8, f8, f8, f8, f8, f8, i4, f8, f8)")( cea.demand.sensible_loads.calc_Im_tot_tabs) cc.compile()