Getting Fortran running on GPU's natively

Hello!

My GPU compiler project booth now compiles Fortran do concurrent kernels to GPU machine code, and I wanted to share where it has got to.

Booth takes CUDA, HIP or Triton and emits machine code directly for AMD RDNA 2/3/4 as well as CDNA, Nvidia PTX, Tenstorrent and Apple (still a WIP with the runtime for apple). It also has its own CPU backend for x86-64 and RISC-V. It does all the good things a compiler does all by itself and doesn’t contain any dependencies like MLIR or LLVM, you only need any C compiler to run it.

What works is elementwise BLAS-1. saxpy, sscal, scopy, sswap, srot and srotm. Currently tested in CI everywhere except Tenstorrent as I’ve just been given a chip and we’re moving to emit machine code rather than their own c++ compiler. I’d like to publicly thank Hot Aisle as they provided the MI300X so I could validate Fortran running by hand.

What does not work: no reductions, so sdot, sasum and snrm2 are absent. Nested loops inside a do concurrent get dropped on the offload path, which blocks CSEVL and the SLATEC special functions (filed as lfortran/lfortran#12369). And intrinsics come out as abs and exp rather than fabsf and expf, so transcendentals do not resolve. Elementwise arithmetic only, but that part is tested.

I carried on the modernisation of SLATEC which you can see here: Zaneham/SLATEC: A modernisation of the SLATEC library. The goal is to have routines that would benefit from GPU acceleration be a part of the CI process.

If anyone has some time I would appreciate feedback. Especially because I come from a historical computing and hobby background so if there’s any routines or parts of Fortran that people would like to see prioritised first, I am all ears. If you do give this a go and find any problems with this, please feel free to reach out to me directly and/or raise an issue. If you’re unsure if it’s something LFortran isn’t supporting yet or something with my compiler you’re more than welcome to leave an issue on Booth too.

This is a new and ongoing project so there will be big gaps and errors so if you hit anything just sing out.

Thanks all!

Zane

5 Likes

I have some benchmarks based on real code here: GitHub - JorgeG94/more_benchmarks_dc_ocean: Yet another set of benchmarks for do cocnurrent for ocean inspired kernesl · GitHub if you’d like to take a look.

Also adding, I wonder how easy/hard would it be to make emit Metal for Apple GPUs…

1 Like

Hi,

Really interesting!

Feel free to try out your compiler on our production “do concurrent” Fortran codes.
The HipFT and POT3D codes have been tested on NVIDIA, Intel, and MAD GPUs - the MAS code only on NVIDIA for now.

– Ron

1 Like

@ZaneHam great work. LFortran has a Metal and some preliminary CUDA backend also, but we’ll need to redesign it to support all of Fortran more easily.

How do you currently use LFortran to hook it up in your compiler? I see your issue at Nested do loops get dropped inside do concurrent on the GPU path · Issue #12369 · lfortran/lfortran · GitHub, it seems you are using our CUDA backend?

Let’s meet over video to brainstorm how how to best design it and how to collaborate. If you are interested, please DM me at https://lfortran.zulipchat.com/.