Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running nested functions using numba

Tags:

python

jit

numba

I am trying to use numba recently to speedup parts of my code in python. I was trying to run function 1 from inside function 2 while they are both compiled with numba but it is not working. Here is my code:

import numba as nb
from math import acos
from time import time

@nb.jit("void()")
def myfunc():
    s = 0
    for i in range(10000000):
        s += acos(0.5)
    print('The sum is: ', s)


@nb.jit("void()")
def myfunc2():
    myfunc()


tic = time()
myfunc2()
toc = time()
print(toc-tic)

When I call myfunc() the code works and I get a result much faster than when I am not using numba. However, when I call the myfunc2 I see this error:

 File "~/.spyder-py3/temp.py", line 22, in <module>
    myfunc2()

RuntimeError: missing Environment

Anybody has any ideas why calling a function from insdie another is not working in this case?

like image 892
user1751189 Avatar asked Feb 04 '18 19:02

user1751189


People also ask

Does Numba work with multiprocessing?

You should not use the multiprocessing package in a Numba code. This will simply not work (Numba will use a fallback implementation which is the basic Python one).

Is Numba better than NumPy?

Large dataFor 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).

Is Numba faster than NumPy?

Numba is generally faster than Numpy and even Cython (at least on Linux). In this benchmark, pairwise distances have been computed, so this may depend on the algorithm.

Does Numba release Gil?

Numba will release the GIL when entering such a compiled function if you passed nogil=True . Code running with the GIL released runs concurrently with other threads executing Python or Numba code (either the same compiled function, or another one), allowing you to take advantage of multi-core systems.


1 Answers

Numba v0.39+

A fix was introduced in v0.39. As per the Release Notes:

PR #2986: Fix environment propagation

See github pull #2986 for more details.

Numba pre-v0.39

This is a known issue. As described in github issue #2411:

Seems like the environment pointer is not passed properly across nopython functions.

Amending as below to remove print() from numba functions should fix this:

import numba as nb
from math import acos
from time import time

@nb.jit("void()")
def myfunc():
    s = 0
    for i in range(10000000):
        s += acos(0.5)
    return s

@nb.jit("void()")
def myfunc2():
    return myfunc()

tic = time()
x = myfunc2()  # 10471975.511390356
toc = time()
print(toc-tic)
like image 140
jpp Avatar answered Sep 30 '22 04:09

jpp