-
Notifications
You must be signed in to change notification settings - Fork 9
/
autorunner.py
387 lines (325 loc) · 11.7 KB
/
autorunner.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
from __future__ import print_function
import os
import sys
import numpy as np
import subprocess as sub
import shutil
import submitter
# TODO organize with inheritance.
####################################################
class RunnerLocal:
''' Object that can accumulate jobs to run and run them together locally.'''
def __init__(self,np='allprocs',nn=1):
''' Note: exelines are prefixed by appropriate mpirun commands.'''
self.exelines=[]
self.np=np
self.nn=nn
self.jobname='none, this runs with out queueing.'
#-------------------------------------
def check_status(self):
return 'done'
#-------------------------------------
def add_task(self,exestr):
''' Accumulate executable commands.
Args:
exestr (str): executible statement. Will be prepended with appropriate mpirun.
'''
if self.np=='allprocs':
self.exelines.append("mpirun {exe}".format(exe=exestr))
else:
self.exelines.append("mpirun -n {tnp} {exe}".format(tnp=self.nn*self.np,exe=exestr))
#-------------------------------------
def add_command(self,cmdstr):
''' Accumulate commands that don't get an MPI command.
Args:
cmdstr (str): executible statement. Will be prepended with appropriate mpirun.
'''
self.exelines.append(cmdstr)
#-------------------------------------
def script(self,scriptfile):
''' Dump accumulated commands into a script for another job to run.
Returns true if the runner had lines to actually execute.'''
if len(self.exelines)==0:
return False
with open(scriptfile,'w') as outf:
outf.write('\n'.join(self.prefix + exelines + self.postfix))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return True
#-------------------------------------
def submit(self,jobname=None):
''' Submit series of commands.'''
if jobname is None:
jobname=self.jobname
if len(self.exelines)==0:
return ''
try:
for line in self.exelines:
result = sub.check_output(line,shell=True)
print(self.__class__.__name__,": executed %s"%line)
except sub.CalledProcessError as err:
print(self.__class__.__name__,": Error: {0}".format(err))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return ''
####################################################
class RunnerPBS:
''' Object that can accumulate jobs to run and run them together in one submission. '''
def __init__(self,queue='batch',
walltime='48:00:00',
jobname='AGRunner',
np='allprocs',nn=1,
prefix=None,
postfix=None
):
''' Note: exelines are prefixed by appropriate mpirun commands.'''
# Good prefix choices (Blue Waters).
# These are needed for Crystal runs.
# module swap PrgEnv-cray PrgEnv-intel
# These are needed for QWalk runs.
#"module swap PrgEnv-cray PrgEnv-gnu",
#"module load acml",
#"module load cblas",
self.exelines=[]
self.np=np
self.nn=nn
self.jobname=jobname
self.queue=queue
self.walltime=walltime
if prefix is None: self.prefix=[]
else: self.prefix=prefix
if postfix is None: self.postfix=[]
else: self.postfix=postfix
self.queueid=[]
#-------------------------------------
def check_status(self):
return submitter.check_PBS_stati(self.queueid)
def add_command(self,cmdstr):
''' Accumulate commands that don't get an MPI command.
Args:
cmdstr (str): executible statement. Will be prepended with appropriate mpirun.
'''
self.exelines.append(cmdstr)
#-------------------------------------
def add_task(self,exestr):
''' Accumulate executable commands.
Args:
exestr (str): executible statement. Will be prepended with appropriate mpirun.
'''
if self.np=='allprocs':
self.exelines.append("mpirun {exe}".format(exe=exestr))
else:
self.exelines.append("mpirun -n {tnp} {exe}".format(tnp=self.nn*self.np,exe=exestr))
#-------------------------------------
def script(self,scriptfile):
''' Dump accumulated commands into a script for another job to run.
Returns true if the runner had lines to actually execute.'''
if len(self.exelines)==0:
return False
with open(scriptfile,'w') as outf:
outf.write('\n'.join(self.prefix + exelines + self.postfix))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return True
#-------------------------------------
def submit(self,jobname=None):
''' Submit series of commands.'''
if jobname is None:
jobname=self.jobname
if len(self.exelines)==0:
#print(self.__class__.__name__,": All tasks completed or queued.")
return
if self.np=='allprocs':
ppnstr=',flags=allprocs'
else:
ppnstr=':ppn=%d'%self.np
jobout=jobname+'.qsub.out'
# Submit all jobs.
qsub=[
"#PBS -q %s"%self.queue,
"#PBS -l nodes=%i%s"%(self.nn,ppnstr),
"#PBS -l walltime=%s"%self.walltime,
"#PBS -j oe ",
"#PBS -N %s "%jobname,
"#PBS -o %s "%jobout,
"cd %s"%os.getcwd(),
] + self.prefix + self.exelines + self.postfix
qsubfile=jobname+".qsub"
with open(qsubfile,'w') as f:
f.write('\n'.join(qsub))
try:
result = sub.check_output("qsub %s"%(qsubfile),shell=True)
self.queueid.append(result.decode().split()[0].split('.')[0])
print(self.__class__.__name__,": Submitted as %s"%self.queueid)
except sub.CalledProcessError as err:
print(self.__class__.__name__,": Error submitting job. Check queue settings.\n\t{0}".format(err))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return qsubfile
####################################################
class FakeRunner:
''' Object that can be used as a runner, but will ignore run commands.
Useful for debugging.'''
def __init__(self):
self.exelines=[]
self.np=0
self.nn=0
self.jobname='FAKERUNNER'
self.queue='FAKEQUEUE'
self.walltime='0:00:00'
self.prefix=[]
self.postfix=[]
self.queueid=[]
#-------------------------------------
def check_status(self):
return 'unknown'
def add_command(self,cmdstr):
pass
#-------------------------------------
def add_task(self,exestr):
pass
#-------------------------------------
def script(self,scriptfile):
return False
#-------------------------------------
def submit(self,jobname=None):
''' Submit series of commands.'''
return ''
####################################################
class PySCFRunnerLocal:
''' Object that can accumulate jobs to run and run them together locally.'''
def __init__(self,np='allprocs'):
''' Note: exelines are prefixed by appropriate mpirun commands.'''
self.exelines=[]
self.queueid=[]
#-------------------------------------
def check_status(self):
return 'done'
#-------------------------------------
def add_task(self,exestr):
''' Accumulate executable commands.
Args:
exestr (str): executible statement. Will be prepended with appropriate mpirun.
'''
self.exelines.append(exestr)
#-------------------------------------
def script(self,scriptfile):
''' Dump accumulated commands into a script for another job to run.
Returns true if the runner had lines to actually execute.'''
if len(self.exelines)==0:
return False
with open(scriptfile,'w') as outf:
outf.write('\n'.join(self.prefix + exelines + self.postfix))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return True
#-------------------------------------
def submit(self,jobname=None,ppath=None):
''' Submit series of commands.
Note: jobname is not used because it doesn't submit anything.'''
sys.path=ppath+sys.path
if len(self.exelines)==0:
#print(self.__class__.__name__,": All tasks completed or queued.")
return ''
try:
for line in self.exelines:
result = sub.check_output(line,shell=True)
print(self.__class__.__name__,": executed %s"%line)
except sub.CalledProcessError as err:
print(self.__class__.__name__,": Error: {0}".format(err))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return ''
####################################################
class PySCFRunnerPBS(RunnerPBS):
''' Specialized Runner for dealing with OMP python commands.'''
def __init__(self,queue='batch',
walltime='12:00:00',
np='allprocs',
nn=1,
jobname=os.getcwd().split('/')[-1]+'_pyscf',
prefix=None,
postfix=None
):
self.np=np
if nn!=1: raise NotImplementedError
self.nn=nn
self.jobname=jobname
self.queue=queue
self.walltime=walltime
self.prefix=prefix
self.postfix=postfix
self.exelines=[]
if prefix is None: self.prefix=[]
else: self.prefix=prefix
if postfix is None: self.postfix=[]
else: self.postfix=postfix
self.queueid=[]
#-------------------------------------
def add_task(self,exestr):
''' Accumulate executable commands.
Args:
exestr (str): executible statement. Will be prepended with appropriate mpirun.
'''
self.exelines.append(exestr)
#-------------------------------------
def script(self,scriptfile):
''' Dump accumulated commands into a script for another job to run.
Returns true if the runner had lines to actually execute.'''
if len(self.exelines)==0:
return False
# Prepend mp specs.
actions=["export OMP_NUM_THREADS=%d"%(self.nn*self.np)]+actions
# Dump script.
with open(scriptfile,'w') as outf:
outf.write('\n'.join(self.prefix + actions + self.postfix))
# Remove exelines so the runner is ready for the next go.
self.exelines=[]
return True
#-------------------------------------
def submit(self,jobname=None,ppath=None):
''' Submit any accumulated tasks.
Args:
jobname (str): name to appear in the queue.
ppath (list): python path needed for the run (default: current path).
'''
if ppath is None: ppath=sys.path
if len(self.exelines)==0:
#print(self.__class__.__name__,": All tasks completed or queued.")
return
if jobname is None: jobname=self.jobname
jobout=jobname+".jobout"
qsublines=[
"#PBS -q %s"%self.queue,
]
if self.np == 'allprocs' :
qsublines+=[
"#PBS -l nodes=%i,flags=allprocs"%self.nn,
]
else:
qsublines+=[
"#PBS -l nodes=%i:ppn=%i"%(self.nn,self.np),
]
qsublines+=[
"#PBS -l walltime=%s"%self.walltime,
"#PBS -j oe",
"#PBS -N %s"%self.jobname,
"#PBS -o %s"%jobout,
"cd ${PBS_O_WORKDIR}",
"export OMP_NUM_THREADS=%d"%(self.nn*self.np),
"export PYTHONPATH=%s"%(':'.join(ppath)),
"cwd=`pwd`"
] + self.prefix + self.exelines + self.postfix
qsubfile=jobname+".qsub"
with open(qsubfile,'w') as f:
f.write('\n'.join(qsublines))
try:
result = sub.check_output("qsub %s"%(qsubfile),shell=True)
self.queueid.append(result.decode().split()[0].split('.')[0])
print(self.__class__.__name__,": Submitted as %s"%self.queueid)
except sub.CalledProcessError as err:
print(self.__class__.__name__,": Error submitting job. Check queue settings.\n\t{0}".format(err))
# Clear out the lines to set up for the next job.
self.exelines=[]
# TODO Specialize a runner for running QWalk jobs in the same directory together.
# Should just have to specialize the run command.