You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
slow sum function for custom arrays with IndexCartesian #33928
I have a custom array MyArray with Base.IndexStyle(a::MyArray) = IndexCartesian(). I noted that the builtin sum function is slower than a naive mysum implementation.
See this MWE
using BenchmarkTools
struct MyArray{T, A<:AbstractArray{T, 3}} <:AbstractArray{T, 3}
data::Aend
Base.size(a::MyArray) =size(a.data)
Base.@propagate_inbounds@inlinefunction Base.getindex(a::MyArray, i, j, k)
@boundscheckcheckbounds(a.data, i, j, k)
@inbounds v = a.data[i, j, k]
return v
end# change this to IndexLinear to get ~5x speed up
Base.IndexStyle(a::MyArray) =IndexCartesian()
functionmysum(a)
S =zero(eltype(a))
for k =1:size(a, 3)
for j =1:size(a, 2)
@simdfor i =1:size(a, 1)
@inbounds S += a[i, j, k]
endendendreturn S
end
N =64
pa =rand(N, N, N)
a =MyArray(pa)
@btimemysum($a)
@btimesum($a)
@btimesum($pa)
Activity
KristofferC commentedon Nov 23, 2019
With
IndexLinear()the following specialized method is hit:julia/base/reduce.jl
Lines 147 to 168 in 1ee8965
which uses
@simdand@inbounds, otherwise, we end up injulia/base/reduce.jl
Lines 39 to 52 in 1ee8965
which does not use these.
brenhinkeller commentedon Nov 20, 2022
Some current timings on 1.8.3 on an M1 mac
adienes commentedon May 5, 2025
still slow
mbauman commentedon May 5, 2025
Yep, this is on my punchlist. It's an easy fix, just need to get #58241 in first. It's this fallback:
julia/base/reduce.jl
Line 450 in a909276