Search code examples
cudagpgpu

CUDA placement new and virtual functions


I'm in a situation where I desperately need virtual functions in a kernel. I know that you can't use objects created in host memory in kernels if they have virtual functions.

If i use placement new with a device pointer allocated by cudaMallocManaged, can I pass that to a kernel and use its virtual functions properly?

The reason I'm asking and not just trying it is because I have a load of other issues with implementing this, and was wondering if it was even possible, because if it isn't I could try the more complicated, but definitely working method explained here.


Solution

  • If i use placement new with a device pointer allocated by cudaMallocManaged, can I pass that to a kernel and use its virtual functions properly?

    No, it won't work. The host constructor that gets called in that case will initialize the object as if it were a host object, and the virtual functions in that object will not work correctly if invoked in device code.