Hi,
I am trying to do some simple computations inside a single or double loop, and I would like to parallelize the computations inside the loops, and so far I couldn’t figure it out. Here is a simple example of what I want to do
using Optim
const n_z = 5
const n_b = 5
const J = 60
const Ht_grid = [1.17; 1.50; 1.92]
const n_ht = length(Ht_grid)
const B_grid = [0.0; 0.00356596; 0.0285277; 0.096281; 0.228222]
const pension_grid = [0.280896; 0.309223; 0.340406; 0.374734; 0.412523]
const vvv_opt_r = zeros(n_z,n_b,J);
const policy_opt_r = zeros(2,n_z,n_b,J);
function test(x, y, z,w)
return x+y+z+w
end
for i_z in 1:n_z, i_b in 1:n_b
y = pension_grid[i_z]
b = B_grid[i_b]
bprime_opt_r = zeros(n_ht)
vvv_r = zeros(n_ht)
for i_ht = 1:n_ht
htilde = Ht_grid[i_ht]
fopt(x) = test(x,b,y,htilde)
res = optimize(fopt, 0, maximum(B_grid))
bprime_opt_r[i_ht] = Optim.minimizer(res)
vvv_r[i_ht] = -Optim.minimum(res)
end
vvv_opt_r[i_z,i_b,J], idx = findmax(vvv_r)
policy_opt_r[1,i_z,i_b,J] = bprime_opt_r[idx]
policy_opt_r[2,i_z,i_b,J] = idx
end
Essentially the code loops over i_z and i_b, calculates something inside for each value of i_z and i_b (independtenly) and saves the resuls in vvv_opt_r, policy_opt_r and c_opt_r.
All the functions (value_age_J_r), arrays and constants (vvv_opt_r, policy_opt_r and c_opt_r, qb, current_rent, etc.) are predifined in a different file that I load with “include(“path”)” command, as well as the packages (such as QuanEcon).
Ideally I would like to loop over both i_z and i_b in parallel and save the results to vvv_opt_r, etc as they come.
Thanks for the help,
Myroslav