How to use supported numpy and math functions with CUDA in Python?

2024/11/16 22:38:38

According to numba 0.51.2 documentation, CUDA Python supports several math functions. However, it doesn't work in the following kernel function:

@cuda.jit
def find_angle(angles):i, j = cuda.grid(2)if i < angles.shape[0] and j < angles.shape[1]:angles[i][j] = math.atan2(j, i)

The output:

numba.core.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
No definition for lowering <built-in function atan2>(int64, int64) -> float64

Am I using the function incorrectly?

Answer

The hint to the source of the problem is here:

No definition for lowering <built-in function atan2>(int64, int64) -> float64

The arguments returned by cuda.grid() (i.e. i, j which you are passing to atan2) are integer values because they are related to indexing.

numba can't find a version of atan2 that it can use that takes two integer arguments and returns a floating-point value:

float64 = atan2(int64, int64)

One possible solution is to convert your atan2 input arguments to match the type that numba seems to want to return from that function, which is evidently float64:

from numba import cuda, float64
import numpy
import math@cuda.jit
def find_angle(angles):i, j = cuda.grid(2)if i < angles.shape[0] and j < angles.shape[1]:angles[i][j] = math.atan2(float64(j), float64(i))block_x = 32
block_y = 32
block = (block_x, block_y)
x = 256
y = 256
grid = (x//block_x, y//block_y) # not for arbitrary x and yangles = numpy.ones((x, y), numpy.float64)
find_angle[grid, block](angles)
https://en.xdnf.cn/q/119140.html

Related Q&A

PYTHON - Remove tuple from list if contained in another list

I have a list of tuples:list_of_tuples = [(4, 35.26), (1, 48.19), (5, 90.0), (3, 90.0)]tuple[0] is an item_IDtuple[1] is an angleI have a list of item_IDs I want to remove/ignore from the list:ignore_I…

How to run a ij loop in Python, and not repeat (j,i) if (i,j) has already been done?

I am trying to implement an "i not equal to j" (i<j) loop, which skips cases where i = j, but I would further like to make the additional requirement that the loop does not repeat the perm…

Split a string with multiple delimiters

I have the string "open this and close that" and I want to obtain "open this and" and "close that". This is my best attempt:>>>print( re.split(r[ ](?=(open|clos…

Extracting a string between 2 chracters using python [duplicate]

This question already has answers here:Python-get string between to characters(4 answers)Closed 7 years ago.I need a Python regex to give me all the strings between ~ and ^ from a string like this:~~~~…

remove empty line printed from hive query output using python

i am performing a hive query and storing the output in a tsv file in the local FS. I am running a for loop for the hive query and passing different parameters. If the hive query returns no output once …

.exceptions.WebDriverException: Message: Can not connect to the Service

struggling to find a solution all over, have latest chrome 117 and also downloaded chromedriver and used the path accordingly in script also tried with chrome browser Although it opens the browser but …

How to call a previous function in a new function? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Using simpleauth to login in with GAE

This question is in the reference of this. As suggested I am using simpleauth to login via linkedin. Now I am having trouble with the redirect_uri. I have successfully deployed dev_appserver.py example…

How do i force my code to print in python

Im having trouble trying to work out an error in my code. It isnt printing the final product and leaving a blank space.playing = True string = "" Alphabet = (z,a,b, c, d, e, f, g, h, i, j, k,…

Adding specific days in python table

I have a dataset (Product_ID,date_time, Sold) which has products sold on various dates. The dates are not consistent and are given for 9 months with random 13 days or more from a month. I have to segre…