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
4

1 回答 1

1

假设您有一些v想要运行pmap的输入。的每个元素v还有一个关联的初始向量向量,这些初始向量存储在矩阵的列中inits

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

关键是编写函数来接收输入和初始化的元组

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

然后将您的输入和初始化压缩在一起

args = collect(zip(v, [inits[:,j] for j in 1:4]))
pmap(f, args)
于 2016-08-10T15:35:57.553 回答