File size: 6,847 Bytes
5ab75a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | classdef parfor_progressbar < handle
% PARFOR_PROGRESSBAR Progress bar suitable for multi-process computation.
%
% H = parfor_progressbar(N, 'message', 'property',value, ...)
% creates a graphical progress bar with N iterations before completion.
% A temporary file in tempdir is used to communicate among threads.
% Any property/value pairs are passed to waitbar internally (optional).
%
% H.iterate(X) updates the progress bar by X iterations.
%
% Example:
% --------
% N=50; %total number of parfor iterations
% hbar = parfor_progressbar(N,'','Name','Parfor Progress'); %create the progress bar
% parfor i=1:N,
% pause(rand*2); % computation
% hbar.iterate(1); % update progress by one iteration
% end
% close(hbar); %close progress bar
%
% Notes:
% ------
% Properties cannot be modified while inside a parfor loop. Use iterate only.
%
% With many short iterations, call iterate() periodically. Example:
% if mod(i,10)==0, hbar.iterate(10); end
%
% Inspired by the parfor_progress script made by Jeremy Scheff:
% http://www.mathworks.com/matlabcentral/fileexchange/32101
%
% See also: WAITBAR, PARFOR.
% Copyright 2015-2016 Cornell University All Rights Reserved.
% Public properties
properties (SetAccess=protected, GetAccess=public)
wbh; % Waitbar figure object handle
N; % Total number of iterations expected before completion
end
properties (Dependent, GetAccess=public)
percent; % Percentage of completed iterations
end
properties (Dependent, SetAccess=public, GetAccess=public)
message; % Message text displayed in waitbar
end
% Internal properties
properties (SetAccess=protected, GetAccess=protected, Hidden)
ipcfile; % Path to temporary file for inter-process communication
htimer; % Timer object that checks ipcfile for completed iterations
computationTimer; % Time of computation used to estimate additional time required
end
methods
%======================== CONSTRUCTOR ========================%
function this = parfor_progressbar(N_init, varargin)
% Create a new progress bar with N_init iterations before completion.
% Create a unique inter-process communication file
for i=1:10
f = sprintf('%s%d.txt', mfilename, round(rand*1000));
this.ipcfile = fullfile(tempdir, f);
if ~exist(this.ipcfile,'file'), break; end
end
if exist(this.ipcfile,'file')
error('Too many temporary files. Clear out tempdir.');
end
% Create a new waitbar
this.N = N_init;
this.wbh = waitbar(0, varargin{:});
% Create Timer to estimate additional computation time
this.computationTimer = tic;
pause(1)
% Create timer to periodically update the waitbar in the GUI thread.
this.htimer = timer( 'ExecutionMode','fixedSpacing', 'Period',1, ...
'BusyMode','drop', 'Name',mfilename, ...
'TimerFcn',@(x,y)this.tupdate );
start(this.htimer);
end
%========================= DESTRUCTOR ========================%
function delete(this)
this.close();
end
function close(this)
% Closer the progress bar and clean up internal state.
% Stop the timer
if isa(this.htimer,'timer') && isvalid(this.htimer),
stop(this.htimer);
pause(0.01);
delete(this.htimer);
end
this.htimer = [];
% Delete the IPC file.
if exist(this.ipcfile,'file'),
delete(this.ipcfile);
end
% Close the waitbar
if ishandle(this.wbh)
close(this.wbh);
end
this.wbh = [];
end
%====================== GET/SET METHODS ======================%
function percent = get.percent(this)
% Calculate the fraction of completed iterations from IPC file.
if ~exist(this.ipcfile, 'file')
percent = 0; % File may not exist before the first iteration
else
fid = fopen( this.ipcfile, 'r' );
percent = sum(fscanf(fid, '%d')) / this.N;
percent = max(0, min(1,percent) );
fclose(fid);
end
end
function set.message(this, newMsg)
% Update the progress bar's displayed message.
if ishandle(this.wbh)
waitbar( this.percent, this.wbh, newMsg );
end
end
function iterate(this, Nitr)
% Update the progress bar by Nitr iterations (or 1 if not specified).
if nargin<2, Nitr = 1; end
fid = fopen(this.ipcfile, 'a');
fprintf(fid, '%d\n', Nitr);
fclose(fid);
end
end %public methods
%===================== INTERNAL METHODS =====================%
methods (Access=protected, Hidden)
function tupdate(this)
% Check the IPC file and update the waitbar with progress.
if ishandle(this.wbh)
g = toc(this.computationTimer)*(1-this.percent)/this.percent;
% Make String for Updated waitbar message
if g <= 120 || g == inf% Write Projected ETA in Seconds if ETA < 2 minutes
messageString = {['Progress: ' num2str(round(this.percent*this.N),'%d') ' of ' num2str(this.N,'%d')];...
['Projected ETA: ' num2str(round(g),'%d') ' Seconds']};
elseif g > 120 && g <= 60*60*2 % Write Projected ETA in Minutes if ETA < 2 Hours
messageString = {['Progress: ' num2str(round(this.percent*this.N),'%d') ' of ' num2str(this.N,'%d')];...
['Projected ETA: ' num2str(g/60,'%0.2f') ' Minutes']};
elseif g > 60*60*2 && g <= 60*60*24 % Write Projected ETA in Hours if ETA < 1 Day
messageString = {['Progress: ' num2str(round(this.percent*this.N),'%d') ' of ' num2str(this.N,'%d')];...
['Projected ETA: ' num2str(g/60/60,'%0.2f') ' Hours']} ;
else % Write Projected ETA in Days
messageString = {['Progress: ' num2str(round(this.percent*this.N),'%d') ' of ' num2str(this.N,'%d')];...
['Projected ETA: ' num2str(g/60/60/24,'%0.2f') ' Days']};
end
waitbar( this.percent, this.wbh ,messageString);
else
% Kill the timer if the waitbar is closed.
close(this);
end
end
end %private methods
end %classdef |