Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another technique you might like to consider is checkpointing. I've used something similar with long-running (more than one day) loops operating in an environment where the machine could become unavailable at any time, e.g. distributed clusters of spare machines in a lab.</p> <p>Basically, you check to see if a 'checkpoint' file exists before you start your loop. If it does than that suggests the loop did not finish successfully last time. It contains information on where the loop got up to as well as any other state that you need to get going again.</p> <p>Here's a simplified example:</p> <pre><code>function myFunction() numIter = 10; startIter = 1; checkpointFilename = 'checkpoint.mat'; % Check for presence of checkpoint file suggesting the last run did not % complete if exist(checkpointFilename, 'file') s = load(checkpointFilename); startIter = s.i; fprintf('Restarting from iteration %d\n', startIter); end for i = startIter:numIter fprintf('Starting iteration %d\n', i); expensiveComputation(); save(checkpointFilename, 'i'); end % We succefully finished. Let's delete our checkpoint file delete(checkpointFilename); function expensiveComputation() % Pretend to do lots of work! pause(1); end end </code></pre> <p>Running and breaking out with ctrl-c part way through looks like this:</p> <pre><code>&gt;&gt; myFunction Starting iteration 1 Starting iteration 2 Starting iteration 3 Starting iteration 4 Operation terminated by user during myFunction/expensiveComputation (line 27) In myFunction (line 18) expensiveComputation(); &gt;&gt; myFunction Restarting from iteration 4 Starting iteration 4 Starting iteration 5 ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload