Different results when using parallelisation by spmd and when not using parallelisation

4 views (last 30 days)
I am running a Monte carlo simulation in which i need to create a very large data set that is done like this:
for r=1:MCREP
Ycom=[];
YLcom=[];
spmd
for it=labindex:numlabs:NT
...(define alpha, delta, x_it,...)
for t=2:TT
yi(t)=xi*alpha+mu*delta+rho*yi(t-1)+beta*x_it(t)+eps_it(t);
yLi(t)=yi(t-1);
end
Ycom=[Ycom yi];
YLcom=[YLcom yLi];
end
end
mm=matlabpool('size');
for i=1:mm
Y(:,(i-1)*(NT/mm)+1:i*(NT/mm))=Ycom{i};
YL(:,(i-1)*(NT/mm)+1:i*(NT/mm))=YLcom{i};
end
... (continue rest of the code, regressions)
end
Basically it creates a really large panel data set. The code creates independent individuals in each column, so the paralellization divides a subset of individuals in each of the workers and then after it is done it puts them together in matrix Y and matrix YL.
All the components of yi and yLi are defined inside the loop.
The code after creating the large panel data set, estimates a regression and gives me the value of the coefficients.
My problem is that if I run without the parallelisation the code takes REALLY LONG time for each repetition. I decide to use spmd over parfor, because every time I run the code using spmd I obtain the same answer but when using parfor I don't.
I understand that this is because in spmd I can "manually" distribute over the workers while in parfor it is done randomly.
I want to know if you can explain to me why do I get different results if I use 0 workers or 8 workers or 16 workers, ...?
I would want to have the same results as if I run without the parallelisation.
Another thing that I tried to do is to parallelise on the very outer loop, the one that runs over the Montecarlo replications, but that does not help to speed the code.
Thank you very much for your help.

Answers (0)

Categories

Find more on Parallel for-Loops (parfor) 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!