MATLAB: Save inside parfor loop at a specific iteration step

parforparsave

Good day, I'm having problems saving inside a parfor loop. What I want to do is to save the results (vector results) from 5000 -> 5000 iterations (parfor loop takes 1 week to complete and I don't want to lose all the temporary result in case that something bad happens with the machine, i.e. reboot, power off..) A simplified version of my code is:
parfor ii = 1:100000
[output1, output2] = MyFunction(~,~,ii)
% from 5000 to 5000 iterations I want to save the vector results
if ii == 1:5000:100000
parsave('results.mat',output1, output2)
% now I want to save the final results
elseif ii == 100000
parsave('results.mat', output1, output2)
end
end
Maybe someone can provide me with an useful parfor.m function, or a way to save it using matfile objects (I don't know how to use them)

Best Answer

Since MyFunction takes as long as it does (at least 6 seconds), I might just save every iteration within a for-drange loop. It takes no time to save a 1e5x1 vector, and the local part on each worker will be smaller still.
output1=distributed.NaN(1,1e5); %pre-allocate
output2=distributed.NaN(1,1e5);
spmd
for i=drange(1:1e5)
[output1(ii), output2(ii)] = MyFunction(~,~,ii);
parsave(['TEMP', num2str(labindex)],...
getLocalPart(output1),getLocalPart(output2));
end
end