-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathslurm.py
More file actions
280 lines (193 loc) · 7.88 KB
/
Copy pathslurm.py
File metadata and controls
280 lines (193 loc) · 7.88 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# 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 slurm.
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 math
import os
import re
import longbow.exceptions as exceptions
import longbow.shellwrappers as shellwrappers
QUERY_STRING = "which sbatch"
def delete(job):
"""Delete a job."""
# Initialise variables.
jobid = job["jobid"]
try:
shellout = shellwrappers.sendtossh(job, ["scancel " + jobid])
except exceptions.SSHError:
raise exceptions.JobdeleteError("Unable to delete job.")
return shellout[0]
def prepare(job):
"""Create the SLURM jobfile ready for submitting jobs."""
# Open file for SLURM script.
slurmfile = os.path.join(job["localworkdir"], "submit.slurm")
jobfile = open(slurmfile, "w+")
# Write the SLURM script
jobfile.write("#!/bin/bash --login\n")
jobfile.write("#SBATCH -J " + job["jobname"] + "\n")
# Queue to submit to (if supplied)
if job["queue"] != "":
jobfile.write("#SBATCH -p " + job["queue"] + "\n")
# Account to charge (if supplied)
if job["account"] != "":
# if no accountflag is provided use the default
if job["accountflag"] == "":
jobfile.write("#SBATCH -A " + job["account"] + "\n")
else:
jobfile.write("#SBATCH " + job["accountflag"] + " " +
job["account"] + "\n")
if job["memory"] != "":
jobfile.write("#SBATCH --mem=" + job["memory"] + "G" + "\n")
# Generic resource (if supplied)
if job["slurm-gres"] != "":
jobfile.write("#SBATCH --gres=" + job["slurm-gres"] + "\n")
# Email user.
if job["email-address"] != "":
if job["email-flags"] != "":
jobfile.write("#SBATCH --mail-type=" + job["email-flags"] + "\n")
jobfile.write("#SBATCH --mail-user=" + job["email-address"] + "\n")
cores = job["cores"]
cpn = job["corespernode"]
# Specify the total number of mpi tasks required
jobfile.write("#SBATCH -n " + cores + "\n")
# If user has specified corespernode for under utilisation then
# set the total nodes (-N) parameter.
if cpn != "":
nodes = float(cores) / float(cpn)
# Make sure nodes is rounded up to the next highest integer
nodes = str(int(math.ceil(nodes)))
jobfile.write("#SBATCH -N " + nodes + "\n")
# Walltime for job
jobfile.write("#SBATCH -t " + job["maxtime"] + ":00\n\n")
jobfile.write("export OMP_NUM_THREADS=1\n\n")
# Redirect stdout
if job["stdout"] != "":
jobfile.write("#SBATCH -o " + job["stdout"] + "\n")
# Redirect stderr
if job["stderr"] != "":
jobfile.write("#SBATCH -e " + job["stderr"] + "\n\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")
jobfile.write("\n")
# Load up modules if required.
if job["modules"] != "":
for module in job["modules"].split(","):
module = module.replace(" ", "")
jobfile.write("module load {0}\n\n" .format(module))
# Handler that is used for job submission.
mpirun = job["handler"]
# Single jobs only need one run command.
if int(job["replicates"]) == 1:
jobfile.write(mpirun + " " + job["executableargs"] + "\n")
# Ensemble jobs need a loop.
elif int(job["replicates"]) > 1:
jobfile.write("basedir = `pwd`\n"
"for i in {1.." + job["replicates"] + "};\n"
"do\n"
" cd $basedir/rep$i/\n"
" " + mpirun + " " + job["executableargs"] +
"\n"
"done\n"
"wait\n")
# Close the file
jobfile.close()
# Append submitfile to list of files ready for staging.
job["upload-include"] = job["upload-include"] + ", submit.slurm"
job["subfile"] = "submit.slurm"
def status(job):
"""Query a job status."""
# Initialise variables.
states = {
"CA": "Cancelled",
"CD": "Completed",
"CF": "Configuring",
"CG": "Completing",
"F": "Failed",
"NF": "Node Failure",
"PD": "Pending",
"PR": "Preempted",
"R": "Running",
"S": "Suspended",
"TO": "Timed out"
}
jobstate = ""
shellout = shellwrappers.sendtossh(job, ["squeue -j " + job["jobid"]])
# 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[4]]
break
if jobstate == "":
jobstate = "Finished"
return jobstate
def submit(job):
"""Submit a job."""
# Change into the working directory and submit the job.
cmd = ["cd " + job["destdir"] + "\n", "sbatch " + job["subfile"]]
# Process the submit
try:
shellout = shellwrappers.sendtossh(job, cmd)
except exceptions.SSHError as inst:
if "violates" in inst.stderr and "job submit 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