From 9d0531c93cca951590eab722a13e0eef8dbe9196 Mon Sep 17 00:00:00 2001 From: AJ0070 Date: Fri, 13 Mar 2026 14:25:08 +0530 Subject: [PATCH 1/3] Initialize HYPRE before creating objects when using plain Julia arrays --- ext/LinearSolveHYPREExt.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ext/LinearSolveHYPREExt.jl b/ext/LinearSolveHYPREExt.jl index c95e86af6..3faca4d1f 100644 --- a/ext/LinearSolveHYPREExt.jl +++ b/ext/LinearSolveHYPREExt.jl @@ -137,6 +137,14 @@ function SciMLBase.init( init_cache_verb = verb_spec end + # Ensure HYPRE (and MPI) are initialized before creating any HYPRE objects. + # This is needed when the user passes plain Julia arrays rather than HYPREArrays, + # because the conversion below would otherwise attempt to call HYPRE before Init(). + if !(A isa HYPREMatrix || b isa HYPREVector || + (u0 !== nothing && u0 isa HYPREVector) || alg.solver isa HYPRESolver) + HYPRE.Init() + end + A = A isa HYPREMatrix ? A : HYPREMatrix(A) b = b isa HYPREVector ? b : HYPREVector(b) u0 = u0 isa HYPREVector ? u0 : (u0 === nothing ? nothing : HYPREVector(u0)) From 424980f3b1c98530b4af4a68d249d3cdcd195793 Mon Sep 17 00:00:00 2001 From: Jash Date: Thu, 4 Jun 2026 09:01:10 +0530 Subject: [PATCH 2/3] Initialize HYPRE before auto-constructing HYPRE arrays in `init` --- ext/LinearSolveHYPREExt.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ext/LinearSolveHYPREExt.jl b/ext/LinearSolveHYPREExt.jl index 209c55277..3a0ba2d27 100644 --- a/ext/LinearSolveHYPREExt.jl +++ b/ext/LinearSolveHYPREExt.jl @@ -45,6 +45,14 @@ is_distributed_comm(comm) = !(comm === nothing) && comm != MPI.COMM_NULL && MPI. auto_hypre_matrix(::HYPREAlgorithm, A::HYPREMatrix) = A auto_hypre_vector(::HYPREAlgorithm, b::HYPREVector) = b +function ensure_hypre_initialized(A, b, u0) + if A isa HYPREMatrix && b isa HYPREVector && (u0 === nothing || u0 isa HYPREVector) + return nothing + end + HYPRE.Init() + return nothing +end + function auto_hypre_matrix(alg::HYPREAlgorithm, A) if !is_distributed_comm(alg.comm) return A isa HYPREMatrix ? A : HYPREMatrix(A) @@ -178,6 +186,8 @@ function SciMLBase.init( init_cache_verb = verb_spec end + # Auto-construction of HYPREMatrix/HYPREVector touches MPI inside HYPRE.jl. + ensure_hypre_initialized(A, b, u0) A = auto_hypre_matrix(alg, A) b = auto_hypre_vector(alg, b) u0 = u0 === nothing ? nothing : auto_hypre_vector(alg, u0) From 8a2a558058af30f81a0c8e8092bdfab8c7bb87da Mon Sep 17 00:00:00 2001 From: Jash Date: Thu, 4 Jun 2026 09:10:30 +0530 Subject: [PATCH 3/3] fix --- ext/LinearSolveHYPREExt.jl | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/ext/LinearSolveHYPREExt.jl b/ext/LinearSolveHYPREExt.jl index 9b8262621..3a0ba2d27 100644 --- a/ext/LinearSolveHYPREExt.jl +++ b/ext/LinearSolveHYPREExt.jl @@ -186,17 +186,11 @@ function SciMLBase.init( init_cache_verb = verb_spec end - # Ensure HYPRE (and MPI) are initialized before creating any HYPRE objects. - # This is needed when the user passes plain Julia arrays rather than HYPREArrays, - # because the conversion below would otherwise attempt to call HYPRE before Init(). - if !(A isa HYPREMatrix || b isa HYPREVector || - (u0 !== nothing && u0 isa HYPREVector) || alg.solver isa HYPRESolver) - HYPRE.Init() - end - - A = A isa HYPREMatrix ? A : HYPREMatrix(A) - b = b isa HYPREVector ? b : HYPREVector(b) - u0 = u0 isa HYPREVector ? u0 : (u0 === nothing ? nothing : HYPREVector(u0)) + # Auto-construction of HYPREMatrix/HYPREVector touches MPI inside HYPRE.jl. + ensure_hypre_initialized(A, b, u0) + A = auto_hypre_matrix(alg, A) + b = auto_hypre_vector(alg, b) + u0 = u0 === nothing ? nothing : auto_hypre_vector(alg, u0) # Create solution vector/initial guess if u0 === nothing