silgon
silgon

Reputation: 7221

Julia Parallel macro does not seem to work

I'm playing around for the first time with parallel computing with julia. I'm having a bit of a headache. So let's say I start julia as follows: julia -p 4. Then I declare the a function for all processors and then I use it with pmap and also with @parallel for.

@everywhere function count_heads(n)
    c::Int = 0
    for i=1:n
        c += rand(Bool)
    end
    n, c  # tuple (input, output)
end

###### first part ######
v=pmap(count_heads, 50000:1000:70000)
println("Result first part")
println(v)

###### second part ######
println("Result second part")
@parallel for i in 50000:1000:70000
    println(count_heads(i))
end

The result is the following.

Result first part
Counting heads function
Any[(50000,24894),(51000,25559),(52000,26141),(53000,26546),(54000,27056),(55000,27426),(56000,28024),(57000,28380),(58000,29001),(59000,29398),(60000,30100),(61000,30608),(62000,31001),(63000,31520),(64000,32200),(65000,32357),(66000,33063),(67000,33674),(68000,34085),(69000,34627),(70000,34902)]
Result second part
    From worker 4:  (61000, From worker 5:  (66000, From worker 2:  (50000, From worker 3:  (56000

Thus, the funcion pmap is apparently working fine but @parallel for is stopping or it doesn't give me the results. Am I doing something wrong?

Thanks!

Update

If at the end of the code I put sleep(10). It does the work correctly.

From worker 5:  (66000,33182)
From worker 3:  (56000,27955)
............
From worker 3:  (56000,27955)

Upvotes: 4

Views: 519

Answers (1)

Alexander Morley
Alexander Morley

Reputation: 4181

Both of your examples work properly on my laptop so I'm not sure but I think this answer might solve your problem!

It should work as expected if you add @sync before the @parallel for

From the julia Parallel Computing Docs http://docs.julialang.org/en/release-0.4/manual/parallel-computing/:

... the reduction operator can be omitted if it is not needed. In that case, the loop executes asynchronously, i.e. it spawns independent tasks on all available workers and returns an array of RemoteRef immediately without waiting for completion. The caller can wait for the RemoteRef completions at a later point by calling fetch() on them, or wait for completion at the end of the loop by prefixing it with @sync, like @sync @parallel for.

So you are maybe calling println on the RemoteRef before it has completed.

Upvotes: 6

Related Questions