Can a parfor loop be restarted?

2 次查看(过去 30 天)
Tyler Warner
Tyler Warner 2018-6-8
回答: OCDER 2018-6-11
I am processing large amounts of files. Each file might take thirty minutes to an hour. Here's the code snippet on the high level:
parfor firstfile:lastfile
[morestuff1, morestuff2] = runfunction(stuff1, stuff2);
end
Problem is that every night I leave it running on the server, something wrong happens and the parfor doesn't complete. I want to be able to catch a loop that fails for any reason and restart it later. Here's what I am proposing as the solution:
files = firstfile:lastfile;
while length(files) > 0
parfor currentfile = files
try
[morestuff1, morestuff2] = runfunction(stuff1, stuff2);
files(files == currentfile) = [];
catch
% not sure what to do here
end
end
end

回答(1 个)

OCDER
OCDER 2018-6-11
Perhaps you can save the input variables used for your runfunction into a separate cell array. Try this for example:
ErrorFile = cell(1, 1000);
parfor k = 1:1000
stuff1 = rand(1);
stuff2 = rand(1);
try
assert(stuff1 > stuff2, 'OOPS: Error in this function!'); %Pretend this is the "runfunction". If there's an error, record the input variables.
catch Msg
ErrorFile{k} = {Msg, k, stuff1, stuff2}; %Save the iteration #, variables, and error message
end
end
ErrorFile(cellfun(@isempty, ErrorFile)) = []; %This stores variables required to redo parfor with errors
cellfun(@(x) display(x{1}), ErrorFile); %Display all the error messages

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by