Parameters and guvectorize

0 views
Skip to first unread message

Carl Sanders

unread,
May 12, 2017, 2:20:40 PM5/12/17
to Numba Public Discussion - Public
Hi all,
First of all, thanks for numba, it's a fantastic tool. I'm currently writing an array reduction in numba, and I'm trying to figure out the fastest way to write this while being able to send in a scalar (or potentially vector) parameter.

The simplest examples where I'm trying to understand timing is something trivial like
@njit
def reduction_normal(parameter, array):
     output
= np.zeros(array.shape[0])
     
for i in range(array.shape[0]):
         
for j in range(array.shape[1]):
             output
[i] += math.exp(parameter)
     
return output
I run this 10 times with parameter=10.0, array->(1000000,10) random ndarray in about 80 ms.


Then things started getting confusing:
@guvectorize([(float64[:], float64[:], float64[:])], '(),(d)->()')
def reduction_gu(parameter, array, output):
    for j in range(array.shape[0]):
        output += math.exp(parameter[0])
and I call reduction_gu(parameter, array) on parameter->10.0, array->(1000000,10) random array (the same as the first example).
Running this gets the same result, but running it 10 times takes over a 1.5 seconds. This is quite expensive obviously, an offsets any gains I was hoping for with the ability to target=parallel with guvectorize. Presumably this is the cost of broadcasting parameters into a 1000000x1 vector?


Is there some good way to do what I'm trying to do? Putting the scalar parameter into a closure around the guvectorized function is faster (about 1 second for the benchmarks above) but still far too slow. Are there any obvious ways forward here? Thanks!
carl
Reply all
Reply to author
Forward
0 new messages