Question about add a .cu in my project

When I want to add a new .cu in my project for postprocessing,there will be a compilation error.
My new files likes this:
and the codes like this:

extern “C” device void post_Process_gpu();

extern “C” global void postProcess_gpu();

include <cuda/ImageProcess_cuda.h>
include <cuda_runtime.h>
include <device_launch_parameters.h>

extern “C” device void post_Process_gpu() {
int thisID = threadIdx.x;
printf(“调用成功 %d\n”, thisID);

extern “C” global void postProcess_gpu() {
post_Process_gpu <<<1, 1 >>> ();

The function post_Process_gpu is where the error is when it be used.

The information of the error is:

CMake Warning (dev) at CMake/FindCUDA.cmake:535 (if):
Policy CMP0054 is not set: Only interpret if() arguments as variables or
keywords when unquoted. Run “cmake --help-policy CMP0054” for policy
details. Use the cmake_policy command to set the policy and suppress this

Quoted variables like "MSVC" will no longer be dereferenced when the policy
is set to NEW.  Since the policy is not set the OLD behavior will be used.

Call Stack (most recent call first):
CMakeLists.txt:157 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.

ptx_files = C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./;C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./;C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./;C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./;C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./
– Could NOT find Vulkan (missing: VULKAN_LIBRARY VULKAN_INCLUDE_DIR)
– Using Win32 for window creation
– Configuring done
– Generating done
– Build files have been written to: C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug
[0/1] Re-running CMake…
[1/3] Building NVCC optixir file lib/ptx/
FAILED: lib/ptx/ C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/
cmd.exe /C “cd /D “C:\ProgramData\NVIDIA Corporation\OptiX SDK 8.0.0\SDK\out\build\x64-Debug\OvaSim\CMakeFiles\OvaSim.dir__\cuda” && “C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe” -E make_directory “C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/.” && “C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe” -D verbose:BOOL=OFF -D check_dependencies:BOOL=OFF -D build_configuration:STRING=Debug -D “generated_file:STRING=C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./” -D “generated_cubin_file:STRING=C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./” -D “generated_fatbin_file:STRING=C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/lib/ptx/./” -P “C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/out/build/x64-Debug/OvaSim/CMakeFiles/OvaSim.dir/__/cuda/””
C:\ProgramData\NVIDIA Corporation\OptiX SDK 8.0.0\SDK\cuda\ error : a device function call cannot be configured
post_Process_gpu <<<1, 1 >>> ();

1 error detected in the compilation of “C:/ProgramData/NVIDIA Corporation/OptiX SDK 8.0.0/SDK/cuda/”.
CMake Error at (message):
Error generating file C:/ProgramData/NVIDIA Corporation/OptiX SDK

ninja: build stopped: subcommand failed.

I am very grateful that you will help me solve this problem !

The OptiX SDK examples are usually assuming that all *.cu files inside the project are OptiX device code.
These are only translated to OptiX module input code which is either PTX source code or OptiX-IR binary code.

The only exception is the optixRayCasting example which demonstrates how to use OptiX only for ray-intersection tests and does ray generation and shading calculations in native CUDA kernels.

Please note that none of the OptiX device functions can be used inside native CUDA kernels.

When you want to use standalone CUDA kernels inside OptiX programs, how that can be integrated into OptiX applications depends on the CUDA API you’re using (runtime, driver).

1.) When using the CUDA Driver API (which the OptiX SDK examples are not doing) then you could compile your kernel to PTX and then compile that at runtime and use the kernel functions inside that.
I’m doing that for a simple multi-GPU compositing kernel in my OptiX examples:
That cuLaunchKernel() mechanism is quite awkward and care needs to be taken to not exceed any hardware limits.

2.) The simpler approach is using the CUDA Runtime API and its kernel launch mechanism with the chevron operator <<<>>> as in your test code.

The main hurdle with that is how to setup a CMakeLists.txt which can handle native CUDA kernels and OptiX device code separately.
That is possible in different ways and with the newer CMake versions that is actually supported with the CMake LANGUAGE CUDA feature as well.

I’ve posted a CMakeLists.txt which shows that here:

Note that this requires CMake 3.27 or newer. Just follow the instructions and you should have a standalone CUDA Runtime API + OptiX device code framework using CMake.

Calling CUDA kernels inside the resulting application then works the same way as in all CUDA Toolkit examples, like this:

And OptiX host and device code would work as before.

You would only need to make sure to use the correct CUDA context and CUDA stream for these kernels to have them working on the same CUDA device data.

1 Like

Sorry,i try the second method but failed…
my cmakelists.txt has be changed like this:

find_package(CUDAToolkit 12.0 REQUIRED)

add_library(imageprocess ImageProcess_cuda.h)
target_compile_features(imageprocess PUBLIC cxx_std_11)

OPTIX_add_sample_executable( OvaSim target_name
OPTIONS -rdc true
target_link_libraries( ${target_name}
target_link_libraries( ${target_name}

It still doesn’t seem to compile the extra .cu files correctly. I don’t know what the problem is. I’m still a rookie to cmake and cuda.

I don’t want to use the find_package (optix) framework at the moment, because changing the current project may have a bigger problem.

You did not at all try the second method, because then your CMakeLists.txt wouldn’t have any remainders of the OptiX SDK CMakeLists.txt. (e.g. the OPTIX_add_sample_executable macro)

I repeat:

If you’re planning to add an example program inside the OptiX SDK application framework which should use CUDA native kernels and OptiX device code, look at the OptiX SDK 8.0.0/SDK/optixRaycasting/CMakeLists.txt file.
Specifically take a good look at this statement which changes only the translation to CUDA object code.


I would not recommend that if you plan to ever give that application to somebody else as binary, because OptiX SDK example executables only work in the location where they have been built due to some hardcoded paths inside the executable for the module input code and possible data (textures, etc). which requires two environment variables to be set when running these executables in another location. Deploying OptiX SDK example programs as standalone programs is not what that application framework is meant for.

Now, if you want to develop a standalone CUDA Runtime API application framework with the CMake native support for the CUDA language and also translate OptiX device code to PTX or OptiX-IR code in the same project, then follow the instructions I posted in the linked thread above by the letter. That is, the part after “If you want to build a CMake project for OptiX from scratch, I would recommend the following:” in that post to get the necessary dependencies (*.cmake scripts and root CMakeLists.txt).

You must use the whole CMakeLists.txt I posted there and add the necessary parts from the root CMakeLists.txt in my example framework into it, esp. the statement setting the CMAKE_MODULE_PATH and the find_package(OptiX80)

Then exchange the list of files in all these set(NAME files) sections with the names HEADERS_HOST, SOURCES_HOST, HEADERS_CUDA, SOURCES_CUDA, HEADERS_OPTIX, SOURCES_OPTIX with the files you want.

For that you better copy all necessary files your application consists of into a local folder where you want your project to live (not inside the OptiX SDK).

Then try getting that CMakeLists.txt to work and produce a solution which builds.

If there is any issue reported by CMake which is not about missing files, read the CMake documentation.

I don’t want to use the find_package (optix) framework at the moment, because changing the current project may have a bigger problem.

All that find_package does, is just finding the desired OptiX SDK version’s include directory.

Inside the OptiX SDK examples that is a relative location. Look for OptiX_INCLUDE inside the OptiX SDK root CMakeLists.txt:

# Search for the OptiX libraries and include files.
find_package(OptiX REQUIRED)

# Add the path to the OptiX headers to our include paths.