2

I would like to use julia's pmap to do something like this:

pmap( f, v, inits[i])

where f is the function I want to call in parallel on v which is some array. I also want to pass some parameters to f (inits), but inits itself is an array, and I want ith parameter to be passed along to the correct process. Does this make sense?

I could do this by sorting of 'rolling my own' version of pmap, since it can be easily done with remotecall_fetch. Here is that implementation if the above was confusing:

    i=1
    nextidx() = (idx=i; i+=1; idx)
    @sync begin
      for k in 1:np
        if k != myid() || np == 1
          @async begin
            while true
              idx = nextidx()
              if idx > chains
                break
              end
              result[idx] = remotecall_fetch(k, mcmc_sub, m, iters, burnin, 
                                             thin, idx, ps[idx], p)
            end
          end
        end
      end
    end
bdeonovic
  • 4,130
  • 7
  • 40
  • 70

1 Answers1

1

Suppose you have some input v that you want to run pmap on. Each element of v also has an associated vector of inits which are stored in the columns of the matrix inits.

inits = reshape(randn(20), 5,4)
v = collect(1:4)

The key is to write your function to take in a tuple of your input and your inits

f = function(args::Tuple{Int64, Vector{Float64}})
  x, inits = args
  return sum(x * inits)
end

and then zip together your input and inits

args = collect(zip(v, [inits[:,j] for j in 1:4]))
pmap(f, args)
bdeonovic
  • 4,130
  • 7
  • 40
  • 70