Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #198

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
MPIPreferences = "3da0fdf6-3ccc-4f1b-acd9-58baa6c99267"
PETSc_jll = "8fa3689e-f0b9-5420-9873-adf6ccf46f2d"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"

[compat]
MPI = "0.20"
Expand All @@ -19,12 +20,12 @@ SparseArrays = "1.10"
julia = "1.10"

[extras]
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"

[targets]
test = ["ForwardDiff", "UnicodePlots", "Test", "Plots", "SparseDiffTools", "Printf"]
33 changes: 32 additions & 1 deletion src/const.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,38 @@ const PetscLogDouble = Cdouble

@enum InsertMode NOT_SET_VALUES INSERT_VALUES ADD_VALUES MAX_VALUES MIN_VALUES INSERT_ALL_VALUES ADD_ALL_VALUES INSERT_BC_VALUES ADD_BC_VALUES


@enum MatOption begin
MAT_OPTION_MIN = -3
MAT_UNUSED_NONZERO_LOCATION_ERR = -2
MAT_ROW_ORIENTED = -1
MAT_SYMMETRIC = 1
MAT_STRUCTURALLY_SYMMETRIC = 2
MAT_FORCE_DIAGONAL_ENTRIES = 3
MAT_IGNORE_OFF_PROC_ENTRIES = 4
MAT_USE_HASH_TABLE = 5
MAT_KEEP_NONZERO_PATTERN = 6
MAT_IGNORE_ZERO_ENTRIES = 7
MAT_USE_INODES = 8
MAT_HERMITIAN = 9
MAT_SYMMETRY_ETERNAL = 10
MAT_NEW_NONZERO_LOCATION_ERR = 11
MAT_IGNORE_LOWER_TRIANGULAR = 12
MAT_ERROR_LOWER_TRIANGULAR = 13
MAT_GETROW_UPPERTRIANGULAR = 14
MAT_SPD = 15
MAT_NO_OFF_PROC_ZERO_ROWS = 16
MAT_NO_OFF_PROC_ENTRIES = 17
MAT_NEW_NONZERO_LOCATIONS = 18
MAT_NEW_NONZERO_ALLOCATION_ERR = 19
MAT_SUBSET_OFF_PROC_ENTRIES = 20
MAT_SUBMAT_SINGLEIS = 21
MAT_STRUCTURE_ONLY = 22
MAT_SORTED_FULL = 23
MAT_FORM_EXPLICIT_TRANSPOSE = 24
MAT_STRUCTURAL_SYMMETRY_ETERNAL = 25
MAT_SPD_ETERNAL = 26
MAT_OPTION_MAX = 27
end

@enum NormType begin
NORM_1 = 0
Expand Down
14 changes: 12 additions & 2 deletions src/ksp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ struct Fn_KSPComputeOperators{T} end
return r_rnorm[]
end

function solve!(x::AbstractVec{$PetscScalar}, ksp::KSP{$PetscScalar}, b::AbstractVec{$PetscScalar})
function solve!(x::AbstractVec{$PetscScalar}, ksp::KSP{$PetscScalar, LT}, b::AbstractVec{$PetscScalar}) where LT
with(ksp.opts) do
@chk ccall((:KSPSolve, $libpetsc), PetscErrorCode,
(CKSP, CVec, CVec), ksp, b, x)
Expand All @@ -241,19 +241,29 @@ struct Fn_KSPComputeOperators{T} end
return x
end

function solve!(x::AbstractVec{$PetscScalar}, tksp::LinearAlgebra.AdjointFactorization{T,K}, b::AbstractVec{$PetscScalar}) where {T,K <: KSP{$PetscScalar}}
ksp = parent(tksp)
with(ksp.opts) do
@chk ccall((:KSPSolveTranspose, $libpetsc), PetscErrorCode,
(CKSP, CVec, CVec), ksp, b, x)
end
return x
end

end

# no generic Adjoint solve defined, but for Real we can use Adjoint
solve!(x::AbstractVec{T}, aksp::Adjoint{T,K}, b::AbstractVec{T}) where {K <: KSP{T}} where {T<:Real} =
solve!(x, transpose(parent(aksp)), b)

const KSPAT{T, LT} = Union{KSP{T, LT}, Transpose{T, KSP{T, LT}}, Adjoint{T, KSP{T, LT}}}
const KSPAT{T, LT} = Union{KSP{T, LT}, Transpose{T, KSP{T, LT}}, Adjoint{T, KSP{T, LT}}, LinearAlgebra.AdjointFactorization{T, KSP{T, LT}}}

LinearAlgebra.ldiv!(x::AbstractVec{T}, ksp::KSPAT{T, LT}, b::AbstractVec{T}) where {T, LT} = solve!(x, ksp, b)
function LinearAlgebra.ldiv!(x::AbstractVector{T}, ksp::KSPAT{T, LT}, b::AbstractVector{T}) where {T, LT}
parent(solve!(AbstractVec(x), ksp, AbstractVec(b)))
end
Base.:\(ksp::KSPAT{T, LT}, b::AbstractVector{T}) where {T, LT} = ldiv!(similar(b), ksp, b)
#Base.:\(kspt::LinearAlgebra.AdjointFactorization{T,KSPT{T, LT}}, b::AbstractVector{T}) where {T, LT} = ldiv!(similar(b), kspt._A', b)


