Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DArray: Add sparse matmul support #507

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/array/darray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ chunks(a::ArrayDomain{N}) where {N} = DomainBlocks(
ntuple(i->first(indexes(a)[i]), Val(N)), map(x->[length(x)], indexes(a)))

(==)(a::ArrayDomain, b::ArrayDomain) = indexes(a) == indexes(b)
Base.getindex(arr::AbstractArray, d::ArrayDomain) = arr[indexes(d)...]

function intersect(a::ArrayDomain, b::ArrayDomain)
if a === b
Expand Down Expand Up @@ -452,7 +451,8 @@ function stage(ctx::Context, d::Distribute)
cs = map(d.domainchunks) do c
# TODO: fix hashing
#hash = uhash(c, Base.hash(Distribute, Base.hash(d.data)))
Dagger.@spawn identity(d.data[c])
data_view = d.data[indexes(c)...]
Dagger.@spawn identity(data_view)
end
end
return DArray(eltype(d.data),
Expand Down
29 changes: 13 additions & 16 deletions src/array/mul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function LinearAlgebra.generic_matmatmul!(
return gemm_dagger!(C, transA, transB, A, B, _add)
end
end
# FIXME: Mixed-precision methods
function _repartition_matmatmul(C, A, B, transA::Char, transB::Char)
partA = A.partitioning.blocksize
partB = B.partitioning.blocksize
Expand Down Expand Up @@ -124,28 +125,26 @@ function gemm_dagger!(
# A: NoTrans / B: NoTrans
for k in range(1, Ant)
mzone = k == 1 ? beta : T(1.0)
Dagger.@spawn BLAS.gemm!(
Dagger.@spawn LinearAlgebra.generic_matmatmul!(
InOut(Cc[m, n]),
transA,
transB,
alpha,
In(Ac[m, k]),
In(Bc[k, n]),
mzone,
InOut(Cc[m, n]),
LinearAlgebra.MulAddMul(alpha, mzone),
)
end
else
# A: NoTrans / B: [Conj]Trans
for k in range(1, Ant)
mzone = k == 1 ? beta : T(1.0)
Dagger.@spawn BLAS.gemm!(
Dagger.@spawn LinearAlgebra.generic_matmatmul!(
InOut(Cc[m, n]),
transA,
transB,
alpha,
In(Ac[m, k]),
In(Bc[n, k]),
mzone,
InOut(Cc[m, n]),
LinearAlgebra.MulAddMul(alpha, mzone),
)
end
end
Expand All @@ -154,28 +153,26 @@ function gemm_dagger!(
# A: [Conj]Trans / B: NoTrans
for k in range(1, Amt)
mzone = k == 1 ? beta : T(1.0)
Dagger.@spawn BLAS.gemm!(
Dagger.@spawn LinearAlgebra.generic_matmatmul!(
InOut(Cc[m, n]),
transA,
transB,
alpha,
In(Ac[k, m]),
In(Bc[k, n]),
mzone,
InOut(Cc[m, n]),
LinearAlgebra.MulAddMul(alpha, mzone),
)
end
else
# A: [Conj]Trans / B: [Conj]Trans
for k in range(1, Amt)
mzone = k == 1 ? beta : T(1.0)
Dagger.@spawn BLAS.gemm!(
Dagger.@spawn LinearAlgebra.generic_matmatmul!(
InOut(Cc[m, n]),
transA,
transB,
alpha,
In(Ac[k, m]),
In(Bc[n, k]),
mzone,
InOut(Cc[m, n]),
LinearAlgebra.MulAddMul(alpha, mzone),
)
end
end
Expand Down
35 changes: 35 additions & 0 deletions test/array/linalg/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,71 @@ function test_gemm!(T, szA, szB, partA, partB)
DA = distribute(A, partA)
DB = distribute(B, partB)

SA = sprand(T, szA..., 0.1)
SB = sprand(T, szA..., 0.1)

DSA = distribute(SA, partA)
DSB = distribute(SB, partB)

## Out-of-place gemm
# No transA, No transB
# Dense
DC = DA * DB
C = A * B
@test collect(DC) ≈ C
# Sparse
DSC = DSA * DSB
SC = SA * SB
@test collect(DSC) ≈ SC

if szA == szB
# No transA, transB
# Dense
DC = DA * DB'
C = A * B'
@test collect(DC) ≈ C
# Sparse
DSC = DSA * DSB'
SC = SA * SB'
@test collect(DSC) ≈ SC

# transA, No transB
# Dense
DC = DA' * DB
C = A' * B
@test collect(DC) ≈ C
# Sparse
DSC = DSA' * DSB
SC = SA' * SB
@test collect(DSC) ≈ SC
end

# transA, transB
# Dense
DC = DA' * DB'
C = A' * B'
@test collect(DC) ≈ C
#= Sparse
DSC = DSA' * DSB'
SC = SA' * SB'
@test collect(DSC) ≈ SC
=#

## In-place gemm
# No transA, No transB
# Dense
C = zeros(T, szC...)
DC = distribute(C, partC)
mul!(C, A, B)
mul!(DC, DA, DB)
@test collect(DC) ≈ C
#= Sparse
SC = zeros(T, szC...)
DSC = distribute(SC, partC)
mul!(SC, SA, SB)
mul!(DSC, DSA, DSB)
@test collect(DSC) ≈ SC
=#

if szA == szB
# No transA, transB
Expand Down
Loading