-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParforProgressStarter2.m
180 lines (158 loc) · 5.53 KB
/
ParforProgressStarter2.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
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
function ppm = ParforProgressStarter2(s, n, percentage, do_debug, run_javaaddpath, show_execution_time)
% Starter function for parfor progress-monitor, that will automatically
% choose between a text or a GUI progress monitor. You can use this
% progress-monitor in both for and parfor loops.
%
% ParforProgressStarter2 is the successor of ParforProgressStarter - it is
% roughly 12 times more performant during GUI mode!
%
% You should use ParforProgressStarter2 like this:
%
%%%%%%%%%%%%%% Begin ParforProgressStarter2 suggested usage %%%%%%%%%%%%%%%
%
% % how many iterations of the for loop do we expect?
% N = 1000;
% % how often should the monitor update it's progress?
% percentage_update = 0.1;
% % show debugging information.
% do_debug = 1;
% % by default we always run 'javaaddpath'. Set this to 0 if you are
% % making use of globals.
% run_javaaddpath = 1;
% % by default we show the execution time once ParforProgress ends.
% show_execution_time = 1;
% try % Initialization
% ppm = ParforProgressStarter2('test', N, percentage_update, do_debug, run_javaaddpath, show_execution_time);
% catch me % make sure "ParforProgressStarter2" didn't get moved to a different directory
% if strcmp(me.message, 'Undefined function or method ''ParforProgressStarter2'' for input arguments of type ''char''.')
% error('ParforProgressStarter2 not in path.');
% else
% % this should NEVER EVER happen.
% msg{1} = 'Unknown error while initializing "ParforProgressStarter2":';
% msg{2} = me.message;
% print_error_red(msg);
% % backup solution so that we can still continue.
% ppm.increment = nan(1, nbr_files);
% end
% end
%
%
% !! PARFOR behaviour has changed !!
%
% With Matlab >= 2014a, "parfor" will automatically startup the
% parallel pool, even if you don't want it:
% "Starting parallel pool (parpool) using the 'local' profile"
%
% With Matlab < 2014a, "parfor" will be ignored and would fallback to
% a simple "for", if parallel pool wasn't manually started.
%
% parfor i = 1 : N
% your_crazy_function();
% ppm.increment(i);
% end
%
% try % use try / catch here, since delete(struct) will raise an error.
% delete(ppm);
% catch me %#ok<NASGU>
% end
%
%%%%%%%%%%%%%% End ParforProgressStarter2 suggested usage %%%%%%%%%%%%%%%%%
%
% Copyright (c) 2010-2023, Andreas Kotowicz
%
%%
if nargin < 2
disp('usage: ppm = ParforProgressStarter2( text, number_runs, update_percentage, do_debug, run_javaaddpath, show_execution_time )');
ppm = [];
return;
end
if nargin < 3
percentage = 0.1;
end
if nargin < 4
do_debug = 0;
end
if nargin < 5
run_javaaddpath = 1;
end
if nargin < 6
show_execution_time = 1;
end
%% determine whether java and awt are available
java_enabled = 1;
if usejava('jvm') == 0
java_enabled = 0;
end
awt_available = 1;
if usejava('awt') == 0
awt_available = 0;
end
% is matlab pool active?
pool_slaves = pool_size();
%% check for different usage scenarios:
% 1 = ParforProgress2(GUI)
% 2 = ParforProgress2(CONSOLE)
% 3 = fallback ParforProgressConsole2
% by default we use the GUI version.
version_to_use = 1;
if java_enabled == 0 % no java -> use console only
version_to_use = 3;
else
if awt_available == 0 % java but no AWT -> java console
version_to_use = 2;
end
end
% check for old matlab version
% - everything before 2008b (7.7) can cause trouble (because of saveobj /
% loadobj)
if get_matlab_version() < 7.07
version_to_use = 3;
end
%% add directory to javapath and path
a = which(mfilename);
dir_to_add = fileparts(a);
pathDelimiter = pathsep; % Gets the path delimiter based on OS
if pool_slaves > 0
if java_enabled == 1 && run_javaaddpath == 1 && ~isInJavaPath(dir_to_add)
pctRunOnAll(['javaaddpath({''' dir_to_add '''})']);
end
if ~isInMatlabPath(dir_to_add, pathDelimiter)
pctRunOnAll(['addpath(''' dir_to_add ''')']);
end
else
if java_enabled == 1 && run_javaaddpath == 1 && ~isInJavaPath(dir_to_add)
javaaddpath({dir_to_add});
end
if ~isInMatlabPath(dir_to_add, pathDelimiter)
addpath(dir_to_add);
end
end
%%
switch version_to_use
case 1
use_gui = 1;
ppm = ParforProgress2(s, n, percentage, do_debug, use_gui, show_execution_time);
case 2
use_gui = 0;
ppm = ParforProgress2(s, n, percentage, do_debug, use_gui, show_execution_time);
case 3
% no java, no awt, or old matlab version
disp('Progress will update in arbitrary order.');
ppm = ParforProgressConsole2(s, n, percentage);
otherwise
disp('not defined');
ppm = [];
end
end
%% Helper functions
function isPresent = isInJavaPath(dir_to_check)
currentStaticJavaPath = javaclasspath('-static');
currentDynamicJavaPath = javaclasspath('-dynamic');
combinedJavaPath = [currentStaticJavaPath; currentDynamicJavaPath];
isPresent = any(strcmp(dir_to_check, combinedJavaPath));
end
function isPresent = isInMatlabPath(dir_to_check, pathDelimiter)
currentPath = strsplit(path, pathDelimiter);
isPresent = any(strcmp(dir_to_check, currentPath));
end
%% EOF