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.