Tullio.jl icon indicating copy to clipboard operation
Tullio.jl copied to clipboard

Unexpected results with variable LHS indices

Open cossio opened this issue 3 years ago • 1 comments

Why these two are different?

using Tullio
w = collect(1.0:2.0);
h = collect(1.0:3.0);
I = zeros(4);
@tullio I[j+k-1] = w[j] * h[k];
# I = [1.0, 2.0, 3.0, 6.0]

vs.

I .= 0
for j ∈ 1:length(w), k in 1:length(h)
    I[j+k-1] += w[j] * h[k]
end
# I = [1.0, 4.0, 7.0, 6.0]

cossio avatar Feb 11 '22 15:02 cossio

Thanks, this is a bug.

I believe this ought to be an error. Tullio doesn't work the way you are imagining here. For each value of the indices on the left, it always iterates a fixed rectangular region of any reduction indices, i.e. those appearing only on the right. This is what it knows how to parallelise safely.

Debug info:

julia> @tullio I[j+k-1] = w[j] * h[k]  verbose=2 grad=false;
┌ Info: ===== Base actor 
│   verbosetidy(ex_act) =
│    quote
│        local @inline(function 𝒜𝒸𝓉!(::Type, ℛ::AbstractArray{𝒯}, w, h, 𝒶𝓍j, 𝒶𝓍k, ♻️ = nothing, 💀 = true) where 𝒯
│                    @inbounds @fastmath(begin
│                                for k = 𝒶𝓍k
│                                    for j = 𝒶𝓍j
│                                        ℛ[(j + k) - 1] = w[j] * h[k]
│                                    end
│                                end
│                            end)
│                end)
└    end
┌ store.
│   arrays = [:w, :h]

│   leftarray = :I
│   leftind = [:j, :k]
│   leftnames = Symbol[]
│   leftraw = Any[:((j + k) - 1)]

│   plusequals = false
│   redfun = :+
│   redind = Symbol[]
│   right = :(w[j] * h[k])
│   rightind = [:j, :k]

┌ Info: left index ranges
│   j = Base.OneTo(2)
└   k = 1:3

mcabbott avatar Feb 11 '22 15:02 mcabbott