Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know how many iterations are left in a parfor loop in Matlab?

I am running a parfor loop in Matlab that takes a lot of time and I would like to know how many iterations are left. How can I get that info?

like image 469
Reed Richards Avatar asked Aug 08 '11 21:08

Reed Richards


1 Answers

I don't believe you can get that information directly from MATLAB, short of printing something with each iteration and counting these lines by hand.

To see why, recall that each parfor iteration executes in its own workspace: while incrementing a counter within the loop is legal, accessing its "current" value is not (because this value does not really exist until completion of the loop). Furthermore, the parfor construct does not guarantee any particular execution order, so printing the iterator value isn't helpful.

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

Maybe someone does have a clever workaround, but I think that the independent nature of the parfor iterations belies taking a reliable count. The restrictions mentioned above, plus those on using evalin, etc. support this conclusion.

As @Jonas suggested, you could obtain the iteration count via side effects occurring outside of MATLAB, e.g. creating empty files in a certain directory and counting them. This you can do in MATLAB of course:

fid = fopen(['countingDir/f' num2str(i)],'w');
fclose(fid);
length(dir('countingDir'));
like image 69
reve_etrange Avatar answered Oct 13 '22 00:10

reve_etrange