numba

Python : Speeding up my Runge-Kutta integration code challenge

拟墨画扇 提交于 2019-12-01 14:10:59
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]-t[i]))) print "Integration took ",time.clock() - start, " s" return np.array(result) def RK4step(rhs,V

How to sum of squares of sum with memory limitations?

廉价感情. 提交于 2019-12-01 13:22:58
问题 This is a follow up of this question: How to do a sum of sums of the square of sum of sums? Where I was asking for help to use einsum (to achieve a great speed increase) and got a great answer. I also got a suggestion to use numba . I've gone and tried both and it seems that after a certain point the speed increase in numba is much better. So how do you speed it up without running into memory issues? 回答1: The solution below presents the 3 different methods to do the simple sum of sums and 4

Numba.vectorize for CUDA: What is the correct signature to return arrays?

血红的双手。 提交于 2019-12-01 00:17:07
I have a function of the following structure, @numba.jit(nopython = True) def foo(X,N): ''' :param X: 1D numpy array :param N: Integer :rtype: 2D numpy array of shape len(X) x N ''' out = np.ones((len(X),N)) out[:,0] = X for i in range(1,N): out[:,i] = X**i+out[:,i-1] return out which I am now trying to run on my GPU. What I tried so far is to write the function in a non-vectorized form (i.e. treat each entry of X separately), and pass the return array as an input: def foo_cuda(x,N,out): ''' :param x: Scalar :param N: Integer :rtype: 1D numpy array of length N ''' out[0] = x for i in range(1,N

Vectorized 2-D moving window in numpy including edges

杀马特。学长 韩版系。学妹 提交于 2019-11-30 21:26:52
I realize my question is fairly similar to Vectorized moving window on 2D array in numpy , but the answers there don't quite satisfy my needs. Is it possible to do a vectorized 2D moving window (rolling window) which includes so-called edge effects? What would be the most efficient way to do this? That is, I would like to slide the center of a moving window across my grid, such that the center can move over each cell in the grid. When moving along the margins of the grid, this operation would return only the portion of the window that overlaps the grid. Where the window is entirely within the

Numba.vectorize for CUDA: What is the correct signature to return arrays?

a 夏天 提交于 2019-11-30 18:49:50
问题 I have a function of the following structure, @numba.jit(nopython = True) def foo(X,N): ''' :param X: 1D numpy array :param N: Integer :rtype: 2D numpy array of shape len(X) x N ''' out = np.ones((len(X),N)) out[:,0] = X for i in range(1,N): out[:,i] = X**i+out[:,i-1] return out which I am now trying to run on my GPU. What I tried so far is to write the function in a non-vectorized form (i.e. treat each entry of X separately), and pass the return array as an input: def foo_cuda(x,N,out): '''

Whenever I try to use @jit on my class method, I get IndentationError: unexpected indent

荒凉一梦 提交于 2019-11-30 16:14:26
I've been trying for several days to get @jit working to speed up my code. Finally I came across this, describing adding @jit to object methods: http://williamjshipman.wordpress.com/2013/12/24/learning-python-eight-ways-to-filter-an-image I have a class called GentleBoostC and I want to speed up the method within it that's called train . train accepts three arguments (a 2D array, a 1D array, and an integer), and returns nothing. This is what I have in code: import numba from numba import jit, autojit, int_, void, float_, object_ class GentleBoostC(object): # lots of functions # and now the

Whenever I try to use @jit on my class method, I get IndentationError: unexpected indent

旧时模样 提交于 2019-11-30 16:08:12
问题 I've been trying for several days to get @jit working to speed up my code. Finally I came across this, describing adding @jit to object methods: http://williamjshipman.wordpress.com/2013/12/24/learning-python-eight-ways-to-filter-an-image I have a class called GentleBoostC and I want to speed up the method within it that's called train . train accepts three arguments (a 2D array, a 1D array, and an integer), and returns nothing. This is what I have in code: import numba from numba import jit,

Numbas parallel vectorized functions

匆匆过客 提交于 2019-11-30 15:50:25
问题 I'm currently experimenting with numba and especially vectorized functions, so I created a sum vectorized function (because it is easy to compare this to np.sum . import numpy as np import numba as nb @nb.vectorize([nb.float64(nb.float64, nb.float64)]) def numba_sum(element1, element2): return element1 + element2 @nb.vectorize([nb.float64(nb.float64, nb.float64)], target='parallel') def numba_sum_parallel(element1, element2): return element1 + element2 array = np.ones(elements) np.testing

Numbas parallel vectorized functions

徘徊边缘 提交于 2019-11-30 15:36:52
I'm currently experimenting with numba and especially vectorized functions, so I created a sum vectorized function (because it is easy to compare this to np.sum . import numpy as np import numba as nb @nb.vectorize([nb.float64(nb.float64, nb.float64)]) def numba_sum(element1, element2): return element1 + element2 @nb.vectorize([nb.float64(nb.float64, nb.float64)], target='parallel') def numba_sum_parallel(element1, element2): return element1 + element2 array = np.ones(elements) np.testing.assert_almost_equal(numba_sum.reduce(array), np.sum(array)) np.testing.assert_almost_equal(numba_sum

Use numba to speed up for loop

故事扮演 提交于 2019-11-30 09:01:13
From what I've read, numba can significantly speed up a python program. Could my program's time efficiency be increased using numba? import numpy as np def f_big(A, k, std_A, std_k, mean_A=10, mean_k=0.2, hh=100): return ( 1 / (std_A * std_k * 2 * np.pi) ) * A * (hh/50) ** k * np.exp( -1*(k - mean_k)**2 / (2 * std_k **2 ) - (A - mean_A)**2 / (2 * std_A**2)) outer_sum = 0 dk = 0.000001 for k in np.arange(dk,0.4, dk): inner_sum = 0 for A in np.arange(dk, 20, dk): inner_sum += dk * f_big(A, k, 1e-5, 1e-5) outer_sum += inner_sum * dk print outer_sum Yes, this is the sort of problem that Numba