-
Notifications
You must be signed in to change notification settings - Fork 56
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
How can @tensor macro be used in variable length systems? #125
Comments
There is some speed advantage with using If the contraction requires dynamic information, which is not known at compile time, you cannot use |
If you really need the speed and you don't have a lot of different contractions you can call the macro from a generated function. The generated function can dynamically dispatch by handing it a Val type. Generated functions have to compile a function for every call of different type they get. This means that each contraction with a different signature would have to be recompiled. So using this approach with rapidly changing contraction orders will introduce a lot of compilation overhead. Here is an example, handing the operators the indices (as Tuples of Ints): using TensorOperations
@generated function contract_operator!(S,
A,
B,
op,
order_A::Val{K}) where {K}
leftside = Expr(:call, :*,
:(A[$(K.a...)]),
:(B[$(K.b...)]),
:(op[$(K.op...)]))
return :(@tensor S[:] = $leftside)
end
S = rand(5,5,5,2,5,5,5,2)
a = rand(5,5,5,5,2)
b = rand(5,5,5,5,2)
op = rand(2,2,2,2)
contraction_order = Val((a = (1,-1,-2,-3,2),
b = (-5,-6,1,-7,3),
op = (2,3,-4,-8)))
contract_operator!(S,a,b,op, contraction_order); |
I'm used to write code in ncon fashion, and it's necessary because I have to deal with arbitrary number of tensors. But I know @ncon is slower than @tensor. How can I keep the notion of ncon, and get the speed of @tensor at the same time?
The text was updated successfully, but these errors were encountered: