-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgpinitparallel.m
182 lines (152 loc) · 5.67 KB
/
gpinitparallel.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
180
181
182
function gp = gpinitparallel(gp)
%GPINITPARALLEL Initialise the Parallel Computing Toolbox.
%
% GP = GPINITPARALLEL(GP) initialises the Parallel Computing Toolbox if
% it has been enabled and the Parallel Computing Toolbox license is
% present.
%
% IMPORTANT:
%
% There is a known JVM bug in versions of MATLAB (all platforms) prior to
% version R2013b (6.3). This causes a failure of the Parallel Computing
% Toolbox in most cases.
%
% There is a fix/workaround for this here:
%
% http://www.mathworks.com/support/bugreports/919688
%
% Please apply this fix if you are using a version prior to R2013b.
%
% Copyright (c) 2009-2015 Dominic Searson
%
% GPTIPS 2
%
% See also GPINIT, GPTOOLBOXCHECK
gp.runcontrol.parallel.ok = false;
parToolbox = gp.info.toolbox.parallel;
if parToolbox && (gp.runcontrol.parallel.auto || gp.runcontrol.parallel.enable)
mps = getMaxPoolSize;
end
%in auto mode - try to start parallel mode with auto settings but exit and
%proceed in standard mode if no license
if gp.runcontrol.parallel.auto
if parToolbox
gp.runcontrol.parallel.enable = true;
gp.runcontrol.parallel.autosize = true;
gp.runcontrol.parallel.numWorkers = mps;
else
gp.runcontrol.parallel.enable = false;
return;
end
end
if gp.runcontrol.parallel.enable
if ~parToolbox
error('You do not have a license for the Parallel Computing Toolbox or it is not installed. Please set gp.runcontrol.parallel.enable = false.');
end
ps = getCurrentPoolSize;
%check if pool is already open and of right size
if (ps > 0) && ( (gp.runcontrol.parallel.autosize && (ps == mps) ) || ...
(~gp.runcontrol.parallel.autosize && (ps == gp.runcontrol.parallel.numWorkers) ) )
gp.runcontrol.parallel.ok = true;
gp.runcontrol.parallel.numWorkers = ps;
if ~gp.runcontrol.quiet
disp(' ');
disp(['GPTIPS: proceeding in parallel mode with ' num2str(gp.runcontrol.parallel.numWorkers) ' workers.']);
disp(' ');
end
else %otherwise stop then start correct size pool
if ps > 0
if ~gp.runcontrol.quiet
disp(' ');
disp('GPTIPS: stopping existing pool for parallel computation.');
disp(' ');
end
if verLessThan('matlab','8.3.0')
matlabpool close;
else
delete(gcp('nocreate'));
end
end
try %start new pool
if ~gp.runcontrol.quiet
disp(' ');
disp('GPTIPS: attempting to start new pool for parallel computation.');
disp(' ');
end
if verLessThan('matlab','8.3.0')
matlabpool close force local;
eval(['matlabpool ' num2str(gp.runcontrol.parallel.numWorkers)]);
else
delete(gcp('nocreate'));
if gp.runcontrol.parallel.autosize
a = parpool;
gp.runcontrol.parallel.numWorkers = a.NumWorkers;
else
parpool(gp.runcontrol.parallel.numWorkers);
end
end
gp.runcontrol.parallel.ok = true;
if ~gp.runcontrol.quiet
disp(' ');
disp(['GPTIPS: proceeding in parallel mode with ' num2str(gp.runcontrol.parallel.numWorkers) ' workers.']);
disp(' ');
end
catch
gp.runcontrol.parallel.ok = false;
if ~gp.runcontrol.quiet
warning('There was a problem starting GPTIPS in parallel computation mode.');
warning('To run GPTIPS in non-parallel mode set gp.runcontrol.parallel.enable = false in your config file.');
error(['GPTIPS: could not start pool for parallel computation because: ' lasterr]);
end
end
end
else
gp.runcontrol.parallel.ok = false;
end
function x = getCurrentPoolSize
%GETCURRENTPOOLSIZE Get the current parallel pool size. Workaround
%function: calls either matlabpool('size') or pool_size hack depending on
%MATLAB version
if verLessThan('matlab', '7.7.0')
x = pool_size;
elseif verLessThan('matlab','8.3.0')
x = matlabpool('size');
else
pool = gcp('nocreate');
if isempty(pool)
x = 0;
else
if pool.Connected
x = pool.NumWorkers;
else
x = 0;
end
end
end
function x = getMaxPoolSize
%getMaxPoolSize - try to get the max number of workers in a pool for the
%default profile on the current machine
if verLessThan('matlab','8.3.0')
c = findResource('scheduler', 'configuration', defaultParallelConfig);
x = c.ClusterSize;
else
c = parcluster();
x = c.NumWorkers;
end
function x = pool_size
%POOL_SIZE - Mathworks hack to return size of current MATLABPOOL. Used for
%versions prior to MATLAB 7.7 (R2008b)
%
% See:
% http://www.mathworks.co.uk/support/solutions/en/data/1-5UDHQP/index.html
session = com.mathworks.toolbox.distcomp.pmode.SessionFactory.getCurrentSession;
if ~isempty( session ) && session.isSessionRunning() && session.isPoolManagerSession()
client = distcomp.getInteractiveObject();
if strcmp( client.CurrentInteractiveType, 'matlabpool' )
x = session.getLabs().getNumLabs();
else
x = 0;
end
else
x = 0;
end