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

Undefined nested iterating variable

Open weymouth opened this issue 2 years ago • 7 comments

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?

weymouth avatar Mar 23 '23 18:03 weymouth

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. https://github.com/JuliaSIMD/Polyester.jl/blob/5185467b7394df6c483d7212f83582baab0106d7/src/closure.jl#L94

chriselrod avatar Mar 23 '23 19:03 chriselrod

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

weymouth avatar Mar 23 '23 19:03 weymouth

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

weymouth avatar Mar 25 '23 10:03 weymouth

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

chriselrod avatar Mar 25 '23 11:03 chriselrod

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.

weymouth avatar Mar 26 '23 19:03 weymouth

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.

chriselrod avatar Mar 27 '23 05:03 chriselrod

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'

SouthEndMusic avatar Dec 07 '23 16:12 SouthEndMusic