3

I have a problem where I would like to execute a number of statements in parallel that are independent of each other.

I have read plenty of examples that suggest this toy example should work:

function f1(x,y,z)
  @sync @async begin
    # these two statements can be computed independently
    v1 = x+y; 
    v2 = y+z;
  end
  return v1*v2
end

However, it seems that the @sync part is not waiting for the results to be completed because I get the following error:

y1 = f1(1,2,3);
ERROR: LoadError: UndefVarError: v1 not defined

I have succeeded in making it work like this:

function f2(x,y,z)
  v1 = @async x+y;
  v2 = @async y+z;
  return wait(v1)*wait(v2)
end

However, in many examples that I have seen, the wait statement seems to be unnecessary if one uses a @sync block.

I'm using Julia 0.6.2.

Any help would be highly appreciated.

1 Answer 1

9

The reason is that @async creates a closure around the expression you pass to it. You can see it by running:

julia> @macroexpand @async begin
       v1 = x+y;
       v2 = y+z;
       end
:((Base.async_run_thunk)((()->begin  # task.jl, line 335:
                begin  # REPL[22], line 2:
                    v1 = x + y # REPL[22], line 3:
                    v2 = y + z
                end
            end)))

This has the implication that v1 and v2 variables will not be visible outside the closure unless they are present in the enclosing function scope.

To fix this you can add local v1, v2 statement at the beginning of f1:

julia> function f1(x,y,z)
           local v1, v2
           @sync @async begin
               v1 = x+y;
               v2 = y+z;
           end
           return v1*v2
       end
f1 (generic function with 1 method)

julia> f1(1,2,3)
15

and as you can see all worked as expected.

Additionally if your enclosing scope were not a function but a global scope you would have to use global keyword to get what you want:

julia> x,y,z = 1,2,3
(1, 2, 3)

julia> @sync @async begin
          global v1 = x+y;
          global v2 = y+z;
       end
Task (done) @0x0000000018be30f0

julia> v1*v2
15
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.