-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SparseArrayInterface] [BlockSparseArrays] Sparse and block sparse do…
…t products
- Loading branch information
Showing
9 changed files
with
141 additions
and
21 deletions.
There are no files selected for viewing
14 changes: 12 additions & 2 deletions
14
NDTensors/src/lib/BlockSparseArrays/src/abstractblocksparsearray/arraylayouts.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 31 additions & 6 deletions
37
NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/arraylayouts.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,48 @@ | ||
using ArrayLayouts: ArrayLayouts, MatMulMatAdd | ||
using ArrayLayouts: ArrayLayouts, Dot, MatMulMatAdd, MatMulVecAdd, MulAdd | ||
using BlockArrays: BlockLayout | ||
using ..SparseArrayInterface: SparseLayout | ||
using LinearAlgebra: mul! | ||
using LinearAlgebra: dot, mul! | ||
|
||
function blocksparse_muladd!( | ||
α::Number, a1::AbstractMatrix, a2::AbstractMatrix, β::Number, a_dest::AbstractMatrix | ||
α::Number, a1::AbstractArray, a2::AbstractArray, β::Number, a_dest::AbstractArray | ||
) | ||
mul!(blocks(a_dest), blocks(a1), blocks(a2), α, β) | ||
return a_dest | ||
end | ||
|
||
function blocksparse_matmul!(m::MulAdd) | ||
α, a1, a2, β, a_dest = m.α, m.A, m.B, m.β, m.C | ||
blocksparse_muladd!(α, a1, a2, β, a_dest) | ||
return a_dest | ||
end | ||
|
||
function ArrayLayouts.materialize!( | ||
m::MatMulMatAdd{ | ||
<:BlockLayout{<:SparseLayout}, | ||
<:BlockLayout{<:SparseLayout}, | ||
<:BlockLayout{<:SparseLayout}, | ||
}, | ||
) | ||
α, a1, a2, β, a_dest = m.α, m.A, m.B, m.β, m.C | ||
blocksparse_muladd!(α, a1, a2, β, a_dest) | ||
return a_dest | ||
blocksparse_matmul!(m) | ||
return m.C | ||
end | ||
function ArrayLayouts.materialize!( | ||
m::MatMulVecAdd{ | ||
<:BlockLayout{<:SparseLayout}, | ||
<:BlockLayout{<:SparseLayout}, | ||
<:BlockLayout{<:SparseLayout}, | ||
}, | ||
) | ||
blocksparse_matmul!(m) | ||
return m.C | ||
end | ||
|
||
function blocksparse_dot(a1::AbstractArray, a2::AbstractArray) | ||
# TODO: Add a check that the blocking of `a1` and `a2` are | ||
# the same, or the same up to a reshape. | ||
return dot(blocks(a1), blocks(a2)) | ||
end | ||
|
||
function Base.copy(d::Dot{<:BlockLayout{<:SparseLayout},<:BlockLayout{<:SparseLayout}}) | ||
return blocksparse_dot(d.A, d.B) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 19 additions & 4 deletions
23
NDTensors/src/lib/SparseArrayInterface/src/abstractsparsearray/arraylayouts.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
using ArrayLayouts: ArrayLayouts, MatMulMatAdd | ||
using ArrayLayouts: ArrayLayouts, Dot, MatMulMatAdd, MatMulVecAdd, MulAdd | ||
|
||
function ArrayLayouts.MemoryLayout(arraytype::Type{<:SparseArrayLike}) | ||
return SparseLayout() | ||
end | ||
|
||
function ArrayLayouts.materialize!( | ||
m::MatMulMatAdd{<:AbstractSparseLayout,<:AbstractSparseLayout,<:AbstractSparseLayout} | ||
) | ||
function sparse_matmul!(m::MulAdd) | ||
α, a1, a2, β, a_dest = m.α, m.A, m.B, m.β, m.C | ||
sparse_mul!(a_dest, a1, a2, α, β) | ||
return a_dest | ||
end | ||
|
||
function ArrayLayouts.materialize!( | ||
m::MatMulMatAdd{<:AbstractSparseLayout,<:AbstractSparseLayout,<:AbstractSparseLayout} | ||
) | ||
sparse_matmul!(m) | ||
return m.C | ||
end | ||
function ArrayLayouts.materialize!( | ||
m::MatMulVecAdd{<:AbstractSparseLayout,<:AbstractSparseLayout,<:AbstractSparseLayout} | ||
) | ||
sparse_matmul!(m) | ||
return m.C | ||
end | ||
|
||
function Base.copy(d::Dot{<:SparseLayout,<:SparseLayout}) | ||
return sparse_dot(d.A, d.B) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters