0

I have a function which I run using pmap. I would like the output to be a vector - p - whose entries are the output of each iteration run parallel. For illustration, the code is as below:

@everywhere function addo(mc,mcf)

p = zeros(Int64,mcf) ;

p[mc] = mc ;

return p 

end 

pmap(mc -> addo(mc,mcf),1:1:mcf) ;

The output I get is

100-element Array{Array{Int64,1},1}:
 [1, 0, 0, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  
 [0, 2, 0, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  
 [0, 0, 3, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  
 ⋮                                                                
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 98, 0, 0] 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 0, 99, 0] 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0  …  0, 0, 0, 0, 0, 0, 0, 0, 0, 100]

Instead, I need something like this, p = [1,2,3,4,...,100];

How do I get the output in the desired format?

user3856486
  • 515
  • 1
  • 4
  • 16
  • When I have more than one return value of different types - Array{Int64,N},SimpleDiGraph{Int64},Bool - and the function is run 5 times. How can I access the second element(SimpleDiGraph) of the the 3rd run ? p[1/2/3] only return the 1st,2nd or 3rd element of the 1st run and p[3,2] returns an error. – user3856486 Mar 09 '19 at 14:02

1 Answers1

0

pmap, similar to map, applies your transformation function,mc -> addo(mc, mcf), to each element in the collection 1:1:mcf. The problem in your code is that for each element in the collection you are creating a new vector of size mcf and the overall result becomes an array of the arrays you create in each transformation, whereas what you want is the result of pmap to be of size mcf. Therefore, for each element in the collection, you should return only one element. pmap already gives you a collection grouping the results of each transformation.

@everywhere function addo(mc)
    return mc
end 

p = pmap(mc -> addo(mc), 1:1:mcf)

This will give you p = [1,2,3,4,...,100].

hckr
  • 5,456
  • 1
  • 20
  • 31