Speed up nested loops with parfor
I’m trying to speed up this part of code. I have a constraint: the inputs of ReturnFn must all be scalars. If it was not for this restriction, I could easily vectorize the code. So I would like to know if there is a way to make the code below faster, while still satisfying this restriction of the inputs of ReturnFn.
Any help is really appreciated!
N_d = 50;
N_a = 300;
N_z = 10;
% ParamCell contains: K_to_L,alpha,delta,pen,gamma,crra
% I need the cell array to handle variable number of inputs in ReturnFn
Fmatrix=zeros(N_d*N_a,N_a,N_z);
parfor i4i5=1:N_z
Fmatrix_z=zeros(N_d*N_a,N_a);
for i3=1:N_a % a today
for i2=1:N_a % a’ tomorrow
for i1=1:N_d % d choice
Fmatrix_z(i1+(i2-1)*N_d,i3)=ReturnFn(d_gridvals(i1),a_gridvals(i2),a_gridvals(i3),z_gridvals(i4i5,1),z_gridvals(i4i5,2),ParamCell{:});
end
end
end
Fmatrix(:,:,i4i5)=Fmatrix_z;
end
function F = f_ReturnFn(d,aprime,a,e,age,K_to_L,alpha,delta,pen,gamma,crra)
% INPUTS (always 5 inputs, plus some extra parameter inputs)
% d: Hours worked
% aprime: Next-period’s assets
% a: Current period assets
% e: Labor efficiency shock
% age: Age of individual: young or old
% TOOLKIT NOTATION
% (d,aprime,a,z), where z = [e;age]
F = -inf;
r = alpha*K_to_L^(alpha-1)-delta;
w = (1-alpha)*K_to_L^alpha;
income = (w*e*d)*(age==1)+pen*(age==2)+r*a;
c = income+a-aprime; % Budget Constraint
if c>0
% NOTE: 0<d<1 is already built into the grid
% WARNING: this will not work if crra=1
inside = (c^gamma)*((1-d)^(1-gamma));
F = inside^(1-crra)/(1-crra);
end
endI’m trying to speed up this part of code. I have a constraint: the inputs of ReturnFn must all be scalars. If it was not for this restriction, I could easily vectorize the code. So I would like to know if there is a way to make the code below faster, while still satisfying this restriction of the inputs of ReturnFn.
Any help is really appreciated!
N_d = 50;
N_a = 300;
N_z = 10;
% ParamCell contains: K_to_L,alpha,delta,pen,gamma,crra
% I need the cell array to handle variable number of inputs in ReturnFn
Fmatrix=zeros(N_d*N_a,N_a,N_z);
parfor i4i5=1:N_z
Fmatrix_z=zeros(N_d*N_a,N_a);
for i3=1:N_a % a today
for i2=1:N_a % a’ tomorrow
for i1=1:N_d % d choice
Fmatrix_z(i1+(i2-1)*N_d,i3)=ReturnFn(d_gridvals(i1),a_gridvals(i2),a_gridvals(i3),z_gridvals(i4i5,1),z_gridvals(i4i5,2),ParamCell{:});
end
end
end
Fmatrix(:,:,i4i5)=Fmatrix_z;
end
function F = f_ReturnFn(d,aprime,a,e,age,K_to_L,alpha,delta,pen,gamma,crra)
% INPUTS (always 5 inputs, plus some extra parameter inputs)
% d: Hours worked
% aprime: Next-period’s assets
% a: Current period assets
% e: Labor efficiency shock
% age: Age of individual: young or old
% TOOLKIT NOTATION
% (d,aprime,a,z), where z = [e;age]
F = -inf;
r = alpha*K_to_L^(alpha-1)-delta;
w = (1-alpha)*K_to_L^alpha;
income = (w*e*d)*(age==1)+pen*(age==2)+r*a;
c = income+a-aprime; % Budget Constraint
if c>0
% NOTE: 0<d<1 is already built into the grid
% WARNING: this will not work if crra=1
inside = (c^gamma)*((1-d)^(1-gamma));
F = inside^(1-crra)/(1-crra);
end
end I’m trying to speed up this part of code. I have a constraint: the inputs of ReturnFn must all be scalars. If it was not for this restriction, I could easily vectorize the code. So I would like to know if there is a way to make the code below faster, while still satisfying this restriction of the inputs of ReturnFn.
Any help is really appreciated!
N_d = 50;
N_a = 300;
N_z = 10;
% ParamCell contains: K_to_L,alpha,delta,pen,gamma,crra
% I need the cell array to handle variable number of inputs in ReturnFn
Fmatrix=zeros(N_d*N_a,N_a,N_z);
parfor i4i5=1:N_z
Fmatrix_z=zeros(N_d*N_a,N_a);
for i3=1:N_a % a today
for i2=1:N_a % a’ tomorrow
for i1=1:N_d % d choice
Fmatrix_z(i1+(i2-1)*N_d,i3)=ReturnFn(d_gridvals(i1),a_gridvals(i2),a_gridvals(i3),z_gridvals(i4i5,1),z_gridvals(i4i5,2),ParamCell{:});
end
end
end
Fmatrix(:,:,i4i5)=Fmatrix_z;
end
function F = f_ReturnFn(d,aprime,a,e,age,K_to_L,alpha,delta,pen,gamma,crra)
% INPUTS (always 5 inputs, plus some extra parameter inputs)
% d: Hours worked
% aprime: Next-period’s assets
% a: Current period assets
% e: Labor efficiency shock
% age: Age of individual: young or old
% TOOLKIT NOTATION
% (d,aprime,a,z), where z = [e;age]
F = -inf;
r = alpha*K_to_L^(alpha-1)-delta;
w = (1-alpha)*K_to_L^alpha;
income = (w*e*d)*(age==1)+pen*(age==2)+r*a;
c = income+a-aprime; % Budget Constraint
if c>0
% NOTE: 0<d<1 is already built into the grid
% WARNING: this will not work if crra=1
inside = (c^gamma)*((1-d)^(1-gamma));
F = inside^(1-crra)/(1-crra);
end
end nested loops, parfor MATLAB Answers — New Questions