Printf in Optix SDK

I am working on the latest Optix SDK, specifically “optixPathTracer”. I am unable to use printf or other print statements inside the cuda file.
When I include stdio.h and run the program, the following error is what I get.
C:/ProgramData/NVIDIA Corporation/OptiX SDK 7.2.0/SDK/optixPathTracer/ catastrophic error: cannot open source file “stdio.h”

I am using Visual studio 2019 with Windows SDK version 10.0.
Any suggestions on how to use printf in cuda kernel would be very useful.

You cannot include MSVS host headers into device code. That only works with headers which are distinguishing between host and device code with some defines present in the respective compilers.

The printf implementation you want inside the device code must come from a CUDA header.

If you search for printf inside the CUDA toolkit include headers, you’ll find it declared inside common_functions.h which is a header you must not include directly, but instead include the header cuda_runtime.h inside your device code.

Means your OptiX device code source file should contain this:

#include <cuda_runtime.h>
#include <optix.h>

and then all OptiX and CUDA device functions should work in your device code.

The same includes can be used in host code to be able to share functions on host and device if they are properly implemented for both with __host__ __device__

If you search through the OptiX SDK 7.2.0 source code you find the cuda_runtime.h include as well in headers used on the device side, for example inside OptiX SDK 7.2.0\SDK\optixWhitted\helpers.h.

Which CUDA Toolkit version are you using?
For OptiX 7.2 I would recommend CUDA 11.1 because that’s the version it’s built with (see OptiX Release Notes).

1 Like

Thank you so much! I did not know that printf had to be included through the header <cuda_runtime.h>.
It works now.