Calling a device function from a RT_PROGRAM

I want to call a host/device function from a program. The function and the program are defined on different files. For example:

class A {

  __host__ __device__ void f();

#include  "A.h"

RT_PROGRAM void program() {
   A a(...);


The only way I found to do this is putting the function definition in the header file. Is there a better way to do this?


These functions are inlined by OptiX anyway. Putting the implementation inside headers is fine.

For these functions I’m always using forceinline device to be sure that the CUDA compiler actually inlines them. I use a
#define RT_FUNCTION forceinline device
for these to match the look to OptiX RT_PROGRAM and RT_CALLABLE_PROGRAM notions.

When using just inline that’s only a hint and depending on the code size or number of function arguments the compiler sometimes generates a call which wasn’t handled by OptiX in the past. In the meantime it’s able to inline PTX call instructions. Won’t harm to help the compiler with the decision.

Real function calls in OptiX are done via its callable programs mechanism.
If you need to implement something like a virtual function table, that isn’t going to work with standard C++ mechanisms in OptiX, but you can build a buffer of bindless callable program IDs which is effectively your function table and you can pick any of the functions simply by indexing into that buffer.