Problems in Parfor Implemenation
4 views (last 30 days)
Show older comments
Panagiotis Veneris
on 19 Jun 2021
Commented: Panagiotis Veneris
on 1 Jul 2021
Hi,
I am new to parallel computing and I am trying to parallelize my (relatively intensive) code so as to make it run faster. However, I facing a lot of issues that I am not able to adress. The code is the following:
% Initial guess for policy function
c_pol = max(cmin, r*ones(S,1) * b_grid);
c_poli = c_pol; % next iteration
% Guess for V_t+1
load CRRA.mat % loads c_pol and n_pol from CRRA case in original GL code (check line before the start of terminal SS part)
v_pol= (1/(1-bet))*(c_pol.^(1-gam)/(1-gam))+ pssi*(((1-n_pol).^(1-eta))/(1-eta));
v_poli = v_pol; % next iteration
dif = 1;
while dif > tol_pol
ui = Pr * (((-v_pol).^(-alpha)).*(c_pol.^(-gam)));
ui = ui(:, b_grid >= -phi);
v_u = Pr *((-v_pol).^(1-alpha));
v_un = v_u(:,b_grid>= -phi);
v_con = v_u(:,b_grid < -phi);
for s=1:S %% here i need parallelization
c = ((1+r) * bet * ((v_un(s,:)).^(alpha/(1-alpha)) .* ui(s, :))).^ (-1/gam);
n = max(0, 1 - fac(s)*c.^gameta);
b = b_grid(b_grid >= -phi) / (1+r) + c - theta(s)*n - z(s);
v = c.^(1-gam).*((1/(1-gam))) + pssi * (((1-n).^(1-eta))/(1-eta)) - bet* ((v_un(s,:)).^(1/(1-alpha)));
if b(1) > -phi
c_c = linspace(cl(s), c(1), Ic); % ***
n_c = max(0, 1 - fac(s)*c_c.^gameta);
b_c = -phi/(1+r) + c_c - theta(s)*n_c - z(s);
vl = cl(1,1).^(1-gam).*((1/(1-gam))) + pssi * (((1-n_c).^(1-eta))/(1-eta))- bet* ((v_un(1,1)).^(1/(1-alpha)));
v_c = linspace(vl(1),v(1),Ic);
b = [b_c(1:Ic-1), b];
c = [c_c(1:Ic-1), c];
v = [ v_c(1:Ic-1), v];
end
c_poli(s, :) = interp1(b, c, b_grid, 'linear', 'extrap'); % true c_t+1
v_poli(s,:) = interp1(b, v, b_grid, 'linear', 'extrap'); % true v_t+1
v_poli(v_poli>0)=-1e-6;
end
% check convergence
c_poli = max(c_poli, cmin);
v_poli = min(v_poli, cmin);
dif1 = max(max(abs(c_poli - c_pol)));
dif2 = max(max(abs(v_poli-v_pol)));
dif = max([dif1,dif2]);
% update
c_pol = c_poli;
v_pol = v_poli;
end
Trying to use ''parfor'' I get the following error ''Unable to classify the variable 'v_poli' in the body of the parfor-loop'', which I don't know how to fix.
Any help would be highly appreciated
0 Comments
Accepted Answer
Walter Roberson
on 19 Jun 2021
Change
v_poli(s,:) = interp1(b, v, b_grid, 'linear', 'extrap'); % true v_t+1
v_poli(v_poli>0)=-1e-6;
to
v_polis = interp1(b, v, b_grid, 'linear', 'extrap'); % true v_t+1
v_polis(v_polis>0)=-1e-6;
v_poli(s,:) = v_polis;
Otherwise you are trying to test all of v_poli() including the rows being created by other loop iterations.
5 Comments
Walter Roberson
on 1 Jul 2021
Unfortunately you have not happened to post enough of the code for me to assess whether that will work.
At the moment it is not clear to me which variables are updated when.
When you use parfor, the iterations must be independent of each other.
For example in the context of a budget cycle, the incoming debt might influence the budget process (because of interest payments), and it might hypothetically be the case that (for the purpose of discussion) that everything else is known variables except the incoming debt -- so (for discussion purposes) if you knew the incoming debt ahead of time for each year, perhaps you could process the years in parallel. But in practice the debt left over from one year is input to the next, so you would have to process the years in sequence instead of in parallel.
More Answers (0)
See Also
Categories
Find more on Cash Flows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!