numba cuda deprecation error : how to update my code?

2024/7/7 5:33:34

Im running a jupyter notebook frome here : https://github.com/noahgift/nuclear_powered_command_line_tools/blob/master/notebooks/numba-cuda.ipynb

The docs of current numba/cuda is here : https://numba.readthedocs.io/en/stable/cuda-reference/kernel.html#kernel-declaration

Im running this line in numba.cuda in python :

from numba import cuda
from numba import *

I have a mandel function as follow:

@jit
def mandel(x, y, max_iters):"""Given the real and imaginary parts of a complex number,determine if it is a candidate for membership in the Mandelbrotset given a fixed number of iterations."""c = complex(x, y)z = 0.0jfor i in range(max_iters):z = z*z + cif (z.real*z.real + z.imag*z.imag) >= 4:return ireturn max_iters

I run cuda with

mandel_gpu = cuda.jit(func_or_sig=mandel,device=True)
# previously  mandel_gpu = cuda.jit(restype=uint32, argtypes=[f8, f8, uint32], device=True)(mandel)

with mandel defined as

  @cuda.jit#(argtypes=[f8, f8, f8, f8, uint8[:,:], uint32])
def mandel_kernel(min_x, max_x, min_y, max_y, image, iters):height = image.shape[0]width = image.shape[1]pixel_size_x = (max_x - min_x) / widthpixel_size_y = (max_y - min_y) / heightstartX, startY = cuda.grid(2)gridX = cuda.gridDim.x * cuda.blockDim.x;gridY = cuda.gridDim.y * cuda.blockDim.y;for x in range(startX, width, gridX):real = min_x + x * pixel_size_xfor y in range(startY, height, gridY):imag = min_y + y * pixel_size_y image[y, x] = mandel_gpu(real, imag, iters)

I get then the following call :

gimage = np.zeros((1024, 1536), dtype = np.uint8)
blockdim = (32, 8)
griddim = (32,16)start = timer()
d_image = cuda.to_device(gimage)
mandel_kernel[griddim, blockdim](-2.0, 1.0, -1.0, 1.0, d_image, 20) 
d_image.to_host()
dt = timer() - startprint("Mandelbrot created on GPU in %f s" % dt)imshow(gimage)
show()

I get then the error :

    ---------------------------------------------------------------------------NotImplementedError                       Traceback (most recent call last)/usr/local/lib/python3.7/dist-packages/numba/core/errors.py in new_error_context(fmt_, *args, **kwargs)822     try:
--> 823         yield824     except NumbaError as e:33 framesNotImplementedError: No definition for lowering <built-in method mandel of _dynfunc._Closure object at 0x7efde36f8590>(float64, float64, int64) -> int64During handling of the above exception, another exception occurred:LoweringError                             Traceback (most recent call last)/usr/local/lib/python3.7/dist-packages/numba/core/errors.py in new_error_context(fmt_, *args, **kwargs)835             else:836                 tb = None
--> 837             raise newerr.with_traceback(tb)838         elif use_new_style_errors():839             raise eLoweringError: Failed in cuda mode pipeline (step: native lowering)
No definition for lowering <built-in method mandel of _dynfunc._Closure object at 0x7efde36f8590>(float64, float64, int64) -> int64File "<ipython-input-14-82f426a4058e>", line 17:
def mandel_kernel(min_x, max_x, min_y, max_y, image, iters):<source elided>imag = min_y + y * pixel_size_y image[y, x] = mandel_gpu(real, imag, iters)^During: lowering "$160call_function.12 = call $152load_global.8(real, imag, iters, func=$152load_global.8, args=[Var(real, <ipython-input-14-82f426a4058e>:14), Var(imag, <ipython-input-14-82f426a4058e>:16), Var(iters, <ipython-input-14-82f426a4058e>:3)], kws=(), vararg=None, varkwarg=None, target=None)" at <ipython-input-14-82f426a4058e> (17)

I have tried to update the code with the comments stating the updated api doesnt take argtypes and restypes but i still get an error and i have no clues how to correct it... Thank you Thanks for your help

Answer

I would recommend taking a few minutes to review how python decorators work, because this is at the core of your problem.

When you do this:

@jit
def mandel(x, y, max_iters):...

what is happening is this:

def mandel(x, y, max_iters):...mandel = jit(mandel)

i.e. your original function is replaced by a Numba processed, jit compiled version of your original Python code. Plainly the Numba GPU compiler can't compile a previously Numba compiled function, thus the error when you try and compile a device function from that already compiled code.

If you did something like this:

def mandel(x, y, max_iters):...mandel_gpu = cuda.jit(mandel, device=True)
mandel = jit(mandel)

I suspect the compilation would work correctly (although I haven't tried it, that is up to you).

https://en.xdnf.cn/q/120306.html

Related Q&A

reverse nested dicts using python

I already referred these posts here, here and here. I have a sample dict like as shown below t = {thisdict:{"brand": "Ford","model": "Mustang","year": …

python how to generate permutations of putting a singular character into a word

No idea how to word this so the title sucks my bad, Basically, I have a 4 letter word and I want to generate every permutation of putting a dash in it. So if my word was Cats, I want to get every permu…

Selenium Scraping Javascript Table

I am stuggling to scrape as per code below. Would apprciate it if someone can have a look at what I am missing? Regards PyProg70from selenium import webdriver from selenium.webdriver import FirefoxOp…

PYTHON REGEXP to replace recognized pattern with the pattern itself and the replacement?

Text- .1. This is just awesome.2. Google just ruined Apple.3. Apple ruined itself! pattern = (dot)(number)(dot)(singlespace)Imagine you have 30 to 40 sentences with paragraph numbers in the above patt…

How can I extract the text between a/a? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How do I access classes and get a dir() of available actions?

I have been trying to get access to available functions for a Match Object from re.search. I am looking for a way to do that similar to how I could do dir(str) and I can find .replace.This is my dir() …

Python - IndexError: list index out of range

Why would data[entities][urls][0][expanded_url] would produce IndexError: list index out of range error? I understand what this error means but cant see why? perhaps too sleepy at 2 am? Please helpd…

Python: Use Regular expression to remove something

Ive got a string looks like thisABC(a =2,b=3,c=5,d=5,e=Something)I want the result to be likeABC(a =2,b=3,c=5)Whats the best way to do this? I prefer to use regular expression in Python.Sorry, somethi…

Python delete row in file after reading it

I python 2.7 I am reading data from file in while loop. When I successfully read row, I would like to delete this row from a file, but I dont know how to do it - Efficient way so i dont waste to much o…

Trying to keep the same type after saving a dataframe in a csv file

When I try to get my dataframe out of the csv file the type of the data changed. Is there a way I can avoid this?