Debian Codegraph @

cylc/flow/job_runner_handlers/pbs.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 PBS (or Torque) by the ``qsub`` command.
17
18 .. cylc-scope:: flow.cylc[runtime][<namespace>]
19
20 PBS 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 = pbs
28
29 .. code-block:: cylc
30 :caption: flow.cylc
31
32 [runtime]
33 [[my_task]]
34 platform = myplatform
35 execution time limit = PT1M
36 [[[directives]]]
37 -V =
38 -q = foo
39 -l nodes = 1
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 #PBS -V
48 #PBS -q foo
49 #PBS -l nodes=1
50 #PBS -l walltime=60
51
52 If :cylc:conf:`execution time limit` is specified, it is used to generate the
53 ``-l walltime`` directive. Do not specify the ``-l walltime`` directive
54 explicitly if :cylc:conf:`execution time limit` is specified. Otherwise, the
55 execution time limit known by the workflow may be out of sync with what is
56 submitted to the job runner.
57
58 .. cylc-scope::
59
60 """
61
62 import re
63
64 from cylc.flow.id import Tokens
65
66
67 class PBSHandler:
68
69 """PBS job submission and manipulation."""
70
71 DIRECTIVE_PREFIX = "#PBS "
72 # PBS fails a job submit if job "name" in "-N name" is too long.
73 # For version 12 or below, this is 15 characters.
74 # You can modify this in the site/user `global.cylc` like this
75 # [platforms]
76 # [[the-name-of-my-pbs-platform]]
77 # job runner = pbs
78 # job name length maximum = 15
79 JOB_NAME_LEN_MAX = 236
80 KILL_CMD_TMPL = "qdel '%(job_id)s'"
81 # N.B. The "qstat JOB_ID" command returns 1 if JOB_ID is no longer in the
82 # system, so there is no need to filter its output.
83 POLL_CMD = "qstat"
84 POLL_CANT_CONNECT_ERR = "cannot connect to server"
85 REC_ID_FROM_SUBMIT_OUT = re.compile(r"^\s*(?P<id>\d+)", re.M)
86 SUBMIT_CMD_TMPL = "qsub '%(job)s'"
87 TIME_LIMIT_DIRECTIVE = "-l walltime"
88
89 def format_directives(self, job_conf):
90 """Format the job directives for a job file."""
91 job_file_path = job_conf['job_file_path']
92 directives = job_conf["directives"].__class__() # an ordereddict
93 # Change task/runM to task-runM in the job name
94 # (PBS 19.2.1+ does not allow '/' in job names)
95 tokens = Tokens(job_conf['task_id'], relative=True)
96 directives["-N"] = (
97 f'{tokens["task"]}.{tokens["cycle"]}'
98 f".{job_conf['workflow_name'].replace('/', '-')}"
99 )
100 job_name_len_max = job_conf['platform'].get(
101 "job name length maximum",
102 self.JOB_NAME_LEN_MAX
103 )
104 if job_name_len_max:
105 directives["-N"] = directives["-N"][0:job_name_len_max]
106
107 directives["-o"] = job_file_path + ".out"
108 directives["-e"] = job_file_path + ".err"
109 if (
110 job_conf["execution_time_limit"]
111 and directives.get(self.TIME_LIMIT_DIRECTIVE) is None
112 ):
113 directives[self.TIME_LIMIT_DIRECTIVE] = "%d" % job_conf[
114 "execution_time_limit"]
115 for key, value in list(job_conf["directives"].items()):
116 directives[key] = value
117 lines = []
118 for key, value in directives.items():
119 if value and " " in key:
120 # E.g. -l walltime=3:00:00
121 lines.append("%s%s=%s" % (self.DIRECTIVE_PREFIX, key, value))
122 elif value:
123 # E.g. -q queue_name
124 lines.append("%s%s %s" % (self.DIRECTIVE_PREFIX, key, value))
125 else:
126 # E.g. -V
127 lines.append(self.DIRECTIVE_PREFIX + key)
128 return lines
129
130 @classmethod
131 def filter_poll_many_output(cls, out):
132 """Strip trailing stuff from the job ID."""
133 return cls.REC_ID_FROM_SUBMIT_OUT.findall(out)
134
135
136 JOB_RUNNER_HANDLER = PBSHandler()