Skip to content

Commit b7bc762

Browse files
committed
Adding some operations to PSparseMatrix
1 parent 9b8ffc1 commit b7bc762

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

src/p_sparse_matrix.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,42 @@ function LinearAlgebra.fillstored!(a::AbstractSplitMatrix,v)
697697
a
698698
end
699699

700+
function Base.:*(a::Number,b::AbstractSplitMatrix)
701+
own_own = a*b.blocks.own_own
702+
own_ghost = a*b.blocks.own_ghost
703+
ghost_own = a*b.blocks.ghost_own
704+
ghost_ghost = a*b.blocks.ghost_ghost
705+
blocks = split_matrix_blocks(own_own,own_ghost,ghost_own,ghost_ghost)
706+
split_matrix(blocks,b.row_permutation,b.col_permutation)
707+
end
708+
709+
function Base.:*(b::AbstractSplitMatrix,a::Number)
710+
a*b
711+
end
712+
713+
for op in (:+,:-)
714+
@eval begin
715+
function Base.$op(a::AbstractSplitMatrix)
716+
own_own = $op(a.blocks.own_own)
717+
own_ghost = $op(a.blocks.own_ghost)
718+
ghost_own = $op(a.blocks.ghost_own)
719+
ghost_ghost = $op(a.blocks.ghost_ghost)
720+
blocks = split_matrix_blocks(own_own,own_ghost,ghost_own,ghost_ghost)
721+
split_matrix(blocks,a.row_permutation,a.col_permutation)
722+
end
723+
function Base.$op(a::AbstractSplitMatrix,b::AbstractSplitMatrix)
724+
@boundscheck @assert a.row_permutation == b.row_permutation
725+
@boundscheck @assert a.col_permutation == b.col_permutation
726+
own_own = $op(a.blocks.own_own,b.blocks.own_own)
727+
own_ghost = $op(a.blocks.own_ghost,b.blocks.own_ghost)
728+
ghost_own = $op(a.blocks.ghost_own,b.blocks.ghost_own)
729+
ghost_ghost = $op(a.blocks.ghost_ghost,b.blocks.ghost_ghost)
730+
blocks = split_matrix_blocks(own_own,own_ghost,ghost_own,ghost_ghost)
731+
split_matrix(blocks,b.row_permutation,b.col_permutation)
732+
end
733+
end
734+
end
735+
700736
function split_format_locally(A,rows,cols)
701737
n_own_rows = own_length(rows)
702738
n_own_cols = own_length(cols)
@@ -1582,6 +1618,52 @@ function psparse_consitent_impl!(B,A,::Type{<:AbstractSplitMatrix},cache)
15821618
end
15831619
end
15841620

1621+
function Base.:*(a::PSparseMatrix,b::PVector)
1622+
Ta = eltype(a)
1623+
Tb = eltype(b)
1624+
T = typeof(zero(Ta)*zero(Tb)+zero(Ta)*zero(Tb))
1625+
c = PVector{Vector{T}}(undef,partition(axes(a,1)))
1626+
mul!(c,a,b)
1627+
c
1628+
end
1629+
1630+
function Base.:*(a::Number,b::PSparseMatrix)
1631+
matrix_partition = map(partition(b)) do values
1632+
a*values
1633+
end
1634+
rows = partition(axes(b,1))
1635+
cols = partition(axes(b,2))
1636+
PSparseMatrix(matrix_partition,rows,cols,b.assembled)
1637+
end
1638+
1639+
function Base.:*(b::PSparseMatrix,a::Number)
1640+
a*b
1641+
end
1642+
1643+
for op in (:+,:-)
1644+
@eval begin
1645+
function Base.$op(a::PSparseMatrix)
1646+
matrix_partition = map(partition(a)) do a
1647+
$op(a)
1648+
end
1649+
rows = partition(axes(a,1))
1650+
cols = partition(axes(a,2))
1651+
PSparseMatrix(matrix_partition,rows,cols,a.assembled)
1652+
end
1653+
function Base.$op(a::PSparseMatrix,b::PSparseMatrix)
1654+
@boundscheck @assert matching_local_indices(axes(a,1),axes(b,1))
1655+
@boundscheck @assert matching_local_indices(axes(a,2),axes(b,2))
1656+
matrix_partition = map(partition(a),partition(b)) do a,b
1657+
$op(a,b)
1658+
end
1659+
rows = partition(axes(b,1))
1660+
cols = partition(axes(b,2))
1661+
assembled = a.assembled && b.assembled
1662+
PSparseMatrix(matrix_partition,rows,cols,assembled)
1663+
end
1664+
end
1665+
end
1666+
15851667
function LinearAlgebra.mul!(c::PVector,a::PSparseMatrix,b::PVector::Number::Number)
15861668
@boundscheck @assert matching_own_indices(axes(c,1),axes(a,1))
15871669
@boundscheck @assert matching_own_indices(axes(a,2),axes(b,1))

test/p_sparse_matrix_tests.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ function p_sparse_matrix_tests(distribute)
217217
A = psparse(I,J,V,row_partition,col_partition) |> fetch
218218
x = pones(partition(axes(A,2)))
219219
y = A*x
220+
@test isa(y,PVector)
220221
dy = y - y
221222

222223
x = IterativeSolvers.cg(A,y)
@@ -299,10 +300,19 @@ function p_sparse_matrix_tests(distribute)
299300
display((A,A))
300301
display((b,b))
301302

302-
#Ar = renumber(A)
303-
#br,cache = renumber(b,partition(axes(Ar,1)),reuse=true)
304-
#cr = Ar\br
305-
#renumber!(c,cr)
303+
LinearAlgebra.fillstored!(A,3)
304+
B = 2*A
305+
@test eltype(partition(B)) == eltype(partition(A))
306+
B = A*2
307+
@test eltype(partition(B)) == eltype(partition(A))
308+
B = +A
309+
@test eltype(partition(B)) == eltype(partition(A))
310+
B = -A
311+
@test eltype(partition(B)) == eltype(partition(A))
312+
C = B+A
313+
@test eltype(partition(C)) == eltype(partition(A))
314+
C = B-A
315+
@test eltype(partition(C)) == eltype(partition(A))
306316

307317
end
308318

0 commit comments

Comments
 (0)