Repository navigation
GPU support for AMD, a script to build PETSc for set_library!, and a CI job for custom libraries - #321
Merged
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 itsdgemm_calls bind to libblastrampoline's LP64 entry points, which have no library behind them. A newcustom-libraryCI job builds it (cached on the script's hash), callsset_library!and runs the test suite. GPU builds pass their options through, e.g.-- --with-cuda=1 --with-cuda-arch=90or-- --with-hip=1 --with-hip-arch=gfx1100.AMD GPUs.
PETScAMDGPUExt(AMDGPU.jl weakdep):with_local_array!andwith_local_arrays!hand outROCArrays for Vecs in HIP memory, andwith_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 baserelease_local_arraybehind asynchronize_devicehook.CUDA. The extension follows the same structure. Mixed host/device pairs in
with_local_arrays!threw aMethodErrorand now work, and the device is synchronized before every hand-back.ex19 runs on CUDA or HIP through a
gpuswitch; its GPU branch was broken.Also:
USING_CUSTOM_LIBin the tests now checkslibrary_info().source.docs/src/man/gpu.mdanddmstag.mdcover AMD, device views, using the CUDA toolkit PETSc was built with, and-use_gpu_aware_mpi 0with MPICH_jll.Tested:
test/gpu/device_arrays.jl hip(24 tests, not inruntests.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 cudaon 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.🤖 Generated with Claude Code