Julia 并发编程 ---- 循环中正确使用并发的方式

提示:由于Julia 使用mmap管理内存,windows环境下 如果报以下异常  SystemError: mmap: 页面文件太小,无法完成操作。需要重新设置系统的内存管理方式参考:内存管理配置。多线程与多进程的使用准则参考:Julia 并发编程 ---- 多线程与多进程的一些使用建议

1:非并发

using Random
const m = MersenneTwister(0);
function dothestuff!(out_N, N, ic, m)
    out_N[:, ic] .= rand(m, N)
end
function dummy_base(m=m, N=100_000,c=256)
    out_N = Array{Float64}(undef,N,c)
    for ic in 1:c
        dothestuff!(out_N, N, ic, m)
    end
    out_N 
end

执行时间:

using BenchmarkTools;
@btime dummy_base();

#执行时间与消耗内存 231.890 ms (770 allocations: 390.66 MiB)

2:多线程并行

using Random
#Mersenne Twister算法译为马特赛特旋转演算法,是伪随机数发生器之一
const mt = MersenneTwister.(1:Threads.nthreads());
function dothestuff!(out_N, N, ic, m)
    out_N[:, ic] .= rand(m, N)
end
function dummy_threads(mt=mt, N=100_000,c=256)
    out_N = Array{Float64}(undef,N,c)
    Threads.@threads for ic in 1:c
        dothestuff!(out_N, N, ic, mt[Threads.threadid()])
    end
    out_N
end

执行时间

using BenchmarkTools;
@btime dummy_threads();
#执行时间与消耗内存,141.481 ms (783 allocations: 390.66 MiB)

3 多进程并行

using Distributed
addprocs(4)

using Random, SharedArrays
@everywhere using Random, SharedArrays, Distributed
@everywhere Random.seed!(myid())

@everywhere function dothestuff!(out_N, N, ic)
    out_N[:, ic] .= rand(N)
end
function dummy_distr(N=100_000,c=256)
    out_N = SharedArray{Float64}(N,c)
    @sync @distributed for ic in 1:c
        dothestuff!(out_N, N, ic)
    end
    out_N
end

执行时间:

using BenchmarkTools;
@btime dummy_distr();
#执行时间与消耗内存   485.634 ms (1043 allocations: 43.89 KiB)

4:使用宏定义 执行多进程

这个例子中一定要注意的是,addprocs(3)得到的进程 有:2,3,4 三个worker ,其中1 worker是默认的住进程。所以 dummy函数中的chains = 4,如果超过这个数字,会报越界异常。进程数设置参考:Julia并发编程 ---- 进程数设置

using Distributed
@everywhere using Distributed, SharedArrays
addprocs(3)
@everywhere function inner_loop!(out_N, chain_number,steps,width)
    N = zeros(steps, width)
    state = zeros(width)
    for i = 1:steps
        state .+= rand(width)
        N[i,:] .= state
    end
    out_N[:,:,chain_number] .= N
    nothing
end
function dummy(steps = 10000, width = 100, chains = 4)
    out_N = SharedArray{Float64}((steps, width, chains); pids = collect(1:4))
    @sync for c = 1:chains
        # print("c=$c\n")
        @spawnat :any inner_loop!(out_N, c, steps,width)
    end
    sdata(out_N)
end

执行时间:

using BenchmarkTools;
@time dummy(out_N);
#执行时间与内存   2.477380 seconds (978.85 k allocations: 50.213 MiB, 0.49% gc time)

如果数据量不是太大,多线程应该就足够了. 多进程需要进程间通信,会额外增加执行时间,同时还要使用SharedArrays 或者DistributeArray 这些重量级的数据结构. 针对SharedArrays 或者DistributeArray区别后面再做介绍。

 

 

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

October-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值