Compile application with openmp target pragma

Hi,
I’m trying to run an application that checks if it runs on GPU or CPU.
#include <stdio.h>
#include <omp.h>

int main()
{
int runningOnGPU = 0;
/* Test if GPU is available using OpenMP4.5 */
#pragma omp target map(from:runningOnGPU)
{
if (omp_is_initial_device() == 0)
runningOnGPU = 1;
}
/*If still running on CPU, GPU must not be available */
if (runningOnGPU)
printf("### Able to use the GPU! ### \n");
else
printf("### Unable to use the GPU, using CPU! ###\n");

return 0;
}
My OS is Ubunto 18.04 LTS, VGA card - Quadro P4000, the driver is nvidia-driver-440-server and Cuda version 10.0.

I tried to follow those documents but none of them worked.
https://freecompilercamp.org/llvm-openmp-build/ - segmentation fault with this build command

clang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda ongpu.c

https://github.com/clang-ykt/clang/wiki - build error
https://kristerw.blogspot.com/2017/04/building-gcc-with-support-for-nvidia.html - compilation error

Where can I find a document with instructions on how to build a simple OpenMP application?

I also tried this document
https://on-demand.gputechconf.com/gtc/2016/presentation/s6510-jeff-larkin-targeting-gpus-openmp.pdf - - compilation error

Hi yaronk3,

I’ll ping Jeff to see if he can help with the Clang issue since I only support the NV HPC Compilers.

As for the code, it does seem fine as it compiles and runs correctly with our pre-release of 20.11 (after I uncomment the section in the code). 20.11 will be released in a few weeks and contain our initial Beta implementation for OpenMP offload to GPUs.

% cat test.c
#include <stdio.h>
#include <omp.h>

int main()
{
int runningOnGPU = 0;
/* Test if GPU is available using OpenMP4.5 */
#pragma omp target map(from:runningOnGPU)
{
if (omp_is_initial_device() == 0)
runningOnGPU = 1;
}
/* If still running on CPU, GPU must not be available */
if (runningOnGPU)
printf("### Able to use the GPU! ### \n");
else
printf("### Unable to use the GPU, using CPU! ###\n");

return 0;
}
% nvc -mp test.c -V20.11; a.out
### Unable to use the GPU, using CPU! ###
% nvc -mp=gpu test.c -V20.11 ; a.out
### Able to use the GPU! ###

-Mat

Hi @yaronk3. I just tried Mat’s corrected code with XL and Clang and was able to run and get the expected results. Can you please try it just to make sure the commenting wasn’t the problem? One issue I’ve hit with Clang before is that if I didn’t get the bytecode version of the OpenMP runtime, it failed to run. This gave an error about failing to offload when offload is mandatory.

I’ve recently began installing llvm/clang using Spack. If that’s an option for you, I’d suggest trying that. Something like spack install llvm@master+cuda cuda_arch=70, but please check the options carefully, because I’m working from memory.

Hi Mat & Jeff,
The commenting isn’t the problem(I edited the question), When I compile C file with Clang and -fopenmp-targets=nvptx64-nvidia-cuda flag I got segmentation fault when running the application.
I’m new to working with GPU, Can you explain how you build Clang and what is XL?
Thanks for the help.

The XL compiler is IBM’s compiler, so if you don’t have access to an IBM machine, that won’t help you. I used it as a sanity check to make sure that the program worked with multiple compilers.

I’ve had limited success building CLANG myself from sources, but I’ve had a lot of success doing it with spack (https://spack.readthedocs.io/en/latest/getting_started.html). On my machine, which has a Volta-generation card, I built using the following command:

$ spack install llvm@master+cuda cuda_arch=70

After that build completed (it took quite a while) I was able to build your code with the command you listed above.

I tried to use spcak but I still got a segmentation fault, When I use Cuda 11.1
“ykain@toronto:~ /home/ykain/spack/opt/spack/linux-ubuntu18.04-skylake_avx512/gcc-7.5.0/llvm-master-56iodrir4cysgqwcpm6vjxfipk55bprj/bin/clang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda --cuda-path=/usr/local/cuda ongpu.c -o ongpu clang-12: warning: Unknown CUDA version. cuda.h: CUDA_VERSION=11010. Assuming the latest supported version 10.1 [-Wunknown-cuda-version] ykain@toronto:~ ./ongpu
Segmentation fault (core dumped)”
When I use Cuda 10.1 I got this error -
“/home/ykain/spack/opt/spack/linux-ubuntu18.04-skylake_avx512/gcc-7.5.0/llvm-master-56iodrir4cysgqwcpm6vjxfipk55bprj/bin/clang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda --cuda-path=/usr/local/cuda ongpu.c
nvlink fatal : Input file ‘/home/ykain/spack/opt/spack/linux-ubuntu18.04-skylake_avx512/gcc-7.5.0/llvm-master-56iodrir4cysgqwcpm6vjxfipk55bprj/lib/libomptarget-nvptx.a:omptarget-nvptx_generated_cancel.cu.o’ newer than toolkit (111 vs 101)
clang-12: error: nvlink command failed with exit code 1 (use -v to see invocation)”

I added the lib directory to LD_LIBRARY_PATH environment argument.

Hi. I’m sorry you’re still having troubles. At this point, we’re beyond my knowledge of CLANG. I would suggest reaching out to the LLVM support channels and see whether anyone there knows what’s going on. I can confirm that your program is working on my machine with CLANG/LLVM built via spack. The unknown cuda version message is a warning and doesn’t prevent successful building and running on my machine. Sorry I couldn’t help more.