-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatlabJob.m
115 lines (99 loc) · 3.74 KB
/
MatlabJob.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
classdef MatlabJob < handle
properties
id % SLURM job id
partition
logFile
matlabBinary = fullfile(matlabroot, 'bin', 'matlab')
deleteFiles = true
state = 'UNKNOWN'
allocCPU = 1
allocMEM = ''
memoryUsed
readFromDisk
wroteToDisk
duration
account
isComplete = false
isFinalized = false
inputFile
outputFile
end
properties ( Constant = true, Access = private )
userAccount = getenv('USER');
gid = primary_group();
matlabCaller = fullfile(fileparts(mfilename('fullpath')), 'matlabcmd.sh');
end
methods
function obj = MatlabJob(cmd, varargin)
if nargin == 0
return
end
if ischar(cmd)
obj.run_cmd(cmd, varargin{:})
elseif isnumeric(cmd) && numel(cmd) == 1
obj.id = cmd;
obj.update_state();
end
end
function run_cmd(obj, cmd)
if ~isempty(obj.logFile)
[folder,~,~] = fileparts(obj.logFile);
else
folder = pwd;
end
% construct base sbatch command: account, folder, group
baseCmd = sprintf(...
'sbatch -A %s -D %s --parsable ', ...
obj.userAccount, folder);
% construct sbatch command: partition, log
baseCmd = sprintf('%s -n1 -N1 -c %d ', ...
baseCmd, obj.allocCPU);
% add memory if specified
if ~isempty(obj.allocMEM)
baseCmd = sprintf('%s --mem-per-cpu %s ', baseCmd, obj.allocMEM);
end
% construct sbatch command: partition, log
cmd = sprintf('%s -p %s -o %s %s -m "%s" "%s"', ...
baseCmd, obj.partition, obj.logFile, obj.matlabCaller, obj.matlabBinary, cmd);
[result, obj.id] = system_out_to_disk(cmd);
obj.id = uint32(sscanf(obj.id,'%u'));
assert(result == 0 || ~isempty(obj.id), 'Submission failed: %s\n', obj.id)
obj.isComplete = false;
obj.account = obj.userAccount;
end
function update_state(obj)
assert(~isempty([obj.id]), 'Undefined job id')
jobInfo = sacct_query(obj.id);
obj.duration = str2double(jobInfo.ElapsedRaw);
obj.partition = jobInfo.Partition;
obj.state = jobInfo.State;
obj.memoryUsed = jobInfo.MaxRSS;
obj.account = jobInfo.Account;
obj.readFromDisk = jobInfo.MaxDiskRead;
obj.wroteToDisk = jobInfo.MaxDiskWrite;
if ismember(obj.state, {'RUNNING', 'PENDING', 'RESIZING', 'REQUEUED'})
obj.isComplete = false;
else
obj.isComplete = true;
end
end
function delete(obj)
if ~strcmp(obj.userAccount, obj.account) && ~isempty(obj.id)
warning('Not cancelling job %d as it belongs to %s', obj.id, obj.account)
return
end
if ~obj.isComplete && ~isempty(obj.id)
cmd = sprintf('scancel %u', obj.id);
result = system(cmd);
assert(result == 0, 'Could not cancel job %u', obj.id)
end
if obj.deleteFiles
warning('off', 'MATLAB:DELETE:FileNotFound')
delete(obj.logFile)
delete(obj.inputFile)
delete(obj.outputFile)
warning('on', 'MATLAB:DELETE:FileNotFound')
end
end
end
end