Skip to content
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

Function pointers #247

Open
pjaaskel opened this issue Nov 4, 2022 · 2 comments
Open

Function pointers #247

pjaaskel opened this issue Nov 4, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@pjaaskel
Copy link
Collaborator

pjaaskel commented Nov 4, 2022

I started testing CHIP-SPV with rocPRIM. It built pretty easily with some minor fixes, but then I encountered the need for device side function pointers and passing function pointers as arguments to kernel functions. This is the basic pattern of templated algorithm/pattern skeletons where the actual applied function is passed as an argument. We can attempt to implement the function pointers by using the Intel extension. It seems to already generate code that uses it for the device part, but what seems to be missing is the host side function address taking functionality for device side functions so we can pass correct values from the host to the device side.

A slightly more portable (but incomplete) implementation for this particular case is to see if we can utilize SPIR-V specialization and specialize the call with the function pointer(s) passed to it and rely on constant propagation to remove the indirection. I used a similar concept in this experiment.

@pjaaskel pjaaskel added the enhancement New feature or request label Nov 4, 2022
@pjaaskel
Copy link
Collaborator Author

pjaaskel commented Nov 4, 2022

A basic test case:

#include <hip/hip_runtime.h>

__host__ __device__ void fptr() {
  printf("Hello function pointer world!\n");
}

typedef void (*FPointer)(void);

__global__ void fptr_call(FPointer ptr) {
  ptr();  
}


int main() {
  hipLaunchKernelGGL(fptr_call, dim3(1), dim3(1), 0, 0, fptr);
  return EXIT_SUCCESS;
}

@pjaaskel
Copy link
Collaborator Author

pjaaskel commented Nov 8, 2022

There was a misunderstanding from my part: CUDA/HIP doesn't support "univeral function pointers" (like I assumed would be the semantics with __host__ __device__ functions). Indirect calls should work only on pointers taken at device side.

Thus, likely rocPRIM does away with all the functor cases with template specialization converting them to direct calls to device side functions.

Keeping this open to track the __device__ side fptr implementation still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants