Skip to content

Change order of function definition in matrix multiplication code #833

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

Merged
Merged
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
262 changes: 0 additions & 262 deletions src/matrix_multiply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,150 +15,6 @@ import LinearAlgebra: BlasFloat, matprod, mul!
@inline *(A::StaticArray{Tuple{N,1},<:Any,2}, B::Adjoint{<:Any,<:StaticVector}) where {N} = vec(A) * B
@inline *(A::StaticArray{Tuple{N,1},<:Any,2}, B::Transpose{<:Any,<:StaticVector}) where {N} = vec(A) * B

"""
gen_by_access(expr_gen, a::Type{<:AbstractArray}, asym = :wrapped_a)

Statically generate outer code for fully unrolled multiplication loops.
Returned code does wrapper-specific tests (for example if a symmetric matrix view is
`U` or `L`) and the body of the if expression is then generated by function `expr_gen`.
The function `expr_gen` receives access pattern description symbol as its argument
and this symbol is then consumed by uplo_access to generate the right code for matrix
element access.

The name of the matrix to test is indicated by `asym`.
"""
function gen_by_access(expr_gen, a::Type{<:StaticVecOrMat}, asym = :wrapped_a)
return expr_gen(:any)
end
function gen_by_access(expr_gen, a::Type{<:Symmetric{<:Any, <:StaticMatrix}}, asym = :wrapped_a)
return quote
if $(asym).uplo == 'U'
$(expr_gen(:up))
else
$(expr_gen(:lo))
end
end
end
function gen_by_access(expr_gen, a::Type{<:Hermitian{<:Any, <:StaticMatrix}}, asym = :wrapped_a)
return quote
if $(asym).uplo == 'U'
$(expr_gen(:up_herm))
else
$(expr_gen(:lo_herm))
end
end
end
function gen_by_access(expr_gen, a::Type{<:UpperTriangular{<:Any, <:StaticMatrix}}, asym = :wrapped_a)
return expr_gen(:upper_triangular)
end
function gen_by_access(expr_gen, a::Type{<:LowerTriangular{<:Any, <:StaticMatrix}}, asym = :wrapped_a)
return expr_gen(:lower_triangular)
end
function gen_by_access(expr_gen, a::Type{<:UnitUpperTriangular{<:Any, <:StaticMatrix}}, asym = :wrapped_a)
return expr_gen(:unit_upper_triangular)
end
function gen_by_access(expr_gen, a::Type{<:UnitLowerTriangular{<:Any, <:StaticMatrix}}, asym = :wrapped_a)
return expr_gen(:unit_lower_triangular)
end
function gen_by_access(expr_gen, a::Type{<:Transpose{<:Any, <:StaticVecOrMat}}, asym = :wrapped_a)
return expr_gen(:transpose)
end
function gen_by_access(expr_gen, a::Type{<:Adjoint{<:Any, <:StaticVecOrMat}}, asym = :wrapped_a)
return expr_gen(:adjoint)
end
function gen_by_access(expr_gen, a::Type{<:SDiagonal}, asym = :wrapped_a)
return expr_gen(:diagonal)
end
"""
gen_by_access(expr_gen, a::Type{<:AbstractArray}, b::Type{<:AbstractArray})

Simiar to gen_by_access with only one type argument. The difference is that tests for both
arrays of type `a` and `b` are generated and `expr_gen` receives two access arguments,
first for matrix `a` and the second for matrix `b`.
"""
function gen_by_access(expr_gen, a::Type{<:StaticMatrix}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:any, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:Symmetric{<:Any, <:StaticMatrix}}, b::Type)
return quote
if wrapped_a.uplo == 'U'
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:up, access_b)
end)
else
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:lo, access_b)
end)
end
end
end
function gen_by_access(expr_gen, a::Type{<:Hermitian{<:Any, <:StaticMatrix}}, b::Type)
return quote
if wrapped_a.uplo == 'U'
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:up_herm, access_b)
end)
else
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:lo_herm, access_b)
end)
end
end
end
function gen_by_access(expr_gen, a::Type{<:UpperTriangular{<:Any, <:StaticMatrix}}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:upper_triangular, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:LowerTriangular{<:Any, <:StaticMatrix}}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:lower_triangular, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:UnitUpperTriangular{<:Any, <:StaticMatrix}}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:unit_upper_triangular, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:UnitLowerTriangular{<:Any, <:StaticMatrix}}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:unit_lower_triangular, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:Transpose{<:Any, <:StaticMatrix}}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:transpose, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:Adjoint{<:Any, <:StaticMatrix}}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:adjoint, access_b)
end)
end
end
function gen_by_access(expr_gen, a::Type{<:SDiagonal}, b::Type)
return quote
return $(gen_by_access(b, :wrapped_b) do access_b
expr_gen(:diagonal, access_b)
end)
end
end

