-
-
Notifications
You must be signed in to change notification settings - Fork 46
Add void* to tabulate_tensor kernel #753
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 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…
927a96e
changes to custom data test for CI
sclaus2 eaf0201
specify void* branch for dolfinx test in github actions
sclaus2 e194c15
trying to set dolfinx refs for ffcx testing for pull request
sclaus2 c8bcc8d
incorporate review suggestions
sclaus2 9c8067c
Merge branch 'main' into sclaus2/add-void-to-kernels
sclaus2 2c41005
add void* argument to test_ds_prisms
sclaus2 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # Copyright (C) 2024 Susanne Claus | ||
| # | ||
| # This file is part of FFCx. (https://www.fenicsproject.org) | ||
| # | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
|
|
||
| def test_tabulate_tensor_integral_add_values(): | ||
| pytest.importorskip("cffi") | ||
|
|
||
| from cffi import FFI | ||
|
|
||
| # Define custom tabulate tensor function in C with a struct | ||
| # Step 1: Define the function in C and set up the CFFI builder | ||
| ffibuilder = FFI() | ||
| ffibuilder.set_source( | ||
| "_cffi_kernelA", | ||
| r""" | ||
| typedef struct { | ||
| size_t size; | ||
| double* values; | ||
| } cell_data; | ||
|
|
||
| void tabulate_tensor_integral_add_values(double* restrict A, | ||
| const double* restrict w, | ||
| const double* restrict c, | ||
| const double* restrict coordinate_dofs, | ||
| const int* restrict entity_local_index, | ||
| const uint8_t* restrict quadrature_permutation, | ||
| void* custom_data) | ||
| { | ||
| // Cast the void* custom_data to cell_data* | ||
| cell_data* custom_data_ptr = (cell_data*)custom_data; | ||
|
|
||
| // Access the custom data | ||
| size_t size = custom_data_ptr->size; | ||
| double* values = custom_data_ptr->values; | ||
|
|
||
| // Use the values in your computations | ||
| for (size_t i = 0; i < size; i++) { | ||
| A[0] += values[i]; | ||
| } | ||
| } | ||
| """, | ||
| ) | ||
| ffibuilder.cdef( | ||
| """ | ||
| typedef struct { | ||
| size_t size; | ||
| double* values; | ||
| } cell_data; | ||
|
|
||
| void tabulate_tensor_integral_add_values(double* restrict A, | ||
| const double* restrict w, | ||
| const double* restrict c, | ||
| const double* restrict coordinate_dofs, | ||
| const int* restrict entity_local_index, | ||
| const uint8_t* restrict quadrature_permutation, | ||
| void* custom_data); | ||
| """ | ||
| ) | ||
|
|
||
| # Step 2: Compile the C code | ||
| ffibuilder.compile(verbose=True) | ||
|
|
||
| # Step 3: Import the compiled library | ||
| from _cffi_kernelA import ffi, lib | ||
|
|
||
| # Define cell data | ||
| values = np.array([2.0, 1.0], dtype=np.float64) | ||
| size = len(values) | ||
| expected_result = np.array([3.0], dtype=np.float64) | ||
|
|
||
| # Define the input arguments | ||
| A = np.zeros(1, dtype=np.float64) | ||
| w = np.array([1.0], dtype=np.float64) | ||
| c = np.array([0.0], dtype=np.float64) | ||
| coordinate_dofs = np.array( | ||
| [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0], dtype=np.float64 | ||
| ) | ||
| entity_local_index = np.array([0], dtype=np.int32) | ||
| quadrature_permutation = np.array([0], dtype=np.uint8) | ||
|
|
||
| # Cast the arguments to the appropriate C types | ||
| A_ptr = ffi.cast("double*", A.ctypes.data) | ||
| w_ptr = ffi.cast("double*", w.ctypes.data) | ||
| c_ptr = ffi.cast("double*", c.ctypes.data) | ||
| coordinate_dofs_ptr = ffi.cast("double*", coordinate_dofs.ctypes.data) | ||
| entity_local_index_ptr = ffi.cast("int*", entity_local_index.ctypes.data) | ||
| quadrature_permutation_ptr = ffi.cast("uint8_t*", quadrature_permutation.ctypes.data) | ||
|
|
||
| # Use ffi.from_buffer to create a CFFI pointer from the NumPy array | ||
| values_ptr = ffi.cast("double*", values.ctypes.data) | ||
|
|
||
| # Allocate memory for the struct | ||
| custom_data = ffi.new("cell_data*") | ||
| custom_data.size = size | ||
| custom_data.values = values_ptr | ||
|
|
||
| # Cast the struct to void* | ||
| custom_data_ptr = ffi.cast("void*", custom_data) | ||
|
|
||
| # Call the function | ||
| lib.tabulate_tensor_integral_add_values( | ||
| A_ptr, | ||
| w_ptr, | ||
| c_ptr, | ||
| coordinate_dofs_ptr, | ||
| entity_local_index_ptr, | ||
| quadrature_permutation_ptr, | ||
| custom_data_ptr, | ||
| ) | ||
|
|
||
| # Assert the result | ||
| np.testing.assert_allclose(A, expected_result, rtol=1e-5) |
This file contains hidden or 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.
There is a bit more complexity possible in practice - instead, I would simply refer the reader to https://numpy.org/doc/stable/reference/arrays.ndarray.html#internal-memory-layout-of-an-ndarray