Julia pmap 实际上并未使用多线程?

问题描述 投票:0回答:1

我在奔跑

julia --threads 4

然后是命令

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

我得到了输出

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)

这清楚地表明这是串行运行的。为什么代码不能并行运行?

parallel-processing julia
1个回答
0
投票

并发任务数设置为

nworkers()
,比
nprocs()
少1,这不是基于线程数,而是基于Julia启动的进程数,可以设置与
-p
--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)
© www.soinside.com 2019 - 2024. All rights reserved.