diff --git a/src/LibPETSc_lib.jl b/src/LibPETSc_lib.jl index 4a95e7d8..87f89b89 100644 --- a/src/LibPETSc_lib.jl +++ b/src/LibPETSc_lib.jl @@ -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 diff --git a/src/mat.jl b/src/mat.jl index be93916e..bf34d71e 100644 --- a/src/mat.jl +++ b/src/mat.jl @@ -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 @@ -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, @@ -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)], @@ -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[] @@ -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( diff --git a/src/matshell.jl b/src/matshell.jl index 53dd7cbd..1d9be93d 100644 --- a/src/matshell.jl +++ b/src/matshell.jl @@ -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) diff --git a/src/vec.jl b/src/vec.jl index 04780da0..ab6facd3 100644 --- a/src/vec.jl +++ b/src/vec.jl @@ -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. @@ -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) @@ -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`. @@ -49,14 +54,14 @@ 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, @@ -64,7 +69,7 @@ function VecSeq( 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, @@ -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[] @@ -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[] @@ -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) @@ -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) diff --git a/test/mat.jl b/test/mat.jl index 111fcb89..ef0b14a2 100644 --- a/test/mat.jl +++ b/test/mat.jl @@ -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) diff --git a/test/vec.jl b/test/vec.jl index 14201b41..319aa565 100644 --- a/test/vec.jl +++ b/test/vec.jl @@ -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)