我在跑
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)
这清楚地表明这是串行运行的。为什么代码不能并行运行?
并发任务数设置为
nworkers()
,比 少 1nprocs()
,这不是基于线程数,而是基于 Julia 启动的进程数,可以使用-p
或进行设置--procs
。