Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Using pmap correctly

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?

like image 758
bdeonovic Avatar asked Jul 08 '14 16:07

bdeonovic


1 Answers

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"
like image 188
waTeim Avatar answered Dec 10 '22 04:12

waTeim