1

Suppose I have these two files.

test_file.jl

using Distributed
function loop(N)
  @distributed for i in 1:N
          println(i)
  end
end

test_file_call.jl

include("test_file.jl")
loop(10)                    

If I run julia -p 2 test_file_call.jl, I expect the function loop executed on different processors, printing out 10 numbers in an arbitrary number. However, this command doesn't render anything.

I'm not sure what I did wrong? It's just a simple loop. Is it possible that I include a parallel loop in file A, write another file, B, that contains this loop, and call B to execute the parallel loop in file A? This two file structure is what I want. Is that doable?

1 Answer 1

3

The problem is that you forgot to @sync your loop so it exits before it actually has time to print anything.

Hence this should be:

function loop(N)
  @sync @distributed for i in 1:N
      println(i, " ",myid())
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

What does @sync do?
@sync waits until all lexically-enclosed uses of @async, @spawn, @spawnat and @distributed are complete. Have a look at docs.julialang.org/en/v1/manual/distributed-computing

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.