6

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

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?

4

1 回答 1

14

函数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"
于 2014-07-21T02:40:13.220 回答