diff --git a/Project.toml b/Project.toml index 6dfcf67..35ba2d9 100644 --- a/Project.toml +++ b/Project.toml @@ -9,3 +9,9 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb" MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" SparseMatricesCSR = "a0a7dd2c-ebf4-11e9-1f05-cf50bc540ca1" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["Test"] diff --git a/src/HYPRE.jl b/src/HYPRE.jl index abc7741..33544f3 100644 --- a/src/HYPRE.jl +++ b/src/HYPRE.jl @@ -66,6 +66,8 @@ using SparseMatricesCSR: SparseMatrixCSR, colvals using MPI: MPI using .LibHYPRE +export HYPREMatrix, HYPREVector + module Internals function check_n_rows end function to_hypre_data end diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..7a06424 --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,78 @@ +using HYPRE +using HYPRE.LibHYPRE +using Test +using SparseArrays +using SparseMatricesCSR + +using MPI +MPI.Init() + +@testset "HYPREMatrix" begin + H = HYPREMatrix() + @test H.IJMatrix == HYPRE_IJMatrix(C_NULL) + @test H.ParCSRMatrix == HYPRE_ParCSRMatrix(C_NULL) + + ilower, iupper = 4, 6 + CSC = convert(SparseMatrixCSC{HYPRE_Complex, HYPRE_Int}, sparse([ + 1 2 0 0 3 + 0 4 0 5 0 + 0 6 7 0 8 + ])) + CSR = sparsecsr(findnz(CSC)..., size(CSC)...) + @test CSC == CSR + csc = HYPRE.Internals.to_hypre_data(CSC, ilower, iupper) + csr = HYPRE.Internals.to_hypre_data(CSR, ilower, iupper) + @test csc[1]::HYPRE_Int == csr[1]::HYPRE_Int == 3 # nrows + @test csc[2]::Vector{HYPRE_Int} == csr[2]::Vector{HYPRE_Int} == [3, 2, 3] # ncols + @test csc[3]::Vector{HYPRE_BigInt} == csr[3]::Vector{HYPRE_BigInt} == [4, 5, 6] # rows + @test csc[4]::Vector{HYPRE_BigInt} == csr[4]::Vector{HYPRE_BigInt} == [1, 2, 5, 2, 4, 2, 3, 5] # cols + @test csc[5]::Vector{HYPRE_Complex} == csr[5]::Vector{HYPRE_Complex} == 1:8 # values + + # Optimizations for CSR matrix + @test csr[4] == CSR.colval + @test_broken csr[4] === CSR.colval + @test csr[5] == CSR.nzval + @test_broken csr[5]::Vector{HYPRE_Complex} === CSR.nzval + + @test_throws ArgumentError HYPRE.Internals.to_hypre_data(CSC, ilower, iupper-1) + @test_throws ArgumentError HYPRE.Internals.to_hypre_data(CSR, ilower, iupper+1) + + # Converting SparseMatrixCS(C|R) to HYPREMatrix + ilower, iupper = 6, 10 + 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(CSR, ilower, iupper) + @test H.IJMatrix != HYPRE_IJMatrix(C_NULL) + @test H.ParCSRMatrix != HYPRE_ParCSRMatrix(C_NULL) +end + +@testset "HYPREVector" begin + h = HYPREVector() + @test h.IJVector == HYPRE_IJVector(C_NULL) + @test h.ParVector == HYPRE_ParVector(C_NULL) + + ilower, iupper = 1, 10 + b = rand(HYPRE_Complex, 10) + nvalues, indices, values = HYPRE.Internals.to_hypre_data(b, ilower, iupper) + @test nvalues::HYPRE_Int == 10 + @test indices::Vector{HYPRE_Int} == collect(1:10) + @test values::Vector{HYPRE_Complex} === b # === for correct eltype + + b = rand(1:10, 10) + nvalues, indices, values = HYPRE.Internals.to_hypre_data(b, ilower, iupper) + @test nvalues::HYPRE_Int == 10 + @test indices::Vector{HYPRE_Int} == collect(1:10) + @test values::Vector{HYPRE_Complex} == b # == for other eltype + @test_throws ArgumentError HYPRE.Internals.to_hypre_data([1, 2], ilower, iupper) + + # Converting Vector to HYPREVector + b = rand(HYPRE_Complex, 10) + h = HYPREVector(b, ilower, iupper) + @test h.IJVector != HYPRE_IJVector(C_NULL) + @test h.ParVector != HYPRE_ParVector(C_NULL) + @test_throws ArgumentError HYPREVector([1, 2], ilower, iupper) +end