1

I want to limit execution runtime of a function I am using in my code. Is it possible?

I am using Windows 10 with MATLAB R2015a (unlike in this question asked before: Matlab time limit for function execution), so has there been any changes to make it possible or are there any tweaks to make it possible?

For example:

H = transpose(homograpyMatrix);
t = projective2d(homograpyMatrix);
result = imwarp(img, t);  % If execution takes more than X seconds - stop running

If imwarp is taking too long to compute (more than predefined X seconds) I want MATLAB to stop the running process. Is it possible?

Please note that I don't want to use tic-toc within a loop as a stopping condition, because this is not the case.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NirZ
  • 105
  • 1
  • 9
  • difficult, Matlab does offer you the tool to compute the time needed to run a function - but to do what you ask, you will need a multi-thread environment where one thread execute the code while another thread keeps the timer. As far as I know, there are limited option on controlling this variable. Here is something you might want to read http://blogs.mathworks.com/pick/2008/05/05/advanced-matlab-timer-objects/ – GameOfThrows Jan 19 '16 at 14:15
  • There is no generic matlab wrapper for this. Some matlab functions such as eig are builtin and there's no hook for interrupts for those functions. – hiandbaii Jan 19 '16 at 14:24
  • If you run on linux I think you should be able to force a program to shutdown after a certain time. Not sure how to do it, but I have seen it being done. – patrik Jan 19 '16 at 15:00

1 Answers1

1

You have two options:

Parallel Computing Toolbox

With the Parallel Computing Toolbox, you can define a timeout for idle calculations. It is important to remember that this timeout is reset whenever your process enters a parfor loop or uses parfeval.

Hard-coded Timeout

In this instance you would implement a hard-coded timeout variable and check it at a regular interval to assess if you have to stop the running process or not.

Suever
  • 64,497
  • 14
  • 82
  • 101
Bruno Sarrant
  • 309
  • 2
  • 7
  • do you mean using something like: http://www.mathworks.com/help/distcomp/batch.html – NirZ Jan 19 '16 at 14:42
  • @NirZ: on parallel TB, I was refering to the preferences panel [see this link](https://www.mathworks.com/help/distcomp/parallel-preferences.html). On the other solution, you can use time functions [see this article for their comparison](https://www.mathworks.com/help/matlab/matlab_prog/measure-performance-of-your-program.html) in order to define a time limit for your program. – Bruno Sarrant Jan 19 '16 at 17:00
  • @NirZ: regarding your question, yes you could use the batch function **IF** you use a pool to handle your treatment – Bruno Sarrant Jan 19 '16 at 17:01