Parallelize nested loops with parfor
4 次查看(过去 30 天)
显示 更早的评论
I'm trying to speed up the simulation of some panel data. I have to simulate first over individuals (for ii from 1 to N) and then for each individual over age from 1 to JJ. The code is slow because inside the two loops there is a bilinear interpolation to do. Since the iterations in the outer loop are independent, I tried to use parfor in the outer loop, but I get the error message "the parfor cannot run due to the way the variable hsim is used".
Could someone explain why and how to solve the problem if possible? Any help is greatly appreciated!
%% Generate fake data for MWE
clear;clc
Nsim = 500;
JJ = 66;
na = 50;
nh = 30;
nz = 7;
a_grid = linspace(-15,100,na)'; % NOT necessarily equispaced!
h_grid = linspace(0,20,nh)'; % NOT necessarily equispaced!
apol = rand(na,nh,nz,JJ);
hpol = rand(na,nh,nz,JJ);
z_sim_ind = unidrnd(nz,Nsim,JJ);
%---------- START REAL CODE
tic
% a_grid and h_grid are column vectors with 50 and 35 elements,
% respectively
% z_sim_ind is a matrix of integers with size [Nsim,JJ]
a_sim = zeros(Nsim,JJ);
h_sim = zeros(Nsim,JJ);
% Find point on a_grid corresponding to zero assets
aa0 = 8;%find_loc(a_grid,0.0);
% Zero housing
hh0 = 1;
a_sim(:,1) = a_grid(aa0);
h_sim(:,1) = h_grid(hh0);
for ii=1:Nsim %with for loop it works but is very slow. parfor is illegal
for jj=1:JJ-1
z_c = z_sim_ind(ii,jj);
apol_interp = griddedInterpolant({a_grid,h_grid},apol(:,:,z_c,jj));
hpol_interp = griddedInterpolant({a_grid,h_grid},hpol(:,:,z_c,jj));
a_sim(ii,jj+1) = apol_interp(a_sim(ii,jj),h_sim(ii,jj));
h_sim(ii,jj+1) = hpol_interp(a_sim(ii,jj),h_sim(ii,jj));
end
end
toc
0 个评论
回答(2 个)
Matt J
2023-1-27
编辑:Matt J
2023-1-27
I don't think it makes sense to use any loops here at all. Just make vectorized calls to your griddedInterpolant objects.
[~,~,m,n]=size(apol);
[~,Jcol]=ndgrid(1:Nsim,1:JJ);
F=griddedInterpolant({a_grid,h_grid,1:m,1:n},apol);
a_sim=F(a_sim(:),h_sim(:), z_sim_ind(:),Jcol(:));
F.Values=hpol;
h_sim=F(a_sim(:),h_sim(:), z_sim_ind(:),Jcol(:));
a_sim=circshift( reshape(a_sim, Nsim,JJ) ,[0,1]);
h_sim=circshift( reshape(h_sim, Nsim,JJ ,[0,1]);
a_sim(:,1) = a_grid(aa0);
h_sim(:,1) = h_grid(hh0);
2 个评论
Matt J
2023-1-27
I think I fixed the discrepancy, but I would really need your input data (in a .mat file) to know for sure.
Matt J
2023-1-27
编辑:Matt J
2023-1-27
Never mind my other answer. I didn't notice that a_sim and h_sim were recursively defined. You can't avoid a loop, but you only need the loop over jj. It cannot be parallelized.
Nsim = 50000;
JJ = 66;
% a_grid and h_grid are column vectors with 50 and 35 elements,
% respectively
% z_sim_ind is a matrix of integers with size [Nsim,JJ]
a_sim = zeros(Nsim,JJ);
h_sim = zeros(Nsim,JJ);
% Find point on a_grid corresponding to zero assets
aa0 = 8;%find_loc(a_grid,0.0);
% Zero housing
hh0 = 1;
a_sim(:,1) = a_grid(aa0);
h_sim(:,1) = h_grid(hh0);
%%%%%%%%%%%%%%%%%%%%%%%
k=(1:Nsim)';
apol_interp = griddedInterpolant({a_grid,h_grid,k},apol(:,:,k,1));
hpol_interp = griddedInterpolant({a_grid,h_grid,k},hpol(:,:,k,1));
for jj=1:JJ-1
z_c = z_sim_ind(:,jj);
I=sub2ind( [Nsim,JJ], zc, repelem(jj,Nsim,1) );
apol_interp.Values = apol(:,:,I);
hpol_interp.Values = hpol(:,:,I);
a_sim(:,jj+1) = apol_interp(a_sim(:,jj),h_sim(:,jj),k);
h_sim(:,jj+1) = hpol_interp(a_sim(:,jj),h_sim(:,jj),k);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!