gozzilli
gozzilli

Reputation: 8357

Parallel loop counter in Julia

In Julia, I would like to print out inside a parallel for loop the number of iterations the loop has completed.

Is this a safe way to do so, and/or is there a better way?

a = SharedArray(Int, 10)
counter = SharedArray(Int, 1)
arr = 1:10
tot = length(arr)
@parallel for i in collect(arr)
    a[i] = i
    counter[1] += 1
    println("$i/$tot")
    sleep(rand()) # simulate a variable amount of computation
end

Upvotes: 0

Views: 666

Answers (1)

niczky12
niczky12

Reputation: 5073

In your question you are not using the shared counter to print the iteration, but the integer i. I'm assuming from your question that you are looking for a way to print constantly increasing iteration numbers within a parallel loop. This is not possible with asynchronous execution as you don't know the order in which the loop is executed.

As an example: let's say processor1 does i=1 and it might take too long. While processor1 executes another processor might complete i=2 and i=3. This would result in i=2,3 printed before i=1.

You can't really get around this problem as this parallel behavior (processes taking over slow processes) is one the main benefits of parallel computing.

Here are the examples:

># parallel sum with async execution
>sum_i =  @parallel (+) for i in 1:10
>  println(i)
>  i
>end

From worker 4:  8
From worker 4:  9
From worker 3:  5
From worker 4:  10
From worker 3:  6
From worker 2:  1
From worker 3:  7
From worker 2:  2
From worker 2:  3
From worker 2:  4

Note the wrong ordering above.

You can produce a constantly increasing list of integers if you pick one processor. These will only be the ones that particular processor sees, but this gives you a sort of indication of how much your function has done:

>sum_i =  @parallel (+) for i in 1:10
>  if myid() == 2
>    println(i)
>  end
>
>  i
>end

From worker 3:  5
From worker 3:  6
From worker 3:  7

Upvotes: 1

Related Questions