This sample demonstrates how to conditionaly disable "target" portion of OpenMP directives, so that it falls back to CPU OpenMP multithreading.
This approach follows SC17 tutorial by Jeff Larkin.
> make
LIBRARY_PATH=/usr/lib/llvm-19/lib clang-19 -gline-tables-only -O3 -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda -Xopenmp-target=nvptx64-nvidia-cuda --offload-target=sm_86 -fuse-ld=lld offload-fallback.c -o offload-fallback
> make test_gpu
USE_GPU=1 LD_LIBRARY_PATH=/usr/lib/llvm-19/lib ./offload-fallback
result[0] = 0.000000
result[1] = 4.000000
result[2] = 8.000000
result[3] = 12.000000
result[4] = 16.000000
result[5] = 20.000000
result[6] = 24.000000
result[7] = 28.000000
result[8] = 32.000000
result[9] = 36.000000
> make test_cpu
USE_GPU=0 LD_LIBRARY_PATH=/usr/lib/llvm-19/lib ./offload-fallback
result[0] = 0.000000
result[1] = 4.000000
result[2] = 8.000000
result[3] = 12.000000
result[4] = 16.000000
result[5] = 20.000000
result[6] = 24.000000
result[7] = 28.000000
result[8] = 32.000000
result[9] = 36.000000
The latest Clang 19 is used above, the same works for Clang 17, however there is a bug concerning OpenMP loops inside if constexpr(...)
branches.