How to use OpenMP in the Optix program?

As mentioned in the title, the CPU code of my optix program needs to be accelerated for some host programs using OpenMP. I attempted to add the option -Xcompiler -fopenmp to the OPTIONS in the CMakefile. However, it did not work. The compiler still warned that it did not recognize the OpenMP precompiled directives. As shown below

warning: ignoring ‘#pragma omp parallel’ [-Wunknown-pragmas]
  597 |   #pragma omp parallel for reduction(max:max_label)

I am using the default CMkae file provided in the SDK. I’m sorry, I’m not very familiar with CMake. Could someone offer me some assistance on how to make the OptiX program support OpenMP?Perhaps the .o file can be compiled separately using g++ (with the -fopenmp option), and then linked (but I’m not sure how to proceed with the default CMake file)

Hi @hkkzzxz24,

The flags -Xcompiler -fopenmp only help when nvcc is compiling CUDA device code. OpenMP is a host/CPU library.

Host programs that use the OptiX SDK are built as ordinary C++ targets, and there’s no special interaction between OptiX and OpenMP at all, so you can just enable OpenMP at the CMake/host level instead. In your root CMakeLists.txt, you’ll can use something like the following snippet, and remove the unnecessary -Xcompiler -fopenmpflags.

find_package(OpenMP REQUIRED)

if(OpenMP_CXX_FOUND)
    target_link_libraries(${TARGET_NAME} PRIVATE OpenMP::OpenMP_CXX)
endif()


David.

1 Like

Thank you so much! I’ve successfully used OpenMP in my project.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.