JuliaSIMD / JuliaSIMD/LoopVectorization.jl

Incorrect results using @turbo with linear array indexing

Offen
#512 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Julia
Sterne
789
Forks
73
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

There was some discussion about this issue [here](https://discourse.julialang.org/t/inconsistent-results-using-loopvectorization-turbo-with-linear-indexing/104481). Essentially, when using linear indexing of an array `@turbo` appears to start indexing the array incorrectly when a offset term is used. An example of this (code and commentary copied directly from the discourse discussion) is below.
```
julia> H = collect(reshape(1.0:6.0, 3, 2))
3×2 Matrix{Float64}:
1.0 4.0
2.0 5.0
3.0 6.0

julia> su = zeros(3);

julia> for k = 1:1
ind = 0 # LinearIndices((3, 2))[1, 1] - 1
for ij in 1:3
su[ij] += H[ij + ind]
end
end

julia> su
3-element Vector{Float64}:
1.0
2.0
3.0

julia> su = zeros(3);

julia> for k = 1:1
ind = 0
@turbo for ij in 1:3
su[ij] += H[ij + ind]
end
end

julia> su
3-element Vector{Float64}:
6.94274366677856e-310
1.0
2.0
```

so `H` is being indexed at 0, 1, 2 instead of 1, 2, 3. The effect doesn’t seem to happen on the left hand expression e.g. `su[ij + ind]` and doesn’t seem to shift more with more additions e.g. `H[ij + ind + ind]`. It does it correctly if the index was added to a literal 0 instead of `ind` or if not added:
```
julia> su = zeros(3);

julia> for k = 1:1
@turbo for ij in 1:3
su[ij] += H[ij + 0]
end
end

julia> su
3-element Vector{Float64}:
1.0
2.0
3.0

julia> su = zeros(3);

julia> for k = 1:1
@turbo for ij in 1:3
su[ij] += H[ij]
end
end

julia> su
3-element Vector{Float64}:
1.0
2.0
3.0
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start by reproducing the provided Julia example with @turbo and linear array indexing, then compare its result with the ordinary loop. Trace the @turbo handling of the offset term in H[ij + ind]. Done means the optimized loop produces the same values as the unoptimized loop, with a regression test covering this case.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
julia
Bereich
performance
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.