# "Portable" GPU support, opinion wanted

**URL:** <https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469>\
**Category:** Help\
**Created:** [August 13, 2024, 11:10am UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469 "2024-08-13T11:10:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorgeg](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jorgeg/32/6835_2.png) [@jorgeg](https://fortran-lang.discourse.group/u/jorgeg)\
**Post date:** [August 13, 2024, 11:10am UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/1 "2024-08-13T11:10:47Z")

</div>

I am aware that cuda fortran and hipfort exist for NVIDIA and AMD GPUs. However, I don’t like the idea of being constrained to using nvfortran and hipfc to compile to get GPU support. I was thinking on writing a big module interface which basically does:

```auto

#ifdef CUDA

... bind(C,"cudaDeviceSynchronize")

#elif HIP

... bind(C,"hipDeviceSynchronize")

#else

??

#endif

```

For cuda/hip, cublas/hipblas, cusolver/hipsolver. Am I doing something extraordinarily stupid? I’ve done some initial tests and doing that and simply doing

`gfortran -cpp a.F90 -L$CUDA_ROOT/lib -lcudart` works fine.

I just would like some community feedback and a straight answer if what I am doing is a waste of my time and I should just force adopt hipfort and cuda fortran.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [August 13, 2024, 12:09pm UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/2 "2024-08-13T12:09:40Z")

</div>

Unless you really need the fine-grained control of Cuda/HIP, or need to call some Cuda/HIP specific libraries, I’d go with OpenACC as the portable GPU option, or at least look into it.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [August 13, 2024, 12:13pm UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/3 "2024-08-13T12:13:32Z")

</div>

I’m not sure how well OpenACC is supported on AMD hardware. But I’m sure AMD is supporting OpenMP, at least on the more compute-centric GPGPUs.

---

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [August 13, 2024, 12:37pm UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/4 "2024-08-13T12:37:04Z")

</div>

Also Intel uses OpenMP for offloading to intel GPUs [Solving Heterogeneous Programming Challenges with Fortran and OpenMP - Intel Community](https://community.intel.com/t5/Blogs/Tech-Innovation/Tools/Solving-Heterogeneous-Programming-Challenges-with-Fortran-and/post/1569529). nvfortran proposes OpenMP offloading syntax (If I understood correctly, it is actually calling OpenACC behind the curtains, not 100% sure). So OpenMP would look like a good candidate for a common denominator for portable GPU support (eventually).

Some interesting links:

> **[Offloading code with compiler directives - LUMI](https://www.lumi-supercomputer.eu/offloading-code-with-compiler-directives/)**
>
> In our last blog we discussed how to convert CUDA applications to HIP. In this blog we will be scratching the surface on how to offload your code to a device with directives. One of the less intrusive ways to parallelize and offload code is through...

[http://www.idris.fr/media/formations/openacc/openmp\_gpu\_idris\_fortran.pdf](http://www.idris.fr/media/formations/openacc/openmp_gpu_idris_fortran.pdf)

> **[20210924-OpenMP-update-for-DOE.pdf](https://www.openmp.org/wp-content/uploads/20210924-OpenMP-update-for-DOE.pdf)**
>
> 4.32 MB

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [August 13, 2024, 12:42pm UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/5 "2024-08-13T12:42:16Z")

</div>

> [@jorgeg](#):
>
> \> “Portable” GPU support  
> \> I don’t like the idea of being constrained to using nvfortran and hipfc to compile to get GPU support.

I agree. GPU support in all languages are abysmal.

The most portable option we have, is to use OpenCL, which only compiles the kernel code during runtime.

However, to use OpenCL, we need to write our kernels in C, which sucks.

We should ask Khronos to support Fortran kernels in OpenCL, which they most likely won’t. Or we can write Fortran to C converters that transpile our Fortran kernel code to C, so OpenCL can run it.

---

<div class="post-metadata">

**Author:** ![jorgeg](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jorgeg/32/6835_2.png) [@jorgeg](https://fortran-lang.discourse.group/u/jorgeg)\
**Post date:** [August 13, 2024, 10:04pm UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/6 "2024-08-13T22:04:59Z")

</div>

one of the main things we’d use out of cuda and hip is cublas and hipblas. For many other things I can adapt to openmp, since OpenACC is not widely supported anymore. See Cray compilers for example.

---

<div class="post-metadata">

**Author:** ![jorgeg](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jorgeg/32/6835_2.png) [@jorgeg](https://fortran-lang.discourse.group/u/jorgeg)\
**Post date:** [August 13, 2024, 10:05pm UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/7 "2024-08-13T22:05:45Z")

</div>

Yeah, for AMD I am mostly concerned about the GPUs in datacenters. Consumer ones will take a bit to catch up still. And yes, OpenMP is supported.

---

<div class="post-metadata">

**Author:** ![dukeleto](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@dukeleto](https://fortran-lang.discourse.group/u/dukeleto)\
**Post date:** [August 15, 2024, 8:45am UTC](https://fortran-lang.discourse.group/t/portable-gpu-support-opinion-wanted/8469/8 "2024-08-15T08:45:15Z")

</div>

FWIW, my recent experience on AMD GPUs with Cray compiler is that OpenACC support, although nowhere near as good as nvfortran support on nvidia GPUs, is _much_ better than OpenMP offload support.
