-
Notifications
You must be signed in to change notification settings - Fork 29
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 to implement n-dimensional functions? #11
Comments
There is nothing built-in to deal with n-dimensional arrays. @generated function spatial_grad_square_ND(arr)
out, add = [], []
for d in 1:ndims(arr)
ind = :i # @gensym ind
inds1 = map(1:ndims(arr)) do di
i = Symbol(ind, di)
di == d ? :($i+1) : i
end
inds2 = map(1:ndims(arr)) do di
i = Symbol(ind, di)
di == d ? :($i-1) : i
end
res = Symbol(:res_,d) # @gensym res
push!(out, :($res = let
@tullio $res = abs2(arr[$(inds1...)] - arr[$(inds2...)])
end))
push!(add, res)
end
push!(out, :(+($(add...))))
quote
$(out...)
end
end |
Thanks for your code, I think I can follow it conceptually. However, with @generated I can't call it. The following output:
What's the problem here? |
Oh I'm sorry, I thought I'd tried this but I messed up. There are indeed closures. It might be possible to make this work with https://github.com/thautwarm/GeneralizedGenerated.jl, however right now it objects to type parameters -- Or it might be possible to avoid closures, I'm not sure. I wanted something like this for #4 too but had not looked recently. |
What you can do is to define a method for each ndims that you may need. Not as elegant but should work: for N in 1:10
inds1 = # work out pieces as before
@eval function spatial_grad_square_ND(arr::AbstractArray{T,$N}) where T
$(out...)
end
end |
Thanks for your help, maybe it's not the best solution, but at least my idea is working and I learnt a lot about metaprogramming from your snippet. For the time being I will use that, I could exchange it once there is a smarter solution... Should I close the issue? |
As you wish... I did a bit more fiddling but don't have a solution yet.
|
Can IRTools.jl help here? |
That I don't know, have not really built any feeling of what it can & can't do. Can you say more? (This comment #4 (comment) has a sketch of what |
It's basically magic. @MasonProtter would know better, but I have the feeling alot of this package would benefit from being pushed down to IR transforms. |
While possible, doing this at the IR level would be incredibly painful I think. |
Hey, not really new information for that issue, but rather problems which appeared using the information from here. Might be helpful if someone runs into similar problems. Cheers, Felix |
Hey,
I'm trying to figure out how we could use Tullio to generalize some n-dimensional functions.
Let's say I've got a n-dimensional array and I want to use Tullio for that. At the moment I need to treat dimensions differently.
Do you have any hints on how to achieve this in a more elegant way? Using the Julia metaprogramming documentation I couldn't figure it out 😞
Thanks,
Felix
The text was updated successfully, but these errors were encountered: