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

Add a mechanism for making device function pointers usable on the host side #94

Open
eyalroz opened this issue Jul 8, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request question Further information is requested

Comments

@eyalroz
Copy link
Owner

eyalroz commented Jul 8, 2021

It is not possible to naively use a __device__-function's pointer in host-side code. However, it is possible to use it if you copy its address from a global device-side variable which holds the address, like so:

__device__ void foo(int x) { /*... */ }
__device__ void (*ptr_on_device) (int x) = foo;

void bar() {
  void  (*ptr_on_host)(int x);
  cuda::memory::region_t pod_region = cuda::memory::locate(ptr_on_device);
  cuda::memory::copy(&ptr_on_host, pod_region.data(), pod_region.size());
  // or using raw CUDA API calls:
  // cudaMemcpyFromSymbol(&ptr_on_host, ptr_on_device, sizeof(void (*)(int)));
  // ... and check the error

  // Now do stuff with ptr_on_host
}

See also this question on StackOverflow.

Perhaps we could add a mechanism abstracting the above to the library. While it would be host-side code mostly, passing device-side function pointers to kernels is definitely a useful "tool" for kernel authors to have.

@eyalroz eyalroz added enhancement New feature or request question Further information is requested labels Jul 8, 2021
@eyalroz eyalroz self-assigned this Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant