File size: 4,859 Bytes
d4035c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
function [jobId, taskId] = parallelDriver(varargin)
% PARALLELDRIVER
%
%   Author:: Andrea Vedaldi

% AUTORIGHTS
% Copyright (C) 2008-09 Andrea Vedaldi
%
% This file is part of the VGG MKL Class and VGG MKL Det code packages,
% available in the terms of the GNU General Public License version 2.

jobopts.parallelize = 0 ;
jobopts.jobId       = NaN ;
jobopts.taskId      = NaN ;
jobopts.jobDir      = '' ;
jobopts.needsJava   = false ;

jobopts.waitJobs    = [] ;
jobopts.numNodes    = 10 ;
jobopts.freeMemMb   = 1900 ;

jobopts = vl_argparse(jobopts, varargin{:}) ;

% waiting list
job_qsub_opts = [] ;

if ~isempty(jobopts.waitJobs)
  jobList = sprintf('%d,', jobopts.waitJobs) ;
  job_qsub_opts = [job_qsub_opts ...
                   ' -hold_jid ' jobList(1:end-1)] ;
end

if jobopts.freeMemMb > 0
  job_qsub_opts = [job_qsub_opts ...
                   sprintf(' -l mem_free=%gm', jobopts.freeMemMb) ...
                   sprintf(' -l vf=%gm', jobopts.freeMemMb)] ;
end

% get who is calling me
drop = dbstack ;
callingMFile = drop(2).name ;
clear drop ;

taskId = jobopts.taskId ;
jobId  = jobopts.jobId ;

% --------------------------------------------------------------------
%                                                      If not parallel
% --------------------------------------------------------------------

if ~ jobopts.parallelize
  fprintf('>>>>> %s <<<<<\n', callingMFile) ;
  return ;
end

% --------------------------------------------------------------------
%                                 If parallel, and taskId is specified
% --------------------------------------------------------------------

if ~isnan(jobopts.taskId)
  return ;
end

% --------------------------------------------------------------------
%                             If parallel, and taskId is not specified
% --------------------------------------------------------------------

tic ;

outputDir  = fullfile(jobopts.jobDir, sprintf('j$JOB_ID-%s', callingMFile)) ;
stdErrPath = fullfile(outputDir, 'err$TASK_ID.txt') ;
stdOutPath = fullfile(outputDir, 'out$TASK_ID.txt') ;

%stdErrPath = fullfile(jobopts.jobDir, 'err$JOB_ID-$TASK_ID.txt') ;
%stdOutPath = fullfile(jobopts.jobDir, 'out$JOB_ID-$TASK_ID.txt') ;

matCmd = sprintf(['jobName=sprintf(''j%%d-%s'',sgeJobId);' ...
                  'jobPath=fullfile(''%s'', jobName);' ...
                  'load(fullfile(jobPath,''conf''));' ...
                  'disp(conf);' ...
                  '%s(conf,''parallelize'',1,''jobId'',sgeJobId,' ...
                  '  ''taskId'',sgeTaskId,''jobDir'',''%s'');'], ...
                 callingMFile, ...
                 jobopts.jobDir, ...
                 callingMFile, ...
                 jobopts.jobDir) ;

if jobopts.needsJava
  scriptName = 'qsub_matlab_java.sh' ;
else
  scriptName = 'qsub_matlab.sh' ;
end

cmd = sprintf(['qsub '                                  ...
               '-h '                                    ...
               '-t 1-%d '                               ...
               '-o ''%s'' '                             ...
               '-e ''%s'' '                             ...
               '-N ''%s'' '                             ...
               ' %s '                                   ...
               '%s "%s"'],                              ...
              jobopts.numNodes,                         ...
              stdOutPath, stdErrPath,                   ...
              callingMFile,                             ...
              job_qsub_opts,                            ...
              scriptName,                               ...
              matCmd) ;

[s,w] = system(cmd) ;
if s ~= 0
  fprintf('Offending command:\n%s\n\n', cmd) ;
  error(w) ;
end

tmp = regexp(w, ' ([0-9]*)\.', 'tokens') ;
jobId = sscanf(tmp{1}{1}, '%d') ;
taskId = NaN ;

% now can setup actual output directory
jobName = sprintf('j%d-%s', jobId, callingMFile) ;
outputDir = fullfile(jobopts.jobDir, jobName) ;
ensuredir(outputDir) ;
%ensuredir(fullfile(outputDir, 'locks')) ;
confPath   = fullfile(outputDir, 'conf.mat') ;
evalin('caller', sprintf('ssave(''%s'', ''conf'')', confPath)) ;

fid = fopen(fullfile(outputDir, 'SGE_SUB_TIME'),'w') ;
fwrite(fid, datestr(now)) ;
fclose(fid) ;

fid = fopen(fullfile(outputDir, 'SGE_CMD'),'w') ;
fwrite(fid, cmd) ;
fclose(fid) ;

fid = fopen(fullfile(outputDir, 'SGE_ID'),'w') ;
fwrite(fid, tmp{1}{1}) ;
fclose(fid) ;

fprintf('Spawning job array ''%s'' with %d instances.\n',  ...
        outputDir, jobopts.numNodes) ;
fprintf('\tSGE job id: %d\n', jobId) ;
fprintf('\tqsub additional options: ''%s''.\n', job_qsub_opts) ;

% remove hold
cmd = sprintf('qalter -h U %d', jobId) ;
[s,w] = system(cmd) ;
if s ~= 0
  fprintf('Offending command:\n%s\n\n', cmd) ;
  error(w) ;
end

% this will ensure that each job directory has a different time stamp
%pause(max(1.5 - toc, 0)) ;