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

Clarify the use multiline @tullio

Open Emmanuel-R8 opened this issue 3 years ago β€’ 1 comments

In the README, looking at the following snippet:

@tullio out[x, y] := @inbounds(begin  # sum over k
        a,b = off[k]
        mat[mod(x+a), mod(y+b)]
    end) (x in axes(mat,1), y in axes(mat,2)) grad=Dual nograd=off

I struggled in my code until I made sure that:

  • The first := after out[x, y] can either be := or = as described above the snippet.
  • However, within the begin ... end, only = can be used. This is not mentioned anywhere.

I wanted to change that with a PR. But I want to confirm first.

Is that correct?

Emmanuel-R8 avatar Mar 09 '22 03:03 Emmanuel-R8

Sorry this package really needs a manual, but I got busy.

@tullio out[x, y] = right evaluates right and writes into out, possibly summing. @tullio out[x, y] := right makes a new array. The macro completely rewrites how this looks.

But what appears within the expression on the right isn't processed much, and at some point I allowed it to take an arbitrary block of code. The code is read (to figure out what indices appear, etc.) but evaluated as it stands. So = has no special meaning, it's just a normal block of code, and the last statement is its value, as usual.

The option verbose=2 will print everything, and the main loop here looks like this:

β”‚                    for y = 𝒢𝓍y
β”‚                        for x = 𝒢𝓍x
β”‚                            π’œπ’Έπ’Έ = if ♻️ === nothing
β”‚                                    zero(𝒯)
β”‚                                else
β”‚                                    β„›[x, y]   # this is part of how it handles memory blocking, allows a re-start
β”‚                                end
β”‚                            for k = 𝒢𝓍k  # this is the sum loop
β”‚                                π’œπ’Έπ’Έ = π’œπ’Έπ’Έ + @inbounds(begin  # this is the block of code provided, untouched
β”‚                                                (a, b) = off[k] 
β”‚                                                mat[mod(x + a), mod(y + b)]
β”‚                                            end)
β”‚                            end
β”‚                            β„›[x, y] = π’œπ’Έπ’Έ  # writing into an output array, created outside this function
β”‚                        end
β”‚                    end

mcabbott avatar Mar 10 '22 00:03 mcabbott