Launch CUDA kernel from OptiX

Hi,

I wonder if someone has successfully launched a CUDA kernel from OptiX. I know how to create the .cu file with the kernel code inside and modify CMake files to get it compiled to a .ptx, but I don’t know how to launch it from my .cpp file.

It would be great if someone can explain how to modify the optixHello sample to launch a dummy kernel from the optixHello.cpp

Regards

Hi @mapic,

Have you seen the OptiX Programming Guide section on OptiX-CUDA interop? https://raytracing-docs.nvidia.com/optix_6_0/guide_6_0/index.html#cuda#interoperability-with-cuda

Launching a CUDA kernel from your cpp file is done just like any CUDA program, with the triple chevrons <<< >>> assuming you’re using the CUDA runtime API.


David.

Hello @dhart,

Yes, I have checked that reference and I’m familiar with the kernel launch syntax, but I’m getting a compiler error related to the ‘<<<>>>’ syntax.

My project is based on one of the OptiX samples, so I build it with CMake. I guess only .cu files are getting compiled with nvcc, but .cpp are not, so the compiler complains. I did a little bit of research on the internet, and I found some people create an independent CUDA project in the same solution, and wrap kernel launches in functions that can be called from the c++ project. I don’t know if this is the correct way to proceed, so maybe you can shed some light.

I’m sorry if this question is more trivial than I think, but I’m still getting familiar with all of this and sometimes find it difficult to get everything properly configured.

Thanks for your help!

Ah, yes I see. The thing to know is your OptiX code and your pure CUDA code need to be compiled differently, and so your pure CUDA kernels need to be in a separate file from any OptiX programs, this is why you see people creating a wrapper function.

The OptiX SDK cmake setup has a variable that you can set for each source file that can be used to compile CUDA kernels. Then you just need to link your program with the CUDA runtime.

Here’s an example of modifying the CMakeLists.txt file for optixHello to include a CUDA kernel:

Tag the CUDA kernel for OBJ format instead of PTX (PTX is the default for

OPTIX_add_sample_executable). Do this before calling OPTIX_add_sample_executable.

set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/optixHello_cuda_kernel.cu
PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ
)

OPTIX_add_sample_executable( optixHello
optixHello.cpp
optixHello_cuda_kernel.cu
draw_color.cu
)

target_link_libraries( optixHello
${CUDA_LIBRARIES}
)

You’ll need to have the host function that uses triple chevrons inside your pure CUDA file (optixHello_cuda_kernel.cu) along with the device code that the kernel runs.

extern “C” host void
Run_OptixHello_Kernel(…)
{
OptixHello_Kernel<<<…>>>(…);
}

That’s exactly the kind of answer I was looking for, I will try it this weekend.

Thank you very much David for the great support ;)