"""
mul_result_structure(a::Type, b::Type)

Expand Down Expand Up @@ -202,99 +58,6 @@ function mul_result_structure(::SDiagonal, ::SDiagonal)
return Diagonal
end

"""
uplo_access(sa, asym, k, j, uplo)

Generate code for matrix element access, for a matrix of size `sa` locally referred to
as `asym` in the context where the result will be used. Both indices `k` and `j` need to be
statically known for this function to work. `uplo` is the access pattern mode generated
by the `gen_by_access` function.
"""
function uplo_access(sa, asym, k, j, uplo)
TAsym = Symbol("T"*string(asym))
if uplo == :any
return :($asym[$(LinearIndices(sa)[k, j])])
elseif uplo == :up
if k < j
return :($asym[$(LinearIndices(sa)[k, j])])
elseif k == j
return :(LinearAlgebra.symmetric($asym[$(LinearIndices(sa)[k, j])], :U))
else
return :(transpose($asym[$(LinearIndices(sa)[j, k])]))
end
elseif uplo == :lo
if k > j
return :($asym[$(LinearIndices(sa)[k, j])])
elseif k == j
return :(LinearAlgebra.symmetric($asym[$(LinearIndices(sa)[k, j])], :L))
else
return :(transpose($asym[$(LinearIndices(sa)[j, k])]))
end
elseif uplo == :up_herm
if k < j
return :($asym[$(LinearIndices(sa)[k, j])])
elseif k == j
return :(LinearAlgebra.hermitian($asym[$(LinearIndices(sa)[k, j])], :U))
else
return :(adjoint($asym[$(LinearIndices(sa)[j, k])]))
end
elseif uplo == :lo_herm
if k > j
return :($asym[$(LinearIndices(sa)[k, j])])
elseif k == j
return :(LinearAlgebra.hermitian($asym[$(LinearIndices(sa)[k, j])], :L))
else
return :(adjoint($asym[$(LinearIndices(sa)[j, k])]))
end
elseif uplo == :upper_triangular
if k <= j
return :($asym[$(LinearIndices(sa)[k, j])])
else
return :(zero($TAsym))
end
elseif uplo == :lower_triangular
if k >= j
return :($asym[$(LinearIndices(sa)[k, j])])
else
return :(zero($TAsym))
end
elseif uplo == :unit_upper_triangular
if k < j
return :($asym[$(LinearIndices(sa)[k, j])])
elseif k == j
return :(oneunit($TAsym))
else
return :(zero($TAsym))
end
elseif uplo == :unit_lower_triangular
if k > j
return :($asym[$(LinearIndices(sa)[k, j])])
elseif k == j
return :(oneunit($TAsym))
else
return :(zero($TAsym))
end
elseif uplo == :upper_hessenberg
if k <= j+1
return :($asym[$(LinearIndices(sa)[k, j])])
else
return :(zero($TAsym))
end
elseif uplo == :transpose
return :(transpose($asym[$(LinearIndices(reverse(sa))[j, k])]))
elseif uplo == :adjoint
return :(adjoint($asym[$(LinearIndices(reverse(sa))[j, k])]))
elseif uplo == :diagonal
if k == j
return :($asym[$k])
else
return :(zero($TAsym))
end
else
error("Unknown uplo: $uplo")
end
end

# Implementations

function mul_smat_vec_exprs(sa, access_a)
Expand Down Expand Up @@ -369,31 +132,6 @@ for TWR in [Adjoint, Transpose, Symmetric, Hermitian, LowerTriangular, UpperTria
@eval _unstatic_array(::Type{$TWR{T,TSA}}) where {S, T, N, TSA<:StaticArray{S,T,N}} = $TWR{T,<:AbstractArray{T,N}}
end

function combine_products(expr_list)
filtered = filter(expr_list) do expr
if expr.head != :call || expr.args[1] != :*
error("expected call to *")
end
for arg in expr.args[2:end]
if isa(arg, Expr) && arg.head == :call && arg.args[1] == :zero
return false
end
end
return true
end
if isempty(filtered)
return :(zero(T))
else
return reduce(filtered) do ex1, ex2
if ex2.head != :call || ex2.args[1] != :*
error("expected call to *")
end

return :(muladd($(ex2.args[2]), $(ex2.args[3]), $ex1))
end
end
end

@generated function _mul(Sa::Size{sa}, Sb::Size{sb}, a::StaticMatMulLike{<:Any, <:Any, Ta}, b::StaticMatMulLike{<:Any, <:Any, Tb}) where {sa, sb, Ta, Tb}
S = Size(sa[1], sb[2])
# Heuristic choice for amount of codegen
Expand Down
Loading