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

remove allocation in oneelement mul #255

Merged
merged 2 commits into from
Jun 5, 2023
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FillArrays"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "1.1.0"
version = "1.1.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
28 changes: 14 additions & 14 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,24 @@ function Base.setindex(A::Zeros{T,N}, v, kj::Vararg{Int,N}) where {T,N}
OneElement(convert(T, v), kj, axes(A))
end

@inline function __mulonel!(y, A, x, alpha, beta)
αx = alpha * x.val
ind1 = x.ind[1]
if iszero(beta)
y .= αx .* view(A, :, ind1)
else
y .= αx .* view(A, :, ind1) .+ beta .* y
end
return y
end

function _mulonel!(y, A, x::OneElementVector, alpha::Number, beta::Number)
check_matmul_sizes(y, A, x)
if x.ind[1] ∉ axes(x,1) # in this case x is all zeros
mul!(y, A, Zeros{eltype(x)}(axes(x)), alpha, beta)
return y
end
αx = alpha * x.val
if iszero(beta)
y .= αx .* view(A, :, x.ind[1])
else
y .= αx .* view(A, :, x.ind[1]) .+ beta .* y
end
__mulonel!(y, A, x, alpha, beta)
y
end

Expand All @@ -77,14 +83,8 @@ function _mulonel!(C, A, B::OneElementMatrix, alpha::Number, beta::Number)
mul!(C, A, Zeros{eltype(B)}(axes(B)), alpha, beta)
return C
end
αB = alpha * B.val
if iszero(beta)
C .= zero(eltype(C))
C[:, B.ind[2]] .= αB .* view(A, :, B.ind[1])
else
lmul!(beta, C)
C[:, B.ind[2]] .+= αB .* view(A, :, B.ind[1])
end
y = @view C[:, B.ind[2]]
__mulonel!(y, A, B, alpha, beta)
C
end

Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ end
@test_throws BoundsError Base.setindex(Zeros(5), 2, 6)

@testset "matmul" begin
A = rand(3,3)
A = reshape(Float64[1:9;], 3, 3)
@testset "vector" begin
w = zeros(size(A,1))
w2 = MVector{length(w)}(w)
Expand Down