Skip to content

Commit

Permalink
Remove some getlibss and VecSeq->VecSeqWithArray
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy E Kozdon committed Jul 20, 2021
1 parent 7abb847 commit 16b5f8e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/LibPETSc_lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function PetscLibType{ST, IT}(petsc_library) where {ST, IT}
LT = typeof(petsc_library)
return PetscLibType{ST, IT, LT}(petsc_library)
end
const UnionPetscLibType = Union{PetscLibType, Type{PetscLibType}}
const UnionPetscLibType = Union{PetscLibType, Type{<:PetscLibType}}

function Base.getproperty(petsclib::PetscLibType, name::Symbol)
function Base.getproperty(petsclib::UnionPetscLibType, name::Symbol)
if name == :PetscScalar
return scalartype(petsclib)
elseif name == :PetscReal
Expand Down
26 changes: 12 additions & 14 deletions src/mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,8 @@ function assemblyend(
end

function Base.size(A::AbstractMat{PetscLib}) where {PetscLib}
petsclib = getlib(PetscLib)
m = Ref{petsclib.PetscInt}()
n = Ref{petsclib.PetscInt}()
m = Ref{PetscLib.PetscInt}()
n = Ref{PetscLib.PetscInt}()
LibPETSc.MatGetSize(PetscLib, A, m, n)
return (m[], n[])
end
Expand Down Expand Up @@ -217,8 +216,8 @@ function setvalues!(
num_rows = length(row0idxs),
num_cols = length(col0idxs),
) where {PetscLib, PetscScalar, PetscInt}
@assert PetscScalar == getlib(PetscLib).PetscScalar
@assert PetscInt == getlib(PetscLib).PetscInt
@assert PetscScalar == PetscLib.PetscScalar
@assert PetscInt == PetscLib.PetscInt
LibPETSc.MatSetValues(
PetscLib,
M,
Expand All @@ -238,8 +237,8 @@ function Base.setindex!(
i::Integer,
j::Integer,
) where {PetscLib}
PetscInt = getlib(PetscLib).PetscInt
PetscScalar = getlib(PetscLib).PetscScalar
PetscInt = PetscLib.PetscInt
PetscScalar = PetscLib.PetscScalar
setvalues!(
M,
[PetscInt(i - 1)],
Expand All @@ -254,7 +253,7 @@ function LinearAlgebra.norm(
M::AbstractMat{PetscLib},
normtype::NormType = NORM_FROBENIUS,
) where {PetscLib}
PetscReal = getlib(PetscLib).PetscReal
PetscReal = PetscLib.PetscReal
r_val = Ref{PetscReal}()
LibPETSc.MatNorm(PetscLib, M, normtype, r_val)
return r_val[]
Expand Down Expand Up @@ -298,13 +297,12 @@ function LinearAlgebra.mul!(
M::MatAT{PetscLib, PetscScalar},
x::Vector{PetscScalar},
) where {PetscScalar, PetscLib}
parent(
LinearAlgebra.mul!(
VecSeq(getlib(PetscLib), y),
M,
VecSeq(getlib(PetscLib), x),
),
LinearAlgebra.mul!(
VecSeqWithArray(PetscLib, y),
M,
VecSeqWithArray(PetscLib, x),
)
return y
end

function LinearAlgebra.issymmetric(
Expand Down
2 changes: 1 addition & 1 deletion src/matshell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function (::MatOp{PetscLib, PetscInt, LibPETSc.MATOP_MULT})(
ptr = r_ctx[]
mat = unsafe_pointer_to_objref(ptr)

PetscScalar = getlib(PetscLib).PetscScalar
PetscScalar = PetscLib.PetscScalar
x = unsafe_localarray(VecPtr(PetscLib, cx); write = false)
y = unsafe_localarray(VecPtr(PetscLib, cy); read = false)

Expand Down
43 changes: 22 additions & 21 deletions src/vec.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AbstractVec
# - VecSeq: wrap
# - VecSeqWithArray: wrap
# - VecMPI (TODO)
# - VecGhost (TODO)
# for the MPI variants we won't be able to attach finalizers, as destroy needs to be called collectively.
Expand All @@ -18,6 +18,11 @@ Base.eltype(
) where {PetscLib, PetscScalar} = PetscScalar
Base.size(v::AbstractVec) = (length(v),)

function destroy(v::AbstractVec{PetscLib}) where {PetscLib}
finalized(PetscLib) || LibPETSc.VecDestroy(PetscLib, v)
return nothing
end

"""
VecPtr(petsclib, v::Vector)
Expand All @@ -34,7 +39,7 @@ VecPtr(::Type{PetscLib}, ptr::CVec) where {PetscLib <: PetscLibType} =
VecPtr(getlib(PetscLib), ptr)

"""
VecSeq(petsclib, v::Vector)
VecSeqWithArray(petsclib, v::Vector)
A standard, sequentially-stored serial PETSc vector, wrapping the Julia vector
`v`.
Expand All @@ -49,22 +54,22 @@ performed automatically
# External Links
$(_doc_external("Vec/VecCreateSeqWithArray"))
"""
mutable struct VecSeq{PetscLib, PetscScalar} <:
mutable struct VecSeqWithArray{PetscLib, PetscScalar} <:
AbstractVec{PetscLib, PetscScalar}
ptr::CVec
array::Vector{PetscScalar}
end
Base.parent(v::VecSeq) = v.array
Base.parent(v::VecSeqWithArray) = v.array

function VecSeq(
function VecSeqWithArray(
petsclib::PetscLib,
array::Vector{PetscScalar};
blocksize = 1,
) where {PetscLib <: PetscLibType, PetscScalar}
comm = MPI.COMM_SELF
@assert initialized(petsclib)
@assert PetscScalar == petsclib.PetscScalar
v = VecSeq{PetscLib, PetscScalar}(C_NULL, array)
v = VecSeqWithArray{PetscLib, PetscScalar}(C_NULL, array)
LibPETSc.VecCreateSeqWithArray(
petsclib,
comm,
Expand All @@ -76,23 +81,22 @@ function VecSeq(
finalizer(destroy, v)
return v
end

function destroy(v::AbstractVec{PetscLib}) where {PetscLib}
finalized(PetscLib) || LibPETSc.VecDestroy(PetscLib, v)
return nothing
end
VecSeqWithArray(
::Type{PetscLib},
x...;
kw...,
) where {PetscLib <: PetscLibType} =
VecSeqWithArray(getlib(PetscLib), x...; kw...)

function Base.length(v::AbstractVec{PetscLib}) where {PetscLib}
petsclib = getlib(PetscLib)
PetscInt = petsclib.PetscInt
PetscInt = PetscLib.PetscInt
r_sz = Ref{PetscInt}()
LibPETSc.VecGetSize(PetscLib, v, r_sz)
return r_sz[]
end

function locallength(v::AbstractVec{PetscLib}) where {PetscLib}
petsclib = getlib(PetscLib)
PetscInt = petsclib.PetscInt
PetscInt = PetscLib.PetscInt
r_sz = Ref{PetscInt}()
LibPETSc.VecGetLocalSize(PetscLib, v, r_sz)
return r_sz[]
Expand All @@ -102,8 +106,7 @@ function LinearAlgebra.norm(
v::AbstractVec{PetscLib},
normtype::LibPETSc.NormType = LibPETSc.NORM_2,
) where {PetscLib}
petsclib = getlib(PetscLib)
PetscReal = petsclib.PetscReal
PetscReal = PetscLib.PetscReal
r_val = Ref{PetscReal}()
LibPETSc.VecNorm(PetscLib, v, normtype, r_val)
return r_val[]
Expand Down Expand Up @@ -141,8 +144,7 @@ function ownershiprange(
vec::AbstractVec{PetscLib},
base_one::Bool = true,
) where {PetscLib}
petsclib = getlib(PetscLib)
PetscInt = petsclib.PetscInt
PetscInt = PetscLib.PetscInt
r_lo = Ref{PetscInt}()
r_hi = Ref{PetscInt}()
LibPETSc.VecGetOwnershipRange(PetscLib, vec, r_lo, r_hi)
Expand Down Expand Up @@ -173,8 +175,7 @@ function unsafe_localarray(
read::Bool = true,
write::Bool = true,
) where {PetscLib}
petsclib = getlib(PetscLib)
PetscScalar = petsclib.PetscScalar
PetscScalar = PetscLib.PetscScalar
r_pv = Ref{Ptr{PetscScalar}}()
if write && read
LibPETSc.VecGetArray(PetscLib, vec, r_pv)
Expand Down
4 changes: 2 additions & 2 deletions test/mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ using LinearAlgebra: norm, mul!, Adjoint, Transpose, issymmetric, ishermitian

x = PetscScalar.(Array(1:num_cols))
y = zeros(PetscScalar, num_rows)
vec_x = PETSc.VecSeq(petsclib, copy(x))
vec_y = PETSc.VecSeq(petsclib, copy(y))
vec_x = PETSc.VecSeqWithArray(petsclib, copy(x))
vec_y = PETSc.VecSeqWithArray(petsclib, copy(y))

# Test mul!
mul!(vec_y, D, vec_x)
Expand Down
4 changes: 2 additions & 2 deletions test/vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ using Test
using PETSc
using LinearAlgebra: norm

@testset "VecSeq" begin
@testset "VecSeqWithArray" begin
N = 10
for petsclib in PETSc.petsclibs
PETSc.initialize(petsclib)
PetscScalar = petsclib.PetscScalar
x = rand(PetscScalar, N)
petsc_x = PETSc.VecSeq(petsclib, x)
petsc_x = PETSc.VecSeqWithArray(petsclib, x)
@test length(petsc_x) == N
@test norm(petsc_x) norm(x)

Expand Down

0 comments on commit 16b5f8e

Please sign in to comment.