JuliaSIMD / JuliaSIMD/LoopVectorization.jl

Nested loop UndefVarError

Offen
#321 3 Kommentare 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

Hey, thanks for the great library!

I'm not sure if this is a bug, but the following code doesn't work for me for no obvious reason:
```julia
function logmean(samples, weights)
logμ = zero(first(samples))
n = length(samples)
k = length(logμ)
@turbo for i in 1:n
for j in 1:k
logμ[j] = logμ[j] + weights[i] * log(samples[i][j])
end
end
return logμ
end
```

```julia
logmean([ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ], [ 0.5, 0.5 ])
UndefVarError: i not defined

Stacktrace:
[1] logmean(samples::Vector{Vector{Float64}}, weights::Vector{Float64})
@ Main ./In[15]:5
[2] top-level scope
@ In[16]:1
[3] eval
@ ./boot.jl:360 [inlined]
[4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
```

The following version works like a charm:
```julia
function logmean(samples, weights)
logμ = zero(first(samples))
n = length(samples)
k = length(logμ)
for i in 1:n
@turbo for j in 1:k
logμ[j] = logμ[j] + weights[i] * log(samples[i][j])
end
end
return logμ
end
```

I thought `LoopVectorization.jl` should be able to handle nested loops? Or are there some special requirements for that?

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start by reproducing the nested-loop example using the @turbo macro, then inspect the macro's handling of loop-variable scope and nesting. Done means the shown nested version runs without UndefVarError while preserving the working outer-loop arrangement's result.

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
42/100

Neue Issues direkt in Ihr Postfach

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