Skip to content

Commit

Permalink
Store if C isa Zeros in a MulAdd (#201)
Browse files Browse the repository at this point in the history
* Store if C isa Zeros in MulAdd

* Bump version to v1.6.0

* Rename field to Czero

* Update comment
  • Loading branch information
jishnub authored Feb 2, 2024
1 parent d542ead commit 20b0f6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArrayLayouts"
uuid = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
authors = ["Sheehan Olver <[email protected]>"]
version = "1.5.3"
version = "1.6.0"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
Expand Down
25 changes: 15 additions & 10 deletions src/muladd.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
### This support BLAS style multiplication
# α * A * B + β C
# A * B * α + C * β
# but avoids the broadcast machinery

# Lazy representation of α*A*B + β*C
# Lazy representation of A*B + C*β
struct MulAdd{StyleA, StyleB, StyleC, T, AA, BB, CC}
α::T
A::AA
B::BB
β::T
C::CC
Czero::Bool # this flag indicates whether C isa Zeros, or a copy of one
# the idea is that if Czero == true, then downstream packages don't need to
# fill C with zero before performing the muladd
end

@inline MulAdd{StyleA,StyleB,StyleC}::T, A::AA, B::BB, β::T, C::CC) where {StyleA,StyleB,StyleC,T,AA,BB,CC} =
MulAdd{StyleA,StyleB,StyleC,T,AA,BB,CC}(α,A,B,β,C)
@inline function MulAdd{StyleA,StyleB,StyleC}::T, A::AA, B::BB, β::T, C::CC;
Czero = C isa Zeros) where {StyleA,StyleB,StyleC,T,AA,BB,CC}
MulAdd{StyleA,StyleB,StyleC,T,AA,BB,CC}(α,A,B,β,C,Czero)
end

@inline function MulAdd{StyleA,StyleB,StyleC}(αT, A, B, βV, C) where {StyleA,StyleB,StyleC}
@inline function MulAdd{StyleA,StyleB,StyleC}(αT, A, B, βV, C; kw...) where {StyleA,StyleB,StyleC}
α,β = promote(αT,βV)
MulAdd{StyleA,StyleB,StyleC}(α, A, B, β, C)
MulAdd{StyleA,StyleB,StyleC}(α, A, B, β, C; kw...)
end

@inline MulAdd(α, A::AA, B::BB, β, C::CC) where {AA,BB,CC} =
MulAdd{typeof(MemoryLayout(AA)), typeof(MemoryLayout(BB)), typeof(MemoryLayout(CC))}(α, A, B, β, C)
@inline MulAdd(α, A::AA, B::BB, β, C::CC; kw...) where {AA,BB,CC} =
MulAdd{typeof(MemoryLayout(AA)), typeof(MemoryLayout(BB)), typeof(MemoryLayout(CC))}(α, A, B, β, C; kw...)

MulAdd(A, B) = MulAdd(Mul(A, B))
function MulAdd(M::Mul)
Expand Down Expand Up @@ -67,15 +72,15 @@ const BlasMatMulVecAdd{StyleA,StyleB,StyleC,T<:BlasFloat} = MulAdd{StyleA,StyleB
const BlasMatMulMatAdd{StyleA,StyleB,StyleC,T<:BlasFloat} = MulAdd{StyleA,StyleB,StyleC,T,<:AbstractMatrix{T},<:AbstractMatrix{T},<:AbstractMatrix{T}}
const BlasVecMulMatAdd{StyleA,StyleB,StyleC,T<:BlasFloat} = MulAdd{StyleA,StyleB,StyleC,T,<:AbstractVector{T},<:AbstractMatrix{T},<:AbstractMatrix{T}}

muladd!(α, A, B, β, C) = materialize!(MulAdd(α, A, B, β, C))
muladd!(α, A, B, β, C; kw...) = materialize!(MulAdd(α, A, B, β, C; kw...))
materialize(M::MulAdd) = copy(instantiate(M))
copy(M::MulAdd) = copyto!(similar(M), M)

_fill_copyto!(dest, C) = copyto!(dest, C)
_fill_copyto!(dest, C::Zeros) = zero!(dest) # exploit special fill! overload

@inline copyto!(dest::AbstractArray{T}, M::MulAdd) where T =
muladd!(M.α, unalias(dest,M.A), unalias(dest,M.B), M.β, _fill_copyto!(dest, M.C))
muladd!(M.α, unalias(dest,M.A), unalias(dest,M.B), M.β, _fill_copyto!(dest, M.C); Czero = M.Czero)

# Modified from LinearAlgebra._generic_matmatmul!
const tilebufsize = 10800 # Approximately 32k/3
Expand Down

2 comments on commit 20b0f6c

@jishnub
Copy link
Member Author

@jishnub jishnub commented on 20b0f6c Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/100096

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.0 -m "<description of version>" 20b0f6c42f70b74b9c0f0fc088148f2e07f68558
git push origin v1.6.0

Please sign in to comment.