-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterDefinition.m
365 lines (302 loc) · 12.3 KB
/
clusterDefinition.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
function out = clusterDefinition(cluster)
% This function will be used to read in the clusternameDesktop.conf or clusternameCluster.conf and to extract
% the necessary information in order to build the cluster profile
% in MATLAB. clusterDefinition will pass back a structure to
% configCluster.
% Copyright 2017-2023 The MathWorks, Inc.
% Determine the location of the clusternameDesktop.conf or clusternameCluster.conf; we assume that
% it is in the same directory as configCluster. Additional logic to support in-place development.
filenameCluster = fullfile(fileparts(mfilename('fullpath')), sprintf('%sCluster.conf',cluster));
filenameRemoteCluster = fullfile(fileparts(mfilename('fullpath')), sprintf('%sRemoteCluster.conf',cluster));
filenameDesktop = fullfile(fileparts(mfilename('fullpath')), sprintf('%sDesktop.conf',cluster));
filenameRemoteDesktop = fullfile(fileparts(mfilename('fullpath')), sprintf('%sRemoteDesktop.conf',cluster));
if (exist(filenameDesktop, 'file') || exist(filenameRemoteDesktop, 'file')) && ...
(exist(filenameCluster, 'file') || exist(filenameRemoteCluster, 'file'))
validYesNoResponse = {'y', 'n', ''};
confMessage = 'Is this configuration for on-cluster use? [Y/n] ';
confResponse = iLoopUntilValidStringInput(confMessage, validYesNoResponse);
if strcmpi(confResponse, 'y') || isempty(confResponse)
if exist(filenameCluster, 'file')
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sCluster.conf',cluster));
fprintf('Using file %sCluster.conf\n',cluster)
else
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sRemoteCluster.conf',cluster));
fprintf('Using file %sRemoteCluster.conf\n',cluster)
end
else
if exist(filenameDesktop, 'file')
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sDesktop.conf',cluster));
fprintf('Using file %sDesktop.conf\n',cluster)
else
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sRemoteDesktop.conf',cluster));
fprintf('Using file %sRemoteDesktop.conf\n',cluster)
end
end
elseif exist(filenameDesktop, 'file')
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sDesktop.conf',cluster));
elseif exist(filenameCluster, 'file')
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sCluster.conf',cluster));
elseif exist(filenameRemoteDesktop, 'file')
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sRemoteDesktop.conf',cluster));
elseif exist(filenameRemoteCluster, 'file')
file = fullfile(fileparts(mfilename('fullpath')), sprintf('%sRemoteCluster.conf',cluster));
else
error('Unable to find matching .conf file for cluster "%s".', cluster)
end
[~, name, ext] = fileparts(file);
configFileName = strcat(name,ext);
% Import cluster definitions
if verLessThan('matlab','9.14')
out = iBackwardsCompatibleClusterDef(file);
else
out = parallel.internal.discover.FilesystemGeneric.parseTemplateFile(file);
end
if isempty(out)
error('Error in reading %s. Please make sure file is filled out correctly.', configFileName);
end
% Clean up
out = iCleanUp(out);
% Error Check
out = iErrorCheckDef(out);
function out = iBackwardsCompatibleClusterDef(file)
% If cluster.conf does not exist or can't be accessed, throw an error
if file < 0
error('Unable to read or access %s\n%s', configFileName, errormsg)
end
out = [];
% Read file
lines = iLoadFile(file);
% Construct mapping of environment variable tokens (i.e. "$VARIABLE") to value
[envVarTokens, envVarValues] = iResolveEnvVarTokens(lines);
% Parse each line
profileData = struct();
heading = string.empty;
for idx = 1:numel(lines)
line = lines(idx);
% Heading directive
if startsWith(line, "[") && endsWith(line, "]")
% Split by dot: "[ ABC.XYZ ]" -> ["ABC", "XYZ"]
heading = split(strtrim(extractBetween(line, "[", "]")), ".");
invalidHeadingParts = iGetInvalidVarNames(heading);
if ~isempty(invalidHeadingParts)
dctSchedulerMessage(2, "Skipping file '%s' because the following line defines the invalid variable name '%s':\n%s", ...
file, invalidHeadingParts(1), line);
return
end
continue
end
% Give up if there isn't an equals sign
if ~contains(line, "=")
dctSchedulerMessage(2, "Skipping file '%s' because the following line could not be interpreted:\n%s", ...
file, line);
return
end
% Get field name and attributes, expecting line of form:
% name (attributes) = value
[name, attributes, value] = iParseLine(line);
invalidNameParts = iGetInvalidVarNames(name);
if ~isempty(invalidNameParts)
dctSchedulerMessage(2, "Skipping file '%s' because the following line defines the invalid variable name '%s':\n%s", ...
file, invalidNameParts(1), line);
return
end
% Ignore OS-specific lines that aren't for this OS
if iHasAnyOSAttribute(attributes) && ~iHasThisOSAttribute(attributes)
continue
end
% Get full name including prepended heading
fullname = [heading; name];
% Resolve environment variables
value = replace(value, envVarTokens, envVarValues);
% Try to convert value to a double or logical
value = iAttemptConversion(value);
% Add PV pair to struct
profileData = setfield(profileData, fullname{:}, value);
end
out = profileData;
% Function iBackwardsCompatibleClusterDef end
end
function out = iCleanUp(out)
% Change fields in 'out' to a char
fields = fieldnames(out);
for i = 1:numel(fields)
if isstring(out.(fields{i}))
out.(fields{i}) = char(out.(fields{i}));
end
end
% Change out.AdditionalProperties fields to a char
propfields = fieldnames(out.AdditionalProperties);
for i = 1:numel(propfields)
if isstring(out.AdditionalProperties.(propfields{i}))
out.AdditionalProperties.(propfields{i}) = char(out.AdditionalProperties.(propfields{i}));
end
end
% Function iCleanUp end
end
function out = iErrorCheckDef(out)
% Error checking and character checks
% Verify that numWorkers is specified and an integer
numWorkersStr = string(out.NumWorkers);
if (strlength(numWorkersStr) == 0)
error('NumWorkers must be specified in the %s configuration file.', configFileName)
elseif ~isa(out.NumWorkers, 'double')
error('NumWorkers must be specified as an integer in the %s configuration file.', configFileName)
end
% Changes ClusterHost value to a char
if isfield(out, 'AdditionalProperties') && isfield(out.AdditionalProperties, 'ClusterHost')
out.AdditionalProperties.ClusterHost = char(out.AdditionalProperties.ClusterHost);
end
% Perform last character check for RemoteJobStorageLocation
% If cluster is running Windows OS, change the second argument to 'pc' from 'unix'
if isfield(out, 'RemoteJobStorageLocation') && (strlength(out.RemoteJobStorageLocation) > 0)
out.RemoteJobStorageLocation = lastCharacterCheck(out.RemoteJobStorageLocation, 'unix');
end
% Change JSL stuct values to a char
if isfield(out, 'JobStorageLocation')
if isfield(out.JobStorageLocation, 'unix')
out.JobStorageLocation.unix = char(out.JobStorageLocation.unix);
elseif isfield(out.JobStorageLocation, 'windows')
out.JobStorageLocation.windows = char(out.JobStorageLocation.windows);
end
end
% Function iErrorCheckDef end
end
function out = lastCharacterCheck(in, machineType)
% Verify that the last string is '/' or '\' and if not, append it
% Set a default in case no modifications are required
out = in;
% Return if out is empty to support remote submission on-cluster
%if isempty(out)
if (strlength(out) == 0)
return
end
% Check to see if we need to append a slash as a last character
if strcmp(machineType, 'pc')
if ~strcmp(in(end), '\')
out = strcat(in, '\');
end
else
if ~strcmp(in(end), '/')
out = strcat(in, '/');
end
end
% Function lastCharacterCheck end
end
function lines = iLoadFile(file)
lines = splitlines(string(fileread(file)));
lines = strtrim(lines); % strip whitespace
lines = lines(lines ~= ""); % remove empties
lines = lines(~startsWith(lines, "#")); % remove comments
% Function iLoadFile end
end
function [envVarTokens, envVarValues] = iResolveEnvVarTokens(lines)
% This is looking for everything between $ and " EXCEPT for =.
% Define the pattern for extracting environment variables and replace the use of the "cellfun" function with a loop
envVarPattern = '"\$\w+"';
envVarTokens = cell(size(lines));
for i = 1:numel(lines)
envVarTokens{i} = regexp(lines{i}, envVarPattern, 'match');
end
envVarTokens = unique([envVarTokens{:}]);
envVarNames = extractBetween(envVarTokens, '"$', '"');
envVarValues = cellfun(@getenv, envVarNames, 'UniformOutput', false);
% Set MATLAB_VERSION_STRING if it is used but unset
verIdx = find(envVarNames == "MATLAB_VERSION_STRING");
if ~isempty(verIdx) && isempty(envVarValues{verIdx})
envVarValues{verIdx} = version('-release');
end
% Change to string
envVarTokens = string(envVarTokens);
envVarValues = string(envVarValues);
% Function iResolveEnvVarTokens end
end
function [name, attributes, value] = iParseLine(line)
% Get field name, attributes and value, expecting line of form:
% name (attributes) = value
% or
% (attributes) name = value
% Implementation exploits the restriction that neither name nor attributes may
% contain an equals sign.
% Extract value
value = strtrim(extractAfter(line, "="));
% Extract "name (attributes)" or "(attributes) name"
nameAndAttrs = strtrim(extractBefore(line, "="));
% Extract and parse attribute specifier
attributeSpecifier = extractBetween(nameAndAttrs,'(',')','Boundaries','inclusive');
attributes = iParseAttributes(extractBetween(attributeSpecifier, "(", ")"));
% Extract and parse name
name = strtrim(erase(nameAndAttrs, attributeSpecifier));
name = split(name, ".");
% Function iParseLine end
end
function value = iAttemptConversion(value)
% Convert value to a double or logical if possible, otherwise leave as string
valueAsDouble = str2double(value);
if ~isnan(valueAsDouble)
value = valueAsDouble;
elseif strcmpi(value, "true")
value = true;
elseif strcmpi(value, "false")
value = false;
elseif iCouldBeTextVector(value)
% Evaluate expression with no allowed functions to parse the string
value = eval(value);
end
% Function iAttemptConversion end
end
function tf = iCouldBeTextVector(str)
tf = iCouldBeStringArray(str) || iCouldBeCellStr(str);
end
function tf = iCouldBeStringArray(str)
tf = contains(str, '[') && contains(str, ']') && contains(str, '"');
end
function tf = iCouldBeCellStr(str)
tf = contains(str, '{') && contains(str, '}') && contains(str, "'");
end
function invalidVarNames = iGetInvalidVarNames(names)
% For the provided names, return any which are not valid MATLAB variable names
isValidName = arrayfun(@isvarname, names);
invalidVarNames = names(~isValidName);
end
function attributes = iParseAttributes(attributeString)
% Expecting string like: attr1, attr2
% Returns a string array of the attributes
attributes = strtrim(split(attributeString, ","));
end
function tf = iHasAttributes(attributesToCheck, attributeList)
% Do the attributesToCheck appear in attributeList?
% Returns a logical array of the same size as attributesToCheck.
hasAttribute = @(attr) any(strcmpi(attr, attributeList));
tf = arrayfun(hasAttribute, attributesToCheck);
end
function tf = iHasAnyAttributes(attributesToCheck, attributeList)
% Do any of the attributesToCheck appear in attributeList?
tf = any(iHasAttributes(attributesToCheck, attributeList));
end
function tf = iHasAnyOSAttribute(attributeList)
% Do any of the OS-specific attributes appear in attributeList?
allOSNames = ["WINDOWS", "UNIX", "MAC", "LINUX"];
tf = iHasAnyAttributes(allOSNames, attributeList);
end
function tf = iHasThisOSAttribute(attributeList)
% Do any of the OS-specific attributes for this OS appear in attributeList?
if ispc
thisOSNames = "WINDOWS";
elseif ismac
thisOSNames = ["UNIX", "MAC"];
else
thisOSNames = ["UNIX", "LINUX"];
end
tf = iHasAnyAttributes(thisOSNames, attributeList);
% Function iHasThisOSAttribute end
end
function returnValue = iLoopUntilValidStringInput(message, validValues)
% Function to loop until a valid response is obtained user input
returnValue = 'null';
while ~any(strcmpi(returnValue, validValues))
returnValue = input(message, 's');
end
% Function iLoopUntilValidStringInput end
end
% clusterDefinition end
end