/
configCluster.m
295 lines (248 loc) · 8.85 KB
/
configCluster.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
function configCluster
% Configure MATLAB to submit to the cluster.
% Copyright 2013-2022 The MathWorks, Inc.
% The version of MATLAB being supported
release = ['R' version('-release')];
% Import cluster definitions
def = clusterDefinition();
% Cluster list
cluster_dir = fullfile(fileparts(mfilename('fullpath')),'IntegrationScripts');
% Listing of setting file(s). Derive the specific one to use.
cluster_list = dir(cluster_dir);
% Ignore . and .. directories
cluster_list = cluster_list(arrayfun(@(x) x.name(1), cluster_list) ~= '.');
len = length(cluster_list);
if len==0
error('No cluster directory exists.')
elseif len==1
cluster = cluster_list.name;
else
cluster = lExtractPfile(cluster_list);
end
% Determine the name of the cluster profile
profile = [cluster ' ' release];
% Delete the profile (if it exists)
% In order to delete the profile, check first if an existing profile. If
% so, check if it's the default profile. If so, set the default profile to
% "local" (otherwise, MATLAB will throw the following warning)
%
% Warning: The value of DefaultProfile is 'name-of-profile-we-want-to-delete' which is not the name of an existing profile. Setting the DefaultProfile to 'local' at the user level. Valid profile names are:
% 'local' 'profile1' 'profile2' ...
%
% This way, we bypass the warning message. Then remove the old incarnation
% of the profile (that we're going to eventually create.)
if verLessThan('matlab','9.13')
% R2022a and older
% Handle to function returning list of cluster profiles
cp_fh = @parallel.clusterProfiles;
% Handle to function returning default cluster profile
dp_fh = @parallel.defaultClusterProfile;
else
% R2022b and newer
% Handle to function returning list of cluster profiles
cp_fh = @parallel.listProfiles;
% Handle to function returning default cluster profile
dp_fh = @parallel.defaultProfile;
end
if any(strcmp(profile,feval(cp_fh))) %#ok<*FVAL>
% The profile exists. Check if it's the default profile.
if strcmp(profile,feval(dp_fh))
% The profile is the default profile. Change the default profile
% to the default profile (local or Processes) to avoid the
% afformentioned warning.
% Get the list of factory profile names
%
% Before R2022b: local
% After R2022a: Processes, Threads
%
% In either case, pick the first one
fpn = parallel.internal.settings.getFactoryProfileNames;
dp_fh(fpn{1});
end
% The profile is not the default profile, safely remove it.
parallel.internal.ui.MatlabProfileManager.removeProfile(profile)
end
% User's local machine's hostname
if strcmp(def.Type, 'shared')
hostname = '';
else
[~, hostname] = system('hostname');
hostname = strtrim(hostname);
end
% Skip this for shared
if ~strcmp(def.Type,'shared')
% If multiple releases were specified in the mdcs.rc
% select the correct one to use.
releaseBreakDown = strsplit(def.ClusterMatlabRoot,',');
matchingRelease = (~cellfun(@isempty,regexp(releaseBreakDown,release,'once')));
if ~matchingRelease
emsg = sprintf(['\n\t The version of MATLAB you are running is not installed on the cluster.\n', ...
'\t Contact your cluster administrator for further assistance. \n']);
error(emsg)
end
releaseToUse = releaseBreakDown{matchingRelease};
releaseToUse = strsplit(releaseToUse,':');
def.ClusterMatlabRoot = releaseToUse{2};
end
% Create the user's local Job Storage Location folder
loc = '';
if strcmp(def.Type, 'shared')
if isempty(def.LocalJobStorageLocation)
rootd = lGetLocalRoot();
else
user = lGetLocalUsername();
rootd = [def.LocalJobStorageLocation user];
loc = '.matlab';
end
elseif strcmp(def.Type, 'nonshared')
if isempty(def.LocalJobStorageLocation)
rootd = lGetLocalRoot();
else
user = lGetLocalUsername();
rootd = [def.LocalJobStorageLocation user];
if ispc
loc = 'MATLAB';
else
loc = '.matlab';
end
end
elseif strcmp(def.Type, 'remote')
user = getenv('USER');
rootd = [def.LocalJobStorageLocation user];
loc = '.matlab';
end
jsl = fullfile(rootd,loc,'3p_cluster_jobs',cluster,release,def.Type);
if exist(jsl,'dir')==false
[status,err,eid] = mkdir(jsl);
if status==false
error(eid,'Can''t make directory %s: %s',jsl,err)
end
end
% Configure the user's remote storage location and assemble the cluster profile.
if strcmp(def.Type, 'shared')
rjsl = '';
user = '';
def.ClusterHost = '';
def.ClusterMatlabRoot = '';
elseif strcmp(def.Type, 'nonshared')
user = lGetRemoteUsername(cluster);
rootd = [def.RemoteJobStorageLocation user];
rjsl = [rootd '/' '.matlab' '/' '3p_cluster_jobs' '/' cluster '/' hostname '/' release '/' def.Type];
hd = lGetHomeDirPath(cluster);
rootd = [def.RemoteJobStorageLocation '/' ];
elseif strcmp(def.Type, 'remote')
if ispc
rootd = [def.RemoteJobStorageLocation user];
rjsl = [rootd '/' '.matlab' '/' '3p_cluster_jobs' '/' cluster '/' hostname '/' release '/' def.Type];
else
rjsl = '';
end
end
assembleClusterProfile(jsl, rjsl, cluster, user, profile, def);
lNotifyUserOfCluster(profile)
% % Validate if you want to
% ps.Profiles(pnidx).validate
end
function cluster_name = lExtractPfile(cl)
% Display profile listing to user to select from
len = length(cl);
for pidx = 1:len
name = cl(pidx).name;
names{pidx,1} = name; %#ok<AGROW>
end
selected = false;
while selected==false
for pidx = 1:len
fprintf('\t[%d] %s\n',pidx,names{pidx});
end
idx = input(sprintf('Select a cluster [1-%d]: ',len));
selected = idx>=1 && idx<=len;
end
cluster_name = cl(idx).name;
end
function r = lGetLocalRoot()
r = fileparts(prefdir);
end
function un = lGetRemoteUsername(cluster)
un = input(['Username on ' upper(cluster) ' (e.g. jdoe): '],'s');
if isempty(un)
error(['Failed to configure cluster: ' cluster])
end
end
function user = lGetLocalUsername()
user = char(java.lang.System.getProperty('user.name'));
end
function assembleClusterProfile(jsl, rjsl, cluster, user, profile, def)
% Create generic cluster profile
c = parallel.cluster.Generic;
% Required mutual fields
% Location of the Integration Scripts
c.IntegrationScriptsLocation = fullfile(fileparts(mfilename('fullpath')),'IntegrationScripts', cluster);
c.NumWorkers = str2num(def.NumWorkers); %#ok<ST2NM>
c.OperatingSystem = 'unix';
% Depending on the submission type, populate cluster profile fields
if strcmp(def.Type, 'shared')
c.HasSharedFilesystem = true;
else
% Set common properties for nonshared and remote
c.AdditionalProperties.Username = user;
c.AdditionalProperties.ClusterHost = def.ClusterHost;
c.ClusterMatlabRoot = def.ClusterMatlabRoot;
if strcmp(def.Type, 'nonshared')
c.AdditionalProperties.RemoteJobStorageLocation = rjsl;
c.HasSharedFilesystem = false;
elseif strcmp(def.Type,'remote')
if ispc
jsl = struct('windows',jsl,'unix',rjsl);
end
c.HasSharedFilesystem = true;
end
end
c.JobStorageLocation = jsl;
% AdditionalProperties for the cluster:
% username, queue, walltime, e-mail, etc.
c.AdditionalProperties.AccountName = '';
c.AdditionalProperties.AdditionalSubmitArgs = '';
c.AdditionalProperties.AuthenticationMode = 'IdentityFile';
c.AdditionalProperties.IdentityFile = '~/.ssh/id_rsa';
c.AdditionalProperties.IdentityFileHasPassphrase = false;
c.AdditionalProperties.Constraint = '';
c.AdditionalProperties.EmailAddress = '';
c.AdditionalProperties.EnableDebug = false;
c.AdditionalProperties.GpuCard = '';
c.AdditionalProperties.GpusPerNode = 0;
c.AdditionalProperties.MemUsage = '4gb';
c.AdditionalProperties.Nodes = 0;
c.AdditionalProperties.ProcsPerNode = 0;
c.AdditionalProperties.QueueName = '';
c.AdditionalProperties.RequireExclusiveNode = false;
c.AdditionalProperties.Reservation = '';
c.AdditionalProperties.WallTime = '';
% MPI Configuration
if verLessThan('matlab','9.6')
% Set to true in versions older than R2019a
% Use the default smpd process manager
c.AdditionalProperties.UseSmpd = true;
else
% and false in R2019a or newer
% Use the new hydra process manager shipped with MATLAB
c.AdditionalProperties.UseSmpd = false;
end
% Save Profile
c.saveAsProfile(profile);
c.saveProfile('Description', profile)
% Set as default profile
parallel.defaultClusterProfile(profile);
end
function lNotifyUserOfCluster(profile)
%{
cluster = split(profile);
cluster = cluster{1};
fprintf(['\n\tMust set QueueName before submitting jobs to %s. E.g.\n\n', ...
'\t>> c = parcluster;\n', ...
'\t>> c.AdditionalProperties.QueueName = ''queue-name'';\n', ...
'\t>> c.saveProfile\n\n'], upper(cluster))
%}
% configCluster completed
fprintf('Complete. Default cluster profile set to "%s".\n', profile)
end