-
-
Notifications
You must be signed in to change notification settings - Fork 45
Add void* to tabulate_tensor kernel #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
37a62bb
11cb033
bb4e90a
49b23a0
bb022ad
9ab688c
52104cb
7beaeef
6ad0d88
abe2391
7914fbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,11 @@ | |
import numpy as np | ||
import numpy.typing as npt | ||
|
||
try: | ||
import numba | ||
except ImportError: | ||
numba = None | ||
|
||
|
||
def dtype_to_c_type(dtype: typing.Union[npt.DTypeLike, str]) -> str: | ||
"""For a NumPy dtype, return the corresponding C type. | ||
|
@@ -80,6 +85,58 @@ def numba_ufcx_kernel_signature(dtype: npt.DTypeLike, xdtype: npt.DTypeLike): | |
types.CPointer(from_dtype(xdtype)), | ||
types.CPointer(types.intc), | ||
types.CPointer(types.uint8), | ||
types.CPointer(types.void), | ||
) | ||
except ImportError as e: | ||
raise e | ||
|
||
|
||
if numba is not None: | ||
|
||
@numba.extending.intrinsic | ||
def empty_void_pointer(typingctx): | ||
"""Custom intrinsic to return an empty void* pointer. | ||
This function creates a void pointer initialized to null (0). | ||
This is used to pass a nullptr to the UFCx tabulate_tensor interface. | ||
|
||
Args: | ||
typingctx: The typing context. | ||
|
||
Returns: | ||
A Numba signature and a code generation function that returns a void pointer. | ||
""" # noqa: D205 | ||
|
||
def codegen(context, builder, signature, args): | ||
null_ptr = context.get_constant(numba.types.voidptr, 0) | ||
return null_ptr | ||
|
||
sig = numba.types.voidptr() | ||
return sig, codegen | ||
|
||
@numba.extending.intrinsic | ||
def get_void_pointer(typingctx, arr): | ||
"""Custom intrinsic to get a void* pointer from a NumPy array. | ||
|
||
This function takes a NumPy array and returns a void pointer to the array's data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually don't know how numpy lays out its data in an ndarray - could we be a bit more precise here on what this void ptr points to? |
||
This is used to pass custom data organised in a NumPy array | ||
to the UFCx tabulate_tensor interface. | ||
|
||
Args: | ||
typingctx: The typing context. | ||
arr: The NumPy array to get the void pointer from. | ||
|
||
Returns: | ||
A Numba signature and a code generation function that returns a void pointer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, more precision on array's data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have expanded on the comment and I have added a test. |
||
to the array's data. | ||
""" | ||
if not isinstance(arr, numba.types.Array): | ||
raise TypeError("Expected a NumPy array") | ||
|
||
def codegen(context, builder, signature, args): | ||
[arr] = args | ||
raw_ptr = numba.core.cgutils.alloca_once_value(builder, arr) | ||
void_ptr = builder.bitcast(raw_ptr, context.get_value_type(numba.types.voidptr)) | ||
return void_ptr | ||
|
||
sig = numba.types.voidptr(arr) | ||
return sig, codegen |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be enough to add a new line after
Custom intrinsic to return an empty void* pointer.
to drop thenoqa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. Done.