Skip to content

Commit da0fa97

Browse files
committed
Non-allocating __maybe_matmul
1 parent 6cc12b8 commit da0fa97

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

lib/BoundaryValueDiffEqCore/src/utils.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,20 @@ function diff!(dx, x)
106106
return dx
107107
end
108108

109-
function __maybe_matmul!(z::AbstractArray, A, b, α = eltype(z)(1), β = eltype(z)(0))
110-
return mul!(z, A, b, α, β)
111-
end
109+
function __maybe_matmul!(z, A, b, α = one(eltype(z)), β = zero(eltype(z)))
110+
# First z = β*z
111+
@inbounds for i in eachindex(z)
112+
z[i] *= β
113+
end
112114

113-
# NOTE: We can implement it as mul! as above but then we pay the cost of moving
114-
# `w` to the GPU too many times. Instead if we iterate of w and w′ we save
115-
# that cost. Our main cost is anyways going to be due to a large `u0` and
116-
# we are going to use GPUs for that
117-
@views function __maybe_matmul!(z, A, b, α = eltype(z)(1), β = eltype(z)(0))
118-
@simd ivdep for j in eachindex(b)
119-
@inbounds @. z = α * A[:, j] * b[j] + β * z
115+
# Then z += α*A*b
116+
@inbounds for j in axes(A, 2)
117+
bj = α * b[j]
118+
for i in axes(A, 1)
119+
z[i] += A[i, j] * bj
120+
end
120121
end
122+
121123
return z
122124
end
123125

0 commit comments

Comments
 (0)