-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·270 lines (206 loc) · 9.13 KB
/
utils.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
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import time
from os import listdir
from os.path import isfile, join
from os import walk
import subprocess
#from basis.utilities import execute
from execUtils import execute
import tempfile
import shutil
import argparse
from argparse import RawTextHelpFormatter
def tic():
#Homemade version of matlab tic and toc functions
import time
global startTime_for_tictoc
startTime_for_tictoc = time.time()
def toc():
import time
if 'startTime_for_tictoc' in globals():
print( "Elapsed time is " + str(time.time() - startTime_for_tictoc) + " seconds." )
else:
print( "Toc: start time not set" )
def submit_PSCBridge(cmd,
stdOut,
stdErr,
jobName,
slurmOptions=[],
MATLABVersion=None,
simulate=True):
"""
This is a helper function to submit slurm task to the c3ddb cluster
"""
def getMCRDir(matVer):
if matVer=='v901':
mcrDir='/pylon2/ms4s88p/batmangh/installed/MATLAB_Compiler_Runtime/v901/'
else:
raise ValueError('install this version, I don''t know where the engine is!')
return mcrDir
slurmLauncher = os.path.dirname(__file__) + '/slurmLauncher_bridges.sh'
matlabExecHelper= os.path.dirname(__file__) + '/runMatlabExec_bridges'
cmdLine = ['sbatch']
cmdLine = cmdLine + ['-o' , stdOut, '-e', stdErr, '--job-name', jobName]
cmdLine = cmdLine + slurmOptions
cmdLine = cmdLine + [slurmLauncher]
if not(MATLABVersion==None):
MCR_DIR = getMCRDir(MATLABVersion)
cmdLine = cmdLine + [matlabExecHelper, MCR_DIR]
cmdLine = cmdLine + cmd
cmdLineString = ''
for c in cmdLine:
cmdLineString = cmdLineString + ' ' + str(c)
if simulate:
print( 'Simulate : ', cmdLineString )
else:
print( 'Running : ', cmdLineString )
# make sure all entries of the cmdLine is string
cmdLine = [str(c) for c in cmdLine ]
e,o = execute(cmdLine,simulate=False, stdout=True)
jobID = o.split()[-1]
return jobID
#p = subprocess.Popen(cmdLine, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#for line in p.stdout:
# print( line )
#p.wait()
def submit_c3ddb(cmd,
stdOut,
stdErr,
jobName,
slurmOptions=[],
MATLABVersion=None,
simulate=True):
"""
This is a helper function to submit slurm task to the c3ddb cluster
"""
def getMCRDir(matVer):
if matVer=='v83':
mcrDir='/home/batmanghelich/installed/MATLAB_Compiler_Runtime/v83/'
else:
raise ValueError('install this version, I don''t know where the engine is!')
return mcrDir
slurmLauncher = os.path.dirname(__file__) + '/slurmLauncher_c3ddb.sh'
matlabExecHelper = os.path.dirname(__file__) + '/runMatlabExec_c3ddb'
cmdLine = ['sbatch']
cmdLine = cmdLine + ['-o' , stdOut, '-e', stdErr, '--job-name', jobName]
cmdLine = cmdLine + slurmOptions
cmdLine = cmdLine + [slurmLauncher]
if not(MATLABVersion==None):
MCR_DIR = getMCRDir(MATLABVersion)
cmdLine = cmdLine + [matlabExecHelper, MCR_DIR]
cmdLine = cmdLine + cmd
cmdLineString = ''
for c in cmdLine:
cmdLineString = cmdLineString + ' ' + str(c)
if simulate:
print( 'Simulate : ', cmdLineString )
else:
print( 'Running : ', cmdLineString )
# make sure all entries of the cmdLine is string
cmdLine = [str(c) for c in cmdLine ]
e,o = execute(cmdLine,simulate=False, stdout=True)
jobID = o.split()[-1]
return jobID
#p = subprocess.Popen(cmdLine, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#for line in p.stdout:
# print( line )
#p.wait()
def checkJobRunningOnC3ddbCluster(jobIDs, sleepTime=2):
finishedFlag = [False]*len(jobIDs)
while not(all(finishedFlag)):
print( "checking ..." )
for cnt in range(len(jobIDs)):
j = jobIDs[cnt]
flag = finishedFlag[cnt]
if flag:
continue
e,o = execute('squeue',stdout=True,quiet=True)
if o.find(j)>0:
print( j + " is still running .... " )
else:
flag = True
if flag:
print( "it seems ", j , "is finished !!" )
finishedFlag[cnt] = True
time.sleep(sleepTime)
if __name__ == '__main__':
#parse input arguments
parser = argparse.ArgumentParser(description="""This program performs some of the modules defined in the utils.
Here is a list of available options :
submitToc3ddb : to submit a job to the c3ddb cluster
Here is somes examples:
Ex1 : ./utils.py submitToc3ddb --jobName testJob --simulate --isMATLAB --slurmOpt "--time 1:00:00" --cmd test.sh "--opt 1 --opt 3 --opt 5"
.""", formatter_class=RawTextHelpFormatter )
#parser.add_argument('--action', help='Specify the action', required=True, type=str, choices=['extractFeaturesFromSuperVoxels'])
subparsers = parser.add_subparsers(dest='action')
# subparser for submit jobs to C3ddb cluster
parser_submit2C3ddb = subparsers.add_parser('submitToc3ddb')
# add a required argument
parser_submit2C3ddb.add_argument('--cmd', nargs='+', help="Command line to run on the cluster", required=True)
parser_submit2C3ddb.add_argument('--slurmOpt', nargs='+', help='set of options for Slurm', required=False, default=[])
parser_submit2C3ddb.add_argument('--isMATLAB', action='store_true', help='Is this a MATLAB code ? ', default=False)
parser_submit2C3ddb.add_argument('--matVersion', type=str, help='MATLAB version', default='v83', required=False)
parser_submit2C3ddb.add_argument('--jobName', type=str, help='name of the job', required=True)
parser_submit2C3ddb.add_argument('--logFileRoot', type=str, help='Root of the log files', required=False, default='/scratch/users/batmanghelich/COPDGene/work/kayhan/logFiles')
parser_submit2C3ddb.add_argument('--simulate', action='store_true', help='dry-run', default=False, required=False)
# subparser for submit jobs to bridge cluster
parser_submit2Bridge = subparsers.add_parser('submitToBridge')
# add a required argument
parser_submit2Bridge.add_argument('--cmd', nargs='+', help="Command line to run on the cluster", required=True)
parser_submit2Bridge.add_argument('--slurmOpt', nargs='+', help='set of options for Slurm', required=False, default=['-p RM-shared'])
parser_submit2Bridge.add_argument('--isMATLAB', action='store_true', help='Is this a MATLAB code ? ', default=False)
parser_submit2Bridge.add_argument('--matVersion', type=str, help='MATLAB version', default='v83', required=False)
parser_submit2Bridge.add_argument('--jobName', type=str, help='name of the job', required=True)
parser_submit2Bridge.add_argument('--logFileRoot', type=str, help='Root of the log files', required=False, default='/home/batmangh/MyPylon1Space/logFiles/COPDGene')
parser_submit2Bridge.add_argument('--simulate', action='store_true', help='dry-run', default=False, required=False)
args = vars(parser.parse_args())
print( args )
if args['action']=='submitToc3ddb':
cmd = reduce(list.__add__, map(str.split,args['cmd'] ) )
print( cmd )
if not(args['slurmOpt']==[]):
slurmOpt = reduce(list.__add__, map(str.split,args['slurmOpt'] ) )
print(slurmOpt)
else:
slurmOpt = []
jobName = args['jobName']
simulate = args['simulate']
stdOut = args['logFileRoot'] + '/' + jobName + '.stdout'
stdErr = args['logFileRoot'] + '/' + jobName + '.stderr'
if args['isMATLAB']:
matVersion = args['matVersion']
else:
matVersion = None
submit_c3ddb(cmd,
stdOut,
stdErr,
jobName,
slurmOptions=slurmOpt,
MATLABVersion=matVersion,
simulate=simulate)
if args['action']=='submitToBridge':
cmd = reduce(list.__add__, map(str.split,args['cmd'] ) )
print( cmd )
if not(args['slurmOpt']==[]):
slurmOpt = reduce(list.__add__, map(str.split,args['slurmOpt'] ) )
print(slurmOpt)
else:
slurmOpt = []
jobName = args['jobName']
simulate = args['simulate']
stdOut = args['logFileRoot'] + '/' + jobName + '.stdout'
stdErr = args['logFileRoot'] + '/' + jobName + '.stderr'
if args['isMATLAB']:
matVersion = args['matVersion']
else:
matVersion = None
submit_PSCBridge(cmd,
stdOut,
stdErr,
jobName,
slurmOptions=slurmOpt,
MATLABVersion=matVersion,
simulate=simulate)