15 from __future__
import print_function
16 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass
as mpslib
17 import Alignment.MillePedeAlignmentAlgorithm.mpslib.tools
as mps_tools
28 """Forward proxy to location visible from the batch system. 31 - `rundir`: directory for storing the forwarded proxy 34 if not mps_tools.check_proxy():
35 print(
"Please create proxy via 'voms-proxy-init -voms cms -rfc'.")
38 local_proxy = subprocess.check_output([
"voms-proxy-info",
"--path"]).
strip()
39 shutil.copyfile(local_proxy, os.path.join(rundir,
".user_proxy"))
43 """Writes 'job.submit' file in `path`. 46 - `path`: job directory 47 - `script`: script to be executed 49 - `lib`: MPS lib object 52 resources = lib.get_class(
"pede").
split(
"_")[1:]
53 job_flavour = resources[-1]
55 job_submit_template=
"""\ 57 executable = {script:s} 58 output = {jobm:s}/STDOUT 59 error = {jobm:s}/STDOUT 62 transfer_output_files = "" 63 request_memory = {pedeMem:d}M 65 # adapted to space used on eos for binaries: 66 request_disk = {disk:d} 68 # adapted to threads parameter in pede options and number of available cores 69 request_cpus = {cpus:d} 71 +JobFlavour = "{flavour:s}" 73 if "bigmem" in resources:
74 job_submit_template +=
"""\ 76 +AccountingGroup = "group_u_CMS.e_cms_caf_bigmem" 78 # automatically remove the job if the submitter has no permissions to run a BigMemJob 79 periodic_remove = !regexp("group_u_CMS.e_cms_caf_bigmem", AccountingGroup) && BigMemJob =?= True 81 job_submit_template +=
"\nqueue\n" 83 print(
"Determine number of pede threads...")
84 cms_process = mps_tools.get_process_object(os.path.join(Path, mergeCfg))
85 pede_options = cms_process.AlignmentProducer.algoConfig.pedeSteerer.options.value()
87 for option
in pede_options:
88 if "threads" in option:
89 n_threads = option.replace(
"threads",
"").
strip()
90 n_threads =
max(
map(
lambda x:
int(x), n_threads.split()))
92 if n_threads > 16: n_threads = 16
96 print(
"Determine required disk space on remote host...")
99 spco = subprocess.check_output
101 cmd = [
"du",
"--apparent-size"]
102 disk_usage = [
int(item.split()[0])
103 for directory
in (
"binaries",
"monitors",
"tree_files")
106 glob.glob(opj(lib.mssDir, directory,
"*"))).splitlines()]
107 disk_usage = sum(disk_usage)
110 job_submit_file = os.path.join(Path,
"job.submit")
111 with open(job_submit_file,
"w")
as f:
112 f.write(job_submit_template.format(script = os.path.abspath(script),
113 jobm = os.path.abspath(path),
114 pedeMem = lib.pedeMem,
115 disk =
int(disk_usage),
117 flavour = job_flavour))
119 return job_submit_file
122 """Writes 'job.submit' file in `path`. 125 - `path`: job directory 126 - `script`: script to be executed 127 - `lib`: MPS lib object 128 - `proxy_path`: path to proxy (only used in case of requested proxy forward) 131 resources = lib.get_class(
"mille").
split(
"_")[1:]
132 job_flavour = resources[-1]
134 job_submit_template=
"""\ 136 executable = {script:s} 137 output = {jobm:s}/STDOUT 138 error = {jobm:s}/STDOUT 139 log = {jobm:s}/HTCJOB 140 notification = Always 141 transfer_output_files = "" 143 +JobFlavour = "{flavour:s}" 145 if proxy_path
is not None:
146 job_submit_template +=
"""\ 147 +x509userproxy = "{proxy:s}" 149 job_submit_template +=
"\nqueue\n" 151 job_submit_file = os.path.join(Path,
"job.submit")
152 with open(job_submit_file,
"w")
as f:
153 f.write(job_submit_template.format(script = os.path.abspath(script),
154 jobm = os.path.abspath(path),
155 flavour = job_flavour,
158 return job_submit_file
162 parser = argparse.ArgumentParser(
163 description=
"Submit jobs that are setup in local mps database to batch system.",
165 parser.add_argument(
"maxJobs", type=int, nargs=
'?', default=1,
166 help=
"number of Mille jobs to be submitted (default: %(default)d)")
167 parser.add_argument(
"-j",
"--job-id", dest =
"job_id", nargs =
"*",
168 help = (
"job IDs to be submitted; " 169 "use either 'job<ID>' or directly '<ID>'"))
170 parser.add_argument(
"-a",
"--all", dest=
"allMille", default=
False,
172 help = (
"submit all setup Mille jobs; " 173 "maxJobs and --job-id are ignored"))
174 parser.add_argument(
"-m",
"--merge", dest=
"fireMerge", default=
False,
176 help = (
"submit all setup Pede jobs; " 177 "maxJobs is ignored, but --job-id is respected"))
178 parser.add_argument(
"-f",
"--force-merge", dest=
"forceMerge", default=
False,
180 help=(
"force the submission of the Pede job in case some "+
181 "Mille jobs are not in the OK state"))
182 parser.add_argument(
"--force-merge-manual", dest=
"forceMergeManual", default=
False,
184 help=(
"force the submission of the Pede job in case some "+
185 "Mille jobs are not in the OK state. Unlike --forceMerge "+
186 "this option assumes the user has edited theScript.sh and "+
187 "alignment_merge.py to consistently pick up only the mille "+
188 "output files that exist"))
189 parser.add_argument(
"-p",
"--forward-proxy", dest=
"forwardProxy", default=
False,
191 help=
"forward VOMS proxy to batch system")
192 args = parser.parse_args(sys.argv[1:])
195 lib = mpslib.jobdatabase()
200 args.maxJobs = lib.nJobs
203 if args.job_id
is None:
204 job_mask = lib.JOBDIR
207 for job_id
in args.job_id:
209 if job_id.startswith(
"job"): job_mask.append(job_id)
210 elif job_id.startswith(
"m"): job_mask.append(
"job"+job_id)
213 job_mask.append(lib.JOBDIR[
int(job_id)-1])
217 print(
"ID provided to '-j/--job-id' is out of range:", job_id)
220 if invalid_id
or job_mask[-1]
not in lib.JOBDIR:
221 print(
"ID provided to '-j/--job-id' is invalid:", job_id)
222 print(
"'-j/--job-id' requires the IDs to exist and to be of either", end=
' ')
223 print(
"of the following formats:")
231 theJobData = os.path.join(os.getcwd(),
"jobData")
234 theJobName =
'mpalign' 235 if lib.addFiles !=
'':
236 theJobName = lib.addFiles
238 fire_htcondor =
False 241 if not args.fireMerge:
243 resources = lib.get_class(
'mille')
246 if 'cmscafspec' in resources:
247 print(
'\nWARNING:\n Running mille jobs on cmscafspec, intended for pede only!\n\n')
248 resources =
'-q cmscafalcamille' 250 elif 'cmscaf' in resources:
252 resources =
'-q'+resources+
' -m g_cmscaf' 253 elif "htcondor" in resources:
256 resources =
'-q '+resources
259 for i
in xrange(lib.nJobs):
260 if lib.JOBDIR[i]
not in job_mask:
continue 261 if lib.JOBSTATUS[i] ==
'SETUP':
262 if nSub < args.maxJobs:
263 if args.forwardProxy:
269 Path = os.path.join(theJobData,lib.JOBDIR[i])
270 scriptPath = os.path.join(Path,
"theScript.sh")
271 if args.forwardProxy:
275 submission =
"condor_submit -batch-name %s %s"%\
276 (theJobName, job_submit_file)
278 submission =
'bsub -J %s %s %s/%s/theScript.sh' % \
279 (theJobName, resources, theJobData, lib.JOBDIR[i])
282 result = subprocess.check_output(submission,
283 stderr=subprocess.STDOUT,
285 except subprocess.CalledProcessError
as e:
287 print(
' '+result, end=
' ')
288 result = result.strip()
292 match = re.search(
r"1 job\(s\) submitted to cluster (\d+)\.", result)
294 match = re.search(
'Job <(\d+)> is submitted', result)
297 lib.JOBSTATUS[i] =
'SUBTD' 298 lib.JOBID[i] = match.group(1)
299 if fire_htcondor: lib.JOBID[i] +=
".0" 301 print(
'Submission of %03d seems to have failed: %s' % (lib.JOBNUMBER[i],result), end=
' ')
308 resources = lib.get_class(
'pede')
309 if 'cmscafspec' in resources:
310 resources =
'-q cmscafalcamille' 311 elif "htcondor" in resources:
314 resources =
'-q '+resources
316 if not fire_htcondor:
318 resources = resources+
' -R \"rusage[mem="%s"]\"' %
str(lib.pedeMem)
322 for i
in xrange(lib.nJobs):
323 if lib.JOBSTATUS[i] !=
'OK':
324 if 'DISABLED' not in lib.JOBSTATUS[i]:
330 while i<len(lib.JOBDIR):
332 if lib.JOBDIR[i]
not in job_mask:
337 if lib.JOBSTATUS[i] !=
'SETUP':
338 print(
'Merge job %d status %s not submitted.' % \
339 (jobNumFrom1, lib.JOBSTATUS[i]))
340 elif not (mergeOK
or args.forceMerge
or args.forceMergeManual):
341 print(
'Merge job',jobNumFrom1,
'not submitted since Mille jobs error/unfinished (Use -m -f to force).')
344 Path = os.path.join(theJobData,lib.JOBDIR[i])
345 backupScriptPath = os.path.join(Path,
"theScript.sh.bak")
346 scriptPath = os.path.join(Path,
"theScript.sh")
352 if not os.path.isfile(backupScriptPath):
353 os.system(
'cp -p '+scriptPath+
' '+backupScriptPath)
356 command =
'cat '+backupScriptPath+
' | grep CONFIG_FILE | head -1 | awk -F"/" \'{print $NF}\'' 357 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
358 mergeCfg = mergeCfg.strip()
364 backupCfgPath = os.path.join(Path, mergeCfg+
".bak")
365 cfgPath = os.path.join(Path, mergeCfg)
366 if not os.path.isfile(backupCfgPath):
367 os.system(
'cp -p '+cfgPath+
' '+backupCfgPath)
370 with open(os.path.join(Path,
".weights.pkl"),
"rb")
as f:
371 weight_conf = cPickle.load(f)
374 mps_tools.run_checked([
"mps_weight.pl",
"-c"])
377 for name,weight
in weight_conf:
378 print(
" ".
join([
"mps_weight.pl",
"-N", name, weight]))
379 mps_tools.run_checked([
"mps_weight.pl",
"-N", name, weight])
382 inCfgPath = theJobData+
'/'+lib.JOBDIR[0]+
'/the.py' 383 command =
'mps_merge.py -w -c '+inCfgPath+
' '+Path+
'/'+mergeCfg+
' '+Path+
' '+
str(lib.nJobs)
387 command =
'mps_scriptm.pl -c '+lib.mergeScript+
' '+scriptPath+
' '+Path+
' '+mergeCfg+
' '+
str(lib.nJobs)+
' '+lib.mssDir+
' '+lib.mssDirPool
392 if os.path.isfile(backupScriptPath):
393 os.system(
'cp -pf '+backupScriptPath+
' '+scriptPath)
396 command =
"cat "+scriptPath+
" | grep '^\s*CONFIG_FILE' | awk -F'=' '{print $2}'" 397 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
398 command =
'basename '+mergeCfg
399 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
400 mergeCfg = mergeCfg.replace(
'\n',
'')
406 backupCfgPath = Path+
'/%s.bak' % mergeCfg
407 cfgPath = Path+
'/%s' % mergeCfg
408 if os.path.isfile(backupCfgPath):
409 os.system(
'cp -pf '+backupCfgPath+
' '+cfgPath)
415 curJobName =
'm'+
str(nMerge)+
'_'+theJobName
418 submission = [
"condor_submit",
419 "-batch-name", curJobName,
422 submission = [
"bsub",
"-J", curJobName, resources, scriptPath]
425 result = subprocess.check_output(submission, stderr=subprocess.STDOUT)
427 except subprocess.CalledProcessError
as e:
430 print(
' '+result, end=
' ')
431 result = result.strip()
435 match = re.search(
r"1 job\(s\) submitted to cluster (\d+)\.", result)
437 match = re.search(
'Job <(\d+)> is submitted', result)
439 lib.JOBSTATUS[i] =
'SUBTD' 440 lib.JOBID[i] = match.group(1)
442 if fire_htcondor: lib.JOBID[i] +=
".0" 443 print(
"jobid is", lib.JOBID[i])
445 print(
'Submission of merge job seems to have failed:',result, end=
' ')
def forward_proxy(rundir)
S & print(S &os, JobReport::InputFile const &f)
static std::string join(char **cmd)
def write_HTCondor_submit_file_mille(path, script, lib, proxy_path=None)
def write_HTCondor_submit_file_pede(path, script, config, lib)