-
Notifications
You must be signed in to change notification settings - Fork 20
/
launcher_creator.py
460 lines (376 loc) · 17 KB
/
launcher_creator.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
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
#!/usr/bin/env python
# Create TACC launchers for Stampede and Lonestar, and just run commands otherwise.
# Depending on the host, launcher_creator.py generated a SLURM or SGE launcher as appropriate.
#
# Copyright 2013, Regents Of The University Of Texas At Austin
# Originally written by Scott Hunicke-Smith
# New version with SLURM support by Benjamin Goetz
try:
import argparse
import subprocess
except:
print 'Try typing "module load python" and then running this again.'
import sys
sys.exit(1)
import os
import sys
import re
lonestar_parameters='''#!/bin/bash
#
# Simple SGE script for submitting multiple serial
# jobs (e.g. parametric studies) using a script wrapper
# to launch the jobs.
#
# To use, build the launcher executable and your
# serial application(s) and place them in your WORKDIR
# directory. Then, edit the CONTROL_FILE to specify
# each executable per process.
#-------------------------------------------------------
#-------------------------------------------------------
#
# <------ Setup Parameters ------>
#
#$ -N {name}
#$ -pe {wayness}way {total_threads}
#$ -q {queue}
#$ -o {name}.o$JOB_ID
#$ -l h_rt={time}
#$ -V
{email_line}{hold_line}#$ -cwd
{allocation_line}
'''
stampede_parameters='''#!/bin/bash
#SBATCH -J {name}
#SBATCH -n {num_jobs}
{num_nodes_line}#SBATCH -p {queue}
#SBATCH -o {name}.o%j
#SBATCH -e {name}.e%j
#SBATCH -t {time}
{allocation_line}
{email_line}'''
bash_and_modules='''
module load launcher
{modules}
{bash_commands}
'''
parametric_job_submission='''
export EXECUTABLE=$TACC_LAUNCHER_DIR/init_launcher
export CONTROL_FILE={control_file}
$TACC_LAUNCHER_DIR/paramrun $EXECUTABLE $CONTROL_FILE
'''
parametric_job_submission_ls5='''
export LAUNCHER_PLUGIN_DIR=$LAUNCHER_DIR/plugins
export LAUNCHER_RMI=SLURM
export LAUNCHER_JOB_FILE={jobfile}
$LAUNCHER_DIR/paramrun
'''
def file_len(fname):
f = open(fname)
contents = f.readlines()
# Only count non-empty lines and count the
# last line even if it has no carriage return
i = 0
for line in contents:
stripped_line = line.strip()
#comment lines
if re.match('^\s*#', stripped_line):
continue
if not stripped_line:
continue
else:
i+=1
f.close()
return i
def generate_lonestar_launcher(results, launcher_file):
"""
Write a Lonestar launcher SGE script.
"""
# Must account (on Lonestar) for normal having 12 threads per node;
if results.queue == 'largemem':
threads_per_node = 24
else:
threads_per_node = 12
if not results.wayness:
results.wayness = 12
if results.allocation:
allocation_line = '#$ -A {}'.format(results.allocation)
else:
allocation_line = ''
if results.email:
email_line = '#$ -M {}\n#$ -m be\n'.format(results.email)
else:
email_line = ''
if results.hold and not results.submit_cmd:
hold_line='#$ -hold_jid {}\n'.format(results.hold)
else:
hold_line=''
if not results.num_nodes:
if results.job:
num_nodes = file_len(results.job)
else:
num_nodes = 1
if num_nodes == 0:
print 'Your job file appears to be empty. Please fix this error.'
return
while num_nodes % results.wayness != 0:
num_nodes += 1
total_threads = num_nodes * threads_per_node / results.wayness
else:
num_nodes = results.num_nodes
total_threads = results.num_nodes * threads_per_node
if not results.submit_cmd:
print 'Job file has %i lines.' % num_nodes
if not results.submit_cmd:
print 'Using {} nodes.'.format(num_nodes)
print 'Writing to {}.'.format(results.launcher)
launcher_file.write(lonestar_parameters.format(
name=results.name,
wayness=results.wayness,
total_threads=total_threads,
walrus_total_threads=total_threads,
queue=results.queue,
time=results.time,
email_line=email_line,
hold_line=hold_line,
allocation_line=allocation_line))
launcher_file.write(bash_and_modules.format(modules=results.modules,
bash_commands=results.bash_commands))
if results.job:
launcher_file.write(parametric_job_submission.format(control_file=results.job))
def generate_stampede_launcher(results, launcher_file):
"""
Write a Stampede launcher SLURM script.
"""
# Must account (on Stampede) for normal having 16 threads per node
if results.queue == 'largemem':
threads_per_node = 32
else:
threads_per_node = 16
num_nodes_line = ''
# if not results.wayness:
# results.wayness = 16
# num_cores_line = ''
if results.allocation:
allocation_line = '#SBATCH -A {}'.format(results.allocation)
else:
allocation_line = ''
if results.email:
email_line = '#SBATCH --mail-type=ALL\n#SBATCH --mail-user={}\n'.format(results.email)
else:
email_line = ''
if not results.num_nodes:
# If a number of nodes is not explicitly specified, calculate based on number of commands in jobs file, and optional wayness.
if results.job:
num_jobs = file_len(results.job)
if num_jobs == 0:
print 'Your job file appears to be empty. Please fix this error.'
return
else:
num_jobs = 1
if results.wayness:
num_nodes, remainder = divmod(num_jobs, results.wayness)
if remainder != 0:
num_nodes += 1
num_nodes_line = '#SBATCH -N {}\n'.format(num_nodes)
else:
num_nodes, remainder = divmod(num_jobs, 16)
if remainder != 0:
num_nodes += 1
else:
# A number of nodes has been specified. Default wayness is 16 on Stampede. Use specified wayness to calculate total tasks.
num_nodes = results.num_nodes
num_nodes_line = '#SBATCH -N {}\n'.format(num_nodes)
if results.wayness:
num_jobs = num_nodes * results.wayness
else:
num_jobs = num_nodes * 16
if not results.submit_cmd:
print 'Using {} nodes.'.format(num_nodes)
print 'Writing to {}.'.format(results.launcher)
launcher_file.write(stampede_parameters.format(
name=results.name,
num_jobs=num_jobs,
num_nodes_line=num_nodes_line,
queue=results.queue,
time=results.time,
email_line=email_line,
allocation_line=allocation_line))
launcher_file.write(bash_and_modules.format(modules=results.modules,
bash_commands=results.bash_commands))
if results.job:
launcher_file.write(parametric_job_submission.format(control_file=results.job))
def generate_ls5_launcher(results, launcher_file):
"""
Write a Lonestar 5 launcher SLURM script.
"""
# Must account (on Lonestar 5) for normal having 16 threads per node
if results.queue == 'largemem':
threads_per_node = 32
elif results.queue == 'hugemem':
threads_per_node = 20
else:
threads_per_node = 24
num_nodes_line = ''
# if not results.wayness:
# results.wayness = 16
# num_cores_line = ''
if results.allocation:
allocation_line = '#SBATCH -A {}'.format(results.allocation)
else:
allocation_line = ''
if results.email:
email_line = '#SBATCH --mail-type=ALL\n#SBATCH --mail-user={}\n'.format(results.email)
else:
email_line = ''
if not results.num_nodes:
# If a number of nodes is not explicitly specified, calculate based on number of commands in jobs file, and optional wayness.
if results.job:
num_jobs = file_len(results.job)
if num_jobs == 0:
print 'Your job file appears to be empty. Please fix this error.'
return
else:
num_jobs = 1
if results.wayness:
num_nodes, remainder = divmod(num_jobs, results.wayness)
if remainder != 0:
num_nodes += 1
num_nodes_line = '#SBATCH -N {}\n'.format(num_nodes)
else:
num_nodes, remainder = divmod(num_jobs, 24)
if remainder != 0:
num_nodes += 1
else:
# A number of nodes has been specified. Default wayness is 16 on Stampede. Use specified wayness to calculate total tasks.
num_nodes = results.num_nodes
num_nodes_line = '#SBATCH -N {}\n'.format(num_nodes)
if results.wayness:
num_jobs = num_nodes * results.wayness
else:
num_jobs = num_nodes * 24
if not results.submit_cmd:
print 'Using {} nodes.'.format(num_nodes)
print 'Writing to {}.'.format(results.launcher)
launcher_file.write(stampede_parameters.format(
name=results.name,
num_jobs=num_jobs,
num_nodes_line=num_nodes_line,
queue=results.queue,
time=results.time,
email_line=email_line,
allocation_line=allocation_line))
launcher_file.write(bash_and_modules.format(modules=results.modules,
bash_commands=results.bash_commands))
if results.job:
launcher_file.write(parametric_job_submission_ls5.format(jobfile=results.job))
def check_time_and_queue(results):
host = subprocess.check_output(["hostname", "-f"]).split('.')[1]
queues = {'ls4':['normal','development','largemem','gpu','vis','serial'],
'stampede':['normal','development','largemem','serial','large','request','normal-mic','gpu','gpudev','vis']}
if host in queues and results.queue not in queues[host]:
sys.exit( 'Your queue selection "' + results.queue + '" is not available.' + '\n' +
'Available queues: ' + ', '.join(queues[host]))
if results.time == None:
parser.print_help()
sys.exit('You did not give a job time (-t hh:mm:ss).')
time_match = re.match(r'(\d?\d):(\d\d):(\d\d)', results.time)
if not time_match:
sys.exit( 'Your time argument -t ' + results.time + ' is not in the right format.' + '\n' +
'Please change it to match (-t hh:mm:ss).' )
hh = int(time_match.group(1))
mm = int(time_match.group(2))
ss = int(time_match.group(3))
if hh == 0 and mm == 0 and ss == 0: sys.exit( 'Please enter a non-zero time value.' )
if mm > 59 or ss > 59: sys.exit( results.time + ' is not a valid time.' )
if host == "ls4" and results.queue == 'development': time_limit = 1
elif host == "ls4" and results.queue == 'serial': time_limit = 12
elif host == "stampede" and results.queue == 'development': time_limit = 2
elif host == "stampede" and \
results.queue in ['gpudev','visdev']: time_limit = 4
elif host == "stampede" and results.queue == 'vis': time_limit = 8
elif host == "stampede" and results.queue == 'serial': time_limit = 12
elif host == "stampede" and \
results.queue in ['normal','largemem','normal-mic']: time_limit = 48
elif host == "ls5" and results.queue == 'development': time_limit = 2
elif host == "ls5" and results.queue == 'gpu': time_limit = 24
elif host == "ls5" and results.queue == 'vis': time_limit = 8
elif host == "ls5" and \
results.queue in ['normal','largemem','hugemem']: time_limit = 48
else: time_limit = 24
if (hh > time_limit) or (hh == time_limit and (mm > 0 or ss > 0)):
sys.exit( 'Your time argument -t ' + results.time + ' exceeds the ' +
str(time_limit) + ' hour limit for the ' + results.queue + ' queue' + '\n' +
'on this machine. Please change queue or reduce requested time.' )
class ArgumentParserDefaultHelp(argparse.ArgumentParser):
'''Simple variation on ArgumentParser. When an error is thrown, this inherited class prints the entire help message.'''
def error(self, message):
self.print_help()
sys.stderr.write('\nerror: {}\n\n'.format(message))
sys.exit(2)
def main():
# Get environment variables, os.environ.get returns 'none' if the environment variable isn't defined
env_allocation = os.environ.get('ALLOCATION')
env_email_address = os.environ.get('EMAIL_ADDRESS')
parser = ArgumentParserDefaultHelp(description='''Create TACC launchers for Stampede and Lonestar, and just run commands otherwise.
Depending on the host, launcher_creator.py generates a SLURM or SGE launcher as appropriate.
Report problems to [email protected]''')
required = parser.add_argument_group('Required')
required.add_argument('-n', action='store', dest='name', required=True, help='The name of your job.')
required.add_argument('-t', action='store', dest='time', required=True, help='The time you want to give to your job. Format: hh:mm:ss')
commands = parser.add_argument_group('Commands', 'You must use at least one of these options to submit your commands for TACC.')
commands.add_argument('-j', action='store', dest='job', help='The name of the job file containing your commands.')
commands.add_argument('-b', action='store', dest='bash_commands', default='', help='A string of Bash commands that are executed before the parametric jobs are launched.')
optional = parser.add_argument_group('Optional')
optional.add_argument('-q', action='store', dest='queue', default='development', help='The TACC allocation for job submission. Default="development"')
optional.add_argument('-a', '-A', action='store', dest='allocation', nargs='?', default=env_allocation, help='The TACC allocation for job submission. You can set a default ALLOCATION environment variable.')
optional.add_argument('-m', action='store', dest='modules', default='', help='A list of module commands. The "launcher" module is always automatically included. Example: -m "module swap intel gcc; module load bedtools"')
optional.add_argument('-w', action='store', dest='wayness', type=int, help='Wayness: the number of commands you want to give each node. Default= 12 on Lonestar, 16 on Stampede.')
optional.add_argument('-N', action='store', dest='num_nodes', type=int, help='Number of nodes to request. You probably don\'t need this option. You ONLY need it if you want to run a job list that isn\'t defined at the time you submit the launcher.')
optional.add_argument('-e', action='store', dest='email', nargs='?', const=env_email_address, help='Your email address if you want to receive an email from Lonestar when your job starts and ends. Without an argument, it will use a default EMAIL_ADDRESS environment variable.')
optional.add_argument('-l', action='store', dest='launcher', help='The name of the *.sge launcher script that will be created. Default="<name>.sge"')
optional.add_argument('-s', dest='submit_cmd', action='store_true', help='Echoes the launcher filename to stdout.')
deprecated = parser.add_argument_group('Deprecated', 'Rewrite any scripts that use this.')
deprecated.add_argument('-H', action='store', dest='hold', help='Hold job in queue until the job specified in this option finishes.')
results = parser.parse_args()
# Handle allocation defaults
# if not results.allocation:
# results.allocation = os.environ.get('ALLOCATION')
# Check for required parameters
# if results.name == None:
# print 'You did not give a job name (-n <name>).'
# parser.print_help()
# return
# Check for valid time and queue entries
check_time_and_queue(results)
# Check for valid email address, if email is given
if results.email:
if not re.match(r'^.+@.+\..{2,4}$', results.email):
print 'Not a valid email address, please check it.'
return
if not (results.job or results.bash_commands):
if os.path.isfile('commands'):
results.job = 'commands'
else:
print '-== Neither a job file nor bash commands given. You need at least one to do anything! ==-'
return
if not results.submit_cmd:
print 'Project {}.'.format(results.name)
print 'Using job file {}.'.format(results.job)
print 'Using {} queue.'.format(results.queue)
print 'For {} time.'.format(results.time)
print 'Using {} allocation.'.format(results.allocation)
# if (results.hold != None) and not results.submit_cmd:
# print 'Holding until {} finishes.'.format(results.hold)
if not results.submit_cmd:
if results.email != None:
print 'Sending start/stop email to {}.'.format(results.email)
else:
print 'Not sending start/stop email.'
host = subprocess.check_output(["hostname", "-f"]).split('.')[1]
if not results.launcher:
results.launcher = '{}.slurm'.format(results.name)
launcher_file = open(results.launcher, 'w')
generate_stampede_launcher(results, launcher_file)
launcher_file.close()
if __name__ == '__main__':
main()