Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple output and numba signatures

Tags:

python

numba

Maybe it is trivial, but I was wondering how to write signatures in the jit decorator when there are several outputs.

For instance :

import numba as nb  @nb.jit(['???(int32, int32, float(:,:), float(:,:))'], nopython=True) def foo(nx, ny, a, b):     for i in range(nx):         for i in range(ny):             do stuff with a & b     return a, b 

What about the performances ? Is it better to write two different functions ?

like image 745
Ipse Lium Avatar asked May 21 '15 01:05

Ipse Lium


People also ask

Does Numba work with strings?

Numba supports (Unicode) strings in Python 3. Strings can be passed into nopython mode as arguments, as well as constructed and returned from nopython mode.

Is Numba better than NumPy?

For larger input data, Numba version of function is must faster than Numpy version, even taking into account of the compiling time. In fact, the ratio of the Numpy and Numba run time will depends on both datasize, and the number of loops, or more general the nature of the function (to be compiled).

Does Numba work with list comprehension?

Numba supports list comprehension, but not the creation of nested list.

Does Numba make NumPy faster?

In short Numba makes Python/NumPy code runs faster. It achieves this by compiling your Python code into native machine code.


2 Answers

You can either use explicit declarations or string declaration :

Tuple with homogeneous types :

@nb.jit(nb.types.UniTuple(nb.float64[:],2)(nb.float64[:]),nopython=True) def f(a) :     return a,a  @nb.jit('UniTuple(float64[:], 2)(float64[:])',nopython=True) def f(a) :     return a,a 

Tuple with heterogeneous types :

@nb.jit(nb.types.Tuple((nb.float64[:], nb.float64[:,:]))(nb.float64[:], nb.float64[:,:]),nopython=True) def f(a, b) :     return a, b  @nb.jit('Tuple((float64[:], float64[:,:]))(float64[:], float64[:,:])',nopython=True) def f(a, b) :     return a, b 

Source : my own experiments, and the source code of Numba : https://github.com/numba/numba

Of course, the solution proposed by DavidW is an excellent workaround when you do not know the exact type :

@nb.jit(nb.typeof((1.0,1.0))(nb.double),nopython=True) def f(a):   return a,a 
like image 147
Jean Lescut Avatar answered Oct 07 '22 22:10

Jean Lescut


According to this newsgroup post you can specify using numba.typeof(<an example of your tuple>)

For example

import numba as nb  # I've put "nopython=True" just to demonstrate it still works # whether you need it is your choice @nb.jit(nb.typeof((1.0,1.0))(nb.double),nopython=True) def f(a):   return a,a  print f(5.0) # returns 5.0,5.0 

You could also build them from the components given in numba.types, but that's probably more work than using typeof

The fact it can do this in nopython mode suggests performance should be OK (tuple unpacking is explicitly listed as a supported feature http://numba.pydata.org/numba-doc/dev/reference/pysupported.html). However, I haven't actually tested the performance.

like image 27
DavidW Avatar answered Oct 07 '22 21:10

DavidW