-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathJobManager.py
More file actions
247 lines (191 loc) · 7.8 KB
/
JobManager.py
File metadata and controls
247 lines (191 loc) · 7.8 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
import time, os
from threading import Thread
class Job:
alg = None
id = None
status = "Not started."
settings = None
def __init__(self, id, alg, settings):
self.id = id
self.alg = alg
self.settings = settings
def printStatus(self):
print "Job ID: " + str(self.id) + "\t" + str(self.alg.name) + " is " + self.status
def setStatusRunning(self):
self.status = "Running."
def setStatusError(self, retcode):
self.status = "Error with retcode = " + str(retcode)
def setStatusFinished(self):
self.status = "Finished."
def run_cluster(self):
return self.alg.run_cluster(self.settings)
def run_local(self):
return self.alg.run_local(self.settings)
class JobThread(Thread):
job = None # The job that this thread will run
use_cluster = False # Are we using a cluster or not?
jobman = None # This is the manager of this job
def __init__(self, job, jobman, settings, use_cluster):
Thread.__init__(self)
self.job = job
self.use_cluster = use_cluster
self.jobman = jobman
def run(self):
if self.use_cluster:
self.job.run_cluster()
else:
self.job.setStatusRunning()
retcode = 0
print "--------RUNNING JOB"
if self.use_cluster:
retcode = self.job.run_cluster()
else:
retcode = self.job.run_local()
print "--------FINISHED JOB"
self.jobman.running.remove(self.job)
print retcode
if retcode == 0:
self.jobman.finished.append(self.job)
self.job.setStatusFinished()
else:
self.jobman.error.append(self.job)
self.job.setStatusError(retcode)
class JobManager:
def __init__(self,settings):
# spawn own thread, start up queue, start connection to server
self.queue = []
self.finished = []
self.running = []
self.error = []
self.job_ids = []
self.threads = []
self.use_cluster = False
self.connection = None
self.curr_id = 0
self.settings = None
running_threads = 0
max_threads = 1
self.settings = settings
if settings["global"]["use_cluster"] == True:
import pbs
self.use_cluster = True
else:
self.use_cluster = False
self.max_threads = settings["global"]["n_processors"]
if self.use_cluster:
# Establish connection to PBS server
serv_addr = settings["global"]["cluster_address"]
# Let the cluster's jobman handle scheduling
self.max_threads = sys.maxint
self.connection = pbs.pbs_connect(serv_addr)
if self.connection < 0:
errno, text = pbs.error()
print "Error, unable to establish connection to PBS server."
print errno, text
sys.exit(1)
def queueJob(self,alg):
# Add this job to the queue, returns job id
j = Job(self.curr_id, alg, self.settings)
self.queue.append(j)
j.status = "In queue."
self.curr_id += 1
return j.id
def runQueue(self):
# Begin running the queue, launching on cluster or running on
# local machine
while len(self.queue) > 0:
os.system("clear")
self.reportStatus()
while self.max_threads > len(self.running) and len(self.queue) > 0:
# Run on cluster
# launch script. one thread per script launched. that thread
# then will wait for the process to finish, do whatever needs
# tidying up, update the job and come back. The entire queue can
# be sent at once, as the cluster manager software will take care
# of actual job scheduling. Our threads just watch that particular
# algorithm's folders for when it finishes. This can be done by
# passing this jobman object along to each thread. Checking the
# status of jobs should result in benign race conditions only.
# Run locally, spawning a thread up until N threads. These should
# behave the same way as the cluster threads, but actually do the
# work instead of idling. Instead of blasting the entire cluster
# at once, these will have to be delegated out by this program.
job = self.queue.pop(0)
self.running.append(job)
#job.run_local()
print "Creating new thread"
jthread = JobThread(job, self, self.settings, self.use_cluster)
print "Launching new thread"
jthread.start()
print "New thread launched."
self.threads.append(jthread)
time.sleep(5)
def waitToClear(self, status=None):
# Hold the incoming thread until the queue is clear
while len(self.queue) + len(self.running) > 0:
os.system("clear")
if status != None:
print "Status: {0}".format(status)
print "Waiting for " + str(len(self.queue) + len(self.running)) + " jobs to finish."
self.reportStatus()
time.sleep(5)
self.reportStatus()
print "Queue finished."
def clear(self):
self.running = []
self.queue = []
self.finished = []
self.error = []
def reportStatus(self):
# TODO: Have this print out statuses
# Print status of all jobs in the queue/running/finished
print "RUNNING JOBS:"
for j in self.running:
j.printStatus()
print "QUEUED JOBS:"
for j in self.queue:
j.printStatus()
print "FINISHED JOBS:"
for j in self.finished:
j.printStatus()
print "ERROR JOBS:"
for j in self.error:
j.printStatus()
def writeClusterScripts(self, alg, settings):
# TODO: Make this check alg first, then global
# Using the alg settings, write the cluster scripts
# into the algorithm's main directory.
algorithm_name = alg.alg_name
settings[alg.alg_name]["cluster_prog_script"] = alg.cwd + "/cluster_run.sh"
run_file = open(settings[alg.alg_name]["cluster_prog_script"], 'w')
run_file.write(alg.cmd)
run_file.flush()
run_file.close()
template_file = open("config/templates/cluster_queue.sh",'r')
template = template_file.readlines()
for i in xrange(len(template)):
line = template[i]
if "{{" in line and "}}" in line:
# Loop through the line and replace all of the params with the
# params from the settings dict.
if line.count("{{") == line.count("}}"):
for p in xrange(line.count("{{")):
line = template[i]
param_str = line[line.index("{{"):line.index("}}")+2]
template[i] = template[i].replace(param_str, \
str(settings[algorithm_name][param_str[2:len(param_str)-2]]))
else:
print "ERROR in template file. Forgot {{ or }}?"
queue_file = open(alg.cwd + "/cluster_queue.sh",'w')
for l in template:
queue_file.write(l)
queue_file.flush()
queue_file.close()
def deleteJob(self,id):
ind = self.queue.index(id, lambda a: a.id)
# TODO: Use pbs to delete job
self.queue.delete(ind)
print "Job " + str(id) + " deleted."
def jobStatus(self,id):
ind = self.queue.index(id, lambda a: a.id)
return self.queue[ind].status