Since S is global, the closure i -> f(S,i) only captures a reference to S.
And since this binding is not defined on the workers we see an error.
For example,
function foo()
S = SharedArray(Int,10)
S[:] = 1:length(S)
output1 = pmap(i -> f(S,i), 1:length(S))
end
foo()
works as expected.
I agree that this is confusing.
On Fri, Aug 12, 2016 at 10:28 AM, 'Greg Plowman' via julia-users <
[email protected]> wrote:
> pmap inside a function also seems to work:
>
> @everywhere f(A,i) = (println("A[$i] = $(A[i])+1"); A[i] += 1)
> wrapped_pmap(A) = pmap(i -> f(A,i), 1:length(A))
>
> S = SharedArray(Int,10)
> S[:] = 1:length(S)
>
> output1 = pmap(i -> f(S,i), 1:length(S)) #error
> output2 = wrapped_pmap(S) #seems to work
>
>
> I'm quite confused about why second version with pmap wrapped in a
> function works.
> Perhaps someone can explain?
>
>
>