-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqsub_slurm.py
More file actions
executable file
·476 lines (431 loc) · 16.7 KB
/
qsub_slurm.py
File metadata and controls
executable file
·476 lines (431 loc) · 16.7 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import sys, os, time
from random import randint
class qsub_hpc:
def write_sh(self,cmd,jobname,sidx,p,h,m,mem,email,wd,mo,pre,post,a,inta,nnode,ngpu,array,constrain,mop):
# Write header
oup = open("%s%i.sb" % (jobname,sidx),"w")
oup.write('#!/bin/bash')
oup.write('\n########## Define Resources Needed with SBATCH Lines ##########\n\n')
oup.write("#SBATCH -n %i -c %i --gres=gpus:%i\n" % (nnode, p, ngpu))
oup.write("#SBATCH --time=%i:%i\n" % (h,m))
oup.write("#SBATCH --mem=%iG\n" % (mem))
if email != "":
oup.write("#SBATCH --mail-user=%s\n" % email)
oup.write("#SBATCH --mail-type=FAIL,BEGIN,END\n")
if jobname != "":
oup.write("#SBATCH -J %s\n" % jobname)
if a != "":
oup.write("#SBATCH -A %s\n" % a)
if inta != "":
oup.write("srun --pty /bin/bash")
if array != "":
oup.write("#SBATCH --array=%s\n" % array)
if constrain != "":
constrain = constrain.split(",")
new=[]
for i in constrain:
s= i.replace("dev-", "")
new.append(s)
constrain1 = "|".join(new)
oup.write("#SBATCH --constraint=[%s]\n" % constrain1)
oup.write('\n########## Command Lines to Run ##########\n\n')
# Set working directory
if wd != "":
oup.write("cd %s\n" % wd)
#module purge
if mop == 'yes':
oup.write("module purge\n")
# Load modules
if mo != []:
for j in mo:
oup.write("module load %s\n" % j)
# Write cmd lines defined in the pre parameter
if pre != "":
oup.write("%s\n" % "".join(open(pre).readlines()))
# Write main cmd line
oup.write("%s\n\n" % cmd.strip())
# Write cmd lines defined in the post parameter
if post != "":
oup.write("%s\n" % "".join(open(post).readlines()))
oup.close()
def submit(self,jobs,sidx,wtime,mem,jobname,p,email,logdir,a,inta,nnode,ngpu,array,wdir="",
module="",pre="",post=""):
runtype = 0
if type(jobs) != list:
runtype = 1
inp = open(jobs)
inl = inp.readlines()
tmp = []
for i in inl:
i = i.strip()
tmp.append(i)
jobs = tmp
if logdir == 0:
logdir = "-o tmp.o -e tmp.e "
else:
logdir = ""
# read modules to be loaded
if module == "":
mo = []
elif os.path.isfile(module):
inp = open(module)
mo = inp.readlines()
else:
mo = module.split(',')
h = wtime/60
m = wtime%60
for i in jobs:
if i.strip() != '' and i[0] != "#":
print(" job %i" % sidx)
self.write_sh(i,jobname,sidx,p,h,m,mem,email,wdir,mo,pre,post,a,inta,nnode,ngpu,array,constrain,mop)
os.system("chmod 755 %s%i.sb" % (jobname,sidx))
os.system("sbatch %s%s%i.sb" % (logdir,jobname,sidx))
sidx += 1
def queue(self,jcommand,stime,nsub,juser,jrange,wtime,mem,jobname,p,email,
logdir,a,inta,nnode,ngpu,array,wdir="",module="",pre="",post=""):
jdict = {}
inp = open(jcommand)
jobs = inp.readlines()
if jrange != "*":
jrange = jrange.split("-")
jobs = jobs[int(jrange[0])-1:int(jrange[1])]
oup = open("%s.log" % jcommand,"w")
j = 0
while j < len(jobs):
oup.write("%s\t" % time.ctime())
print("%s\t" % time.ctime())
#####
# Check number of jobs in quene so far
#####
rint = randint(1,1e6)
os.system("squeue -l -u %s > TMP.%i" % (juser,rint))
time.sleep(2)
inp = open("TMP.%i" % rint)
inl = inp.readlines()
if len(inl) != 0:
currj = len(inl)-5
else:
currj = 0
inp.close()
os.system("rm TMP.%i" % rint)
#####
# Submit more jobs so there is always nsub number of jobs in quene
#####
if currj < nsub:
jseg = jobs[j:j+(nsub-currj)]
oup.write("submit %i\t" % len(jseg))
print("current %i, submit %i\n" % (currj,len(jseg)))
self.submit(jseg,j+1,wtime,mem,jobname,p,email,logdir,a,inta,nnode,ngpu,array,wdir,module)
j += len(jseg)
else:
oup.write("waiting\t")
print("waiting\t")
oup.write("submited so far: %i\n" % j)
print("submited so far: %i\n" % j)
time.sleep(stime)
#submit(self,jobs,sidx,wtime,mem,jobname,p,email,logdir,a,i,nnode,ngpu,array,wdir="",module="",pre="",post=""):
print("Done!")
def qdel(self,duser,drange):
#if "" not in [duser,drange]:
# print("Specify either user OR range! Quit!...\n"
# sys.exit(0)
print("User :",duser)
print("Range:",drange)
if duser != "":
os.system("squeue -l -u %s > TMP_qdel" % duser)
inp = open("TMP_qdel")
inl = inp.readlines()[5:]
dlist = []
for i in inl:
dlist.append(i.split(".")[0])
print("Kill:")
c = 1
for i in dlist:
try:
print(" %i of %i" % (c,len(dlist)))
os.system("scancel %s" % i)
c += 1
except ValueError:
pass
os.system("rm TMP_qdel")
else:
drange = drange.split("-")
b = int(drange[0])
e = int(drange[1])+1 # inclusive
for i in range(b,e):
print("scancel %s" % i)
os.system("scancel %i" % i)
print("Done!")
#
# Take a list of Job IDS in the following format:
# 12345
# 12345.cmgr01
#
def qdel2(self,klist):
klist = open(klist).readlines()
for i in klist:
i = i.strip().split(".")[0]
print(i)
os.system("scancel %s" % i)
print("Done!")
def check_running(self,user):
'''
Note: Not tested with the new slurm scheduler, only change made was qstat to squeue -l
'''
os.system("squeue -l -u %s" % user)
inp = open("TMP_check_running_log")
inl = inp.readlines()
countQ = 0
countR = 0
countE = 0
countH = 0
flagStart = 0
flagUser = 0
if user != "":
flagUser = 1
UD = {} # {username:{"R":x,"Q":x,"E":x,"H":x}}
for i in inl:
if i[:3] == "---":
flagStart = 1
elif flagStart:
i = i.split(" ")
tmp = []
for j in i:
if j != "":
tmp.append(j)
U = tmp[2]
S = tmp[4]
if U not in UD:
UD[U] = {S:1}
elif S not in UD[U]:
UD[U][S] = 1
else:
UD[U][S] +=1
if tmp[4] == "R":
countR += 1
elif tmp[4] == "Q":
countQ += 1
elif tmp[4] == "E":
countE += 1
elif tmp[4] == "H":
countH += 1
else:
print("UNK:",tmp[4])
print("Running :",countR)
print("Queueing:",countQ)
print("Held :",countH)
print("Err :",countE)
flag = ['R','Q','H','E']
if flagUser:
if user in UD:
counts = []
for i in flag:
if i in UD[user]:
counts.append(UD[user][i])
else:
counts.append(0)
print("User: %s, %i running, %i in queue, %i held, %i with error" % \
(user,counts[0],counts[1],counts[2],counts[3]))
else:
print("USer: %s, no job..." % user)
else:
print("User: [R,Q,H,E,T]")
users = UD.keys()
users.sort()
for i in users:
print('%s: [' % i,)
T = 0
for j in flag:
if j in UD[i]:
print(UD[i][j],)
T += UD[i][j]
else:
print(0,)
print('%i]' % T)
os.system("rm TMP_check_running_log")
def check_err(self,jobname):
'''
Note: Not updated for slurm
'''
files = os.listdir("./")
# err log files
efiles = []
for i in files:
if i.find(jobname) != -1 and i.find(".sh.e") != -1:
efiles.append(i)
efiles.sort()
# go through err log files
print("With error:")
eidx = []
for i in efiles:
s = os.path.getsize("./%s" % i)
try:
idx = int(i[i.find(jobname)+len(jobname):i.find(".sh.e")])
if s != 0:
inp = open(i)
inl = inp.readlines()
print(" %s:" % i,inl)
eidx.append(idx)
except ValueError:
print(" %s:" % i,"job index err")
continue
print("###############")
print("Compile new command line. Note that this rely on the presence")
print("of the shell script file in the same folder as the err log.")
print("In addition, it just look for the last non-empty lines in .sh.")
print("###############")
print(" %i jobs failed" % len(eidx))
oup = open("cmd_%s_witherr" % (jobname),"w")
for i in eidx:
# read the shell script
inp = open("%s%i.sh" % (jobname,i))
inl = inp.readlines()
# Look for the last non-empty line
inl.reverse()
for j in inl:
j = j.strip()
if j != "":
oup.write("%s\n" % j)
break
print("Command lines of jobs with err: cmd_%s_witherr" % jobname)
print("Done!")
def rmlb(self,astr):
astr = astr.strip()
return astr
def help(self):
print("\nFunctions (-f):")
print(" submit - create shell script and submit job bsaed on a file")
print(" where each line is one job. NEED: c, OPT: w,m,J,p,ei,wd,mo,")
print(" pre,post")
print(" queue - submit jobs sequentially. NEED:c,u, OPT:s,n,r,w,m,")
print(" J,p,e,k,wd,mo")
print(" qdel - delete jobs, NEED: r or u (if want to kill all)")
print(" qdel2 - delete jobs based on a kill list (k)")
print(" check_running - check how many job have R stat, OPT: u")
print(" check_err - check any job error and compile a new cmd line")
print(" file, NEED: J")
print("")
print("Parameters:")
print(" c - the command line file, one line per job, cmd start with #")
print(" will not run.")
print(" e - email, default ''. If given, FAIL, BEGIN, & END will be sent")
print(" s - sec between qstat check, default 10")
print(" nnode - number of nodes to request, default 1")
print(" p - number of CPUs to use, default 1, same as -ncpu")
print(" ngpu - number of GPUs to use, default 0")
print(" n - number of jobs to submit at a time , default 10")
print(" u - which user to monitor")
print(" r - jobnum1-jobnum2, jobnum-, or all (default)")
print(" w - wtime, in minutes. Defaulit 10 min.")
print(" wd- working dir")
print(" m - mem in GB, default 1")
print(" mo- a file or a string with a list of modules to load, if a str,")
print(" use ',' as delimiter")
print(" J - job name")
print(" pre - a file with additional commands common among all jobs to")
print(" appended BEFORE the variable command line")
print(" post- a file with additional commands common among all jobs to")
print(" appended AFTER the variable command line")
print(" o - keep all log files (1) or rename to tmp.o/tmp.e (0)")
print(" k - a list of job ids to kill")
print(" A - name of buy-in node")
print(" array - Range if running an array job (i.e. 1-10)")
print(" i - Run in interactive mode, default = '' s")
print(" mop - 'yes' if you want modules to be purged before loading new modules")
print(" constrain - add dev-node that you want to constrain the run to")
print("")
sys.exit(0)
if __name__ == '__main__':
qsub = qsub_hpc()
f = c = u = e = wd = k = mo = pre = post = a = array = inta = ""
s = n = 10
w = 600
m = o = p = nnode = 1
ngpu = 0
r = "*"
J = "job"
mop = "no"
constrain = ""
for i in range(1,len(sys.argv),2):
if sys.argv[i] == "-f":
f = sys.argv[i+1]
elif sys.argv[i] == "-c":
c = sys.argv[i+1]
elif sys.argv[i] == "-e":
e = sys.argv[i+1]
elif sys.argv[i] == "-s":
s = int(sys.argv[i+1])
elif sys.argv[i] == "-n":
n = int(sys.argv[i+1])
elif sys.argv[i] == "-nnode":
nnode = int(sys.argv[i+1])
elif sys.argv[i] == "-p" or sys.argv[i] == "-ncpu":
p = int(sys.argv[i+1])
elif sys.argv[i] == "-ngpu":
ngpu = int(sys.argv[i+1])
elif sys.argv[i] == "-u":
u = sys.argv[i+1]
elif sys.argv[i] == "-r":
r = sys.argv[i+1]
elif sys.argv[i] == "-w":
w = int(sys.argv[i+1])*60
elif sys.argv[i] == "-m":
m = int(sys.argv[i+1])
elif sys.argv[i] == "-J":
J = sys.argv[i+1]
elif sys.argv[i] == "-o":
o = int(sys.argv[i+1])
elif sys.argv[i] == "-wd":
wd = sys.argv[i+1]
elif sys.argv[i] == "-k":
k = sys.argv[i+1]
elif sys.argv[i] == "-mo":
mo = sys.argv[i+1]
elif sys.argv[i] == "-pre":
pre = sys.argv[i+1]
elif sys.argv[i] == "-post":
post = sys.argv[i+1]
elif sys.argv[i] == "-A":
a = sys.argv[i+1]
elif sys.argv[i] == "-i":
inta = sys.argv[i+1]
elif sys.argv[i] == "-array":
array = sys.argv[i+1]
elif sys.argv[i] == "-mop":
mop = sys.argv[i+1]
elif sys.argv[i] == "-constrain":
constrain = sys.argv[i+1]
else:
print("UNKNOWN FLAG:",sys.argv[i])
print("Do -h to get help.")
sys.exit(0)
if f == "submit":
if "" in [c]:
print("\nNeed cmd line file, user name\n")
qsub.help()
qsub.submit(c,0,w,m,J,p,e,o,a,inta,nnode,ngpu,array,wd,mo,pre,post)
elif f == "queue":
if "" in [c,u]:
print("\nNeed cmd line file, user name\n")
qsub.help()
qsub.queue(c,s,n,u,r,w,m,J,p,e,o,a,inta,nnode,ngpu,array,wd,mo,pre,post)
elif f == "qdel":
if r == "" and u == "":
print("\nNeed range or user name\n")
qsub.help()
qsub.qdel(u,r)
elif f == "qdel2":
if k == "":
print("\nNeed kill list\n")
qsub.help()
qsub.qdel2(k)
elif f == "check_running":
qsub.check_running(u)
elif f == "check_err":
if J == "job":
print("\nYou are using the default job name, make sure this is really what")
print("you want to check... Go ahead anyway")
qsub.check_err(J)
else:
print("\nUnknown function...\n")
qsub.help()