Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
name: CI

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

# The fpm release used by every job. The macOS jobs build it from source, checked against this digest.
env:
NNQS_FPM_VERSION: "0.13.0"
NNQS_FPM_SHA256: 001cff6cf1145f215baa0888ba27acc06cea30a89b79d1a8be97db3fffbc8cd2
# The OpenCoarrays release that the gfortran coarray job builds, and the commit its tag names.
NNQS_OPENCOARRAYS_VERSION: "2.10.3"
NNQS_OPENCOARRAYS_COMMIT: 3d0fa68dc95f05f9c17bd3e7dd0d34e6f530e429

jobs:
test:
name: ${{ matrix.toolchain.compiler }} ${{ matrix.toolchain.version }} ${{ matrix.toolchain.label }}
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
# Single-image builds, which need no coarray support. Each links Ubuntu's reference LAPACK. gfortran needs version
# 15 or later for the locality specifiers (shared, local) on do concurrent.
toolchain:
- { compiler: gcc, version: "15", fpm: gfortran, flags: "-std=f2018 -O2 -g -fbacktrace" }
- { compiler: gcc, version: "16", fpm: gfortran, flags: "-std=f2018 -O2 -g -fbacktrace" }
- { compiler: intel, version: "2025.3", fpm: ifx, flags: "-stand f18 -O2" }
- { compiler: intel, version: "2026.1", fpm: ifx, flags: "-stand f18 -O2" }
- { compiler: intel, version: "2026.1", fpm: ifx, label: "runtime checks",
flags: "-stand f18 -check all -check nouninit -O0 -g -traceback" }
- { compiler: flang, version: "23", fpm: flang, flags: "-O2" }
steps:
- uses: actions/checkout@v7
- name: Install LAPACK
run: sudo apt-get update -q && sudo apt-get install -y -q liblapack-dev
- uses: fortran-lang/setup-fortran@v1
if: matrix.toolchain.compiler != 'flang'
with:
compiler: ${{ matrix.toolchain.compiler }}
version: ${{ matrix.toolchain.version }}
# setup-fortran checks for the release by grepping apt.llvm.org's dists listing, which is now
# served compressed, so the check fails although the packages exist. Install them directly.
- name: Install LLVM Flang from apt.llvm.org
if: matrix.toolchain.compiler == 'flang'
run: |
v=${{ matrix.toolchain.version }}
curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc > /dev/null
echo "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-$v main" | sudo tee /etc/apt/sources.list.d/llvm-toolchain-$v.list
sudo apt-get update -q
sudo apt-get install -y -q flang-$v clang-$v libomp-$v-dev libclang-rt-$v-dev
echo "/usr/lib/llvm-$v/bin" >> "$GITHUB_PATH"
- uses: fortran-lang/setup-fpm@v10
with:
fpm-version: v${{ env.NNQS_FPM_VERSION }}
- name: Build
run: fpm build --compiler ${{ matrix.toolchain.fpm }} --flag "${{ matrix.toolchain.flags }}"
- name: Run tests
run: fpm test --compiler ${{ matrix.toolchain.fpm }} --flag "${{ matrix.toolchain.flags }}"

coarrays:
name: coarrays, ${{ matrix.name }}, 4 images
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
include:
# ifx has its own coarray runtime. gfortran uses OpenCoarrays, through its caf and cafrun wrappers. fpm calls
# caf through ci/fpm-caf, which keeps caf from leaving out its libraries when fpm links.
- { name: ifx 2026.1, compiler: intel, version: "2026.1", fpm: ifx, runner: "",
flags: "-DNNQS_COARRAY -coarray=shared -coarray-num-images=4 -O2" }
- { name: gfortran 15 and OpenCoarrays, compiler: gcc, version: "15", fpm: ci/fpm-caf, runner: "cafrun -n 4",
flags: "-DNNQS_COARRAY -O2" }
steps:
- uses: actions/checkout@v7
- name: Install LAPACK
run: sudo apt-get update -q && sudo apt-get install -y -q liblapack-dev
- uses: fortran-lang/setup-fortran@v1
with:
compiler: ${{ matrix.compiler }}
version: ${{ matrix.version }}
- name: Build OpenCoarrays from source
if: matrix.fpm == 'ci/fpm-caf'
# Ubuntu's OpenCoarrays is built for its default gfortran 13, which is too old for this code, so OpenCoarrays
# is built with gfortran 15. It needs MPICH: from gfortran 15 on, OpenCoarrays refuses Open MPI, which
# identifies one-sided memory windows differently on each image. MPICH's wrappers are pointed at gfortran 15
# and its gcc, not Ubuntu's default compilers. The release's tag is checked against the commit it names.
run: |
sudo apt-get install -y -q libmpich-dev mpich cmake
echo "MPICH_CC=$CC" >> "$GITHUB_ENV"; echo "MPICH_FC=$FC" >> "$GITHUB_ENV"
export MPICH_CC="$CC" MPICH_FC="$FC"
src="$RUNNER_TEMP/opencoarrays-src"; build="$RUNNER_TEMP/opencoarrays-build"
git clone -q --depth 1 --branch "$NNQS_OPENCOARRAYS_VERSION" https://github.com/sourceryinstitute/OpenCoarrays.git "$src"
test "$(git -C "$src" rev-parse HEAD)" = "$NNQS_OPENCOARRAYS_COMMIT"
cmake -S "$src" -B "$build" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$HOME/opencoarrays" \
-DCMAKE_Fortran_COMPILER="$FC" -DCMAKE_C_COMPILER="$CC" \
-DMPI_C_COMPILER=mpicc.mpich -DMPI_Fortran_COMPILER=mpif90.mpich \
-DMPIEXEC_EXECUTABLE=/usr/bin/mpiexec.mpich
cmake --build "$build" -j 4
cmake --install "$build"
echo "$HOME/opencoarrays/bin" >> "$GITHUB_PATH"
- uses: fortran-lang/setup-fpm@v10
with:
fpm-version: v${{ env.NNQS_FPM_VERSION }}
- name: Build
run: fpm build --compiler ${{ matrix.fpm }} --flag "${{ matrix.flags }}"
- name: Run tests on every image
run: >-
fpm test --compiler ${{ matrix.fpm }} --flag "${{ matrix.flags }}"
${{ matrix.runner && format('--runner "{0}"', matrix.runner) || '' }}

platforms:
name: ${{ matrix.os }} ${{ matrix.compiler }} ${{ matrix.version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
# setup-fpm's macOS binaries need Homebrew's gcc@12 runtime, which the runners don't have, so fpm is built
# from source. LAPACK comes from Apple's Accelerate, which macOS installs as liblapack and libblas.
- { os: macos-15, compiler: gcc, version: "16", flags: "-std=f2018 -O2 -g -fbacktrace" }
- { os: macos-15-intel, compiler: gcc, version: "15", flags: "-std=f2018 -O2 -g -fbacktrace" }
steps:
- uses: actions/checkout@v7
- uses: fortran-lang/setup-fortran@v1
id: setup-fortran
with:
compiler: ${{ matrix.compiler }}
version: ${{ matrix.version }}
- name: Build fpm from its single-file source
run: |
mkdir -p "$HOME/fpm-bin" && cd "$HOME/fpm-bin"
src="fpm-$NNQS_FPM_VERSION.F90"
curl -fsSLO "https://github.com/fortran-lang/fpm/releases/download/v$NNQS_FPM_VERSION/$src"
echo "$NNQS_FPM_SHA256 $src" | shasum -a 256 -c -
"${{ steps.setup-fortran.outputs.fc }}" -O2 -J . "$src" -o fpm
echo "$HOME/fpm-bin" >> "$GITHUB_PATH"
- name: Build
run: fpm build --compiler ${{ steps.setup-fortran.outputs.fc }} --flag "${{ matrix.flags }}"
- name: Run tests
run: fpm test --compiler ${{ steps.setup-fortran.outputs.fc }} --flag "${{ matrix.flags }}"

windows:
name: windows-2025 gfortran (MSYS2 UCRT64)
runs-on: windows-2025
defaults:
run:
shell: msys2 {0}
steps:
- uses: actions/checkout@v7
# MSYS2 packages gfortran, reference LAPACK and fpm together.
- uses: msys2/setup-msys2@v2
with:
msystem: UCRT64
update: true
install: >-
git
mingw-w64-ucrt-x86_64-gcc-fortran
mingw-w64-ucrt-x86_64-lapack
mingw-w64-ucrt-x86_64-fpm
- name: Build
run: fpm build --compiler gfortran --flag "-std=f2018 -O2 -g -fbacktrace"
- name: Run tests
run: fpm test --compiler gfortran --flag "-std=f2018 -O2 -g -fbacktrace"

checks:
name: gfortran 16 runtime checks and sanitizers
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v7
- name: Install LAPACK
run: sudo apt-get update -q && sudo apt-get install -y -q liblapack-dev
- uses: fortran-lang/setup-fortran@v1
with:
compiler: gcc
version: "16"
- uses: fortran-lang/setup-fpm@v10
with:
fpm-version: v${{ env.NNQS_FPM_VERSION }}
- name: Runtime checks
run: fpm test --compiler gfortran --flag "-std=f2018 -g -fcheck=all -fbacktrace"
- name: Address and undefined behavior sanitizers
env:
ASAN_OPTIONS: detect_leaks=1:exitcode=99
run: >-
fpm test --compiler gfortran
--flag "-std=f2018 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer"
--link-flag "-fsanitize=address,undefined"
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
test
build
*.pdf
*.gif
74 changes: 40 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,48 +587,54 @@ from

## Building with fpm

The only dependency of this project is the Intel MKL distribution of
LAPACK. With a system installation of [Intel
oneAPI](https://www.intel.com/content/www/us/en/developer/tools/oneapi/toolkits.html)
Base and HPC toolkits (including MKL), the project can be built and run
on Windows 10/11 and Linux with
[fpm](https://github.com/fortran-lang/fpm) from the project root using a
single command, assuming the shell environment has sourced the oneAPI
environment variables beforehand.

To target an $n$ core CPU with SIMD instructions, the project can be
built and run on Windows 10/11 using the command

```powershell
fpm run --compiler ifort --flag "/Qcoarray /Qcoarray-num-images:n /Qopenmp /Qopenmp-simd" --link-flag "mkl_lapack95_lp64.lib mkl_intel_lp64.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib"
```

and on Linux using the command
The project builds with [fpm](https://github.com/fortran-lang/fpm) and
any Fortran 2018 compiler that supports locality specifiers on `do
concurrent`, which for gfortran means version 15 or later. CI tests
gfortran 15 and 16, Intel ifx and LLVM flang on Linux, gfortran on
macOS and Windows, and runs on four images with ifx and with gfortran
and OpenCoarrays. fpm fetches the one
Fortran dependency,
[IO-Fortran-Library](https://github.com/acbbullock/IO-Fortran-Library).
The project also needs LAPACK, installed as `liblapack` and `libblas`:

* Linux: reference LAPACK or OpenBLAS, such as Ubuntu's `liblapack-dev`
or Fedora's `lapack-devel`.
* macOS: nothing to install, as Apple's Accelerate framework provides
both.
* Windows: `mingw-w64-ucrt-x86_64-lapack` in MSYS2.

From the project root, run the tests and then the program with

```bash
fpm run --compiler ifort --flag "-coarray -coarray-num-images=n -qopenmp -qopenmp-simd" --link-flag "-Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_lapack95_lp64.a ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a ${MKLROOT}/lib/intel64/libmkl_core.a -liomp5 -lpthread -lm -ldl"
fpm test
fpm run --profile release
```

with equivalent features.

Here, `n` is the number of images to execute, which generally should
equal the number of CPU cores available. We then enable the generation
of multi-threaded code with OpenMP and SIMD compilation. Finally, the
link flag specifies the MKL and OpenMP runtime libraries for static
linking, provided by the [Intel Link Line
Advisor](https://www.intel.com/content/www/us/en/developer/tools/oneapi/onemkl-link-line-advisor.html).

To target an $n$ core CPU and an Intel GPU for acceleration, the project
can be built and run on Windows 10/11 using the command
Built this way, the program runs as a single image and needs no coarray
support. To run $n$ images in parallel, define `NNQS_COARRAY` and turn on
the compiler's coarray features. With ifx, use

```powershell
fpm run --compiler ifx --flag "/Qcoarray /Qcoarray-num-images:n /Qiopenmp /Qopenmp-targets:spir64 /Qopenmp-target-do-concurrent" --link-flag "mkl_lapack95_lp64.lib mkl_intel_lp64.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib OpenCL.lib"
```bash
fpm run --compiler ifx --flag "-DNNQS_COARRAY -coarray=shared -coarray-num-images=n -O2"
```

and on Linux using the command
With gfortran and [OpenCoarrays](http://www.opencoarrays.org), use

```bash
fpm run --compiler ifx --flag "-coarray -coarray-num-images=n -fiopenmp -fopenmp-targets=spir64 -fopenmp-target-do-concurrent" --link-flag "-Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_lapack95_lp64.a ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_intel_thread.a ${MKLROOT}/lib/intel64/libmkl_core.a -liomp5 -lOpenCL -lpthread -lm -ldl"
fpm run --compiler ci/fpm-caf --flag "-DNNQS_COARRAY -O2" --runner "cafrun -n n"
```

with equivalent features.
where `ci/fpm-caf` runs OpenCoarrays' `caf` wrapper for fpm. fpm passes
`-cpp` when it links, and `caf` takes `-cpp` to mean it is only compiling,
so it would leave out its libraries. With gfortran 15 or later,
OpenCoarrays 2.10.3 or later is needed, built with MPICH rather than
Open MPI.

Here, `n` is the number of images to run, which generally should equal
the number of CPU cores available. LLVM flang can build only the
single-image program, because it doesn't yet ship a coarray runtime.

With ifx, the `do concurrent` loops can also be offloaded to an Intel
GPU by adding `-fiopenmp -fopenmp-targets=spir64
-fopenmp-target-do-concurrent` to the flags. Choose the device with the
`OMP_DEFAULT_DEVICE` environment variable.
2 changes: 0 additions & 2 deletions app/main.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ program main
!-------------------------------------------------------------------------------------------------------------------
use, intrinsic :: iso_fortran_env, only: rk=>real32
use nnqs, only: RestrictedBoltzmannMachine !! Neural network type
use omp_lib !! OpenMP module
implicit none (type,external) !! No implicit types or interfaces

!! Variable Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -14,7 +13,6 @@ program main

!! Begin Executable Code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
call random_init(repeatable=.false., image_distinct=.true.) !! Initialize random number generator
call omp_set_default_device(1) !! Set OpenMP offload device (device id depends on system)

psi = RestrictedBoltzmannMachine(v_units=spins, h_units=hidden_units) !! Create instance
call psi%stochastic_optimization(ising_params=[ -0.5_rk, 0.1_rk ]) !! Input [J,B] and train network
Expand Down
17 changes: 17 additions & 0 deletions ci/fpm-caf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# Runs OpenCoarrays' caf for fpm: fpm build --compiler ci/fpm-caf ...
#
# caf takes -cpp, like -c, to mean it is only compiling, and then leaves out the coarray and MPI libraries. fpm passes
# -cpp when it links, whenever the flags define a macro such as NNQS_COARRAY, so the link fails with undefined
# references to _gfortran_caf_*. This removes -cpp from commands without -c, which are the ones that link. A link has
# nothing to preprocess, so -cpp does nothing there. fpm takes any compiler whose name contains "caf" to be caf.
set -euo pipefail

args=("$@")
if [[ " $* " != *" -c "* ]]; then
args=()
for arg in "$@"; do
[[ "$arg" == "-cpp" ]] || args+=("$arg")
done
fi
exec caf "${args[@]}"
5 changes: 4 additions & 1 deletion fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ copyright = "© 2022-2023, Austin Bullock & Andrew Wasserbeck"
auto-executables = true
auto-tests = true
auto-examples = true
# LAPACK's Fortran 77 interface, from any LAPACK installed as liblapack and libblas: reference LAPACK, OpenBLAS, or
# Apple's Accelerate on macOS.
link = ["lapack", "blas"]

[install]
library = false

[dependencies]
IO-Fortran-Library = { git="https://github.com/acbbullock/IO-Fortran-Library", branch="main" }
IO-Fortran-Library = { git = "https://github.com/acbbullock/IO-Fortran-Library", tag = "v2.3.0" }
Loading
Loading