Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Is it possible to create a vector of SVectors from a Matrix using Tullio? #161

Closed
shayandavoodii opened this issue Dec 27, 2022 · 2 comments

Comments

@shayandavoodii
Copy link

shayandavoodii commented Dec 27, 2022

Is it possible to create a vector of SVectors from a Matrix using Tullio?
I tried the following:

using StaticArrays
using Tullio

r = rand(5, 1000);
v = Vector{SVector{5, Float64}}(undef, size(r, 2))

@tullio v[i][j] := begin
  SVector{5, Float64}(r[:, i])
end (i in 1:1000, j in 1:5)

But it throws an error:

ERROR: UndefVarError: i not defined
Stacktrace:
 [1] ℳ𝒶𝓀ℯ
   @ C:\Users\Shayan\.julia\packages\Tullio\NGyNM\src\macro.jl:807 [inlined]
 [2] (::Tullio.Eval{var"#ℳ𝒶𝓀ℯ#41"{var"#𝒜𝒸𝓉!#40"}, Nothing})     (args::Matrix{Float64})
   @ Tullio C:\Users\Shayan\.julia\packages\Tullio\NGyNM\src\eval.jl:20
 [3] top-level scope
   @ C:\Users\Shayan\.julia\packages\Tullio\NGyNM\src\macro.jl:976

I'm looking for the best way to do this using Tullio if it is possible.

@mcabbott
Copy link
Owner

Tullio doesn't really know how to make an array of arrays, nor even to write into them -- it thinks vs[i] is the name of the array, should really notice and have a better error:

julia> vs = [rand(3) for _ in 1:4];

julia> @tullio vs[i][j] = i+j;
ERROR: UndefVarError: `i` not defined

julia> @tullio w[i] := vs[i][j];  # reading is fine

julia> w  sum.(vs)
true

It is possible to make slices without ever talking about their inner indices, so that it doesn't have to think about them:

julia> r = rand(5, 1000);

julia> using StaticArrays

julia> @tullio out[i] := SVector{5, Float64}(r[:, i]);

julia> out[1]
5-element SVector{5, Float64} with indices SOneTo(5):
 0.7766767901258985
 0.7154993774154197
 0.37558253875140246
 0.9544276819410628
 0.841961127323515

julia> size(out)
(1000,)

julia> summary(@tullio _[i] := r[:, i])
"1000-element Vector{Vector{Float64}}"

julia> @btime @tullio out[i] := SVector{5, Float64}($r[:, i]);
  min 35.333 μs, mean 316.602 μs (1002 allocations, 132.86 KiB)

However, this is making r[:, 1]::Vector each time before making an SVector, so it won't be fast. Tullio does not have ambitions to be good at applying functions to slices, etc. It gets confused by things like @tullio _[i,j] := vcat(r[:,i],i)[j] and even if this did work, it would still be a loop over all i, j, thus applying the function N^2 times.

TensorCast is more interested in slices. And (with slightly obscure notation) has special paths for StaticArrays. This should just call reinterpret but appears to waste a few ns:

julia> using TensorCast

julia> @btime @cast out2[i] := $r{:5, i};
  min 24.515 ns, mean 184.877 ns (2 allocations, 80 bytes)

julia> @btime reinterpret(SVector{5, Float64}, $r)
  min 4.500 ns, mean 4.648 ns (0 allocations)

@shayandavoodii
Copy link
Author

Thank you so much for this comprehensive explanation! I figured out why I faced the problem. Nonetheless, wouldn't it be better if you merged these two packages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants