# GPU offloading in Fortran

**URL:** <https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120>\
**Category:** Advocacy\
**Created:** [January 22, 2025, 9:10pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120 "2025-01-22T21:10:12Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [January 27, 2025, 10:01am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/21 "2025-01-27T10:01:28Z")

</div>

Nice, looks like you beat me on time! what happens if you remove the `--link-flag` from the command line and leave `--flag="-fopenacc"` alone?

---

<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:** [January 27, 2025, 10:02am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/22 "2025-01-27T10:02:02Z")

</div>

I’ve reduced it to just: `FPM_FFLAGS="-fopenacc" FPM_LDFLAGS="-lcublas" fpm run` and it works 🙂 @hkvzjal you can check out the repo I linked for a mini example of what you shared. You’ll need an nvidia GPU and the time to build a gcc from scratch…

---

<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:** [January 27, 2025, 10:02am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/23 "2025-01-27T10:02:52Z")

</div>

> [@FedericoPerini](#):
>
> I would look into environemnt variables

I might then create an env setup to make sure this does not happen again! thanks so much 🙂

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [January 27, 2025, 10:04am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/24 "2025-01-27T10:04:52Z")

</div>

Ok so since fpm does not support additional flags in the manifest, you still need to add them when `fpm run`ning. But, if the executable is built already, you shouldn’t have to add the `FPM_LDFLAGS`. Note there is an [open proposal](https://github.com/fortran-lang/fpm/issues/1096) to support additional flags in the manifest, it could be the right place to discuss/add about OpenACC support (I imagine it similar to the OpenMP one, that is currently a metapackage i.e. its support is added as a `dependencies.openmp = "*"`, we could think of having the same for openACC)

---

<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:** [January 27, 2025, 10:06am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/25 "2025-01-27T10:06:00Z")

</div>

> [@FedericoPerini](#):
>
> OpenACC support (I imagine it similar to the OpenMP one)

Yes, I started with openacc because the example linked by @hkvzjal was in openacc. I can also add a openmp one and start the discussion

---

<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:** [January 27, 2025, 12:03pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/26 "2025-01-27T12:03:02Z")

</div>

> [@jorgeg](#):
>
> You’ll need an nvidia GPU and the time to build a gcc from scratch…

Nice @jorgeg, it is great that you made it into an fpm repo 😃 I do have an nvidia gpu, I have not compiled gcc from scratch, I though there was an option to install the required additional dependencies, I’ll take a look at that.

> [@jorgeg](#):
>
> I can also add a openmp one and start the discussion

This would be great

---

<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:** [January 27, 2025, 12:05pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/27 "2025-01-27T12:05:35Z")

</div>

> [@hkvzjal](#):
>
> I have not compiled gcc from scratch

If you use the script from [here](https://github.com/lmarzen/OpenMP-nvptx-offload-build-tools/blob/main/gcc_build.sh) after editing the paths it is literally just ./script.sh just go get coffee or something while you do this, it took a while 🙂

---

<div class="post-metadata">

**Author:** ![sumseq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sumseq/32/3083_2.png) [@sumseq](https://fortran-lang.discourse.group/u/sumseq)\
**Post date:** [January 28, 2025, 7:48pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/28 "2025-01-28T19:48:31Z")

</div>

It has double precision emulation - see here: [https://www.sumseq.com/files/2024\_OAS\_Talk\_RCaplan.pdf](https://www.sumseq.com/files/2024_OAS_Talk_RCaplan.pdf)

---

<div class="post-metadata">

**Author:** ![sumseq](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sumseq/32/3083_2.png) [@sumseq](https://fortran-lang.discourse.group/u/sumseq)\
**Post date:** [January 28, 2025, 7:54pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/29 "2025-01-28T19:54:33Z")

</div>

As mentioned, the nvfortran compiler supports Fortran 2003, with a lot of more modern spec features supported (including some from 2023). The major missing feature is co-arrays.

As for GPUs, no one is “baking GPU support into Fortran”.  
Instead, compilers are using the features of Fortran to offload to GPUs.  
“do concurrent” has nothing to do with GPUs specifically, it simply states to the compiler that the loop can be executed out-of-order (no dependencies).  
Since this usually also means it can be run in parallel, compilers have decided to allow the user to use DC for multi-threading parallelism on CPUs and offload to GPUs.  
See here for details of this using NVIDIA, Intel and AMDl GPUs: [https://www.sumseq.com/files/2024\_OAS\_Talk\_RCaplan.pdf](https://www.sumseq.com/files/2024_OAS_Talk_RCaplan.pdf)

– Ron

---

<div class="post-metadata">

**Author:** ![gronki](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gronki](https://fortran-lang.discourse.group/u/gronki)\
**Post date:** [January 28, 2025, 10:31pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/30 "2025-01-28T22:31:38Z")

</div>

Interesting. I was under the impression that the nvfortran supported the language subset so old (like 2003) that there was even no point for me trying to compile my codes with it, but probably my knowledge was outdated. I will give it a try!

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [January 28, 2025, 11:22pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/31 "2025-01-28T23:22:48Z")

</div>

> [@gronki](#):
>
> I was under the impression that the nvfortran supported the language subset so old (like 2003)

I think it’s actually the case… There will be a new version based on flang-new (therefore supporting F2018), but it’s not yet ready.

---

<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:** [January 29, 2025, 8:47am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/32 "2025-01-29T08:47:54Z")

</div>

> [@PierU](#):
>
> flang-new

Talking about flang new, have you tried to build it and succeeded?

I can’t get it to build.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [January 29, 2025, 9:25am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/33 "2025-01-29T09:25:12Z")

</div>

Nope (didn’t try)…

---

<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:** [January 29, 2025, 11:41pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/34 "2025-01-29T23:41:08Z")

</div>

> [@hkvzjal](#):
>
> I though there was an option to install the required additional dependencies

It turns out there is. Tried the following on a Ubuntu 22.04:

```auto
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt install gcc-12 g++-12 gfortran-12 gcc-12-offload-nvptx  

```

And managed to offload using OpenMP.

At first I wanted to do it with gfortran 13 but there seems to be a bug in the packaging, as I got this error when compiling a code:

```auto
x86_64-linux-gnu-accel-nvptx-none-gcc-13: fatal error: cannot read spec file ‘libgomp.spec’: No such file or directory   
compilation terminated.

```

Which a search on google just tells me it is a missing component and that the issue seems to not have been solved so far [Bug #2036593 “Compilation error: absent libgomp.spec with gcc-13...” : Bugs : gcc-13 package : Ubuntu](https://bugs.launchpad.net/ubuntu/+source/gcc-13/+bug/2036593)

@jorgeg I tried compiling from source using the script you shared but I got another error

```auto
cc1plus: fatal error: gengtype-lex.cc: No such file or directory
compilation terminated.

```

I’ll keep this gcc-12 version for the moment.

---

<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:** [January 29, 2025, 11:49pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/35 "2025-01-29T23:49:32Z")

</div>

> [@hkvzjal](#):
>
> I tried compiling from source using the script you shared but I got another error

Ah super sad, for me it worked with no issues. I used gcc 11.4 to bootstrap I believe.

---

<div class="post-metadata">

**Author:** ![rouson](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@rouson](https://fortran-lang.discourse.group/u/rouson)\
**Post date:** [February 1, 2025, 8:56am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/36 "2025-02-01T08:56:17Z")

</div>

The script that I use to build `flang-new` from source is [fresh-llvm-build.sh](https://github.com/rouson/handy-dandy/blob/a70daad3807d97455eaa6300aede3f4da35ce324/src/fresh-llvm-build.sh#L1) in the handy-dandy repository under my GitHub user name: rouson. With that script in my `PATH`, I execute something like `git clone git@github.com:llvm/llvm-project && cd llvm-project && fresh-llvm-build.sh`. I’ve used this script on macOS and Linux. My goal with the scripts in handy-dandy is just to capture steps that work for me on systems that I use frequently so that I don’t have to remember the steps every time. For other users, the scripts are probably better for reading than for running because I haven’t made any attempt to make the scripts work on any systems other than the ones that I frequently use. With that said, pull requests that make the script more portable or more useful are welcome.

---

<div class="post-metadata">

**Author:** ![rouson](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@rouson](https://fortran-lang.discourse.group/u/rouson)\
**Post date:** [February 1, 2025, 9:04am UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/37 "2025-02-01T09:04:22Z")

</div>

Also, the [just-write-fortran](https://go.lbl.gov/just-write-fortra) talk that I previously cited here contains a URL for cloning an llvm-project fork and a `git` tag on that fork for a November 2024 version of `flang-new` that can parallelize `do concurrent` on CPUs and that I _think_ (but haven’t verified) can offload _some_ cases of `do concurrent` to GPUs. The reason that I haven’t verified the GPU offloading is that I know that it can’t yet offload the code of interest to me, but the capability to offload code like mine is under development.

---

<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:** [February 1, 2025, 1:46pm UTC](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120/38 "2025-02-01T13:46:40Z")

</div>

I’ll test this tomorrow morning! Thanks

[Previous page](https://fortran-lang.discourse.group/t/gpu-offloading-in-fortran/9120.md?page=1)
