From 975c69d6992e4a6879bbb19fab5f77d58b7f1bec Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sun, 24 Jul 2022 00:52:52 +0200 Subject: [PATCH] Default rows for HYPREMatrix(::SparseMatrixCS(C|R)) and HYPREVector(::Vector) This patch adds defaults for ilower and iupper in constructors when i) using SparseMatrixCS(C|R) and Vector and ii) when no communicator is passed. In this case we simply assume the users wants a single-processor solve, and default to passing 1 and size(A, 1) as the default values. In the future this could possibly use the HYPRE sequential compiler flag to completely avoid MPI. --- src/HYPRE.jl | 7 +++---- test/runtests.jl | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/HYPRE.jl b/src/HYPRE.jl index 07b11a2..294ded2 100644 --- a/src/HYPRE.jl +++ b/src/HYPRE.jl @@ -207,7 +207,6 @@ function Internals.to_hypre_data(A::SparseMatrixCSR, ilower, iupper) return nrows, ncols, rows, cols, values end -# TODO: Default to ilower = 1, iupper = size(B, 1)? function HYPREMatrix(comm::MPI.Comm, B::Union{SparseMatrixCSC,SparseMatrixCSR}, ilower, iupper) A = HYPREMatrix(comm, ilower, iupper) nrows, ncols, rows, cols, values = Internals.to_hypre_data(B, ilower, iupper) @@ -216,7 +215,7 @@ function HYPREMatrix(comm::MPI.Comm, B::Union{SparseMatrixCSC,SparseMatrixCSR}, return A end -HYPREMatrix(B::Union{SparseMatrixCSC,SparseMatrixCSR}, ilower, iupper) = +HYPREMatrix(B::Union{SparseMatrixCSC,SparseMatrixCSR}, ilower=1, iupper=size(B, 1)) = HYPREMatrix(MPI.COMM_WORLD, B, ilower, iupper) ######################### @@ -231,7 +230,6 @@ function Internals.to_hypre_data(x::Vector, ilower, iupper) end # TODO: Internals.to_hypre_data(x::SparseVector, ilower, iupper) (?) -# TODO: Default to ilower = 1, iupper = length(x)? function HYPREVector(comm::MPI.Comm, x::Vector, ilower, iupper) b = HYPREVector(comm, ilower, iupper) nvalues, indices, values = Internals.to_hypre_data(x, ilower, iupper) @@ -240,7 +238,8 @@ function HYPREVector(comm::MPI.Comm, x::Vector, ilower, iupper) return b end -HYPREVector(x::Vector, ilower, iupper) = HYPREVector(MPI.COMM_WORLD, x, ilower, iupper) +HYPREVector(x::Vector, ilower=1, iupper=length(x)) = + HYPREVector(MPI.COMM_WORLD, x, ilower, iupper) function Base.copy!(x::Vector, h::HYPREVector) ilower, iupper = Internals.get_proc_rows(h) diff --git a/test/runtests.jl b/test/runtests.jl index 8bf873c..2dfbc79 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -52,18 +52,36 @@ end CSC = sprand(5, 10, 0.3) CSR = sparsecsr(findnz(CSC)..., size(CSC)...) @test CSC == CSR + H = HYPREMatrix(CSC, ilower, iupper) @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) H = HYPREMatrix(MPI.COMM_WORLD, CSC, ilower, iupper) @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) + H = HYPREMatrix(CSR, ilower, iupper) @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) H = HYPREMatrix(MPI.COMM_WORLD, CSR, ilower, iupper) @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) + + # Default to owning all rows + CSC = sprand(10, 10, 0.3) + CSR = sparsecsr(findnz(CSC)..., size(CSC)...) + H = HYPREMatrix(CSC) + @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) + @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) + @test H.comm == MPI.COMM_WORLD + @test H.ilower == H.jlower == 1 + @test H.iupper == H.jupper == 10 + H = HYPREMatrix(CSR) + @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) + @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) + @test H.comm == MPI.COMM_WORLD + @test H.ilower == H.jlower == 1 + @test H.iupper == H.jupper == 10 end function tomain(x) @@ -192,6 +210,13 @@ end @test h.IJVector != HYPRE_IJVector(C_NULL) @test h.ParVector != HYPRE_ParVector(C_NULL) @test_throws ArgumentError HYPREVector([1, 2], ilower, iupper) + # Default to owning all rows + h = HYPREVector(b) + @test h.IJVector != HYPRE_IJVector(C_NULL) + @test h.ParVector != HYPRE_ParVector(C_NULL) + @test h.comm == MPI.COMM_WORLD + @test h.ilower == 1 + @test h.iupper == 10 ilower, iupper = 1, 10 b = rand(HYPRE_Complex, 10)