Programming Nvidia GPUs
CUDA libraries
Nvidia offers various CUDA libraries, such as cuBLAS, cuFFT and cuSOLVER. These are just libraries, so can be used from any compiler, not just Nvidia's. But they are not drop-in replacements for the traditional BLAS library etc. for many reasons.
Firstly cuBLAS does not cover the whole of BLAS, and covers almost none of Lapack. cuSolver has routines with similar functionality to some of Lapack, but with different calling conventions, and using different algorithms.
Secondly, a typical calling sequence looks very different. A call to gesv is likely to become something like:
cudaMalloc(...) // Reserve memory on GPU cudaMemcpy(...) // Copy data from host to GPU ...Create(&handle) // Initialise library cusolver... // Make the call cudaMemcpy(...) // Copy data from GPU to host cudaFree(...) // Undo the mallocs
To which one can add the complication that some arguments to cusolver reside on the GPU, and some on the host.
If one checks return codes (and one should!), the code becomes quiet verbose. Ideally one is able to use the output of one library call as the input of another without copying it back to the host.
Directives
The offloading directives introduced in verion 4 of OpenMP (the target directive) are arguably the most portable offloading directives. The same source, in theory, should work with any current, or even future, accelerator. However, compiler support for the target is required.
An alternative to OpenMP is OpenACC. OpenACC is just concerned with accelerators, whereas OpenMP was originally concerned with SMP parallelism, and then gained support for accelerators and vectorisation too.
OpenMP and OpenACC are mostly directive-based, and support C, C++ and Fortran. Both support writing functions which are entirely offloaded to the GPU.
CUDA
The CUDA programming language is developed by Nvidia, and is
basically C++ with some extra syntax. In particular the qualifier
__global__ marks functions which should run on the GPU,
and, when calling such a function, one specifies how many copies
should be run simultaneously. The program starts running on the
host, and it is still necessary to call cudaMemcpy to
move data between the host and the GPU.
Compilers
GCC
The gcc, g++ and gfortran compilers support offloading with
OpenMP. This support is not necessarily included by default. One may
need to install a package with a name like
gcc-14-offload-nvptx.
Some versions of GCC default to including options which the offloader does not support. This leads to rather long compilation lines, such as
gcc -fopenmp -foffload=nvptx-none -fcf-protection=none -fno-stack-protector -no-pie
Clang
Very similar to GCC. It needs a suitable libomp-dev
package installed.
Nvidia
Very confusing.
The compilers nvc, nvc++ and nvfortran are traditional compilers, and one can use nvidia GPUs via CUDA libraries or directives, just as with GCC and Clang.
The compiler nvcc is a CUDA compiler. It looks normal, in that if
given standard C/C++, it is likely to produce a working executable
which will run exclusively on the host. It will actually do this by
calling the OS's underlying C++ compiler (g++ on Linux). However, it
undertands the extra syntax which CUDA adds to C++, and will compile
appropriately for Nvidia graphics cards when that syntax is
used. Source code which needs a CUDA, rather than a C++, compiler
should have the extension .cu, and not one of the
extensions traditionally associated with C++, such
as .cc or .cpp.