In my package, I’d like to offer a convenience function like this:

function gaussian(σ::Real=1.0)
@eval function (x)
exp(-abs2(x) / $(float(4σ)))
end
end

I want the @eval because I don’t want the 4σ to be computed at every evaluation

kernel = gaussian(3.0)
kernel(0.2)

After a long while, I realized that kernel is not defined on all workers, and seemingly neither

@everywhere kernel = gaussian(3.0)

nor putting @everywhere in the definition of gaussian helps. Something that works with ordinary variables also doesn’t work

@everywhere kernel = $kernel

because Julia claims to not know what to interpolate on the r.h.s. (UndefVarError). How can I both keep the convenience of calling a function-generating function and have the resulting function defined everywhere?

I have tried with some of the @code_ macros (most of which I don’t master very well, I should say), and it did seem to me like there was the floatmul(%3, 4.0) (or something like this in the code. But maybe I was doing it wrong?

That was the only way I could come up with to avoid that (stupid) extra operation. I understand that this does not change the world, but it felt strange and I thought there should be a way to “precompute” this. Interestingly, when defined this way, there is no problem with kernel = gaussian(3.) being passed to different workers, without being @everywhered. So, many thanks for your help!

Could someone explain the use of @eval here and what the OP was trying to achieve? Suppose the OP didn’t have trouble with the parallelism, how does the @eval work? If I do

Yes, but the intention is to apply the same kernel = gaussian(3.) to many different arguments, i.e., kernel(x), and precompute as much as possible that depends on σ=3.0.