"""
Expand Down
22 changes: 22 additions & 0 deletions src/mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,26 @@ function MatSetValuesStencil! end
return nothing
end

function MatSetOption(mat::AbstractMat{$PetscScalar},
op::MatOption,
flg::Bool)

fl = PETSC_TRUE
if !flg
fl = PETSC_FALSE
end
@chk ccall((:MatSetOption, $libpetsc), PetscErrorCode,
(CMat,
MatOption,
PetscBool),
mat,
op,
fl
)
return nothing
end





Expand Down Expand Up @@ -283,6 +303,7 @@ function MatSetValuesStencil! end
return val[]
end


function assemblybegin(M::AbstractMat{$PetscScalar}, t::MatAssemblyType=MAT_FINAL_ASSEMBLY)
@chk ccall((:MatAssemblyBegin, $libpetsc), PetscErrorCode, (CMat, MatAssemblyType), M, t)
return nothing
Expand All @@ -291,6 +312,7 @@ function MatSetValuesStencil! end
@chk ccall((:MatAssemblyEnd, $libpetsc), PetscErrorCode, (CMat, MatAssemblyType), M, t)
return nothing
end

#function view(mat::AbstractMat{$PetscScalar}, viewer::AbstractViewer{$PetscLib}=ViewerStdout($petsclib, mat.comm))
# if assembled(mat)
# @chk ccall((:MatView, $libpetsc), PetscErrorCode,
Expand Down
8 changes: 3 additions & 5 deletions test/old_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ PETSc.initialize()
PETSc.destroy(pc_extra)

# set new pc, check ref counts are modified by ksp
pc_new = PETSc.PC{Float64}(MPI.COMM_SELF)
pc_new = PETSc.PC{Float64}(MPI.COMM_SELF);
@test PETSc.nrefs(pc_new) == 1
PETSc.setpc!(ksp, pc_new)
@test PETSc.nrefs(pc_new) == 2
Expand All @@ -57,10 +57,8 @@ PETSc.initialize()
w = M'*x
@test w ≈ S'*x

# FIXME
# y = ksp' \ w
# @test S'*y ≈ w rtol=1e-8

y = ksp' \ w
@test S'*y ≈ w rtol=1e-8

f!(y,x) = y .= 2 .*x

Expand Down
42 changes: 20 additions & 22 deletions test/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,22 @@ using PETSc
opts["nothing_opt"] = nothing
@test "" == opts["nothing_opt"]
@test "1" == opts["new_opt"]

# FIXME
#=

# Check that viewer is working
_stdout = stdout
(rd, wr) = redirect_stdout()
@show opts

@test readline(rd) == "opts = #PETSc Option Table entries:"
@test readline(rd) == "-da_grid_x 100"
@test readline(rd) == "-da_grid_y 100"
@test readline(rd) == "-ksp_monitor"
@test readline(rd) == "-ksp_view"
@test readline(rd) == "-mg_levels_0_pc_type ilu"
@test readline(rd) == "-new_opt 1"
@test readline(rd) == "-nothing_opt"
@test readline(rd) == "-pc_mg_levels 1"
@test readline(rd) == "-pc_type mg"
@test readline(rd) == "-da_grid_x 100 # (source: code)"
@test readline(rd) == "-da_grid_y 100 # (source: code)"
@test readline(rd) == "-ksp_monitor # (source: code)"
@test readline(rd) == "-ksp_view # (source: code)"
@test readline(rd) == "-mg_levels_0_pc_type ilu # (source: code)"
@test readline(rd) == "-new_opt 1 # (source: code)"
@test readline(rd) == "-nothing_opt # (source: code)"
@test readline(rd) == "-pc_mg_levels 1 # (source: code)"
@test readline(rd) == "-pc_type mg # (source: code)"
@test readline(rd) == "#End of PETSc Option Table entries"
@test readline(rd) == ""

Expand All @@ -66,22 +64,22 @@ using PETSc
show(stdout, "text/plain", glo_opts)
end
@test readline(rd) == "#PETSc Option Table entries:"
@test readline(rd) == "-da_grid_x 100"
@test readline(rd) == "-da_grid_y 100"
@test readline(rd) == "-ksp_monitor"
@test readline(rd) == "-ksp_view"
@test readline(rd) == "-mg_levels_0_pc_type ilu"
@test readline(rd) == "-new_opt 1"
@test readline(rd) == "-nothing_opt"
@test readline(rd) == "-pc_mg_levels 1"
@test readline(rd) == "-pc_type mg"
@test readline(rd) == "-da_grid_x 100 # (source: code)"
@test readline(rd) == "-da_grid_y 100 # (source: code)"
@test readline(rd) == "-ksp_monitor # (source: code)"
@test readline(rd) == "-ksp_view # (source: code)"
@test readline(rd) == "-mg_levels_0_pc_type ilu # (source: code)"
@test readline(rd) == "-new_opt 1 # (source: code)"
@test readline(rd) == "-nothing_opt # (source: code)"
@test readline(rd) == "-pc_mg_levels 1 # (source: code)"
@test readline(rd) == "-pc_type mg # (source: code)"
@test readline(rd) == "#End of PETSc Option Table entries"

show(stdout, "text/plain", glo_opts)
@test readline(rd) == "#No PETSc Option Table entries"

redirect_stdout(_stdout)
=#


PETSc.finalize(petsclib)
end
Expand Down
Loading