I’ve done a lot of GPU programming, albeit in C/C++, and I am very curious at how to do it in Fortran without being limited to the compiler of my choosing.
The stdlib project aims to provide a standard library for Fortran, it is a very active project and it is advancing quickly. I am using it in some projects of my own and have been very happy with it.
Templates are a funny thing, currently the best way to do this is to use fypp to do some pre-processing and generate code for different datatypes. It is a bit annoying to rely on a python tool but hey, Fortran is moving in the right direction.
Now, to go back to GPUs. If you write CUDA/HIP kernels and call them from Fortran you’re limited to them being accessible via an "extern" C
interface, which limits things quite a bit since C has no concept of classes, etc. so objects are out of the question. This then forces your Fortran to adapt to this and limits what cool features of Fortran you can use.
An additional thing is that right now, cuda_fortran and hipfort are weird; cuda_fortran is only in the nvfortran compiler, which is nice but it still a bit…wobbly, missing a couple screws. Hipfort is best if you build it on the fly with your project, I struggled a lot to get this working without building it myself.
Probably the easiest way to use Fortran + GPUs is offloading matrix algebra routines via accelerated libraries, since for this you can just write some bind(C)
interfaces to the cublas/hipblas/magma_gpu calls. However, if you want to access the nice features to allocate memory etc, you need those bind(C) routines too and there’s is no single big project that provides these.
Then you have directive driven offloading via OpenMP, OpenACC. This is 100% compiler dependent and makes things, in my experience, very annoying.
Standard parallelism using DO CONCURRENT is another good example, however also limited to the compiler.
If I were to choose something, I would write a set of glue subroutines that allow me to call explicit kernels written in CUDA/HIP, but it will need a lot of glue.