Skip to content

Commit 53fc7ab

Browse files
author
Jeremy E Kozdon
committed
WIP: Start add parallel matrix types
1 parent aad1ec4 commit 53fc7ab

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

src/mat.jl

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,109 @@ function MatSeqDense(
100100
return mat
101101
end
102102

103+
"""
104+
MatAIJ(
105+
petsclib::PetscLib,
106+
comm::MPI.Comm,
107+
loc_num_rows::Integer,
108+
loc_num_cols::Integer,
109+
diag_nonzeros::Union{Integer, Vector},
110+
off_diag_nonzeros::Union{Integer, Vector};
111+
glo_num_rows = PETSC_DETERMINE,
112+
glo_num_cols = PETSC_DETERMINE,
113+
setup = true
114+
) where {PetscLib <: PetscLibType}
115+
116+
Create an MPI PETSc sparse array on the `comm` using AIJ format (also known as a
117+
compressed sparse row or CSR format) of size `glo_num_rows X glo_num_cols` with
118+
local size `loc_num_rows X loc_num_cols`.
119+
120+
The diagonal block and off-diagonal block non-zeros are `diag_nonzeros` and
121+
`off_diag_nonzeros` which can be either an integer (same for all rows) or a
122+
Vector of `PetscInt`s with on entry per row.
123+
124+
Memory allocation is handled by PETSc and garbage collection can be used.
125+
126+
If `glo_num_rows isa Integer` or `glo_num_cols isa Integer` then the
127+
corresponding local variable can be `PETSC_DECIDE`.
128+
129+
If `setup == true` then [`setup!`](@ref) is called
130+
131+
# External Links
132+
$(_doc_external("Mat/MatCreateAIJ"))
133+
$(_doc_external("Mat/MatSetUp"))
134+
135+
!!! note
136+
137+
The user is responsible for calling `destroy(mat)` on the `MatAIJ` since
138+
this cannot be handled by the garbage collector do to the MPI nature of the
139+
object.
140+
"""
141+
mutable struct MatAIJ{PetscLib, PetscScalar} <:
142+
AbstractMat{PetscLib, PetscScalar}
143+
ptr::CMat
144+
end
145+
146+
function MatAIJ(
147+
petsclib::PetscLib,
148+
comm::MPI.Comm,
149+
loc_num_rows::Integer,
150+
loc_num_cols::Integer,
151+
diag_nonzeros::Union{Integer, Vector},
152+
off_diag_nonzeros::Union{Integer, Vector};
153+
glo_num_rows = PETSC_DETERMINE,
154+
glo_num_cols = PETSC_DETERMINE,
155+
setup = true,
156+
) where {PetscLib <: PetscLibType}
157+
@assert initialized(petsclib)
158+
PetscScalar = petsclib.PetscScalar
159+
mat = MatAIJ{PetscLib, PetscScalar}(C_NULL)
160+
if diag_nonzeros isa Integer
161+
diag_nonzero = diag_nonzeros
162+
diag_nonzeros = C_NULL
163+
else
164+
diag_nonzero = -1
165+
end
166+
if off_diag_nonzeros isa Integer
167+
off_diag_nonzero = off_diag_nonzeros
168+
off_diag_nonzeros = C_NULL
169+
else
170+
off_diag_nonzero = -1
171+
end
172+
173+
LibPETSc.MatCreateAIJ(
174+
petsclib,
175+
comm,
176+
loc_num_rows,
177+
loc_num_cols,
178+
glo_num_rows,
179+
glo_num_cols,
180+
diag_nonzero,
181+
diag_nonzeros,
182+
off_diag_nonzero,
183+
off_diag_nonzeros,
184+
mat,
185+
)
186+
187+
setup && setup!(mat)
188+
189+
return mat
190+
end
191+
192+
"""
193+
setup!(mat::AbstractMat)
194+
195+
Set up the interal data for `mat`
196+
197+
# External Links
198+
$(_doc_external("Mat/MatSetUp"))
199+
"""
200+
function setup!(mat::AbstractMat{PetscLib}) where {PetscLib}
201+
@assert initialized(PetscLib)
202+
LibPETSc.MatSetUp(PetscLib, mat)
203+
return mat
204+
end
205+
103206
"""
104207
assemble!(M::AbstractMat[, t::MatAssemblyType = MAT_FINAL_ASSEMBLY)
105208
@@ -151,6 +254,35 @@ function assemblyend!(
151254
return M
152255
end
153256

257+
"""
258+
ownershiprange(mat::AbstractMat, [base_one = true])
259+
260+
The range of row indices owned by this processor, assuming that the `mat` is
261+
laid out with the first `n1` rows on the first processor, next `n2` rows on the
262+
second, etc. For certain parallel layouts this range may not be well defined.
263+
264+
If the optional argument `base_one == true` then base-1 indexing is used,
265+
otherwise base-0 index is used.
266+
267+
!!! note
268+
269+
unlike the C function, the range returned is inclusive (`idx_first:idx_last`)
270+
271+
# External Links
272+
$(_doc_external("Mat/MatGetOwnershipRange"))
273+
"""
274+
function ownershiprange(
275+
mat::AbstractMat{PetscLib},
276+
base_one::Bool = true,
277+
) where {PetscLib}
278+
PetscInt = PetscLib.PetscInt
279+
r_lo = Ref{PetscInt}()
280+
r_hi = Ref{PetscInt}()
281+
LibPETSc.MatGetOwnershipRange(PetscLib, mat, r_lo, r_hi)
282+
return base_one ? ((r_lo[] + PetscInt(1)):(r_hi[])) :
283+
((r_lo[]):(r_hi[] - PetscInt(1)))
284+
end
285+
154286
function Base.size(A::AbstractMat{PetscLib}) where {PetscLib}
155287
m = Ref{PetscLib.PetscInt}()
156288
n = Ref{PetscLib.PetscInt}()

0 commit comments

Comments
 (0)