Skip to content

Commit d6a3e9a

Browse files
Sébastien LoiselSébastien Loisel
authored andcommitted
Use system MPI for STRUMPACK build (fix 4-rank MPI tests)
- Use MPI wrapper compilers (mpicc, mpicxx, mpif90) instead of bundled MPICH - Add system MPI detection and clear error message if not found - Update docs to require system MPICH installation - Tests now correctly show "aggregated across 4 ranks"
1 parent 4b6db9d commit d6a3e9a

3 files changed

Lines changed: 82 additions & 35 deletions

File tree

docs/src/guide/strumpack.md

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,25 @@ Stick with MUMPS when:
2929

3030
### Prerequisites
3131

32-
**System MPI is required.** The JLL-provided MPI wrappers have broken paths and cannot be used for building PETSc from source.
32+
**System MPI is required.** The JLL-provided MPI wrappers have broken paths and cannot be used for building PETSc from source. MPI.jl must be configured to use the same system MPI before building.
3333

3434
```bash
3535
# macOS
36-
brew install open-mpi
36+
brew install mpich
3737

3838
# Ubuntu/Debian
39-
sudo apt-get install libopenmpi-dev
39+
sudo apt-get install mpich libmpich-dev
4040
```
4141

4242
Then configure MPI.jl to use system MPI:
4343

4444
```julia
4545
using MPIPreferences
46-
47-
# On macOS (Open MPI from Homebrew)
48-
MPIPreferences.use_system_binary(;
49-
library_names=["libmpi"],
50-
extra_paths=["/opt/homebrew/Cellar/open-mpi/5.0.8/lib"], # Adjust version as needed
51-
mpiexec="/opt/homebrew/bin/mpiexec"
52-
)
53-
54-
# On Linux (usually auto-detects)
5546
MPIPreferences.use_system_binary()
56-
5747
# Restart Julia after configuring
5848
```
5949

60-
**Important**: MPI.jl must use the **same MPI implementation** as the PETSc build. If PETSc is built with Open MPI, MPI.jl must also use Open MPI. Mixing Open MPI and MPICH causes crashes.
50+
**Important**: MPI.jl must use the **same MPI implementation** as the PETSc build. Since we build with system MPI (detected from `mpicc` in PATH), MPI.jl must also use system MPI. Mixing different MPI implementations causes crashes.
6151

6252
### One-Time Build
6353

@@ -267,10 +257,10 @@ sudo apt-get install build-essential gfortran cmake
267257
**Missing MPI**: The build requires system MPI (not the JLL wrappers). Install it and configure MPI.jl:
268258
```bash
269259
# macOS
270-
brew install open-mpi
260+
brew install mpich
271261

272262
# Ubuntu/Debian
273-
sudo apt-get install libopenmpi-dev
263+
sudo apt-get install mpich libmpich-dev
274264
```
275265

276266
```julia
@@ -292,15 +282,10 @@ using MPI
292282
MPI.identify_implementation() # Should match what PETSc was built with
293283
```
294284

295-
The STRUMPACK build uses system MPI (usually Open MPI from Homebrew). Configure MPI.jl to match:
285+
The STRUMPACK build uses system MPI (detected from `mpicc` in PATH). Configure MPI.jl to use the same system MPI:
296286
```julia
297287
using MPIPreferences
298-
# On macOS with Homebrew Open MPI:
299-
MPIPreferences.use_system_binary(;
300-
library_names=["libmpi"],
301-
extra_paths=["/opt/homebrew/Cellar/open-mpi/5.0.8/lib"],
302-
mpiexec="/opt/homebrew/bin/mpiexec"
303-
)
288+
MPIPreferences.use_system_binary()
304289
# Then restart Julia and delete compiled caches
305290
```
306291

src/build_petsc.jl

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# This module provides functions to download, build, and configure PETSc with
33
# both STRUMPACK (for GPU acceleration and MPI scalability) and MUMPS (proven direct solver).
44
#
5-
# IMPORTANT: Requires system MPI (brew install open-mpi or apt-get install libopenmpi-dev)
5+
# IMPORTANT: Requires system MPI (brew install mpich or apt-get install mpich libmpich-dev)
66
# The JLL-provided MPI wrappers have broken paths and cannot be used for building.
7+
# MPI.jl must also be configured to use system MPI: MPIPreferences.use_system_binary()
78

89
using Scratch
910
using Downloads
@@ -109,20 +110,21 @@ compression features.
109110
110111
# Prerequisites
111112
**System MPI is required.** The JLL-provided MPI wrappers have broken paths.
113+
MPI.jl must be configured to use the same system MPI before building.
112114
113115
```bash
114116
# macOS
115-
brew install open-mpi
117+
brew install mpich
116118
117119
# Ubuntu/Debian
118-
sudo apt-get install libopenmpi-dev
120+
sudo apt-get install mpich libmpich-dev
119121
```
120122
121123
Then configure MPI.jl to use system MPI:
122124
```julia
123125
using MPIPreferences
124126
MPIPreferences.use_system_binary()
125-
# Restart Julia
127+
# Restart Julia before building
126128
```
127129
128130
# Arguments
@@ -306,18 +308,35 @@ end
306308

