朱莉娅(Julia):将pmap与矩阵一起使用 [英] Julia: use of pmap with matrices

查看:81
本文介绍了朱莉娅(Julia):将pmap与矩阵一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对pmap的使用有疑问.我认为这是一个简单/明确的答案,但仍然无法弄清楚!我目前正在运行一个循环,其中50个迭代中的每个迭代都是独立的,因此应该可以并行运行它,并应提高速度.它使用具有多个输入和输出的函数,这些输入和输出都是向量和标量的混合体.我需要为50个迭代保存每个函数的输出,以供以后使用.这是不并行时的代码基础.

I have a question about the use of pmap. I think it's a simple/obvious answer but still can't figure it out! I am currently running a loop where each of 50 iterations is separate and so running it in parallel should be possible and should improve speed. It uses a function that has multiple inputs and outputs, which are both a mixture of vectors and scalars. I need to save the outputs of the function for each of the 50 iterations for later use. Here are the basics of the code when not in parallel.

A=Array(Float64, 500,50)
b=Array(Float64,50)

for i in 1:50

    A[:,i],b[i] = func(i,x,y,z)

end

关于如何实现此目标的任何建议都是并行的?我正在使用v0.3 Julia.

Any advice for how to implement this is parallel? I'm using v0.3 Julia.

谢谢.

大卫

推荐答案

这对我有用.

@everywhere x,y,z = 1,2,3
@everywhere function f(i,x,y,z) 
    sleep(1)
    return(ones(500)*i, i+x+y+z)
end

naive = @time map(i -> f(i,x,y,z), 1:50)
parallel = @time pmap(i -> f(i,x,y,z), 1:50)

A = [x[1] for x in parallel]
b = [x[2] for x in parallel]

让我知道是否有人可以建议一种更优雅的方法来从pmap生成的元组数组中获取A和b.

Let me know if anyone can suggest a more elegant way to get A and b out of the array of tuples that is produced by pmap.

时间(在8个进程上运行时)符合我们的预期

The timings (when run on 8 processes) are as we would expect

elapsed time: 5.063214725 seconds (94436 bytes allocated)
elapsed time: 0.815228485 seconds (288864 bytes allocated)

这篇关于朱莉娅(Julia):将pmap与矩阵一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