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

Undefined nested iterating variable #108

Closed
weymouth opened this issue Mar 23, 2023 · 7 comments · Fixed by #140
Closed

Undefined nested iterating variable #108

weymouth opened this issue Mar 23, 2023 · 7 comments · Fixed by #140
Labels
good first issue Good for newcomers

Comments

@weymouth
Copy link

weymouth commented Mar 23, 2023

This simple example throws an error

using Polyester
x = collect(1:12)
y = zeros(6)
@batch for i in eachindex(y)
    y[i] = sum(x[j] for j in 2i-oneunit(i):2i)
end
y

ERROR: UndefVarError: j not defined

I can redefine the loop to get rid of the variable j in this case, but shouldn't this work in principle?

@chriselrod
Copy link
Member

but shouldn't this work in principle?

Yes, Polyester doesn't realize j is defined by your generator, and thinks it is a variable you're passing into/using inside your loop.
You can modify the code inside src/closure.jl to recognize the generator.

function extractargs!(

@weymouth
Copy link
Author

I'm terrible at macros, so I will personally not be diving into that. ;-)

@weymouth
Copy link
Author

I found that blocking the closure into it's own function is a general workaround. ie

using Polyester
x = collect(1:12)
y = zeros(6)
closure(i,x) = sum(x[j] for j in 2i-oneunit(i):2i)
@batch for i in eachindex(y)
    y[i] = closure(i,x)
end
y

@chriselrod
Copy link
Member

Does that work on ARM?
This is still a Polyester bug.

@chriselrod chriselrod reopened this Mar 25, 2023
@weymouth
Copy link
Author

I don't have a computer with an ARM, so I can't check, sorry. The github CLI tests all passed but I'm not sure if any of those are ARM.

@chriselrod
Copy link
Member

Nevermind, seems to work fine on ARM.

Still, this should be left open because workarounds or not, it is a real issue with the package.

@SouthEndMusic
Copy link

I found a similar problem when trying to loop over an iterable of functions:

using Polyester

functions = [x -> n*x for n in 1:3]
data = rand(100)

@batch for i in eachindex(data)
   for f in functions
      data[i] += f(data[i])
   end
end
# Yields 'ERROR: UndefVarError: `f` not defined'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
3 participants