Skip to content

GPU support for AMD, a script to build PETSc for set_library!, and a CI job for custom libraries - #321

Merged
filoferra merged 6 commits into
JuliaParallel:mainfrom
filoferra:amdgpu-ext
Oct 28, 2026
Merged

filoferra merged 6 commits into
JuliaParallel:mainfrom
filoferra:amdgpu-ext

Conversation

@filoferra

Copy link
Copy Markdown
Collaborator

This adds AMD GPU support next to CUDA, together with the tooling to build and link a PETSc for it, and CI for the custom-library path.

Building a custom PETSc. scripts/build_petsc.sh PREFIX [-j N] [-- CONFIGURE_OPTIONS...] builds PETSc 3.25 as a shared library against the MPI library MPI.jl loads and Julia's own BLAS and LAPACK (libblastrampoline's ILP64 _64_ names, as PETSc_jll does), with Triangle and CTetGen. A PETSc with its own BLAS gives silently wrong results inside Julia, since its dgemm_ calls bind to libblastrampoline's LP64 entry points, which have no library behind them. A new custom-library CI job builds it (cached on the script's hash), calls set_library! and runs the test suite. GPU builds pass their options through, e.g. -- --with-cuda=1 --with-cuda-arch=90 or -- --with-hip=1 --with-hip-arch=gfx1100.

AMD GPUs. PETScAMDGPUExt (AMDGPU.jl weakdep): with_local_array! and with_local_arrays! hand out ROCArrays for Vecs in HIP memory, and with_field_views! gives DMStag views of device arrays for KernelAbstractions kernels. Arrays go back to PETSc only after the device is done, because the GPU packages launch on their own streams. That wait sits in the base release_local_array behind a synchronize_device hook.

CUDA. The extension follows the same structure. Mixed host/device pairs in with_local_arrays! threw a MethodError and now work, and the device is synchronized before every hand-back.

ex19 runs on CUDA or HIP through a gpu switch; its GPU branch was broken.

Also:

  • USING_CUSTOM_LIB in the tests now checks library_info().source.
  • dmstag_stokes_test uses a dense LU reference instead of UMFPACK.
  • docs/src/man/gpu.md and dmstag.md cover AMD, device views, using the CUDA toolkit PETSc was built with, and -use_gpu_aware_mpi 0 with MPICH_jll.

Tested:

  • The full suite against PETSc_jll and against a library built by the script.
  • test/gpu/device_arrays.jl hip (24 tests, not in runtests.jl) on a Radeon Pro W7800 with ROCm 7.1 against a HIP build. ex19 with 3- and 4-level multigrid converges in 3 Newton steps, as on the CPU, with 96% of the KSP flops on the GPU.
  • test/gpu/device_arrays.jl cuda on an NVIDIA GH200 (aarch64, CUDA 12.6, CUDA.jl 6) against a CUDA build from the script: 24/24. ex19 converges in 3 Newton steps on CPU and GPU at 129² and 257², with all KSP flops on the GPU.
  • Both extensions load together without method ambiguities.

🤖 Generated with Claude Code

`scripts/build_petsc.sh` builds PETSc 3.25 as a shared library against the MPI library loaded by MPI.jl and Julia's own BLAS (libblastrampoline's ILP64 names, as PETSc_jll does), with Triangle and CTetGen for simplex meshes.

A new CI job builds it, caches it and runs the test suite through set_library!, and the full suite passes locally against it.
The test suite now detects a custom library correctly, and the DMStag Stokes reference solve uses a dense LU so it runs on builds without SuiteSparse.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
With AMDGPU.jl loaded and a PETSc built with HIP, with_local_array! and with_local_arrays! hand out ROCArrays for Vecs in device memory, and with_local_arrays! also pairs a host Vec with a device one, as on coarser multigrid levels.
Arrays go back to PETSc only after the device has finished, since AMDGPU.jl launches kernels on its own stream. ex19 with 3-level multigrid converges in the same 3 Newton steps..
with_field_views! on a DMStag whose vectors live on the GPU gives views of device arrays, which KernelAbstractions kernels take with the same ghost-inclusive indices as on the host. A test on an AMD GPU checks a divergence kernel against the host result.
The docs note that broadcasting over a view runs on the host, since OffsetArray doesn't pass its parent's broadcast style on, and show broadcasting over parent(view) instead.
ex19 takes a gpu = :cuda or :hip switch and runs its GPU branch again: it hands the device COO values to MatSetValuesCOO as an Array over the device pointer, and its residual uses with_local_arrays!, which also covers the coarser multigrid levels where one Vec is on the host. On AMD it converges in the same 3 Newton steps as on the CPU. The CUDA extension now handles a host Vec paired with a device Vec, and waits for CUDA.jl's kernels before handing arrays back to PETSc, as the AMDGPU extension does; neither worked before. The CUDA path is not tested on an NVIDIA GPU.
test/gpu/device_arrays.jl takes the backend as its argument, cuda or hip, and runs the same checks on either: device arrays from with_local_array! and with_local_arrays!, mixed host/device pairs, and a DMStag kernel compared against the host.
@filoferra
filoferra merged commit 833449c into JuliaParallel:main Oct 28, 2026
6 checks passed
@filoferra
filoferra deleted the amdgpu-ext branch October 28, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant