-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParforProgress2.m
143 lines (118 loc) · 4.64 KB
/
ParforProgress2.m
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
%
% do NOT use this object by itself!
% use ParforProgressStarter2() instead.
%
% most of the code here is from:
% http://www.mathworks.com/matlabcentral/fileexchange/24594-parfor-progress-monitor
%
% ParforProgress2 - M object to make 'ParforProgressClient2' and
% 'ParforProgressServer2' objects easier to use. Create one of these on the
% client outside your PARFOR loop with a name for the window. Pass the
% object to the PARFOR loop, and have the workers call "increment" at the
% end of each iteration. This sends a notification back to the server which
% then updates the GUI.
%
% Example:
%
% N = 100;
% ppm = ParforProgress2('my task', N);
% parfor i = 1 : N
% rand(1);
% ppm.increment(i);
% end
% delete(ppm);
%
%
% Copyright (c) 2010-2014, Andreas Kotowicz
classdef ParforProgress2 < handle
properties (GetAccess = private, SetAccess = private)
Port
HostName
OldVersion = 0;
DEBUG = 0;
end
properties (Transient, GetAccess = private, SetAccess = private)
JavaBit
end
methods (Static)
function o = loadobj(X)
% Once we've been loaded, we need to reconstruct ourselves
% correctly as a client
o = ParforProgress2( {X.HostName, X.Port, X.OldVersion, X.DEBUG} );
% disp('loading');
end
end
methods
function o = ParforProgress2(s, n, percentage, do_debug, use_gui, show_execution_time)
% ParforProgress Build a Parfor Progress Monitor
% Use the syntax: ParforProgress( 'Window Title', N, percentage, do_debug, use_gui )
% where N is the number of iterations in the PARFOR loop
% initalize client
if nargin == 1 && iscell(s)
% "Private" constructor used for the clients
o.JavaBit = ParforProgressClient2.createClient(s{1}, s{2}, s{4});
o.Port = [];
% initialize server
elseif (nargin == 6 || nargin == 5 || nargin == 4 || nargin == 3 || nargin == 2)
if nargin < 6
show_execution_time = 1;
end
if nargin < 5
use_gui = 1;
end
if nargin < 4
do_debug = 0;
end
if nargin < 3
percentage = 0.1;
end
% check for old matlab versions.
if get_matlab_version < 7.07
o.OldVersion = 1;
end
o.JavaBit = ParforProgressServer2.createServer(s, n, percentage, use_gui, show_execution_time);
o.Port = double(o.JavaBit.getPort());
% Get the client host name from pctconfig - needs
% distcomp toolbox.
% cfg = pctconfig;
% o.HostName = cfg.hostname;
% gethostname() is also problematic, because laptop might
% not be connect to network (will have local IP only).
%o.HostName = gethostname();
address = java.net.InetAddress.getLocalHost;
o.HostName = char(address.getHostAddress);
o.DEBUG = do_debug;
else
error( 'Public constructor is: ParforProgress2(''Text'', N, percentage, do_debug, use_gui)' );
end
end
function X = saveobj(o)
% keep port, hostname, matlab version and debug flag
X.Port = o.Port;
X.HostName = o.HostName;
X.OldVersion = o.OldVersion;
X.DEBUG = o.DEBUG;
% disp('saving');
end
function increment(o, i) %#ok<INUSD>
% i is a fake input so we stay compatible with
% "ParforProgressConsole2.m"
% Something is wrong with matlab 2008a and the saveobj /
% loadobj methods. There is no 'JavaBit' in the 'o' if you
% have matlabpool (<= 2013a) / parpool (>= 2013b) enabled.
if o.OldVersion == 0
o.JavaBit.increment();
end
end
function delete(o)
% Close the UI
% somethings wrong with matlab 2008a and the saveobj /
% loadobj methods. there is no 'JavaBit' in the 'o' if you
% have matlabpool (<= 2013a) / parpool (>= 2013b) enabled.
if o.OldVersion == 0
o.JavaBit.done();
end
end
end
end
%% EOF