2 %PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.
4 % your working directory, and then keeping track of the parfor loop
's
5 % progress within that file. This workaround is necessary because parfor
6 % workers cannot communicate with one another so there is no simple way
7 % to know which iterations have finished and which haven't.
9 % PARFOR_PROGRESS(N) initializes the progress monitor for a set of N
10 % upcoming calculations.
12 % PARFOR_PROGRESS updates the progress inside your parfor loop and
13 % displays an updated progress bar.
18 % To suppress output from any of these functions, just ask for a return
19 % variable from the function calls, like PERCENT = PARFOR_PROGRESS which
20 % returns the percentage of completion.
27 % pause(rand); % Replace with real code
34 % By Jeremy Scheff - jdscheff@gmail.com - http:
36 error(nargchk(0, 1, nargin, 'struct'));
43 w = 50; % Width of progress bar
46 f = fopen('parfor_progress.txt', 'w');
48 error('Do you have write permissions for %s?', pwd);
50 fprintf(f, '%d\n', N); % Save N at the top of progress.txt
54 disp([' 0%[>', repmat(' ', 1, w), ']']);
57 delete('parfor_progress.txt');
61 disp([repmat(
char(8), 1, (w+9)),
char(10), '100%[', repmat('=', 1, w+1), ']']);
64 if ~exist('parfor_progress.txt', 'file')
65 error('parfor_progress.txt not found. Run PARFOR_PROGRESS(N) before PARFOR_PROGRESS to initialize parfor_progress.txt.');
68 f = fopen('parfor_progress.txt', 'a');
72 f = fopen('parfor_progress.txt', 'r');
73 progress = fscanf(f, '%d');
75 percent = (length(progress)-1)/progress(1)*100;
78 perc = sprintf('%3.0f%%', percent); % 4 characters wide, percentage
79 disp([repmat(
char(8), 1, (w+9)),
char(10), perc, '[', repmat('=', 1, round(percent*w/100)), '>', repmat(' ', 1, w - round(percent*w/100)), ']']);
function percent = parfor_progress(N)
PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor. PARFOR_PROGRESS works by crea...