Skip to content

Commit 985a085

Browse files
author
Jeremy E Kozdon
committed
Try to get Vec working
1 parent 0149ce0 commit 985a085

10 files changed

Lines changed: 129 additions & 102 deletions

File tree

gen/generator.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ printer_blacklist = [
1414
"PetscOptions", "_n_PetscOptions",
1515
"PetscViewer", "_p_PetscViewer",
1616
"Vec", "_p_Vec",
17+
"PetscObject", "_p_PetscObject",
1718
#
1819
# Remove types defined in const.jl
1920
#

gen/prologue.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ const MPI_REPLACE = MPI.MPI_REPLACE
2121
const PetscOptions = Ptr{Cvoid}
2222
const PetscViewer = Ptr{Cvoid}
2323
const Vec = Ptr{Cvoid}
24+
const PetscObject = Ptr{Cvoid}

lib/petsc_library.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const MPI_REPLACE = MPI.MPI_REPLACE
2121
const PetscOptions = Ptr{Cvoid}
2222
const PetscViewer = Ptr{Cvoid}
2323
const Vec = Ptr{Cvoid}
24+
const PetscObject = Ptr{Cvoid}
2425

2526
const __darwin_off_t = Int64
2627

@@ -56,10 +57,6 @@ mutable struct _p_PetscToken end
5657

5758
const PetscToken = Ptr{_p_PetscToken}
5859

59-
mutable struct _p_PetscObject end
60-
61-
const PetscObject = Ptr{_p_PetscObject}
62-
6360
const PetscObjectId = PetscInt64
6461

6562
const PetscObjectState = PetscInt64

src/LibPETSc.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export PetscLibType
88
export petsclibs
99
export PetscBool
1010
export PETSC_TRUE
11+
export UnionPetscLibType
12+
export getlib
1113

1214
include("const.jl")
1315
include("startup.jl")

src/PETSc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222
include("init.jl")
2323
include("viewer.jl")
2424
include("options.jl")
25-
# include("vec.jl")
25+
include("vec.jl")
2626
# include("mat.jl")
2727
# include("matshell.jl")
2828
# include("ksp.jl")

src/init.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Check if `petsclib` is initialized
66
# External Links
77
$(_doc_external("Sys/PetscInitialized"))
88
"""
9-
function initialized(petsclib::PetscLibType)
9+
function initialized(petsclib)
1010
r_flag = Ref{LibPETSc.PetscBool}()
1111
LibPETSc.PetscInitialized(petsclib, r_flag)
1212
return r_flag[] == LibPETSc.PETSC_TRUE
@@ -33,7 +33,7 @@ function initialize()
3333
return nothing
3434
end
3535

36-
function initialize(petsclib::PetscLibType)
36+
function initialize(petsclib)
3737
if !initialized(petsclib)
3838
MPI.Initialized() || MPI.Init()
3939
LibPETSc.PetscInitializeNoArguments(petsclib)
@@ -60,7 +60,7 @@ function finalize()
6060
return nothing
6161
end
6262

63-
function finalize(petsclib::PetscLibType)
63+
function finalize(petsclib)
6464
if !finalized(petsclib)
6565
LibPETSc.PetscFinalize(petsclib)
6666
end
@@ -75,7 +75,7 @@ Check if `petsclib` is finalized
7575
# External Links
7676
$(_doc_external("Sys/PetscFinalized"))
7777
"""
78-
function finalized(petsclib::PetscLibType)
78+
function finalized(petsclib)
7979
r_flag = Ref{LibPETSc.PetscBool}()
8080
LibPETSc.PetscFinalized(petsclib, r_flag)
8181
return r_flag[] == LibPETSc.PETSC_TRUE

src/lib.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ function PetscLibType{ST, IT}(petsc_library) where {ST, IT}
1414
LT = typeof(petsc_library)
1515
return PetscLibType{ST, IT, LT}(petsc_library)
1616
end
17+
const UnionPetscLibType = Union{PetscLibType, Type{PetscLibType}}
18+
19+
function Base.getproperty(petsclib::PetscLibType, name::Symbol)
20+
if name == :PetscScalar
21+
return scalartype(petsclib)
22+
elseif name == :PetscReal
23+
return realtype(petsclib)
24+
elseif name == :PetscInt
25+
return inttype(petsclib)
26+
else
27+
return getfield(petsclib, name)
28+
end
29+
end
1730

1831
"""
1932
scalartype(petsclib::PetscLibType)
@@ -102,3 +115,7 @@ macro for_petsc(expr)
102115
end
103116
end
104117
end
118+
119+
@for_petsc begin
120+
getlib(::Type{$PetscLib}) = $petsclib
121+
end

src/sys.jl

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const CPetscObject = Ptr{Cvoid}
22

3-
const UnionPetscTypes = Union{
4-
Options,
5-
}
3+
const UnionPetscTypes = Union{Options, AbstractVec}
64

75
# allows us to pass PETSc_XXX objects directly into CXXX ccall signatures
86
Base.cconvert(::Type{CPetscObject}, obj::UnionPetscTypes) = obj
@@ -13,46 +11,30 @@ function Base.unsafe_convert(::Type{Ptr{CPetscObject}}, obj::UnionPetscTypes)
1311
convert(Ptr{CPetscObject}, pointer_from_objref(obj))
1412
end
1513

16-
#=
17-
@for_libpetsc begin
18-
function getcomm(
19-
obj::Union{
20-
AbstractKSP{$PetscScalar},
21-
AbstractMat{$PetscScalar},
22-
AbstractVec{$PetscScalar},
23-
},
14+
function getcomm(obj::Union{AbstractVec{PetscLib}}) where {PetscLib}
15+
comm = MPI.Comm()
16+
LibPETSc.PetscObjectGetComm(PetscLib, obj, comm)
17+
18+
#XXX We should really increase the petsc reference counter.
19+
# But for for some reason the PETSc says that this communicator is
20+
# unknown
21+
#=
22+
# Call the PetscCommDuplicate to increase reference count
23+
@chk ccall(
24+
(:PetscCommDuplicate, $libpetsc),
25+
PetscErrorCode,
26+
(MPI.MPI_Comm, Ptr{MPI.MPI_Comm}, Ptr{Cvoid}),
27+
comm,
28+
comm,
29+
C_NULL,
2430
)
25-
comm = MPI.Comm()
26-
@chk ccall(
27-
(:PetscObjectGetComm, $libpetsc),
28-
PetscErrorCode,
29-
(CPetscObject, Ptr{MPI.MPI_Comm}),
30-
obj,
31-
comm,
32-
)
33-
34-
#XXX We should really increase the petsc reference counter.
35-
# But for for some reason the PETSc says that this communicator is
36-
# unknown
37-
#=
38-
# Call the PetscCommDuplicate to increase reference count
39-
@chk ccall(
40-
(:PetscCommDuplicate, $libpetsc),
41-
PetscErrorCode,
42-
(MPI.MPI_Comm, Ptr{MPI.MPI_Comm}, Ptr{Cvoid}),
43-
comm,
44-
comm,
45-
C_NULL,
46-
)
4731
48-
# Register PetscCommDestroy to decriment the reference count
49-
finalizer(PetscCommDestroy, comm)
50-
=#
32+
# Register PetscCommDestroy to decriment the reference count
33+
finalizer(PetscCommDestroy, comm)
34+
=#
5135

52-
return comm
53-
end
36+
return comm
5437
end
55-
=#
5638

5739
#=
5840
#XXX Not sure why this doesn't work

src/vec.jl

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,99 @@
66

77
const CVec = Ptr{Cvoid}
88

9-
abstract type AbstractVec{T} <: AbstractVector{T} end
10-
scalartype(::AbstractVec{T}) where {T} = T
9+
abstract type AbstractVec{PetscLib, PetscScalar} <: AbstractVector{PetscScalar} end
10+
11+
Base.eltype(
12+
::Type{V},
13+
) where {
14+
V <: AbstractVec{PetscLib, PetscScalar},
15+
} where {PetscLib, PetscScalar} = PetscScalar
16+
Base.eltype(
17+
v::AbstractVec{PetscLib, PetscScalar},
18+
) where {PetscLib, PetscScalar} = PetscScalar
19+
Base.size(v::AbstractVec) = (length(v),)
1120

1221
"""
1322
VecSeq(v::Vector)
1423
15-
A standard, sequentially-stored serial PETSc vector, wrapping the Julia vector `v`.
24+
A standard, sequentially-stored serial PETSc vector, wrapping the Julia vector
25+
`v`.
1626
17-
This reuses the array `v` as storage, and so `v` should not be `resize!`-ed or otherwise have its length modified while the PETSc object exists.
27+
This reuses the array `v` as storage, and so `v` should not be `resize!`-ed or
28+
otherwise have its length modified while the PETSc object exists.
1829
19-
This should only be need to be called for more advanced uses, for most simple usecases, users should be able to pass `Vector`s directly and have the wrapping performed automatically
30+
This should only be need to be called for more advanced uses, for most simple
31+
usecases, users should be able to pass `Vector`s directly and have the wrapping
32+
performed automatically
2033
2134
# External Links
2235
$(_doc_external("Vec/VecCreateSeqWithArray"))
2336
"""
24-
mutable struct VecSeq{T} <: AbstractVec{T}
37+
mutable struct VecSeq{PetscLib, PetscScalar} <:
38+
AbstractVec{PetscLib, PetscScalar}
2539
ptr::CVec
26-
array::Vector{T}
40+
array::Vector{PetscScalar}
41+
end
42+
Base.parent(v::VecSeq) = v.array
43+
44+
function VecSeq(
45+
petsclib::PetscLib,
46+
array::Vector{PetscScalar};
47+
blocksize = 1,
48+
comm::MPI.Comm = MPI.COMM_SELF,
49+
) where {PetscLib, PetscScalar}
50+
@assert initialized(petsclib)
51+
@assert PetscScalar == petsclib.PetscScalar
52+
v = VecSeq{PetscLib, PetscScalar}(C_NULL, array)
53+
LibPETSc.VecCreateSeqWithArray(
54+
petsclib,
55+
comm,
56+
blocksize,
57+
length(array),
58+
array,
59+
v,
60+
)
61+
finalizer(destroy, v)
62+
return v
2763
end
2864

29-
Base.eltype(::Type{V}) where {V<:AbstractVec{T}} where T = T
30-
Base.eltype(v::AbstractVec{T}) where {T} = T
31-
Base.size(v::AbstractVec) = (length(v),)
32-
Base.parent(v::AbstractVec) = v.array
65+
function destroy(v::AbstractVec{PetscLib}) where {PetscLib}
66+
finalized(PetscLib) || LibPETSc.VecDestroy(PetscLib, v)
67+
return nothing
68+
end
3369

34-
@for_libpetsc begin
35-
function VecSeq(comm::MPI.Comm, X::Vector{$PetscScalar}; blocksize=1)
36-
@assert initialized($petsclib)
37-
v = VecSeq(C_NULL, X)
38-
@chk ccall((:VecCreateSeqWithArray, $libpetsc), PetscErrorCode,
39-
(MPI.MPI_Comm, $PetscInt, $PetscInt, Ptr{$PetscScalar}, Ptr{CVec}),
40-
comm, blocksize, length(X), X, v)
41-
finalizer(destroy, v)
42-
return v
43-
end
44-
function destroy(v::AbstractVec{$PetscScalar})
45-
finalized($petsclib) ||
46-
@chk ccall((:VecDestroy, $libpetsc), PetscErrorCode, (Ptr{CVec},), v)
47-
return nothing
48-
end
49-
function Base.length(v::AbstractVec{$PetscScalar})
50-
r_sz = Ref{$PetscInt}()
51-
@chk ccall((:VecGetSize, $libpetsc), PetscErrorCode,
52-
(CVec, Ptr{$PetscInt}), v, r_sz)
53-
return r_sz[]
54-
end
55-
function LinearAlgebra.norm(v::AbstractVec{$PetscScalar}, normtype::NormType=NORM_2)
56-
r_val = Ref{$PetscReal}()
57-
@chk ccall((:VecNorm, $libpetsc), PetscErrorCode,
58-
(CVec, NormType, Ptr{$PetscReal}),
59-
v, normtype,r_val)
60-
return r_val[]
61-
end
70+
function Base.length(v::AbstractVec{PetscLib}) where {PetscLib}
71+
petsclib = getlib(PetscLib)
72+
PetscInt = petsclib.PetscInt
73+
r_sz = Ref{PetscInt}()
74+
LibPETSc.VecGetSize(PetscLib, v, r_sz)
75+
return r_sz[]
76+
end
6277

78+
function LinearAlgebra.norm(
79+
v::AbstractVec{PetscLib},
80+
normtype::LibPETSc.NormType = LibPETSc.NORM_2,
81+
) where {PetscLib}
82+
petsclib = getlib(PetscLib)
83+
PetscReal = petsclib.PetscReal
84+
r_val = Ref{PetscReal}()
85+
LibPETSc.VecNorm(PetscLib, v, normtype, r_val)
86+
return r_val[]
87+
end
88+
89+
90+
function view(
91+
vec::AbstractVec{PetscLib},
92+
viewer = LibPETSc.PETSC_VIEWER_STDOUT_(PetscLib, getcomm(vec))
93+
) where PetscLib
94+
LibPETSc.VecView(PetscLib, vec, viewer)
95+
return nothing
96+
end
97+
Base.show(io::IO, opts::AbstractVec) = _show(io, opts)
98+
Base.print_array(io::IO, opts::AbstractVec) = _show(io, opts)
99+
100+
#=
101+
@for_libpetsc begin
63102
function assemblybegin(V::AbstractVec{$PetscScalar})
64103
@chk ccall((:VecAssemblyBegin, $libpetsc), PetscErrorCode, (CVec,), V)
65104
return nothing
@@ -77,13 +116,6 @@ Base.parent(v::AbstractVec) = v.array
77116
r_lo[]:(r_hi[]-$PetscInt(1))
78117
end
79118
80-
function view(vec::AbstractVec{$PetscScalar}, viewer::AbstractViewer{$PetscLib}=ViewerStdout($petsclib, getcomm(vec)))
81-
@chk ccall((:VecView, $libpetsc), PetscErrorCode,
82-
(CVec, CPetscViewer),
83-
vec, viewer);
84-
return nothing
85-
end
86-
87119
function localsize(v::AbstractVec{$PetscScalar})
88120
return r_sz[]
89121
end
@@ -140,14 +172,9 @@ Use `read=false` if the array is write-only; `write=false` if read-only.
140172
"""
141173
unsafe_localarray
142174
143-
function Base.show(io::IO, ::MIME"text/plain", vec::AbstractVec)
144-
_show(io, vec)
145-
end
146-
147175
VecSeq(X::Vector{T}; kwargs...) where {T} = VecSeq(MPI.COMM_SELF, X; kwargs...)
148176
AbstractVec(X::AbstractVector) = VecSeq(X)
149177
150-
151178
"""
152179
ownership_range(vec::AbstractVec)
153180
@@ -159,4 +186,4 @@ Note: unlike the C function, the range returned is inclusive (`idx_first:idx_las
159186
$(_doc_external("Vec/VecGetOwnershipRange"))
160187
"""
161188
ownershiprange
162-
189+
=#

test/options.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Test
22
using PETSc
33

4-
@testset "options tests" begin
4+
@testset "options" begin
55
kw_opts = (
66
ksp_monitor = nothing,
77
ksp_view = true,
@@ -84,7 +84,7 @@ using PETSc
8484
end
8585
end
8686

87-
@testset "parse_options tests" begin
87+
@testset "parse_options" begin
8888
@test begin
8989
julia = joinpath(Sys.BINDIR, Base.julia_exename())
9090
run(`$(julia) --project -e "using PETSc

0 commit comments

Comments
 (0)