Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/material/numba.ipynb
934 views
Kernel: Python 3

Open In Colab

Numba

Compile numerical functions for faster running

import time import numpy as np from numba import jit import pandas as pd
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) <ipython-input-2-1194e744ea9c> in <module>() 1 import time 2 import numpy as np ----> 3 from numba import jit 4 import pandas as pd ImportError: No module named 'numba'

Example of the problem: integer summation

st=time.time() imax=10000 for i in range(imax): for j in range(imax): a=i+j print(time.time()-st)
9.316529273986816
def suma(imax): for i in range(imax): for j in range(imax): a=i+j return a
st=time.time() a=suma(imax) print(time.time()-st)
4.799173831939697

Precompile the function with @jit of numba

# jit decorator tells Numba to compile this function. # The argument types will be inferred by Numba when function is called. @jit def suma(imax): for i in range(imax): for j in range(imax): a=i+j return a
st=time.time() a=suma(imax) print(time.time()-st)
0.09396576881408691
a=pd.DataFrame()# []#0

A more complicated example with arrays

a=np.array( [0,0]) np.concatenate((a,a))
array([0, 0, 0, 0])
# jit decorator tells Numba to compile this function. # The argument types will be inferred by Numba when function is called. @jit#(nopython=True) def suma(imax,a): for i in range(imax): for j in range(imax): a[i]=i+j return(a)
imax=10000 s=time.time() a=np.zeros(imax) a=suma(imax,a) print(time.time()-s)
0.00023245811462402344
s=time.time() a=np.zeros(imax) a=suma(imax,a) print(time.time()-s)
0.0002434253692626953
s=time.time() a=np.zeros(imax) a=suma(imax,a) print(time.time()-s)
0.0002474784851074219
>>> x = np.arange(-5, 5, 0.1) >>> y = np.arange(-5, 5, 0.1) >>> xx, yy = np.meshgrid(x, y, sparse=True) >>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2) #>>> h = plt.contourf(x,y,z)
imax
10000
>>> x = np.arange(0, imax, 1) >>> y = np.arange(0, imax, 1) >>> xx, yy = np.meshgrid(x, y, sparse=False) >>> z = xx+yy
xx
array([[ 0, 1, 2, ..., 9997, 9998, 9999], [ 0, 1, 2, ..., 9997, 9998, 9999], [ 0, 1, 2, ..., 9997, 9998, 9999], ..., [ 0, 1, 2, ..., 9997, 9998, 9999], [ 0, 1, 2, ..., 9997, 9998, 9999], [ 0, 1, 2, ..., 9997, 9998, 9999]])
from numba import jit import numpy as np @jit(nopython=True) def f(n): #return np.array([ [ [x,y] for x in range(n) ] for y in range(n) ]) for y in range(n): return np.array([ (x,y) for x in range(n) ] )
s=time.time() a=f(10000) print(time.time()-s)
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) <ipython-input-21-ad659232eeb3> in <module>() 1 s=time.time() ----> 2 a=f(10000) 3 print(time.time()-s) /usr/local/lib/python3.5/dist-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws) 305 argtypes.append(self.typeof_pyval(a)) 306 try: --> 307 return self.compile(tuple(argtypes)) 308 except errors.TypingError as e: 309 # Intercept typing error that may be due to an argument /usr/local/lib/python3.5/dist-packages/numba/dispatcher.py in compile(self, sig) 577 578 self._cache_misses[sig] += 1 --> 579 cres = self._compiler.compile(args, return_type) 580 self.add_overload(cres) 581 self._cache.save_overload(sig, cres) /usr/local/lib/python3.5/dist-packages/numba/dispatcher.py in compile(self, args, return_type) 78 impl, 79 args=args, return_type=return_type, ---> 80 flags=flags, locals=self.locals) 81 # Check typing error if object mode is used 82 if cres.typing_error is not None and not flags.enable_pyobject: /usr/local/lib/python3.5/dist-packages/numba/compiler.py in compile_extra(typingctx, targetctx, func, args, return_type, flags, locals, library) 777 pipeline = Pipeline(typingctx, targetctx, library, 778 args, return_type, flags, locals) --> 779 return pipeline.compile_extra(func) 780 781 /usr/local/lib/python3.5/dist-packages/numba/compiler.py in compile_extra(self, func) 360 self.lifted = () 361 self.lifted_from = None --> 362 return self._compile_bytecode() 363 364 def compile_ir(self, func_ir, lifted=(), lifted_from=None): /usr/local/lib/python3.5/dist-packages/numba/compiler.py in _compile_bytecode(self) 736 """ 737 assert self.func_ir is None --> 738 return self._compile_core() 739 740 def _compile_ir(self): /usr/local/lib/python3.5/dist-packages/numba/compiler.py in _compile_core(self) 723 724 pm.finalize() --> 725 res = pm.run(self.status) 726 if res is not None: 727 # Early pipeline completion /usr/local/lib/python3.5/dist-packages/numba/compiler.py in run(self, status) 246 # No more fallback pipelines? 247 if is_final_pipeline: --> 248 raise patched_exception 249 # Go to next fallback pipeline 250 else: /usr/local/lib/python3.5/dist-packages/numba/compiler.py in run(self, status) 238 try: 239 event(stage_name) --> 240 stage() 241 except _EarlyPipelineCompletion as e: 242 return e.result /usr/local/lib/python3.5/dist-packages/numba/compiler.py in stage_nopython_backend(self) 656 """ 657 lowerfn = self.backend_nopython_mode --> 658 self._backend(lowerfn, objectmode=False) 659 660 def stage_compile_interp_mode(self): /usr/local/lib/python3.5/dist-packages/numba/compiler.py in _backend(self, lowerfn, objectmode) 611 self.library.enable_object_caching() 612 --> 613 lowered = lowerfn() 614 signature = typing.signature(self.return_type, *self.args) 615 self.cr = compile_result(typing_context=self.typingctx, /usr/local/lib/python3.5/dist-packages/numba/compiler.py in backend_nopython_mode(self) 598 self.return_type, 599 self.calltypes, --> 600 self.flags) 601 602 def _backend(self, lowerfn, objectmode): /usr/local/lib/python3.5/dist-packages/numba/compiler.py in native_lowering_stage(targetctx, library, interp, typemap, restype, calltypes, flags) 898 lower.lower() 899 if not flags.no_cpython_wrapper: --> 900 lower.create_cpython_wrapper(flags.release_gil) 901 env = lower.env 902 call_helper = lower.call_helper /usr/local/lib/python3.5/dist-packages/numba/lowering.py in create_cpython_wrapper(self, release_gil) 227 self.context.create_cpython_wrapper(self.library, self.fndesc, 228 self.env, self.call_helper, --> 229 release_gil=release_gil) 230 231 def setup_function(self, fndesc): /usr/local/lib/python3.5/dist-packages/numba/targets/cpu.py in create_cpython_wrapper(self, library, fndesc, env, call_helper, release_gil) 147 fndesc, env, call_helper=call_helper, 148 release_gil=release_gil) --> 149 builder.build() 150 library.add_ir_module(wrapper_module) 151 /usr/local/lib/python3.5/dist-packages/numba/callwrapper.py in build(self) 120 121 api = self.context.get_python_api(builder) --> 122 self.build_wrapper(api, builder, closure, args, kws) 123 124 return wrapper, api /usr/local/lib/python3.5/dist-packages/numba/callwrapper.py in build_wrapper(self, api, builder, closure, args, kws) 174 175 retty = self._simplified_return_type() --> 176 obj = api.from_native_return(retty, retval, env_manager) 177 builder.ret(obj) 178 /usr/local/lib/python3.5/dist-packages/numba/pythonapi.py in from_native_return(self, typ, val, env_manager) 1323 "prevented the return of " \ 1324 "optional value" -> 1325 out = self.from_native_value(typ, val, env_manager) 1326 return out 1327 /usr/local/lib/python3.5/dist-packages/numba/pythonapi.py in from_native_value(self, typ, val, env_manager) 1337 1338 c = _BoxContext(self.context, self.builder, self, env_manager) -> 1339 return impl(typ, val, c) 1340 1341 def reflect_native_value(self, typ, val, env_manager=None): /usr/local/lib/python3.5/dist-packages/numba/targets/boxing.py in box_array(typ, val, c) 307 nativeary = nativearycls(c.context, c.builder, value=val) 308 if c.context.enable_nrt: --> 309 np_dtype = numpy_support.as_dtype(typ.dtype) 310 dtypeptr = c.env_manager.read_const(c.env_manager.add_const(np_dtype)) 311 # Steals NRT ref /usr/local/lib/python3.5/dist-packages/numba/numpy_support.py in as_dtype(nbtype) 134 return as_dtype(nbtype.dtype) 135 raise NotImplementedError("%r cannot be represented as a Numpy dtype" --> 136 % (nbtype,)) 137 138 NotImplementedError: Failed at nopython (nopython mode backend) (int64 x 2) cannot be represented as a Numpy dtype
a[[2]]
array([[2, 0]])
a=(1,2)
np.concatenate( (a,a))
array([1, 2, 1, 2])
n_i=101 n_i%10==0
False