Add a usecase discussion of how to deal with function pointers#577
Open
crtrott wants to merge 1 commit into
Open
Add a usecase discussion of how to deal with function pointers#577crtrott wants to merge 1 commit into
crtrott wants to merge 1 commit into
Conversation
38b379e to
dbbe563
Compare
JBludau
reviewed
Sep 4, 2024
Contributor
JBludau
left a comment
There was a problem hiding this comment.
I think it is a good addition
| I can use the function pointer in a host parallel_for! | ||
| foo called from host | ||
| Now I will crash if we compiled for CUDA/HIP | ||
| cudaDeviceSynchronize() error( cudaErrorIllegalAddress): an illegal memory access was encountered /home/crtrott/Kokkos/kokkos/core/src/Cuda/Kokkos_Cuda_Instance.cpp:153 |
Contributor
There was a problem hiding this comment.
Suggested change
| cudaDeviceSynchronize() error( cudaErrorIllegalAddress): an illegal memory access was encountered /home/crtrott/Kokkos/kokkos/core/src/Cuda/Kokkos_Cuda_Instance.cpp:153 | |
| cudaDeviceSynchronize() error( cudaErrorIllegalAddress): an illegal memory access was encountered /kokkos/core/src/Cuda/Kokkos_Cuda_Instance.cpp:153 |
| }; | ||
| ``` | ||
|
|
||
| This class is templated on the function pointer type, contains a pointer for both the host and the device version, and has a templated operator that forwards all the arguments, and calls the appropriate function pointer depending on call site. |
Contributor
There was a problem hiding this comment.
Suggested change
| This class is templated on the function pointer type, contains a pointer for both the host and the device version, and has a templated operator that forwards all the arguments, and calls the appropriate function pointer depending on call site. | |
| This class is templated on the function pointer type, contains a pointer for both the host and the device version, and has a templated operator that copies all the arguments, and calls the appropriate function pointer depending on the call site. |
I find it kind of important to make the distinction here as it adds another drawback.
| }; | ||
| ``` | ||
|
|
||
| However we still need to initialize the two function pointers, using the device initialization approach from before, and also initializing the host side. Note: the order of initialization and `deep_copy` matters, because `deep_copy` will overwrite both member poitners in our wrapper class: |
Contributor
There was a problem hiding this comment.
Suggested change
| However we still need to initialize the two function pointers, using the device initialization approach from before, and also initializing the host side. Note: the order of initialization and `deep_copy` matters, because `deep_copy` will overwrite both member poitners in our wrapper class: | |
| However we still need to initialize the two function pointers using the device initialization approach from before, and also initializing the host side. Note: the order of initialization and `deep_copy` matters, because `deep_copy` will overwrite both member poitners in our wrapper class: |
| ``` | ||
|
|
||
| While this generally works (and note this code will also work if you simply compile for host-only) there may be architectures in the future where this fails. | ||
| Also we in principle strongly discourage the use of function pointers - the amount of possible problems you run into is fairly significant. |
Contributor
There was a problem hiding this comment.
Suggested change
| Also we in principle strongly discourage the use of function pointers - the amount of possible problems you run into is fairly significant. | |
| Also we strongly discourage the use of function pointers - the amount of possible problems you run into is fairly significant. |
I would just make that unconditional
masterleinad
reviewed
Sep 4, 2024
Comment on lines
+24
to
+25
| KOKKOS_IF_ON_HOST(printf("foo called from host\n");) | ||
| KOKKOS_IF_ON_DEVICE(printf("foo called from device\n");) |
Contributor
There was a problem hiding this comment.
Suggested change
| KOKKOS_IF_ON_HOST(printf("foo called from host\n");) | |
| KOKKOS_IF_ON_DEVICE(printf("foo called from device\n");) | |
| KOKKOS_IF_ON_HOST(Kokkos::printf("foo called from host\n");) | |
| KOKKOS_IF_ON_DEVICE(Kokkos::printf("foo called from device\n");) |
Comment on lines
+37
to
+38
| KOKKOS_IF_ON_HOST(printf("foo called from host\n");) | ||
| KOKKOS_IF_ON_DEVICE(printf("foo called from device\n");) |
Contributor
There was a problem hiding this comment.
Suggested change
| KOKKOS_IF_ON_HOST(printf("foo called from host\n");) | |
| KOKKOS_IF_ON_DEVICE(printf("foo called from device\n");) | |
| KOKKOS_IF_ON_HOST(Kokkos::printf("foo called from host\n");) | |
| KOKKOS_IF_ON_DEVICE(Kokkos::printf("foo called from device\n");) |
| Aborted (core dumped) | ||
| ``` | ||
|
|
||
| *The function pointer we created was for a host function: you can not use it on device same as you can't dereference data pointers to host data!* |
Contributor
There was a problem hiding this comment.
Suggested change
| *The function pointer we created was for a host function: you can not use it on device same as you can't dereference data pointers to host data!* | |
| *The function pointer we created was for a host function: you can not use it on the device just like you can't dereference data pointers to host data!* |
| Kokkos::deep_copy(A, A_v); | ||
| ``` | ||
|
|
||
| We are leveraging here the fact that `deep_copy` allows you to copy from and to a host scalar value, from a `View` of rank-0. |
Contributor
There was a problem hiding this comment.
Suggested change
| We are leveraging here the fact that `deep_copy` allows you to copy from and to a host scalar value, from a `View` of rank-0. | |
| We are leveraging here the fact that `deep_copy` allows you to copy in both direction between a host scalar value and a `View` of rank-0. |
|
|
||
| We are leveraging here the fact that `deep_copy` allows you to copy from and to a host scalar value, from a `View` of rank-0. | ||
|
|
||
| If we do this `A` will contain a function pointer to a device function. That means we can capture `A` into a parallel region and execute it on the device, but now it will crash on the host. |
Contributor
There was a problem hiding this comment.
Suggested change
| If we do this `A` will contain a function pointer to a device function. That means we can capture `A` into a parallel region and execute it on the device, but now it will crash on the host. | |
| If we do this, `A` will contain a function pointer to a device function. That means we can capture `A` into a parallel region and execute it on the device, but now it will crash on the host. |
Comment on lines
+125
to
+126
| KOKKOS_IF_ON_HOST(printf("foo called from host\n");) | ||
| KOKKOS_IF_ON_DEVICE(printf("foo called from device\n");) |
Contributor
There was a problem hiding this comment.
Suggested change
| KOKKOS_IF_ON_HOST(printf("foo called from host\n");) | |
| KOKKOS_IF_ON_DEVICE(printf("foo called from device\n");) | |
| KOKKOS_IF_ON_HOST(Kokkos::printf("foo called from host\n");) | |
| KOKKOS_IF_ON_DEVICE(Kokkos::printf("foo called from device\n");) |
|
|
||
| ## Creating a dual function pointer object | ||
|
|
||
| To do better than this we need something which contains both the device function pointer and the host function pointer. |
Contributor
There was a problem hiding this comment.
Suggested change
| To do better than this we need something which contains both the device function pointer and the host function pointer. | |
| To do better than this, we need something which contains both the device function pointer and the host function pointer. |
| }; | ||
| ``` | ||
|
|
||
| However we still need to initialize the two function pointers, using the device initialization approach from before, and also initializing the host side. Note: the order of initialization and `deep_copy` matters, because `deep_copy` will overwrite both member poitners in our wrapper class: |
Contributor
There was a problem hiding this comment.
Suggested change
| However we still need to initialize the two function pointers, using the device initialization approach from before, and also initializing the host side. Note: the order of initialization and `deep_copy` matters, because `deep_copy` will overwrite both member poitners in our wrapper class: | |
| However, we still need to initialize the two function pointers, using the device initialization approach from before, and also initializing the host side. Note: the order of initialization and `deep_copy` matters, because `deep_copy` will overwrite both member poitners in our wrapper class: |
| } | ||
| ``` | ||
|
|
||
| While this generally works (and note this code will also work if you simply compile for host-only) there may be architectures in the future where this fails. |
Contributor
There was a problem hiding this comment.
Suggested change
| While this generally works (and note this code will also work if you simply compile for host-only) there may be architectures in the future where this fails. | |
| While this generally works (and note this code will also work if you simply compile for host-only), there may be architectures in the future where this fails. SYCL (and therefore the SYCL backend) doesn't allow using function pointers on the device for example. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Here is the rendered version: https://github.com/crtrott/kokkos-core-wiki/blob/function-pointers/docs/source/usecases/FunctionPointers.md