Debian Codegraph @

cylc/flow/job_runner_handlers/moab.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Submits job scripts to the Moab workload manager with ``msub``.
17
18 .. cylc-scope:: flow.cylc[runtime][<namespace>]
19
20 Moab directives can be provided in the flow.cylc file; the syntax is
21 very similar to PBS:
22
23 .. code-block:: cylc
24 :caption: global.cylc
25
26 [platforms]
27 [[myplatform]]
28 job runner = moab
29
30 .. code-block:: cylc
31 :caption: flow.cylc
32
33 [runtime]
34 [[my_task]]
35 platform = myplatform
36 execution time limit = PT1M
37 [[[directives]]]
38 -V =
39 -q = foo
40 -l nodes = 1
41
42 These are written to the top of the job script like this:
43
44 .. code-block:: bash
45
46 #!/bin/bash
47 # DIRECTIVES
48 #PBS -V
49 #PBS -q foo
50 #PBS -l nodes=1
51 #PBS -l walltime=60
52
53 (Moab understands ``#PBS`` directives).
54
55 If :cylc:conf:`execution time limit` is specified, it is used to generate the
56 ``-l walltime`` directive. Do not specify the ``-l walltime`` directive
57 explicitly if :cylc:conf:`execution time limit` is specified. Otherwise, the
58 execution time limit known by the workflow may be out of sync with what is
59 submitted to the job runner.
60
61 .. cylc-scope::
62
63 """
64
65 import re
66
67 from cylc.flow.id import Tokens
68
69
70 class MoabHandler:
71
72 """Moab job runner job submission and manipulation."""
73
74 DIRECTIVE_PREFIX = "#PBS "
75 KILL_CMD_TMPL = "mjobctl -c '%(job_id)s'"
76 # N.B. The "qstat JOB_ID" command returns 1 if JOB_ID is no longer in the
77 # system, so there is no need to filter its output.
78 POLL_CMD = "checkjob"
79 REC_ID_FROM_SUBMIT_OUT = re.compile(r"""\A\s*(?P<id>\S+)\s*\Z""")
80 SUBMIT_CMD_TMPL = "msub '%(job)s'"
81 TIME_LIMIT_DIRECTIVE = "-l walltime"
82
83 def format_directives(self, job_conf):
84 """Format the job directives for a job file."""
85 job_file_path = job_conf['job_file_path']
86 directives = job_conf["directives"].__class__() # an ordereddict
87
88 tokens = Tokens(job_conf['task_id'], relative=True)
89 directives["-N"] = (
90 f'{tokens["task"]}.{tokens["cycle"]}.{job_conf["workflow_name"]}'
91 )
92 directives["-o"] = job_file_path + ".out"
93 directives["-e"] = job_file_path + ".err"
94 if (job_conf["execution_time_limit"] and
95 directives.get(self.TIME_LIMIT_DIRECTIVE) is None):
96 directives[self.TIME_LIMIT_DIRECTIVE] = "%d" % job_conf[
97 "execution_time_limit"]
98 # restartable?
99 directives.update(job_conf["directives"])
100 lines = []
101 for key, value in directives.items():
102 if value and " " in key:
103 # E.g. -l walltime=3:00:00
104 lines.append("%s%s=%s" % (self.DIRECTIVE_PREFIX, key, value))
105 elif value:
106 # E.g. -q queue_name
107 lines.append("%s%s %s" % (self.DIRECTIVE_PREFIX, key, value))
108 else:
109 # E.g. -V
110 lines.append(self.DIRECTIVE_PREFIX + key)
111 return lines
112
113
114 JOB_RUNNER_HANDLER = MoabHandler()