307309
function _build_petsc_with_strumpack(src_dir::String, install_dir::String, with_debugging::Bool,
308310
with_cuda::Bool, verbose::Bool)
309-
# Build configuration flags - includes both STRUMPACK and MUMPS
310-
# Bundle MPICH with PETSc for ABI compatibility (avoids mismatch between build/test environments)
311-
# Note: We use MPICH instead of Open MPI because Open MPI 5.x requires PMIx server setup
311+
# Check that system MPI is available
312+
if !_check_system_mpi()
313+
error("""
314+
System MPI not found! STRUMPACK build requires system MPI (not Julia's MPI JLL).
315+
316+
Install system MPI first:
317+
macOS: brew install mpich
318+
Linux: sudo apt-get install mpich libmpich-dev
319+
320+
Then configure MPI.jl to use system MPI:
321+
using MPIPreferences
322+
MPIPreferences.use_system_binary()
323+
# Restart Julia before building
324+
""")
325+
end
326+
327+
# Build configuration flags - uses system MPI via wrapper compilers
328+
# System MPI is required for proper mpiexec functionality in containers/CI environments
312329
configure_flags = [
313330
"--prefix=$install_dir",
314-
"--download-mpich", # Bundle MPICH for guaranteed ABI compatibility
315-
"--with-fc=gfortran", # Fortran needed for fblaslapack
331+
# Use MPI wrapper compilers directly - PETSc will detect MPI from these
332+
"--with-cc=mpicc",
333+
"--with-cxx=mpicxx",
334+
"--with-fc=mpif90", # Fortran needed for fblaslapack (use MPI wrapper)
316335
"--with-debugging=$(with_debugging ? 1 : 0)",
317336
"--with-shared-libraries=1",
318337
]
319338
if verbose
320-
@info "Building with bundled MPICH (for ABI compatibility)"
339+
@info "Using system MPI via wrapper compilers (mpicc, mpicxx, mpif90)"
321340
end
322341

323342
# Add common dependencies

test/runtests.jl

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,51 @@
11
using Test
22
using MPI
33

4+
# Check if PETSc is loading the expected library when JULIA_PETSC_LIBRARY is set.
5+
# PETSc.jl uses @static to check the env var at precompile time, so if the cache
6+
# was compiled without JULIA_PETSC_LIBRARY, it uses PETSc_jll even if the env var
7+
# is now set.
8+
#
9+
# IMPORTANT: Pkg.test() runs with --startup-file=no by default, so startup.jl
10+
# settings don't apply during precompilation. For STRUMPACK testing, run tests
11+
# directly: JULIA_PETSC_LIBRARY="..." julia --project=. test/runtests.jl
12+
function check_petsc_library()
13+
lib_env = get(ENV, "JULIA_PETSC_LIBRARY", "")
14+
if isempty(lib_env)
15+
return # Using default JLL, no check needed
16+
end
17+
18+
# Load PETSc and check the loaded library
19+
@eval using PETSc
20+
libs_str = string(PETSc.libs)
21+
22+
if occursin(lib_env, libs_str) || occursin("petsc_strumpack", libs_str)
23+
println("[runtests.jl] PETSc library OK: using custom build")
24+
else
25+
@warn """
26+
PETSc library mismatch!
27+
28+
JULIA_PETSC_LIBRARY is set but PETSc loaded the default JLL library.
29+
30+
This happens because PETSc.jl checks JULIA_PETSC_LIBRARY at precompile time.
31+
If running via Pkg.test(), the env var isn't visible during precompilation.
32+
33+
For STRUMPACK testing, run tests directly instead:
34+
JULIA_PETSC_LIBRARY="$lib_env" julia --project=. test/runtests.jl
35+
36+
Tests will continue with the default PETSc library (no STRUMPACK).
37+
""" expected=lib_env loaded=libs_str
38+
end
39+
end
40+
441
# Precompile all dependencies once before any mpiexec to avoid concurrent compilation
542
# This significantly reduces hangs due to pidfile contention and MPI initialization conflicts
643
try
7-
# Force precompilation of all test dependencies
44+
# Check PETSc library - warns if JULIA_PETSC_LIBRARY is set but not loaded
45+
check_petsc_library()
46+
47+
# Force precompilation of all test dependencies (PETSc already loaded in check_petsc_library)
848
@eval using SafePETSc
9-
@eval using PETSc
1049
@eval using LinearAlgebra
1150
@eval using SparseArrays
1251
println("Precompilation complete for test environment")
@@ -28,6 +67,10 @@ function run_mpi_test(test_file::AbstractString; nprocs::Integer=4, expect_succe
2867
# This ensures LocalPreferences.toml is honored and avoids recompilation under mpiexec
2968
test_proj = Base.active_project()
3069
cmd = `$mpiexec_cmd -n $nprocs $(Base.julia_cmd()) --project=$test_proj $test_file`
70+
# Pass through JULIA_PETSC_LIBRARY for custom PETSc builds (e.g., STRUMPACK)
71+
if haskey(ENV, "JULIA_PETSC_LIBRARY")
72+
cmd = addenv(cmd, "JULIA_PETSC_LIBRARY" => ENV["JULIA_PETSC_LIBRARY"])
73+
end
3174
proc = run(ignorestatus(cmd))
3275
ok = success(proc)
3376
if ok != expect_success

0 commit comments

Comments
 (0)