Debian Codegraph @

cylc/flow/job_runner_handlers/lsf.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 IBM Platform LSF by the ``bsub`` command.
17
18 .. cylc-scope:: flow.cylc[runtime][<namespace>]
19
20 LSF directives can be provided in the flow.cylc file:
21
22 .. code-block:: cylc
23 :caption: global.cylc
24
25 [platforms]
26 [[myplatform]]
27 job runner = lsf
28
29 .. code-block:: cylc
30 :caption: flow.cylc
31
32 [runtime]
33 [[my_task]]
34 platform = myplatform
35 execution time limit = PT10M
36 [[[directives]]]
37 -q = foo
38
39 These are written to the top of the job script like this:
40
41 .. code-block:: bash
42
43 #!/bin/bash
44 # DIRECTIVES
45 #BSUB -q = foo
46 #BSUB -W = 10
47
48 If :cylc:conf:`execution time limit` is specified, it is used to generate the
49 ``-W`` directive. Do not specify the ``-W`` directive
50 explicitly if :cylc:conf:`execution time limit` is specified. Otherwise, the
51 execution time limit known by the workflow may be out of sync with what is
52 submitted to the job runner.
53
54 .. cylc-scope::
55
56 """
57
58 import math
59 import re
60
61 from cylc.flow.id import Tokens
62
63
64 class LSFHandler():
65 """IBM Platform LSF bsub job submission"""
66
67 DIRECTIVE_PREFIX = "#BSUB "
68 FAIL_SIGNALS = ("EXIT", "ERR", "XCPU", "TERM", "INT", "SIGUSR2")
69 KILL_CMD_TMPL = "bkill '%(job_id)s'"
70 POLL_CMD = "bjobs"
71 REC_ID_FROM_SUBMIT_OUT = re.compile(r"^Job <(?P<id>\d+)>")
72 SUBMIT_CMD_TMPL = "bsub"
73 TIME_LIMIT_DIRECTIVE = "-W"
74
75 @classmethod
76 def format_directives(cls, job_conf):
77 """Format the job directives for a job file."""
78 job_file_path = job_conf['job_file_path']
79 directives = job_conf["directives"].__class__()
80 tokens = Tokens(job_conf['task_id'], relative=True)
81 directives["-J"] = (
82 f'{tokens["task"]}.{tokens["cycle"]}.{job_conf["workflow_name"]}'
83 )
84 directives["-o"] = job_file_path + ".out"
85 directives["-e"] = job_file_path + ".err"
86 if (
87 job_conf["execution_time_limit"]
88 and directives.get(cls.TIME_LIMIT_DIRECTIVE) is None
89 ):
90 directives[cls.TIME_LIMIT_DIRECTIVE] = str(math.ceil(
91 job_conf["execution_time_limit"] / 60))
92 for key, value in list(job_conf["directives"].items()):
93 directives[key] = value
94 lines = []
95 for key, value in directives.items():
96 if value:
97 lines.append("%s%s %s" % (cls.DIRECTIVE_PREFIX, key, value))
98 else:
99 lines.append("%s%s" % (cls.DIRECTIVE_PREFIX, key))
100 return lines
101
102 @classmethod
103 def get_submit_stdin(cls, job_file_path, _):
104 """Return proc_stdin_arg, proc_stdin_value."""
105 return (open(job_file_path), None) # noqa: SIM115 (open fh by design)
106
107
108 JOB_RUNNER_HANDLER = LSFHandler()