Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use numba on a member function of a class?

Tags:

python

oop

numba

I'm using the stable version of Numba 0.30.1.

I can do this:

import numba as nb @nb.jit("void(f8[:])",nopython=True)                              def complicated(x):                                       for a in x:         b = a**2.+a**3. 

as a test case, and the speedup is enormous. But I don't know how to proceed if I need to speed up a function inside a class.

import numba as nb def myClass(object):     def __init__(self):         self.k = 1     #@nb.jit(???,nopython=True)                                  def complicated(self,x):                                           for a in x:             b = a**2.+a**3.+self.k 

What numba type do I use for the self object? I need to have this function inside a class since it needs to access a member variable.

like image 625
dbrane Avatar asked Jan 20 '17 17:01

dbrane


People also ask

Can you use Numba in a class?

Numba supports code generation for classes via the numba. jitclass() decorator. A class can be marked for optimization using this decorator along with a specification of the types of each field.

Is Numba faster than NumPy?

For the uninitiated Numba is an open-source JIT compiler that translates a subset of Python/NumPy code into an optimized machine code using the LLVM compiler library. In short Numba makes Python/NumPy code runs faster.

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.

Can Numba work with lists?

Lists must be strictly homogeneous: Numba will reject any list containing objects of different types, even if the types are compatible (for example, [1, 2.5] is rejected as it contains a int and a float ).


1 Answers

I was in a very similar situation and I found a way to use a Numba-JITed function inside of a class.

The trick is to use a static method, since this kind of methods are not called prepending the object instance to the argument list. The downside of not having access to self is that you cannot use variables defined outside of the method. So you have to pass them to the static method from a calling method that has access to self. In my case I did not need to define a wrapper method. I just had to split the method I wanted to JIT compile into two methods.

In the case of your example, the solution would be:

from numba import jit  class MyClass:     def __init__(self):         self.k = 1      def calculation(self):         k = self.k         return self.complicated([1,2,3],k)      @staticmethod     @jit(nopython=True)                                  def complicated(x,k):                                           for a in x:             b = a**2 .+ a**3 .+ k 
like image 84
Marduk Avatar answered Oct 10 '22 21:10

Marduk