__init__() got an unexpected keyword argument 'py_func'

Viewed 7234

We will use the numba.jit decorator for the function we want to compute over the GPU. The decorator has several parameters but we will work with only the target parameter. Target tells the jit to compile codes for which source(“CPU” or “Cuda”). “Cuda” corresponds to GPU. However, if CPU is passed as an argument then the jit tries to optimize the code run faster on CPU and improves the speed too. But i got the error -- init() got an unexpected keyword argument 'py_func'

from numba import jit, cuda 
import numpy as np 
# to measure exec time 
from timeit import default_timer as timer    

# normal function to run on cpu 
def func(a):                                 
    for i in range(10000000): 
        a[i]+= 1      

# function optimized to run on gpu  
@jit(target ="cuda")                          
def func2(a): 
    for i in range(10000000): 
        a[i]+= 1
if __name__=="__main__": 
    n = 10000000                            
    a = np.ones(n, dtype = np.float64) 
    b = np.ones(n, dtype = np.float32) 
  
    start = timer() 
    func(a) 
    print("without GPU:", timer()-start)     
  
    start = timer() 
    func2(a) 
    print("with GPU:", timer()-start) 
4 Answers

The numba.jit generator does not have a target parameter, as far as I can see. I believe you need @cuda.jit.

@cuda.jit worked

@cuda.jit
def func2a(a):
    # Thread id in a 1D block
    tx = cuda.threadIdx.x
    # Block id in a 1D grid
    ty = cuda.blockIdx.x
    # Block width, i.e. number of threads per block
    bw = cuda.blockDim.x
    # Compute flattened index inside the array
    pos = tx + ty * bw
    if pos < a.size:  # Check array boundaries
        a[pos]+= 1
    
    start = timer()
    # func2(a)
    threadsperblock = 32
    blockspergrid = (a.size + (threadsperblock - 1)) // threadsperblock
    func2a[blockspergrid, threadsperblock](a)
    print("with GPU:", timer()-start)

without GPU: 3.2932322000001477 with GPU: 0.15669049999996787

from numba import jit, cuda
import numpy as np
# to measure exec time
from timeit import default_timer as timer

# normal function to run on cpu
def func(a):
for i in range(10000000):
    a[i]+= 1

# function optimized to run on gpu
def func2(a):
for i in range(10000000):
    a[i]+= 1
if __name__=="__main__":
n = 10000000
a = np.ones(n, dtype = np.float64)
b = np.ones(n, dtype = np.float32)

start = timer()
func(a)
print("without GPU:", timer()-start)    

start = timer()
func2(a)
print("with GPU:", timer()-start)

without GPU: 13.218794100000196 with GPU: 13.509453999999096

Related