How to find out how many iterations are left in the parfor loop in Matlab?

I am running a parr loop in Matlab which is time consuming and I would like to know how many iterations are left. How can I get this information?

+4
source share
5 answers

I do not believe that you can get this information directly from MATLAB without printing something with each iteration and reading these lines manually.

To find out why, remind you that each parfor iteration parfor performed in its own workspace: while incrementing a counter in a loop is legal, accessing its β€œcurrent” value is not (since this value does not really exist until the loop completes). In addition, the parfor construct parfor not guarantee any particular execution order, so printing the value of an iterator is not useful.

 cnt = 0; parfor i=1:n cnt = cnt + 1; % legal disp(cnt); % illegal disp(i); % legal ofc. but out of order end 

Someone might have a smart workaround, but I think the independent nature of the parfor iterations contradicts the correct count. The limitations mentioned above, plus those that use evalin , etc., support this conclusion.

As suggested by @Jonas, you can get an iteration counter using side effects that happen outside of MATLAB, for example. creating empty files in a specific directory and counting them. This can be done in MATLAB, of course:

 fid = fopen(['countingDir/f' num2str(i)],'w'); fclose(fid); length(dir('countingDir')); 
+5
source

Try this FEX file: http://www.mathworks.com/matlabcentral/fileexchange/32101-progress-monitor--progress-bar--that-works-with-parfor

You can easily change it to return the iteration number instead of displaying a progress bar.

+2
source

Something like a progress bar could be done similarly to this ...

Before the parfor :

 fprintf('Progress:\n'); fprintf(['\n' repmat('.',1,m) '\n\n']); 

And during the cycle:

 fprintf('\b|\n'); 

Here m is the total number of iterations,. shows the total number of iterations, and | shows the number of completed iterations. \n ensures that characters are printed in a parfor loop.

+1
source

If you just want to know how much time is left, you can run the program after recording the maximum time, and then do it

 tStart = tic; parfor i=1:n tElapsed = toc(tStart;) disp(['Time left in min ~ ', num2str( ( tMax - tElapsed ) / 60 ) ]); ... end 
0
source

All Articles