JuliaSIMD / JuliaSIMD/LoopVectorization.jl
Plain for loop faster than @turbo
- Langage dominant
- Julia
- Étoiles
- 789
- Forks
- 73
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
Consider the following:
```julia
using LoopVectorization, StrideArraysCore, BenchmarkTools
randn_stridearray(size...) = StrideArray(randn(Float32, size...), static.(size))
y = randn_stridearray(64, 32, 256);
x = randn_stridearray(64, 32, 256);
b = randn_stridearray(32);
function foo!(y, x, b)
@turbo for i in axes(y, 1), c in axes(y, 2), n in axes(y, 3)
y[i, c, n] = x[i, c, n] + b[c]
end
return nothing
end
function foo2!(y, x, b)
for n in axes(y, 3)
@turbo for i in axes(y, 1), c in axes(y, 2)
y[i, c, n] = x[i, c, n] + b[c]
end
end
return nothing
end
```
I would expect `foo2` to perform slightly worse or the same as `foo`.
However, the result is
```
julia> @btime foo!($y, $x, $b)
153.958 μs (0 allocations: 0 bytes)
julia> @btime foo2!($y, $x, $b)
51.583 μs (0 allocations: 0 bytes)
```
What is happening here? Am I doing something wrong?
If it matters: I am running this on an Apple M1.
Edit:
A plain for loop is about as fast as `foo2!`:
```
julia> function foo3!(y, x, b)
for n in axes(y, 3)
for c in axes(y, 2)
for i in axes(y, 1)
y[i, c, n] = x[i, c, n] + b[c]
end
end
end
return nothing
end
foo3! (generic function with 1 method)
julia> @btime foo3!($y, $x, $b)
52.375 μs (0 allocations: 0 bytes)
```
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Start by reproducing the reported timings for foo!, foo2!, and foo3! from the issue using the shown Julia, LoopVectorization, StrideArraysCore, and BenchmarkTools setup. Compare the generated behavior or benchmark results for the three loop forms; the investigation is complete when the performance difference is explained and any needed project change is identified.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- julia
- Domaine
- performance
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 35/100