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
124 parser = argparse.ArgumentParser(
125 description=
"Submit jobs that are setup in local mps database to batch system.",
127 parser.add_argument(
"maxJobs", type=int, nargs=
'?', default=1,
128 help=
"number of Mille jobs to be submitted (default: %(default)d)")
129 parser.add_argument(
"-j",
"--job-id", dest =
"job_id", nargs =
"*",
130 help = (
"job IDs to be submitted; " 131 "use either 'job<ID>' or directly '<ID>'"))
132 parser.add_argument(
"-a",
"--all", dest=
"allMille", default=
False,
134 help = (
"submit all setup Mille jobs; " 135 "maxJobs and --job-id are ignored"))
136 parser.add_argument(
"-m",
"--merge", dest=
"fireMerge", default=
False,
138 help = (
"submit all setup Pede jobs; " 139 "maxJobs is ignored, but --job-id is respected"))
140 parser.add_argument(
"-f",
"--force-merge", dest=
"forceMerge", default=
False,
142 help=(
"force the submission of the Pede job in case some "+
143 "Mille jobs are not in the OK state"))
144 parser.add_argument(
"--force-merge-manual", dest=
"forceMergeManual", default=
False,
146 help=(
"force the submission of the Pede job in case some "+
147 "Mille jobs are not in the OK state. Unlike --forceMerge "+
148 "this option assumes the user has edited theScript.sh and "+
149 "alignment_merge.py to consistently pick up only the mille "+
150 "output files that exist"))
151 parser.add_argument(
"-p",
"--forward-proxy", dest=
"forwardProxy", default=
False,
153 help=
"forward VOMS proxy to batch system")
154 args = parser.parse_args(sys.argv[1:])
157 lib = mpslib.jobdatabase()
162 args.maxJobs = lib.nJobs
165 if args.job_id
is None:
166 job_mask = lib.JOBDIR
169 for job_id
in args.job_id:
171 if job_id.startswith(
"job"): job_mask.append(job_id)
172 elif job_id.startswith(
"m"): job_mask.append(
"job"+job_id)
175 job_mask.append(lib.JOBDIR[
int(job_id)-1])
179 print(
"ID provided to '-j/--job-id' is out of range:", job_id)
182 if invalid_id
or job_mask[-1]
not in lib.JOBDIR:
183 print(
"ID provided to '-j/--job-id' is invalid:", job_id)
184 print(
"'-j/--job-id' requires the IDs to exist and to be of either", end=
' ')
185 print(
"of the following formats:")
193 theJobData = os.path.join(os.getcwd(),
"jobData")
196 theJobName =
'mpalign' 197 if lib.addFiles !=
'':
198 theJobName = lib.addFiles
200 fire_htcondor =
False 203 if not args.fireMerge:
205 resources = lib.get_class(
'mille')
208 if 'cmscafspec' in resources:
209 print(
'\nWARNING:\n Running mille jobs on cmscafspec, intended for pede only!\n\n')
210 resources =
'-q cmscafalcamille' 212 elif 'cmscaf' in resources:
214 resources =
'-q'+resources+
' -m g_cmscaf' 215 elif "htcondor" in resources:
218 resources =
'-q '+resources
221 for i
in xrange(lib.nJobs):
222 if lib.JOBDIR[i]
not in job_mask:
continue 223 if lib.JOBSTATUS[i] ==
'SETUP':
224 if nSub < args.maxJobs:
225 if args.forwardProxy:
230 submission =
'bsub -J %s %s %s/%s/theScript.sh' % \
231 (theJobName, resources, theJobData, lib.JOBDIR[i])
234 result = subprocess.check_output(submission,
235 stderr=subprocess.STDOUT,
237 except subprocess.CalledProcessError
as e:
239 print(
' '+result, end=
' ')
240 result = result.strip()
243 match = re.search(
'Job <(\d+)> is submitted', result)
246 lib.JOBSTATUS[i] =
'SUBTD' 247 lib.JOBID[i] = match.group(1)
249 print(
'Submission of %03d seems to have failed: %s' % (lib.JOBNUMBER[i],result), end=
' ')
256 resources = lib.get_class(
'pede')
257 if 'cmscafspec' in resources:
258 resources =
'-q cmscafalcamille' 259 elif "htcondor" in resources:
262 resources =
'-q '+resources
264 if not fire_htcondor:
266 resources = resources+
' -R \"rusage[mem="%s"]\"' %
str(lib.pedeMem)
270 for i
in xrange(lib.nJobs):
271 if lib.JOBSTATUS[i] !=
'OK':
272 if 'DISABLED' not in lib.JOBSTATUS[i]:
278 while i<len(lib.JOBDIR):
280 if lib.JOBDIR[i]
not in job_mask:
285 if lib.JOBSTATUS[i] !=
'SETUP':
286 print(
'Merge job %d status %s not submitted.' % \
287 (jobNumFrom1, lib.JOBSTATUS[i]))
288 elif not (mergeOK
or args.forceMerge
or args.forceMergeManual):
289 print(
'Merge job',jobNumFrom1,
'not submitted since Mille jobs error/unfinished (Use -m -f to force).')
292 Path = os.path.join(theJobData,lib.JOBDIR[i])
293 backupScriptPath = os.path.join(Path,
"theScript.sh.bak")
294 scriptPath = os.path.join(Path,
"theScript.sh")
300 if not os.path.isfile(backupScriptPath):
301 os.system(
'cp -p '+scriptPath+
' '+backupScriptPath)
304 command =
'cat '+backupScriptPath+
' | grep CONFIG_FILE | head -1 | awk -F"/" \'{print $NF}\'' 305 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
306 mergeCfg = mergeCfg.strip()
312 backupCfgPath = os.path.join(Path, mergeCfg+
".bak")
313 cfgPath = os.path.join(Path, mergeCfg)
314 if not os.path.isfile(backupCfgPath):
315 os.system(
'cp -p '+cfgPath+
' '+backupCfgPath)
318 with open(os.path.join(Path,
".weights.pkl"),
"rb")
as f:
319 weight_conf = cPickle.load(f)
322 mps_tools.run_checked([
"mps_weight.pl",
"-c"])
325 for name,weight
in weight_conf:
326 print(
" ".
join([
"mps_weight.pl",
"-N", name, weight]))
327 mps_tools.run_checked([
"mps_weight.pl",
"-N", name, weight])
330 inCfgPath = theJobData+
'/'+lib.JOBDIR[0]+
'/the.py' 331 command =
'mps_merge.py -w -c '+inCfgPath+
' '+Path+
'/'+mergeCfg+
' '+Path+
' '+
str(lib.nJobs)
335 command =
'mps_scriptm.pl -c '+lib.mergeScript+
' '+scriptPath+
' '+Path+
' '+mergeCfg+
' '+
str(lib.nJobs)+
' '+lib.mssDir+
' '+lib.mssDirPool
340 if os.path.isfile(backupScriptPath):
341 os.system(
'cp -pf '+backupScriptPath+
' '+scriptPath)
344 command =
"cat "+scriptPath+
" | grep '^\s*CONFIG_FILE' | awk -F'=' '{print $2}'" 345 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
346 command =
'basename '+mergeCfg
347 mergeCfg = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=
True)
348 mergeCfg = mergeCfg.replace(
'\n',
'')
354 backupCfgPath = Path+
'/%s.bak' % mergeCfg
355 cfgPath = Path+
'/%s' % mergeCfg
356 if os.path.isfile(backupCfgPath):
357 os.system(
'cp -pf '+backupCfgPath+
' '+cfgPath)
363 curJobName =
'm'+
str(nMerge)+
'_'+theJobName
366 submission = [
"condor_submit",
367 "-batch-name", curJobName,
370 submission = [
"bsub",
"-J", curJobName, resources, scriptPath]
373 result = subprocess.check_output(submission, stderr=subprocess.STDOUT)
375 except subprocess.CalledProcessError
as e:
378 print(
' '+result, end=
' ')
379 result = result.strip()
383 match = re.search(
r"1 job\(s\) submitted to cluster (\d+)\.", result)
385 match = re.search(
'Job <(\d+)> is submitted', result)
387 lib.JOBSTATUS[i] =
'SUBTD' 388 lib.JOBID[i] = match.group(1)
390 if fire_htcondor: lib.JOBID[i] +=
".0" 391 print(
"jobid is", lib.JOBID[i])
393 print(
'Submission of merge job seems to have failed:',result, end=
' ')
def forward_proxy(rundir)
S & print(S &os, JobReport::InputFile const &f)
def write_HTCondor_submit_file(path, script, config, lib)
static std::string join(char **cmd)