Julia: Using pmap correctly

荒凉一梦 提交于 2020-01-13 02:09:09

问题


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"}

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

EDIT:

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"}

How can I pass in more arguments to pmap?


回答1:


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

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

The function f will be sent an argument for each collection

Example Multi-argument Function

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

pmap use 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 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"


来源:https://stackoverflow.com/questions/24637064/julia-using-pmap-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!