/
lsf.py
executable file
·259 lines (167 loc) · 6.46 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
258
259
# Longbow is Copyright (C) of James T Gebbie-Rayet and Gareth B Shannon 2015.
#
# This file is part of the Longbow software which was developed as part of the
# HECBioSim project (http://www.hecbiosim.ac.uk/).
#
# HECBioSim facilitates and supports high-end computing within the UK
# biomolecular simulation community on resources such as ARCHER.
#
# Longbow is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option) any later
# version.
#
# Longbow is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Longbow. If not, see <http://www.gnu.org/licenses/>.
"""
This module contains the code to interact with the various flavours of 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.corelibs.exceptions as exceptions
import Longbow.corelibs.shellwrappers as shellwrappers
QUERY_STRING = "env | grep -i 'lsf'"
def delete(job):
"""
Method for deleting job.
"""
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")
if job["jobname"] is not "":
# 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["cluster"] is not "":
jobfile.write("#BSUB -m " + job["cluster"] + "\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:
for item in scripts:
jobfile.write(item.strip() + "\n\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):
"""
Method for querying job.
"""
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 = ""
try:
shellout = shellwrappers.sendtossh(job, ["bjobs -u " + job["user"]])
except exceptions.SSHError:
raise
# 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.
try:
# Now match the jobid against the list of jobs, extract the line and
# split it into a list
job = [line for line in stdout if job["jobid"] in line][0].split()
jobstate = states[job[2]]
except (IndexError, KeyError):
jobstate = "Finished"
return jobstate
def submit(job):
"""
Method for submitting 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(shellout[0], shellout[1]))
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