Value function backward induction

The way I’ve done this (or similar things) before is to compute a set of indices 1:N such that each index corresponds to a point in the state space. So you would distribute over that 1:N, and then construct which point it corresponds to within the loop.

Eg instead of

nb = 12
nz = 8

@sync @distributed for ib = 1:nb
      for  z = 1:nz
              vf = solve_problem(params,ib,z,age)
      end
end

you would do something like

nb = 12
nz = 8
N = nb*nz
@sync @distributed for i = 1:N
    ib,iz = indicesfrom(i,nb,nz)  
    vf = solve_problem(params,ib,iz,age)
end

where you just need to figure out what that indicesfrom function should look like. CartesianIndices should help.

4 Likes

Thank you very much for your suggestion. It makes a lot of sense

CartesianIndices((nb,nz))[i]

2 Likes

I thought @distributed did that automatically, but looking at the internals that’s not the case. I will have to fix that in my code https://github.com/amrods/vfi-fh-dp/blob/master/VFI-alg-parallel.jl :man_shrugging: