ROCm / hipfort

Fortran interfaces for ROCm libraries
https://rocm.docs.amd.com/projects/hipfort/en/latest/
Other
66 stars 34 forks source link

Building hipfort applications with CMake #133

Open fluidnumerics-joe opened 7 months ago

fluidnumerics-joe commented 7 months ago

Hey all, A google search on this topic turns up this page with a nice outline, but no content : https://rocm.docs.amd.com/projects/hipfort/en/latest/how_to_guides/getting_started.html

Is there any documentation available on building fortran applications that depend on hipfort with CMake ?

cgmb commented 6 months ago

Thanks for bringing that to my attention, Joe. Sorry for the slow response, my GitHub notification settings got a bit scrambled in the transition to the ROCm organization.

I think I promised to fill that out and completely forgot about it. That's embarrassing. I'll get that filled in as soon as possible.

fluidnumerics-joe commented 5 months ago

Hey @cgmb - any updates here ? image

Currently guessing-and-checking on using hipfort with a CMake build system. It would be great if

find_package(hipfort REQUIRED)

or something to that effect would be possible..

cgmb commented 5 months ago

The short version is that if you've installed ROCm 6.0.2, then you can use a search like find_package(hipfort REQUIRED COMPONENTS hip rocblas rocsparse) and then link with target_link_libraries(yourlib PRIVATE hipfort::rocblas hipfort::rocsparse hipfort::hip).

fluidnumerics-joe commented 5 months ago

I can upgrade to ROCm 6.0.2 . However, I use other builds of hipfort from source. Since I don't always just use the system compiler for Fortran and Fortran compilers notoriously don't generate .mod files that are compatible with other compilers, do any of the branches of hipfort on this repo come with/create the hipfort-config.cmake on installation?

fluidnumerics-joe commented 5 months ago

The short version is that if you've installed ROCm 6.0.2, then you can use a search like find_package(hipfort REQUIRED COMPONENTS hip rocblas rocsparse) and then link with target_link_libraries(yourlib PRIVATE hipfort::rocblas hipfort::rocsparse hipfort::hip).

This throws errors since we need the correct includes directories for the .mod files when building Fortran code ...

fluidnumerics-joe commented 5 months ago

Also, this error is what I was referring to when building with another compiler other than the system compiler

      |       1
Fatal Error: File '/opt/rocm/include/hipfort/amdgcn/hipfort.mod' opened at (1) is not a GNU Fortran module file
cgmb commented 5 months ago

do any of the branches of hipfort on this repo come with/create the hipfort-config.cmake on installation?

Anything from ROCm 6.0.0 or later. The CMake files just weren't getting packaged in official releases until 6.0.2. However, hipfort only generates the CMake files if the underlying math libraries are available at build time, so you'll need to have *-dev packages installed for the math libraries (and possibly set -DCMAKE_PREFIX_PATH to find them).

Also, this error is what I was referring to when building with another compiler other than the system compiler

Hmmm... I only was testing with a single compiler. Thanks for reporting this issue.

fluidnumerics-joe commented 5 months ago

To summarize, many sites, including ours, want to offer multiple Fortran compilers which would then necessitate multiple installs of hipfort. Right now, this requires installing ROCm from the package manager and manually removing hipfort. From here, hipfort is installed for each compiler and module files are provided to load hipfort.

Unfortunately , the hipconfig.cmake files think that it should be searching in a parent directory for hip, hipBLAS, etc. CMake package config files which fails a CMake build. For now, I'm using find_libraries with a hint to look in HIPFORT_ROOT, an environment variable set by our module files.

Alternatively, we could build HIP from source, and all of the accelerated libraries just to accommodate the way the CMake config is written, but building all of the accelerated libraries from source is not documented.

fluidnumerics-joe commented 5 months ago

@cgmb - I'm curious to know how stable it is to build HIP, HIPFort, and the accelerated libraries (ROCblas, etc.) with Spack.

cgmb commented 5 months ago

I quite like Spack. They lag a bit behind the official releases, but it's a great package manager and it has official support from AMD. It's particularly good if you want to reduce the size of your binaries by building everything for a single GPU architecture.

With that said, I've never actually used it for hipfort. I was just using it for the math libraries themselves.

fluidnumerics-joe commented 5 months ago

Works great for hipfort. I usually will run a spack checksum hipfort@x.x.x to add the latest hipfort version to the package in spack. This way we can build multiple instances of hipfort with various compilers. Right now, our CMake solution looks something like

find_package(hip REQUIRED)
find_package(hipblas REQUIRED)
find_library(HIPFORT_LIBRARIES NAMES libhipfort-amdgcn.a HINTS ENV HIPFORT_ROOT REQUIRED)
find_path(HIPFORT_INCLUDE_DIRS hipfort.mod HINTS ENV HIPFORT_ROOT PATH_SUFFIXES include/hipfort/amdgcn/ REQUIRED)

when building for AMD GPUs. We also have to remove the hipfort provided by the system package manager so that's not accidentally found.