-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add void* to tabulate_tensor kernel #749
Closed
Closed
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
37a62bb
add void* to tabulate_tensor
11cb033
try to trigger CI
bb4e90a
try to trigger CI
49b23a0
run ruff
bb022ad
rename user_data custom_data
9ab688c
add numba functions to obtain empty void* and conversion of numpy arr…
52104cb
fix ruff check
7beaeef
add line to remove noqa
6ad0d88
Merge branch 'main' into sclaus/add-void-to-kernels
mscroggs abe2391
expand comment for numba intrinsic function
7914fbe
add test to use a struct in C-function similar to tabulate_tensor usi…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,59 @@ 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. | ||
""" | ||
|
||
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. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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?