朱莉娅:正确使用pmap [英] Julia: Using pmap correctly

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

问题描述

为什么这不做我认为应该做的事情:

Why doesn't this do what I think it should:

benjamin@benjamin-VirtualBox:~$ julia -p 3
julia> @everywhere(function foom(bar::Vector{Any}, k::Integer) println(repeat(bar[2],bar[1])); return bar; end)
julia> foo={{1,"a"},{2,"b"},{3,"c"}}
julia> pmap(foom, foo, 5)
    From worker 2:  a
1-element Array{Any,1}:
 {1,"a"}

,这就是它的全部输出.我期望pmap遍历foo中的每个元组并在其上调用foom.

and that is all it outputs. I was expecting pmap to iterate through each tuple in foo and call foom on it.

当我在以下地方不传递其他参数时,它将正常工作:

It works correctly when I don't pass other arguments in:

julia> @everywhere(function foom(bar::Vector{Any}) println(repeat(bar[2],bar[1])); return bar; end)

julia> pmap(foom, foo)
    From worker 3:  bb
    From worker 2:  a
    From worker 4:  ccc
3-element Array{Any,1}:
 {1,"a"}
 {2,"b"}
 {3,"c"}

如何将更多参数传递给pmap?

How can I pass in more arguments to pmap?

推荐答案

函数 pmap 确实接受任意数量的参数,每个参数都是一个集合

The function pmap does take any number of arguments each one a collection

function pmap(f, lsts...; err_retry=true, err_stop=false)

函数f将为每个集合发送一个参数

The function f will be sent an argument for each collection

julia> @everywhere f(s,count)=(println("process id = $(myid()) s = $s count = $count");repeat(s,count))

pmap使用1

julia> pmap((a1,a2)->f(a1,a2),{"a","b","c"},{2,1,3})
    From worker 3:  process id = 3 s = b count = 1
    From worker 2:  process id = 2 s = a count = 2
    From worker 4:  process id = 4 s = c count = 3
3-element Array{Any,1}:
 "aa" 
 "b"  
 "ccc"

pmap使用2

或者,参数可以作为一个外部集合中的参数集合发送,该外部集合可以被填充为目标函数的多个参数

pmap use 2

Alternately, arguments can be sent as collection of arguments from one outer collection which can be spatted into multiple arguments for the target function

julia> pmap((args)->f(args...),{{"a",2},{"b",1},{"c",3}})
    From worker 2:  process id = 2 s = a count = 2
    From worker 3:  process id = 3 s = b count = 1
    From worker 4:  process id = 4 s = c count = 3
3-element Array{Any,1}:
 "aa" 
 "b"  
 "ccc"

这篇关于朱莉娅:正确使用pmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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