numba

How to nest numba jitclass

旧城冷巷雨未停 提交于 2020-01-12 05:12:42
问题 I'm trying to understand how the @jitclass decorator works with nested classes. I have written two dummy classes: fifi and toto fifi has a toto attribute. Both classes have the @jitclass decorator but compilation fails. Here's the code: fifi.py from numba import jitclass, float64 from toto import toto spec = [('a',float64),('b',float64),('c',toto)] @jitclass(spec) class fifi(object): def __init__(self, combis): self.a = combis self.b = 2 self.c = toto(combis) def mySqrt(self,x): s = x for i

Python : Speeding up my Runge-Kutta integration code challenge

不问归期 提交于 2020-01-11 07:52:47
问题 I am using the attached code to integrate a version of Fitzhugh-Nagumo model : from scipy.integrate import odeint import numpy as np import time P = {'epsilon':0.1, 'a1':1.0, 'a2':1.0, 'b':2.0, 'c':0.2} def fhn_rhs(V,t,P): u,v = V[0],V[1] u_t = u - u**3 - v v_t = P['epsilon']*(u - P['b']*v - P['c']) return np.stack((u_t,v_t)) def integrate(func,V0,t,args,step='RK4'): start = time.clock() P = args[0] result=[V0] for i,tmp in enumerate(t[1:]): result.append(RK4step(func,result[i],tmp,P,(t[i+1]

Passing a class object as a function argument, in Numba optimized Python

扶醉桌前 提交于 2020-01-04 05:54:37
问题 I want to pass a class object to a function. I can make it work, but I am wondering if there is a type I can assign it? I have a "minimal" example of what I am trying to do. spec = [("a", float64),("b",float64)] @jitclass(spec) class SOMETHING_3(): def __init__(self): self.a = 1.1 self.b = 2.3 def sum(self): return self.a + self.b @jit(float64(float64, XXX), nopython = True) def get_sum_3(c, someobj): d = 0 for i in range(1000): for j in range(1000): d += c + someobj.sum() return d If I

numba caching issue: cannot cache function / no locator available for file

柔情痞子 提交于 2020-01-03 17:34:09
问题 I am trying to deploy a codebase that has a number numba.njit functions with cache=True . It works fine running locally (Mac OS X 10.12.3), but on the remote machine (Ubuntu 14.04 on AWS) I am getting the following error: RuntimeError at /portal/ cannot cache function 'filter_selection': no locator available for file: '/srv/run/miniconda/envs/mbenv/lib/python2.7/site-packages/mproj/core_calcs/filter.py' I looked through the numba codebase, and I saw this file: https://github.com/numba/numba

Optimize Double loop in python

倾然丶 夕夏残阳落幕 提交于 2020-01-02 02:04:30
问题 I am trying to optimize the following loop : def numpy(nx, nz, c, rho): for ix in range(2, nx-3): for iz in range(2, nz-3): a[ix, iz] = sum(c*rho[ix-1:ix+3, iz]) b[ix, iz] = sum(c*rho[ix-2:ix+2, iz]) return a, b I tried different solutions and found using numba to calculate the sum of the product leads to better performances: import numpy as np import numba as nb import time @nb.autojit def sum_opt(arr1, arr2): s = arr1[0]*arr2[0] for i in range(1, len(arr1)): s+=arr1[i]*arr2[i] return s def

numba - guvectorize is very slow compared to jit

懵懂的女人 提交于 2020-01-01 19:31:23
问题 I was experimenting with Numba's @jit and @guvectorize and find that @guvectorize is considerably slower than @jit . For example, I have the following code that calculates the rolling moving average: import numpy as np from numba import * @guvectorize(['void(float64[:], float64[:], float64[:])'], '(n),()->(n)') def sma(x, m, y): n = x.shape[0] mi = int(m) y[:] *= np.nan for i in range(mi-1, n): for j in range(i-mi+1, i+1): y[i] = x[j] if j == i-m+1 else y[i]+x[j] y[i] /= double(mi) @jit

Cuda: library nvvm not found

喜夏-厌秋 提交于 2019-12-31 12:12:13
问题 I am trying to run the code below but an error is reported: NvvmSupportError: libNVVM cannot be found. Do conda install cudatoolkit : library nvvm not found My development environment is: Ubuntu 17.04, Spyder/Python3.5 and I have installed via conda (numba and cudatoolkit). Nvidia GPUs (GTX 1070 and GTX 1060). import numpy as np from timeit import default_timer as timer from numba import vectorize @vectorize(["float32(float32, float32)"], target='cuda') def VecADD(a,b): return a+b n =

Is it expected for numba's efficient square euclidean distance code to be slower than numpy's efficient counterpart?

筅森魡賤 提交于 2019-12-31 04:09:07
问题 I modify the most efficient code from (Why this numba code is 6x slower than numpy code?) so that it can handle x1 being (n, m) @nb.njit(fastmath=True,parallel=True) def euclidean_distance_square_numba_v5(x1, x2): res = np.empty((x1.shape[0], x2.shape[0]), dtype=x2.dtype) for a_idx in nb.prange(x1.shape[0]): for o_idx in range(x2.shape[0]): val = 0. for i_idx in range(x2.shape[1]): tmp = x1[a_idx, i_idx] - x2[o_idx, i_idx] val += tmp * tmp res[a_idx, o_idx] = val return res However, it is

Python: can numba work with arrays of strings in nopython mode?

白昼怎懂夜的黑 提交于 2019-12-30 19:30:26
问题 I am using pandas 0.16.2, numpy 1.9.2 and numba 0.20. Is there any way to get numba to support arrays of strings in nopython mode? Alternatively, could I somehow convert strings to numbers which numba would recognise? I have to run certain loops on an array of strings (a column from a pandas dataframe); if I could use numba the code would be substantially faster. I have come up with this minimal example to show what I mean: import numpy as np import numba x=np.array(['some','text','this','is'

Python: can numba work with arrays of strings in nopython mode?

会有一股神秘感。 提交于 2019-12-30 19:29:53
问题 I am using pandas 0.16.2, numpy 1.9.2 and numba 0.20. Is there any way to get numba to support arrays of strings in nopython mode? Alternatively, could I somehow convert strings to numbers which numba would recognise? I have to run certain loops on an array of strings (a column from a pandas dataframe); if I could use numba the code would be substantially faster. I have come up with this minimal example to show what I mean: import numpy as np import numba x=np.array(['some','text','this','is'