15 from __future__
import print_function
16 from builtins
import range
17 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass
as mpslib
18 import Alignment.MillePedeAlignmentAlgorithm.mpslib.tools
as mps_tools
29 """Forward proxy to location visible from the batch system. 32 - `rundir`: directory for storing the forwarded proxy 35 if not mps_tools.check_proxy():
36 print(
"Please create proxy via 'voms-proxy-init -voms cms -rfc'.")
39 local_proxy = subprocess.check_output([
"voms-proxy-info",
"--path"]).
strip()
40 shutil.copyfile(local_proxy, os.path.join(rundir,
".user_proxy"))
44 """Writes 'job.submit' file in `path`. 47 - `path`: job directory 48 - `script`: script to be executed 50 - `lib`: MPS lib object 53 resources = lib.get_class(
"pede").
split(
"_")[1:]
54 job_flavour = resources[-1]
56 job_submit_template=
"""\ 58 executable = {script:s} 59 output = {jobm:s}/STDOUT 60 error = {jobm:s}/STDOUT 63 transfer_output_files = "" 64 request_memory = {pedeMem:d}M 66 # adapted to space used on eos for binaries: 67 request_disk = {disk:d} 69 # adapted to threads parameter in pede options and number of available cores 70 request_cpus = {cpus:d} 72 +JobFlavour = "{flavour:s}" 74 if "bigmem" in resources:
75 job_submit_template +=
"""\ 77 +AccountingGroup = "group_u_CMS.e_cms_caf_bigmem" 79 # automatically remove the job if the submitter has no permissions to run a BigMemJob 80 periodic_remove = !regexp("group_u_CMS.e_cms_caf_bigmem", AccountingGroup) && BigMemJob =?= True 82 job_submit_template +=
"\nqueue\n" 84 print(
"Determine number of pede threads...")
85 cms_process = mps_tools.get_process_object(os.path.join(Path, mergeCfg))
86 pede_options = cms_process.AlignmentProducer.algoConfig.pedeSteerer.options.value()
88 for option
in pede_options:
89 if "threads" in option:
90 n_threads = option.replace(
"threads",
"").
strip()
91 n_threads =
max(
map(
lambda x:
int(x), n_threads.split()))
93 if n_threads > 16: n_threads = 16
97 print(
"Determine required disk space on remote host...")
100 spco = subprocess.check_output
102 cmd = [
"du",
"--apparent-size"]
103 disk_usage = [
int(item.split()[0])
104 for directory
in (
"binaries",
"monitors",
"tree_files")
107 glob.glob(opj(lib.mssDir, directory,
"*"))).splitlines()]
108 disk_usage = sum(disk_usage)
111 job_submit_file = os.path.join(Path,
"job.submit")
112 with open(job_submit_file,
"w")
as f:
113 f.write(job_submit_template.format(script = os.path.abspath(script),
114 jobm = os.path.abspath(path),
115 pedeMem = lib.pedeMem,
116 disk =
int(disk_usage),
118 flavour = job_flavour))
120 return job_submit_file
123 """Writes 'job.submit' file in `path`. 126 - `path`: job directory 127 - `script`: script to be executed 128 - `lib`: MPS lib object 129 - `proxy_path`: path to proxy (only used in case of requested proxy forward) 132 resources = lib.get_class(
"mille").
split(
"_")[1:]
133 job_flavour = resources[-1]
135 job_submit_template=
"""\ 137 executable = {script:s} 138 output = {jobm:s}/STDOUT 139 error = {jobm:s}/STDOUT 140 log = {jobm:s}/HTCJOB 141 notification = Always 142 transfer_output_files = "" 144 +JobFlavour = "{flavour:s}" 146 if proxy_path
is not None:
147 job_submit_template +=
"""\ 148 +x509userproxy = "{proxy:s}" 150 job_submit_template +=
"\nqueue\n" 152 job_submit_file = os.path.join(Path,
"job.submit")
153 with open(job_submit_file,
"w")
as f:
154 f.write(job_submit_template.format(script = os.path.abspath(script),
155 jobm = os.path.abspath(path),
156 flavour = job_flavour,
159 return job_submit_file
163 parser = argparse.ArgumentParser(
164 description=
"Submit jobs that are setup in local mps database to batch system.",
166 parser.add_argument(
"maxJobs", type=int, nargs=
'?', default=1,
167 help=
"number of Mille jobs to be submitted (default: %(default)d)")
168 parser.add_argument(
"-j",
"--job-id", dest =
"job_id", nargs =
"*",
169 help = (
"job IDs to be submitted; " 170 "use either 'job<ID>' or directly '<ID>'"))
171 parser.add_argument(
"-a",
"--all", dest=
"allMille", default=
False,
173 help = (
"submit all setup Mille jobs; " 174 "maxJobs and --job-id are ignored"))
175 parser.add_argument(
"-m",
"--merge", dest=
"fireMerge", default=
False,
177 help = (
"submit all setup Pede jobs; " 178 "maxJobs is ignored, but --job-id is respected"))
179 parser.add_argument(
"-f",
"--force-merge", dest=
"forceMerge", default=
False,
181 help=(
"force the submission of the Pede job in case some "+
182 "Mille jobs are not in the OK state"))
183 parser.add_argument(
"--force-merge-manual", dest=
"forceMergeManual", default=
False,
185 help=(
"force the submission of the Pede job in case some "+
186 "Mille jobs are not in the OK state. Unlike --forceMerge "+
187 "this option assumes the user has edited theScript.sh and "+
188 "alignment_merge.py to consistently pick up only the mille "+
189 "output files that exist"))
190 parser.add_argument(
"-p",
"--forward-proxy", dest=
"forwardProxy", default=
False,
192 help=
"forward VOMS proxy to batch system")
193 args = parser.parse_args(sys.argv[1:])
196 lib = mpslib.jobdatabase()
201 args.maxJobs = lib.nJobs
204 if args.job_id
is None:
205 job_mask = lib.JOBDIR
208 for job_id
in args.job_id:
210 if job_id.startswith(
"job"): job_mask.append(job_id)
211 elif job_id.startswith(
"m"): job_mask.append(
"job"+job_id)
214 job_mask.append(lib.JOBDIR[
int(job_id)-1])
218 print(
"ID provided to '-j/--job-id' is out of range:", job_id)
221 if invalid_id
or job_mask[-1]
not in lib.JOBDIR:
222 print(
"ID provided to '-j/--job-id' is invalid:", job_id)
223 print(
"'-j/--job-id' requires the IDs to exist and to be of either", end=
' ')
224 print(
"of the following formats:")
232 theJobData = os.path.join(os.getcwd(),
"jobData")
235 theJobName =
'mpalign' 236 if lib.addFiles !=
'':
237 theJobName = lib.addFiles
239 fire_htcondor =
False 242 if not args.fireMerge:
244 resources = lib.get_class(
'mille')
247 if 'cmscafspec' in resources:
248 print(
'\nWARNING:\n Running mille jobs on cmscafspec, intended for pede only!\n\n')
249 resources =
'-q cmscafalcamille' 251 elif 'cmscaf' in resources:
253 resources =
'-q'+resources+
' -m g_cmscaf' 254 elif "htcondor" in resources:
257 resources =
'-q '+resources
260 for i
in range(lib.nJobs):
261 if lib.JOBDIR[i]
not in job_mask:
continue 262 if lib.JOBSTATUS[i] ==
'SETUP':
263 if nSub < args.maxJobs:
264 if args.forwardProxy:
270 Path = os.path.join(theJobData,lib.JOBDIR[i])
271 scriptPath = os.path.join(Path,
"theScript.sh")
272 if args.forwardProxy:
276 submission =
"condor_submit -batch-name %s %s"%\
277 (theJobName, job_submit_file)
279 submission =
'bsub -J %s %s %s/%s/theScript.sh' % \
280 (theJobName, resources, theJobData, lib.JOBDIR[i])
283 result = subprocess.check_output(submission,
284 stderr=subprocess.STDOUT,
286 except subprocess.CalledProcessError
as e:
288 print(
' '+result, end=
' ')
289 result = result.strip()
293 match = re.search(
r"1 job\(s\) submitted to cluster (\d+)\.", result)
295 match = re.search(
'Job <(\d+)> is submitted', result)
298 lib.JOBSTATUS[i] =
'SUBTD' 299 lib.JOBID[i] = match.group(1)
300 if fire_htcondor: lib.JOBID[i] +=
".0" 302 print(
'Submission of %03d seems to have failed: %s' % (lib.JOBNUMBER[i],result), end=
' ')
309 resources = lib.get_class(
'pede')
310 if 'cmscafspec' in resources:
311 resources =
'-q cmscafalcamille' 312 elif "htcondor" in resources:
315 resources =
'-q '+resources
317 if not fire_htcondor:
319 resources = resources+
' -R \"rusage[mem="%s"]\"' %
str(lib.pedeMem)
323 for i
in range(lib.nJobs):
324 if lib.JOBSTATUS[i] !=
'OK':
325 if 'DISABLED' not in lib.JOBSTATUS[i]:
331 while i<len(lib.JOBDIR):
333 if lib.JOBDIR[i]
not in job_mask:
338 if lib.JOBSTATUS[i] !=
'SETUP':
339 print(
'Merge job %d status %s not submitted.' % \
340 (jobNumFrom1, lib.JOBSTATUS[i]))
341 elif not (mergeOK
or args.forceMerge
or args.forceMergeManual):
342 print(
'Merge job',jobNumFrom1,
'not submitted since Mille jobs error/unfinished (Use -m -f to force).')
345 Path = os.path.join(theJobData,lib.JOBDIR[i])
346 backupScriptPath = os.path.join(Path,
"theScript.sh.bak")
347 scriptPath = os.path.join(Path,
"theScript.sh")
353 if not os.path.isfile(backupScriptPath):
354 os.system(
'cp -p '+scriptPath+
' '+backupScriptPath)
357 command =
'cat '+backupScriptPath+
' | grep CONFIG_FILE | head -1 | awk -F"/" \'{print $NF}\'' 358 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
359 mergeCfg = mergeCfg.strip()
365 backupCfgPath = os.path.join(Path, mergeCfg+
".bak")
366 cfgPath = os.path.join(Path, mergeCfg)
367 if not os.path.isfile(backupCfgPath):
368 os.system(
'cp -p '+cfgPath+
' '+backupCfgPath)
371 with open(os.path.join(Path,
".weights.pkl"),
"rb")
as f:
372 weight_conf = cPickle.load(f)
375 mps_tools.run_checked([
"mps_weight.pl",
"-c"])
378 for name,weight
in weight_conf:
379 print(
" ".
join([
"mps_weight.pl",
"-N", name, weight]))
380 mps_tools.run_checked([
"mps_weight.pl",
"-N", name, weight])
383 inCfgPath = theJobData+
'/'+lib.JOBDIR[0]+
'/the.py' 384 command =
'mps_merge.py -w -c '+inCfgPath+
' '+Path+
'/'+mergeCfg+
' '+Path+
' '+
str(lib.nJobs)
388 command =
'mps_scriptm.pl -c '+lib.mergeScript+
' '+scriptPath+
' '+Path+
' '+mergeCfg+
' '+
str(lib.nJobs)+
' '+lib.mssDir+
' '+lib.mssDirPool
393 if os.path.isfile(backupScriptPath):
394 os.system(
'cp -pf '+backupScriptPath+
' '+scriptPath)
397 command =
"cat "+scriptPath+
" | grep '^\s*CONFIG_FILE' | awk -F'=' '{print $2}'" 398 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
399 command =
'basename '+mergeCfg
400 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
401 mergeCfg = mergeCfg.replace(
'\n',
'')
407 backupCfgPath = Path+
'/%s.bak' % mergeCfg
408 cfgPath = Path+
'/%s' % mergeCfg
409 if os.path.isfile(backupCfgPath):
410 os.system(
'cp -pf '+backupCfgPath+
' '+cfgPath)
416 curJobName =
'm'+
str(nMerge)+
'_'+theJobName
419 submission = [
"condor_submit",
420 "-batch-name", curJobName,
423 submission = [
"bsub",
"-J", curJobName, resources, scriptPath]
426 result = subprocess.check_output(submission, stderr=subprocess.STDOUT)
428 except subprocess.CalledProcessError
as e:
431 print(
' '+result, end=
' ')
432 result = result.strip()
436 match = re.search(
r"1 job\(s\) submitted to cluster (\d+)\.", result)
438 match = re.search(
'Job <(\d+)> is submitted', result)
440 lib.JOBSTATUS[i] =
'SUBTD' 441 lib.JOBID[i] = match.group(1)
443 if fire_htcondor: lib.JOBID[i] +=
".0" 444 print(
"jobid is", lib.JOBID[i])
446 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)