这是官方对于1.3.0中新增可组合的多线程并行机制的链接地址
新增的@spawn宏可以对递归算法进行并行计算
我尝试了一下示例代码,确实如文中所展示的那样
for n in 1 2 4 8 12; do JULIA_NUM_THREADS=$n /Applications/Julia-1.3.app/Contents/Resources/julia/bin/julia psort.jl; done
2.349335 seconds (336.93 k allocations: 703.161 MiB, 3.52% gc time)
1.389839 seconds (337.14 k allocations: 703.155 MiB, 5.98% gc time)
0.894538 seconds (337.16 k allocations: 703.157 MiB, 5.44% gc time)
0.718098 seconds (337.19 k allocations: 703.159 MiB, 6.50% gc time)
0.707881 seconds (337.21 k allocations: 703.159 MiB, 8.25% gc time)
随着线程增多而速度加快,但是当我对自己的递归算法进行并行后发现并没有任何实质变化!
for n in 1 2 4 8 12; do JULIA_NUM_THREADS=$n /Applications/Julia-1.3.app/Contents/Resources/julia/bin/julia parallel_main.jl; done
3.333085 seconds (13.95 M allocations: 1.090 GiB, 9.05% gc time)
3.197171 seconds (13.95 M allocations: 1.090 GiB, 8.74% gc time)
3.260522 seconds (13.95 M allocations: 1.090 GiB, 8.74% gc time)
3.200274 seconds (13.95 M allocations: 1.090 GiB, 9.10% gc time)
3.029139 seconds (13.95 M allocations: 1.090 GiB, 7.25% gc time)
最奇怪的是,理论上并行后消耗的内存应该会增加,但是看显示的结果信息,貌似就没有并行处理,连消耗的内存都是一样的,一头雾水,我也是按照类似psort中的那种写法,二分后把一个划分开线程来做,主线程继续计算另一个划分,感觉没错啊!
parallel_main.jl文件中主要的核心递归函数如下所示
function parallel_task(interval::Interval, depth::Int64, nnet::Network, property::Int64)
reach = forward_network(nnet, interval)
result = check_inclusion(reach.sym,nnet, property,depth)
if result.status == :unknown
LG, UG = get_gradient(nnet, reach.LΛ, reach.UΛ)
max_impact_inds = get_maximum_impact_interval_index2(reach.sym.interval, LG, UG)
left_interval, right_interval = split_interval(reach.sym.interval, max_impact_inds,nnet,property,depth)
adv_found && return
t = @spawn parallel_task(left_interval,depth+1,nnet,property)
parallel_task(right_interval,depth+1,nnet,property)
wait(t)
end
end
不知道有没有大佬知道我的代码是哪里存在问题,求指点迷津,谢谢了!!