Home > data-vis > supporting_functions > parfor_progress.m

parfor_progress

PURPOSE ^

PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.

SYNOPSIS ^

function percent = parfor_progress(N)

DESCRIPTION ^

PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.
   PARFOR_PROGRESS works by creating a file called parfor_progress.txt in
   your working directory, and then keeping track of the parfor loop's
   progress within that file. This workaround is necessary because parfor
   workers cannot communicate with one another so there is no simple way
   to know which iterations have finished and which haven't.

   PARFOR_PROGRESS(N) initializes the progress monitor for a set of N
   upcoming calculations.

   PARFOR_PROGRESS updates the progress inside your parfor loop and
   displays an updated progress bar.

   PARFOR_PROGRESS(0) deletes parfor_progress.txt and finalizes progress
   bar.

   To suppress output from any of these functions, just ask for a return
   variable from the function calls, like PERCENT = PARFOR_PROGRESS which
   returns the percentage of completion.

   Example:

      N = 100;
      parfor_progress(N);
      parfor i=1:N
         pause(rand); % Replace with real code
         parfor_progress;
      end
      parfor_progress(0);

   See also PARFOR.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function percent = parfor_progress(N)
0002 %PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.
0003 %   PARFOR_PROGRESS works by creating a file called parfor_progress.txt in
0004 %   your working directory, and then keeping track of the parfor loop's
0005 %   progress within that file. This workaround is necessary because parfor
0006 %   workers cannot communicate with one another so there is no simple way
0007 %   to know which iterations have finished and which haven't.
0008 %
0009 %   PARFOR_PROGRESS(N) initializes the progress monitor for a set of N
0010 %   upcoming calculations.
0011 %
0012 %   PARFOR_PROGRESS updates the progress inside your parfor loop and
0013 %   displays an updated progress bar.
0014 %
0015 %   PARFOR_PROGRESS(0) deletes parfor_progress.txt and finalizes progress
0016 %   bar.
0017 %
0018 %   To suppress output from any of these functions, just ask for a return
0019 %   variable from the function calls, like PERCENT = PARFOR_PROGRESS which
0020 %   returns the percentage of completion.
0021 %
0022 %   Example:
0023 %
0024 %      N = 100;
0025 %      parfor_progress(N);
0026 %      parfor i=1:N
0027 %         pause(rand); % Replace with real code
0028 %         parfor_progress;
0029 %      end
0030 %      parfor_progress(0);
0031 %
0032 %   See also PARFOR.
0033 
0034 % By Jeremy Scheff - jdscheff@gmail.com - http://www.jeremyscheff.com/
0035 
0036 error(nargchk(0, 1, nargin, 'struct'));
0037 
0038 if nargin < 1
0039     N = -1;
0040 end
0041 
0042 percent = 0;
0043 w = 50; % Width of progress bar
0044 
0045 if N > 0
0046     f = fopen('parfor_progress.txt', 'w');
0047     if f<0
0048         error('Do you have write permissions for %s?', pwd);
0049     end
0050     fprintf(f, '%d\n', N); % Save N at the top of progress.txt
0051     fclose(f);
0052     
0053     if nargout == 0
0054         disp(['  0%[>', repmat(' ', 1, w), ']']);
0055     end
0056 elseif N == 0
0057     delete('parfor_progress.txt');
0058     percent = 100;
0059     
0060     if nargout == 0
0061         disp([repmat(char(8), 1, (w+9)), char(10), '100%[', repmat('=', 1, w+1), ']']);
0062     end
0063 else
0064     if ~exist('parfor_progress.txt', 'file')
0065         error('parfor_progress.txt not found. Run PARFOR_PROGRESS(N) before PARFOR_PROGRESS to initialize parfor_progress.txt.');
0066     end
0067     
0068     f = fopen('parfor_progress.txt', 'a');
0069     fprintf(f, '1\n');
0070     fclose(f);
0071     
0072     f = fopen('parfor_progress.txt', 'r');
0073     progress = fscanf(f, '%d');
0074     fclose(f);
0075     percent = (length(progress)-1)/progress(1)*100;
0076     
0077     if nargout == 0
0078         perc = sprintf('%3.0f%%', percent); % 4 characters wide, percentage
0079         disp([repmat(char(8), 1, (w+9)), char(10), perc, '[', repmat('=', 1, round(percent*w/100)), '>', repmat(' ', 1, w - round(percent*w/100)), ']']);
0080     end
0081 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005