Skip to content

Commit 16b5f8e

Browse files
author
Jeremy E Kozdon
committed
Remove some getlibss and VecSeq->VecSeqWithArray
1 parent 7abb847 commit 16b5f8e

6 files changed

Lines changed: 41 additions & 42 deletions

File tree

src/LibPETSc_lib.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ function PetscLibType{ST, IT}(petsc_library) where {ST, IT}
1313
LT = typeof(petsc_library)
1414
return PetscLibType{ST, IT, LT}(petsc_library)
1515
end
16-
const UnionPetscLibType = Union{PetscLibType, Type{PetscLibType}}
16+
const UnionPetscLibType = Union{PetscLibType, Type{<:PetscLibType}}
1717

18-
function Base.getproperty(petsclib::PetscLibType, name::Symbol)
18+
function Base.getproperty(petsclib::UnionPetscLibType, name::Symbol)
1919
if name == :PetscScalar
2020
return scalartype(petsclib)
2121
elseif name == :PetscReal

src/mat.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,8 @@ function assemblyend(
162162
end
163163

164164
function Base.size(A::AbstractMat{PetscLib}) where {PetscLib}
165-
petsclib = getlib(PetscLib)
166-
m = Ref{petsclib.PetscInt}()
167-
n = Ref{petsclib.PetscInt}()
165+
m = Ref{PetscLib.PetscInt}()
166+
n = Ref{PetscLib.PetscInt}()
168167
LibPETSc.MatGetSize(PetscLib, A, m, n)
169168
return (m[], n[])
170169
end
@@ -217,8 +216,8 @@ function setvalues!(
217216
num_rows = length(row0idxs),
218217
num_cols = length(col0idxs),
219218
) where {PetscLib, PetscScalar, PetscInt}
220-
@assert PetscScalar == getlib(PetscLib).PetscScalar
221-
@assert PetscInt == getlib(PetscLib).PetscInt
219+
@assert PetscScalar == PetscLib.PetscScalar
220+
@assert PetscInt == PetscLib.PetscInt
222221
LibPETSc.MatSetValues(
223222
PetscLib,
224223
M,
@@ -238,8 +237,8 @@ function Base.setindex!(
238237
i::Integer,
239238
j::Integer,
240239
) where {PetscLib}
241-
PetscInt = getlib(PetscLib).PetscInt
242-
PetscScalar = getlib(PetscLib).PetscScalar
240+
PetscInt = PetscLib.PetscInt
241+
PetscScalar = PetscLib.PetscScalar
243242
setvalues!(
244243
M,
245244
[PetscInt(i - 1)],
@@ -254,7 +253,7 @@ function LinearAlgebra.norm(
254253
M::AbstractMat{PetscLib},
255254
normtype::NormType = NORM_FROBENIUS,
256255
) where {PetscLib}
257-
PetscReal = getlib(PetscLib).PetscReal
256+
PetscReal = PetscLib.PetscReal
258257
r_val = Ref{PetscReal}()
259258
LibPETSc.MatNorm(PetscLib, M, normtype, r_val)
260259
return r_val[]
@@ -298,13 +297,12 @@ function LinearAlgebra.mul!(
298297
M::MatAT{PetscLib, PetscScalar},
299298
x::Vector{PetscScalar},
300299
) where {PetscScalar, PetscLib}
301-
parent(
302-
LinearAlgebra.mul!(
303-
VecSeq(getlib(PetscLib), y),
304-
M,
305-
VecSeq(getlib(PetscLib), x),
306-
),
300+
LinearAlgebra.mul!(
301+
VecSeqWithArray(PetscLib, y),
302+
M,
303+
VecSeqWithArray(PetscLib, x),
307304
)
305+
return y
308306
end
309307

310308
function LinearAlgebra.issymmetric(

src/matshell.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function (::MatOp{PetscLib, PetscInt, LibPETSc.MATOP_MULT})(
3939
ptr = r_ctx[]
4040
mat = unsafe_pointer_to_objref(ptr)
4141

42-
PetscScalar = getlib(PetscLib).PetscScalar
42+
PetscScalar = PetscLib.PetscScalar
4343
x = unsafe_localarray(VecPtr(PetscLib, cx); write = false)
4444
y = unsafe_localarray(VecPtr(PetscLib, cy); read = false)
4545

src/vec.jl

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# AbstractVec
2-
# - VecSeq: wrap
2+
# - VecSeqWithArray: wrap
33
# - VecMPI (TODO)
44
# - VecGhost (TODO)
55
# 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(
1818
) where {PetscLib, PetscScalar} = PetscScalar
1919
Base.size(v::AbstractVec) = (length(v),)
2020

21+
function destroy(v::AbstractVec{PetscLib}) where {PetscLib}
22+
finalized(PetscLib) || LibPETSc.VecDestroy(PetscLib, v)
23+
return nothing
24+
end
25+
2126
"""
2227
VecPtr(petsclib, v::Vector)
2328
@@ -34,7 +39,7 @@ VecPtr(::Type{PetscLib}, ptr::CVec) where {PetscLib <: PetscLibType} =
3439
VecPtr(getlib(PetscLib), ptr)
3540

3641
"""
37-
VecSeq(petsclib, v::Vector)
42+
VecSeqWithArray(petsclib, v::Vector)
3843
3944
A standard, sequentially-stored serial PETSc vector, wrapping the Julia vector
4045
`v`.
@@ -49,22 +54,22 @@ performed automatically
4954
# External Links
5055
$(_doc_external("Vec/VecCreateSeqWithArray"))
5156
"""
52-
mutable struct VecSeq{PetscLib, PetscScalar} <:
57+
mutable struct VecSeqWithArray{PetscLib, PetscScalar} <:
5358
AbstractVec{PetscLib, PetscScalar}
5459
ptr::CVec
5560
array::Vector{PetscScalar}
5661
end
57-
Base.parent(v::VecSeq) = v.array
62+
Base.parent(v::VecSeqWithArray) = v.array
5863

59-
function VecSeq(
64+
function VecSeqWithArray(
6065
petsclib::PetscLib,
6166
array::Vector{PetscScalar};
6267
blocksize = 1,
6368
) where {PetscLib <: PetscLibType, PetscScalar}
6469
comm = MPI.COMM_SELF
6570
@assert initialized(petsclib)
6671
@assert PetscScalar == petsclib.PetscScalar
67-
v = VecSeq{PetscLib, PetscScalar}(C_NULL, array)
72+
v = VecSeqWithArray{PetscLib, PetscScalar}(C_NULL, array)
6873
LibPETSc.VecCreateSeqWithArray(
6974
petsclib,
7075
comm,
@@ -76,23 +81,22 @@ function VecSeq(
7681
finalizer(destroy, v)
7782
return v
7883
end
79-
80-
function destroy(v::AbstractVec{PetscLib}) where {PetscLib}
81-
finalized(PetscLib) || LibPETSc.VecDestroy(PetscLib, v)
82-
return nothing
83-
end
84+
VecSeqWithArray(
85+
::Type{PetscLib},
86+
x...;
87+
kw...,
88+
) where {PetscLib <: PetscLibType} =
89+
VecSeqWithArray(getlib(PetscLib), x...; kw...)
8490

8591
function Base.length(v::AbstractVec{PetscLib}) where {PetscLib}
86-
petsclib = getlib(PetscLib)
87-
PetscInt = petsclib.PetscInt
92+
PetscInt = PetscLib.PetscInt
8893
r_sz = Ref{PetscInt}()
8994
LibPETSc.VecGetSize(PetscLib, v, r_sz)
9095
return r_sz[]
9196
end
9297

9398
function locallength(v::AbstractVec{PetscLib}) where {PetscLib}
94-
petsclib = getlib(PetscLib)
95-
PetscInt = petsclib.PetscInt
99+
PetscInt = PetscLib.PetscInt
96100
r_sz = Ref{PetscInt}()
97101
LibPETSc.VecGetLocalSize(PetscLib, v, r_sz)
98102
return r_sz[]
@@ -102,8 +106,7 @@ function LinearAlgebra.norm(
102106
v::AbstractVec{PetscLib},
103107
normtype::LibPETSc.NormType = LibPETSc.NORM_2,
104108
) where {PetscLib}
105-
petsclib = getlib(PetscLib)
106-
PetscReal = petsclib.PetscReal
109+
PetscReal = PetscLib.PetscReal
107110
r_val = Ref{PetscReal}()
108111
LibPETSc.VecNorm(PetscLib, v, normtype, r_val)
109112
return r_val[]
@@ -141,8 +144,7 @@ function ownershiprange(
141144
vec::AbstractVec{PetscLib},
142145
base_one::Bool = true,
143146
) where {PetscLib}
144-
petsclib = getlib(PetscLib)
145-
PetscInt = petsclib.PetscInt
147+
PetscInt = PetscLib.PetscInt
146148
r_lo = Ref{PetscInt}()
147149
r_hi = Ref{PetscInt}()
148150
LibPETSc.VecGetOwnershipRange(PetscLib, vec, r_lo, r_hi)
@@ -173,8 +175,7 @@ function unsafe_localarray(
173175
read::Bool = true,
174176
write::Bool = true,
175177
) where {PetscLib}
176-
petsclib = getlib(PetscLib)
177-
PetscScalar = petsclib.PetscScalar
178+
PetscScalar = PetscLib.PetscScalar
178179
r_pv = Ref{Ptr{PetscScalar}}()
179180
if write && read
180181
LibPETSc.VecGetArray(PetscLib, vec, r_pv)

test/mat.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ using LinearAlgebra: norm, mul!, Adjoint, Transpose, issymmetric, ishermitian
6464

6565
x = PetscScalar.(Array(1:num_cols))
6666
y = zeros(PetscScalar, num_rows)
67-
vec_x = PETSc.VecSeq(petsclib, copy(x))
68-
vec_y = PETSc.VecSeq(petsclib, copy(y))
67+
vec_x = PETSc.VecSeqWithArray(petsclib, copy(x))
68+
vec_y = PETSc.VecSeqWithArray(petsclib, copy(y))
6969

7070
# Test mul!
7171
mul!(vec_y, D, vec_x)

test/vec.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ using Test
22
using PETSc
33
using LinearAlgebra: norm
44

5-
@testset "VecSeq" begin
5+
@testset "VecSeqWithArray" begin
66
N = 10
77
for petsclib in PETSc.petsclibs
88
PETSc.initialize(petsclib)
99
PetscScalar = petsclib.PetscScalar
1010
x = rand(PetscScalar, N)
11-
petsc_x = PETSc.VecSeq(petsclib, x)
11+
petsc_x = PETSc.VecSeqWithArray(petsclib, x)
1212
@test length(petsc_x) == N
1313
@test norm(petsc_x) norm(x)
1414

0 commit comments

Comments
 (0)