Debian Codegraph @

cylc/flow/job_runner_handlers/sge.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 Sun/Oracle Grid Engine with ``qsub``.
17
18 .. cylc-scope:: flow.cylc[runtime][<namespace>]
19
20 SGE directives can be provided in the flow.cylc file:
21
22 .. code-block:: cylc
23 :caption: global.cylc
24
25 [platforms]
26 [[sge_platform]]
27 job runner = sge
28
29 .. code-block:: cylc
30 :caption: flow.cylc
31
32 [runtime]
33 [[my_task]]
34 platform = sge_platform
35 execution time limit = P1D
36 [[[directives]]]
37 -cwd =
38 -q = foo
39 -l h_data = 1024M
40
41 These are written to the top of the job script like this:
42
43 .. code-block:: bash
44
45 #!/bin/bash
46 # DIRECTIVES
47 #$ -cwd
48 #$ -q foo
49 #$ -l h_data=1024M
50 #$ -l h_rt=24:00:00
51
52 If :cylc:conf:`execution time limit` is specified, it is used to generate the
53 ``-l h_rt`` directive. Do not specify the ``-l h_rt`` directive explicitly if
54 :cylc:conf:`execution time limit` is specified. Otherwise, the execution time
55 limit known by the workflow may be out of sync with what is submitted to the
56 job runner.
57
58 .. cylc-scope::
59
60 """
61
62 import re
63
64 from cylc.flow.id import Tokens
65
66
67 class SGEHandler:
68
69 """SGE qsub job submission"""
70
71 DIRECTIVE_PREFIX = "#$ "
72 KILL_CMD_TMPL = "qdel '%(job_id)s'"
73 # N.B. The "qstat -j JOB_ID" command returns 1 if JOB_ID is no longer in
74 # the system, so there is no need to filter its output.
75 POLL_CMD = "qstat"
76 REC_ID_FROM_SUBMIT_OUT = re.compile(r"\D+(?P<id>\d+)\D+")
77 SUBMIT_CMD_TMPL = "qsub '%(job)s'"
78 TIME_LIMIT_DIRECTIVE = "-l h_rt"
79
80 def format_directives(self, job_conf):
81 """Format the job directives for a job file."""
82 job_file_path = job_conf['job_file_path']
83 directives = job_conf['directives'].__class__()
84 tokens = Tokens(job_conf['task_id'], relative=True)
85 directives['-N'] = (
86 f'{job_conf["workflow_name"]}.{tokens["task"]}.{tokens["cycle"]}'
87 )
88 directives['-o'] = job_file_path + ".out"
89 directives['-e'] = job_file_path + ".err"
90 if (job_conf["execution_time_limit"] and
91 directives.get(self.TIME_LIMIT_DIRECTIVE) is None):
92 directives[self.TIME_LIMIT_DIRECTIVE] = "%d:%02d:%02d" % (
93 job_conf["execution_time_limit"] / 3600,
94 (job_conf["execution_time_limit"] / 60) % 60,
95 job_conf["execution_time_limit"] % 60)
96 for key, value in list(job_conf['directives'].items()):
97 directives[key] = value
98 lines = []
99 for key, value in directives.items():
100 if value and " " in key:
101 # E.g. -l h_rt=3:00:00
102 lines.append("%s%s=%s" % (self.DIRECTIVE_PREFIX, key, value))
103 elif value:
104 # E.g. -q queue_name
105 lines.append("%s%s %s" % (self.DIRECTIVE_PREFIX, key, value))
106 else:
107 # E.g. -V
108 lines.append("%s%s" % (self.DIRECTIVE_PREFIX, key))
109 return lines
110
111 @classmethod
112 def get_poll_many_cmd(cls, _):
113 """Return poll command"""
114 # No way to run POLL_CMD on specific job id(s). List all user's jobs.
115 # job_runner_mgr._jobs_poll_runner checks requested id in list.
116 return [cls.POLL_CMD]
117
118
119 JOB_RUNNER_HANDLER = SGEHandler()