What programming techniques can I use to avoid wasted time with long computations that get interrupted unintentionally?

1 view (last 30 days)
There are a couple of reasons why long computations can get interrupted unintentionally:
1. Machine crashes
2. MATLAB crashes or runs out of memory
3. Hitting Ctrl-C unintentionally
4. Losing the connection to a license server does not interrupt a longer computation (see related solution). But in case you request a toolbox or another product licensed through a license server during the computation and the connection is interrupted, this interrupts the whole computation.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Apr 2023
Edited: MathWorks Support Team on 19 Apr 2023
This article focuses on mitigation strategies in above cases.
In addition, it is recommended to investigate programming techniques to speed up code execution as well as software solutions like Parallel Computing Toolbox https://www.mathworks.com/products/parallel-computing.html and MATLAB Distributed Computing Server https://www.mathworks.com/products/matlab-parallel-server.html. You can also look at avoiding memory issues, see the Tech-Notes “Memory Management Guide” https://www.mathworks.com/help/matlab/performance-and-memory.html and “Avoiding Out of Memory Errors” https://www.mathworks.com/help/matlab/performance-and-memory.html
1. To mitigate the negative effects of an interrupted MATLAB computation, the primary countermeasure is saving intermediate results during the computation and prepare the code to be able to reload the saved results.
Example a):
n=40;
v=zeros(n,1);
for k=1:n
disp([num2str(k), ' of ', num2str(n)])
% Think of this being a very time consuming computation
M=rand(4000);
v(k)=sum(sum(M));
% Save the result vector into a MAT file
save my_lifeline_file v
end
Depending on your data, you may choose to save only parts of the computational results or to not save them every time.
Example b):
if mod(k,4)==0
% Save the result vector into a MAT file
save my_lifeline_file v
disp(['Save for k=', num2str(k)])
end
You can also incorporate timestamps into the filename in order to overwrite existing files.
Lastly, consider what you would actually do in case you need to recover the data and prepare the code.
Example c):
n=40;
v=zeros(n,1);
recover=0; % Set recover to 1 after a crash during a computation
if recover
load my_lifeline_file
m=max(find(v));
else
m=1;
end
for k=m:n
2. If you use functionality of toolboxes or other products licensed through a license server, you may choose to either explicitly checkout the product through the LICENSE command (option a) or verify the availability of the product key (option b).
a)
load mandrill % start of MATLAB code
license('checkout','image_toolbox'); % actively checks out the Image Processing Toolbox
% longer computations
X=imrotate(X, 135); % uses a command from the Image Processing Toolbox
disp('done')
b)
load mandrill % start of MATLAB code
t=license('test','image_toolbox'); % tests availability of the Image Processing Toolbox
if t
X=imrotate(X, 135); % uses a command from the Image Processing Toolbox
disp('done')
else
disp('Image Processing Toolbox not available')
end
To identify product names for the use in the LICENSE command, open the license file. The product names can be found next to the INCREMENT keyword.

More Answers (0)

Categories

Find more on Manage Products in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!