Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numba 0.35.0 : Use NumPy out parameter

I'm doing some tests to better understand how Numba works with NumPy, here I'm trying to see if Numba can handle out parameter.

import numpy as np 
from numba import njit , jit
from time import time 

@njit
def mult(a,b, N = 1000000):
    c = np.zeros_like(a)
    for i in range(N):
        np.multiply(a, b, out=c)
    return c

d = np.asarray([1,2,3,4,5,6,7,8,9])
e = np.asarray([1,2,3,4,5,6,7,8,9])

t = time()
e = mult(d,e)

print "Time Elapsed :" + str(time() - t)

Without using Numba, the code goes well. It's even quicker than using @jit decoration : ~1.2s against ~1.6s with my configuration.

Using @njit it leads to that error :

 LoweringError: unsupported keyword arguments when calling Function(<ufunc 'multiply'>)

Though, Reading the Numba 0.15.1 doc. , they say out parameter is supported. What can I do against this ?

like image 613
snoob dogg Avatar asked Jul 14 '26 04:07

snoob dogg


1 Answers

It's just that numba in nopython mode doesn't support keyword-argument. It works if you pass it as positional argument:

@njit
def mult(a,b, N = 1000000):
    c = np.zeros_like(a)
    for i in range(N):
        np.multiply(a, b, c)
    return c

However using loops that always do the same thing can be a problem with numba because sometimes the numba compiler notices that the result doesn't change between loops and it completely optimizes the loop away - essentially resulting in flawed timings. However in this case I don't think this happened, but you need to be careful when using an aggressive compiler like numba and timing it against a "naive" Python approach.

like image 163
MSeifert Avatar answered Jul 15 '26 17:07

MSeifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!