generated from automaticanalysis/aa-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaaq_qsub_monitor_jobs.m
174 lines (148 loc) · 4.89 KB
/
aaq_qsub_monitor_jobs.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
function Nfinished = aaq_qsub_monitor_jobs(job_location, intervaltime, clearscreen, nloops)
if nargin == 0
job_location = '/home/dp01/matlab/jobs/';
intervaltime = 5;
nloops = Inf;
clearscreen = true;
end
if nargin < 2
intervaltime = 5;
nloops = Inf;
clearscreen = true;
end
fclose('all');
status_complete = false;
mainloopind = 0;
while mainloopind < nloops
C = {'' '' ''};
mainloopind = mainloopind + 1;
fclose('all');
fid = 0;
try
T = splitstring(ls([job_location '*/*diary.txt']));
catch %#ok<CTCH>
disp('No Jobs Found!')
pause(1) % allow time for ctrl+c to work
continue
end
jobids = zeros(size(T));
for ii = 1:length(T)
[st, en] = regexp(T{ii}, 'Job[0-9]+');
if ~isempty(st)
jobids(ii) = str2double(T{ii}(st+3:en));
end
end
[jobids, uind] = unique(jobids);
T = T(uind);
T(jobids == 0) = {};
jobids(jobids == 0) = [];
if length(fid) < max(jobids)
fid(max(jobids)) = 0; %#ok<AGROW>
end
for ind = 1:length(jobids)
ii = jobids(ind);
jobpath = [fileparts(T{ind}) '/'];
if exist([jobpath 'Task1.out.mat'], 'file')
try
load([jobpath 'Task1.out.mat'], 'finishtime','errormessage')
catch %#ok<CTCH>
% Best to restart the entire loop in case job folder was deleted
disp('Trying again...')
pause(1)
continue
end
job_status = getlines([jobpath 'Task1.state.mat']);
job_status = strtrim(job_status{end});
C{ii,2} = job_status;
if strcmp(job_status,'finished')
status_complete(ii) = true;
else
status_complete(ii) = false;
end
end
C{ii,1} = ['Job' num2str(ii)];
if strcmp(C{ii,2},'running')
if rem(ind, 5)
tout = getlines([jobpath 'Task1.diary.txt']);
C{ii,3} = strcat(tout{end});
end
elseif ~isempty(errormessage)
C{ii,3} = errormessage;
C{ii,2} = 'error';
status_complete(ii) = 0;
end
end
if clearscreen
clc
end
%
% status_complete(cellfun(@isempty, C(:,1), 'UniformOutput',true)) = 1;
% if any(status_complete == 0)
% disp('')
% Cprint = fliplr(C(status_complete == 0,:)');
% fprintf('%s \t %s \t %s \n',Cprint{:})
% disp('')
% else
% disp('No jobs running')
% end
Nrunning = sum(strcmp(C(:,2),'running'));
Nfinished = sum(strcmp(C(:,2),'finished'));
Nerror = sum(strcmp(C(:,2),'error'));
Npending = sum(strcmp(C(:,2),'pending'));
disp('-----------------------------------------------')
fprintf('Running: %d | Pending: %d | Finished: %d | Error: %d \n', Nrunning, Npending, Nfinished, Nerror)
pause(intervaltime)
end
function tout = getlines(fname)
fid = fopen(fname, 'r');
t = ''; tout = {''};
while ischar(t)
t = fgets(fid);
if ischar(t)
t = deblank(t);
tout{end+1} = t; %#ok<AGROW>
end
end
fclose(fid);
end
function rv = splitstring( str, varargin )
%SPLITSTRING Split string into cell array
% ARRAY = SPLITSTRING( STR, DELIM, ALLOWEMPTYENTRIES ) splits the
% character string STR, using the delimiter DELIM (which must be a
% character array). ARRAY is a cell array containing the resulting
% strings. If DELIM is not specified, space delimiter is assumed (see
% ISSPACE documentation). ALLOWEMPTYENTRIES should be a logical single
% element, specifying weather empty elements should be included in the
% results. If not specified, the value of ALLOWEMPTYENTRIES is false.
%
% Example:
% arr = splitstring( 'a,b,c,d', ',' )
delim = '';
AllowEmptyEntries = false;
if numel(varargin) == 2
delim = varargin{1};
AllowEmptyEntries = varargin{2};
elseif numel(varargin) == 1
if islogical(varargin{1})
AllowEmptyEntries = varargin{1};
else
delim = varargin{1};
end
end
if isempty(delim)
delim = ' ';
ind2 = find( isspace( str ) );
else
ind2 = strfind( str, delim );
end
startpos = [1, ind2+length(delim)];
endpos = [ind2-1, length(str)];
rv = cell( 1, length(startpos) );
for i=1:length(startpos)
rv{i} = str(startpos(i):endpos(i));
end
if ~AllowEmptyEntries
rv = rv( ~strcmp(rv,'') );
end
end
end