/
lsf.py
257 lines (173 loc) · 7.09 KB
/
lsf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# BSD 3-Clause License
#
# Copyright (c) 2017, Science and Technology Facilities Council and
# The University of Nottingham
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""A module containing the code to interact with LSF.
delete(job)
A method for deleting a single job.
prepare(job)
The method for creating the job submission file for a single job.
status(job)
The method for checking the status of a job.
submit(job)
The method for submitting a single job.
"""
import os
import re
import longbow.exceptions as exceptions
import longbow.shellwrappers as shellwrappers
QUERY_STRING = "env | grep -i 'lsf'"
def delete(job):
"""Delete a job."""
# Initialise variables.
jobid = job["jobid"]
try:
shellout = shellwrappers.sendtossh(job, ["bkill " + jobid])
except exceptions.SSHError:
raise exceptions.JobdeleteError("Unable to delete job.")
return shellout[0]
def prepare(job):
"""Create the LSF jobfile ready for submitting jobs."""
# Open file for LSF script.
lsffile = os.path.join(job["localworkdir"], "submit.lsf")
jobfile = open(lsffile, "w+")
# Write the PBS script
jobfile.write("#!/bin/bash --login\n")
# Single job
if int(job["replicates"]) == 1:
jobfile.write("#BSUB -J " + job["jobname"] + "\n")
# Job array
elif int(job["replicates"]) > 1:
jobfile.write("#BSUB -J " + job["jobname"] + "[1-" +
job["replicates"] + "]\n")
if job["queue"] is not "":
jobfile.write("#BSUB -q " + job["queue"] + "\n")
if job["lsf-cluster"] is not "":
jobfile.write("#BSUB -m " + job["lsf-cluster"] + "\n")
if job["memory"] is not "":
jobfile.write('#BSUB -R "rusage[mem=' + job["memory"] + 'G]"\n')
# Account to charge (if supplied).
if job["account"] is not "":
# if no accountflag is provided use the default
if job["accountflag"] is "":
jobfile.write("#BSUB -P " + job["account"] + "\n")
else:
jobfile.write("#BSUB " + job["accountflag"] + " " +
job["account"] + "\n")
# Email user.
if job["email-address"] is not "":
if job["email-flags"] is not "":
jobfile.write("#BSUB " + job["email-flags"] + "\n")
jobfile.write("#BSUB -u " + job["email-address"] + "\n")
jobfile.write("#BSUB -W " + job["maxtime"] + "\n")
jobfile.write("#BSUB -n " + job["cores"] + "\n")
# Load any custom scripts.
if job["scripts"] != "":
scripts = job["scripts"].split(',')
if len(scripts) > 0:
jobfile.write("\n")
for item in scripts:
jobfile.write(item.strip() + "\n")
if job["modules"] is not "":
for module in job["modules"].split(","):
module = module.replace(" ", "")
jobfile.write("\n" + "module load {0}\n\n" .format(module))
# A quick work-around for the hartree phase 3 machines
if job["handler"] == "mpiexec.hydra":
mpirun = job["handler"]
else:
mpirun = job["handler"] + " -lsf"
# Single job
if int(job["replicates"]) == 1:
jobfile.write(mpirun + " " + job["executableargs"] + "\n")
# Job array
elif int(job["replicates"]) > 1:
jobfile.write("cd rep${LSB_JOBINDEX}/\n" + mpirun + " " +
job["executableargs"] + "\n")
# Close the file (housekeeping)
jobfile.close()
# Append lsf file to list of files ready for staging.
job["upload-include"] = job["upload-include"] + ", submit.lsf"
job["subfile"] = "submit.lsf"
def status(job):
"""Query a job status."""
# Initialise variables.
states = {
"DONE": "Job Exited Properly",
"EXIT": "Job Exited in Error",
"PEND": "Queued",
"PSUSP": "Suspended",
"RUN": "Running",
"SSUSP": "Suspended",
"UNKWN": "Unknown Status",
"USUSP": "Suspended",
"WAIT": "Waiting for Start Time",
"ZOMBI": "Zombie Job"
}
jobstate = ""
shellout = shellwrappers.sendtossh(job, ["bjobs -u " + job["user"]])
# PBS will return a table, so split lines into a list.
stdout = shellout[0].split("\n")
# Look up the job state and convert it to Longbow terminology.
# Now match the jobid against the list of jobs, extract the line and
# split it into a list
for line in stdout:
line = line.split()
if len(line) > 0 and job["jobid"] in line[0]:
jobstate = states[line[2]]
break
if jobstate == "":
jobstate = "Finished"
return jobstate
def submit(job):
"""Submit a job."""
# cd into the working directory and submit the job.
cmd = ["cd " + job["destdir"] + "\n", "bsub < " + job["subfile"]]
# Process the submit
try:
shellout = shellwrappers.sendtossh(job, cmd)
except exceptions.SSHError as inst:
if "limit" in inst.stderr:
raise exceptions.QueuemaxError
else:
raise exceptions.JobsubmitError(
"Something went wrong when submitting. The following output "
"came back from the SSH call:\nstdout: {0}\nstderr {1}"
.format(inst.stdout, inst.stderr))
try:
# Do the regex in Longbow rather than in the subprocess.
jobid = re.search(r'\d+', shellout[0]).group()
except AttributeError:
raise exceptions.JobsubmitError(
"Could not detect the job id during submission, this means that "
"either the submission failed in an unexpected way, or that "
"Longbow could not understand the returned information.")
# Put jobid into the job dictionary.
job["jobid"] = jobid