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

UndefVarError from closure inside @batch #129

Closed
svretina opened this issue Jan 14, 2024 · 2 comments
Closed

UndefVarError from closure inside @batch #129

svretina opened this issue Jan 14, 2024 · 2 comments

Comments

@svretina
Copy link

svretina commented Jan 14, 2024

I am trying to use the @batch as an alternative to the Base.Threads.@threads but inside my loops I need to define some closures which produced an UndefVarError. A MWE is the following

function func()
    @batch for i in 1:5
        f(x)=x
    end
end
func()

will produce:

ERROR: UndefVarError: `x` not defined
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/Polyester/HaBfT/src/closure.jl:387 [inlined]
 [2] func()
   @ Main ./REPL[90]:2
 [3] top-level scope
   @ REPL[91]:1
 [4] top-level scope
   @ ~/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428

Is there a workaround or its a case that I cannot use @batch?

@chriselrod
Copy link
Member

You could define a callable struct instead.

julia> struct Mul{T}
           x::T
       end

julia> (x::Mul)(y) = x.x * y

julia> using Polyester

julia> function func!(y,x)
           @batch for i = eachindex(y,x)
               y[i] = Mul(i)(x[i])
           end
       end
func! (generic function with 1 method)

julia> x = rand(1000); y = similar(x);

julia> func!(y, x)

julia> y == x .* eachindex(y,x)
true

@svretina
Copy link
Author

thanks for your reply! That solves my issue, thank you.

although it worked and I got 0 allocations with @batch, for my case
the performace was worse than @threads but this should be due to my
definitions of the function-like objects etc.

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