numba

Design heuristics for writing Python classes that interact with `scipy.integrate.odeint`?

巧了我就是萌 提交于 2019-12-24 02:39:14
问题 Introduction scipy.integrate.odeint requires as its first argument, a function that computes the derivatives of the variables we want to integrate over (which I'll refer to as d_func , for "derivative function" from now on). d_func has to be written by the user, in Python code. A great way to get a boost of performance using Numba is to @jit the d_func (because d_func is called many times during integration). I have questions about how to write performant code when d_func is complicated

2D array to represent a huge python dict, COOrdinate like solution to save memory

家住魔仙堡 提交于 2019-12-23 12:50:30
问题 I try to update a dict_with_tuples_key with the data from an array: myarray = np.array([[0, 0], # 0, 1 [0, 1], [1, 1], # 1, 2 [1, 2], # 1, 3 [2, 2], [1, 3]] ) # a lot of this with shape~(10e6, 2) dict_with_tuples_key = {(0, 1): 1, (3, 7): 1} # ~10e6 keys Using an array to store the dict values, (thanks to @MSeifert) we get this: def convert_dict_to_darray(dict_with_tuples_key, myarray): idx_max_array = np.max(myarray, axis=0) idx_max_dict = np.max(dict_with_tuples_key.keys(), axis=0) lens =

Using numba for cosine similarity between a vector and rows in a matix

扶醉桌前 提交于 2019-12-23 03:35:11
问题 Found this gist using numba for fast computation of cosine similarity. import numba @numba.jit(target='cpu', nopython=True) def fast_cosine(u, v): m = u.shape[0] udotv = 0 u_norm = 0 v_norm = 0 for i in range(m): if (np.isnan(u[i])) or (np.isnan(v[i])): continue udotv += u[i] * v[i] u_norm += u[i] * u[i] v_norm += v[i] * v[i] u_norm = np.sqrt(u_norm) v_norm = np.sqrt(v_norm) if (u_norm == 0) or (v_norm == 0): ratio = 1.0 else: ratio = udotv / (u_norm * v_norm) return ratio Results look

Numba and guvectorize for CUDA target: Code running slower than expected

放肆的年华 提交于 2019-12-22 18:38:26
问题 Notable details Large datasets (10 million x 5), (200 x 10 million x 5) Numpy mostly Takes longer after every run Using Spyder3 Windows 10 First thing is attempting to use guvectorize with the following function. I am passing in a bunch of numpy arrays and attempting to use them to multiply across two of the arrays. This works if run with a target other than cuda. However, when switched to cuda it results in an unknown error being: File "C:\ProgramData\Anaconda3\lib\site-packages\numba\cuda

Why this python class is not working with numba jitclass?

风流意气都作罢 提交于 2019-12-22 14:02:22
问题 I have written the following code with the help of numpy and I want to improve the performance with numba. I am not sure why it is not working as I have set all the variables as per numba system. I am trying to speed up this code as I would be working with large data sets in the future. import numpy as np import math from numba import jitclass from numba import float64,int64 spec =[ ('spacing',float64), ('n_iterations',int64), ('np_emptyhouses',float64[:,:]), ('np_agenthouses',float64[:,:]),

Numba - How to fill 2D array in parallel

一曲冷凌霜 提交于 2019-12-22 13:48:36
问题 I have a function that operates on a 2D matrix on float64(x,y). Basic concept: for each combination of rows (no. rows choose 2) count the number of positiv values after subtraction (row1 - row2). In a 2Dmatrix of int64(y,y) store this value in index [row1,row2] if value is above a certain threshold and [row2,row1] if below. I've implemented that and decorated it with @njit(parallel=False), that works fine @njit(parallel=True) seems to give no speedup. Trying to speed up the whole thing I had

Need help vectorizing code or optimizing

℡╲_俬逩灬. 提交于 2019-12-22 09:06:53
问题 I am trying to do a double integral by first interpolating the data to make a surface. I am using numba to try and speed this process up, but it's just taking too long. Here is my code, with the images needed to run the code located at here and here. 回答1: Noting that your code has a quadruple-nested set of for loops, I focused on optimizing the inner pair. Here's the old code: for i in xrange(K.shape[0]): for j in xrange(K.shape[1]): print(i,j) '''create an r vector ''' r=(i*distX,j*distY,z)

How can you implement a C callable from Numba for efficient integration with nquad?

痞子三分冷 提交于 2019-12-22 06:31:54
问题 I need to do a numerical integration in 6D in python. Because the scipy.integrate.nquad function is slow I am currently trying to speed things up by defining the integrand as a scipy.LowLevelCallable with Numba. I was able to do this in 1D with the scipy.integrate.quad by replicating the example given here: import numpy as np from numba import cfunc from scipy import integrate def integrand(t): return np.exp(-t) / t**2 nb_integrand = cfunc("float64(float64)")(integrand) # regular integration

How to specify numba jitclass when the class's attribute contains another class instance?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 05:22:09
问题 I'm trying to use numba to boost the python performance of scipy.integrate.odeint. To this end, I have to use @nb.jit(nopython=True) for the function defining the ODE system. However, this function has to take another python-class instance as an argument in my program. I had to jit the class also with @nb.jitclass(spec) with appropriate specs. This worked fine, until I found a serious issue when the specs of the class includes another type of class instance as its method. My code is following

Numba 3x slower than numpy

北城以北 提交于 2019-12-21 04:28:23
问题 We have a vectorial numpy get_pos_neg_bitwise function that use a mask=[132 20 192] and a df.shape of (500e3, 4) that we want to accelerate with numba. from numba import jit import numpy as np from time import time def get_pos_neg_bitwise(df, mask): """ In [1]: print mask [132 20 192] In [1]: print df [[ 1 162 97 41] [ 0 136 135 171] ..., [ 0 245 30 73]] """ check = (np.bitwise_and(mask, df[:, 1:]) == mask).all(axis=1) pos = (df[:, 0] == 1) & check neg = (df[:, 0] == 0) & check pos = np