朱莉娅:正确使用pmap

时间:2014-07-08 16:31:38

标签: parallel-processing julia pmap

为什么这不符合我的想法:

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。

编辑:

当我没有传递其他参数时,它正常工作:

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?

1 个答案:

答案 0 :(得分:10)

函数 pmap 确实会将任意数量的参数作为集合

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

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

多参数函数示例

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

或者,可以将参数作为一个外部集合的参数集合发送,这些集合可以被喷射到目标函数的多个参数中

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"