Search code examples
parallel-processingjulia

Julia pmap not actually using multiple threads?


I'm running

julia --threads 4

Then the commands

using Distributed
@time pmap(x->begin println(x); sleep(1); println(x); x end, 1:10);

I get the output

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
 10.766960 seconds (2.50 M allocations: 140.642 MiB, 0.20% gc time, 6.88% compilation time)

Which clearly indicates that this is being run serially. Why is the code not being run in parallel?


Solution

  • The number of concurrent tasks is set to nworkers(), which is one less than nprocs(), which is not based on the number of threads but rather the number of processes Julia is started with, which can be set with -p or --procs.

    $ julia --procs auto  # set to num logical cores; can also be an explicit number
    
    julia> using Distributed
    
    julia> nworkers()
    4
    
    julia> @time pmap(x->begin println(x); sleep(1); println(x); x end, 1:10);
          From worker 5:    2
          From worker 2:    3
          From worker 3:    1
          From worker 4:    4
          From worker 3:    1
          From worker 5:    2
          From worker 4:    4
          From worker 2:    3
          From worker 4:    7
          From worker 5:    5
          From worker 3:    6
          From worker 2:    8
          From worker 5:    5
          From worker 4:    7
          From worker 5:    9
          From worker 3:    6
          From worker 2:    8
          From worker 4:    10
          From worker 5:    9
          From worker 4:    10
      3.047513 seconds (61.13 k allocations: 4.138 MiB, 1.12% compilation time)