-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrawler2.py
More file actions
341 lines (307 loc) · 11.4 KB
/
crawler2.py
File metadata and controls
341 lines (307 loc) · 11.4 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
import os
import glob
import crawlset
import sys
import numpy as np
from crawldefs import *
from batch_system import *
from sets import Set
if __name__=="__main__":
#Check which resources are in use and compare to our max allotment
#Create auxiliary folders if they don't exist
os.system("mkdir hopper")
os.system("mkdir waitlist")
try:
f=open(".home","r")
top = f.read().split('\n')[0]
f.close()
except:
os.system("echo $(pwd)>.home")
f=open(".home","r")
top = f.read().split('\n')[0]
f.close()
if "DRYRUN" in sys.argv[:]:
dryrun=True
else:
dryrun=False
rude=False
if "RUDE" in sys.argv[:]:
rude=True
mode="normal"
if "batch" in sys.argv[:]:
mode="batch"
f=open("nnodes.crwl","r")
nnodes=float(f.read().split('\n')[0])+1.0 #Chances are this is being called by one of the current jobs!
f.close()
#resources={}
#for m in MODELS.keys():
#rf=open("running_"+m+".crwl","r")
#resources[m] = rf.read().split('\n')[0]
#resources[m] = resources[m].split()
##print resources[m]
#rf.close()
if rude:
resources,running = getjobs(rude=rude)
else:
resources = getjobs(rude=rude)
running = 0
for r in list(resources.keys()):
#for n in range(0,len(resources[r])):
#running+=int(resources[r][n])*1.0/MODELS[r]
running += np.sum(resources[r])
print(str(running)+" of "+str(nnodes)+" nodes used")
priority = True
while running < nnodes and priority: #First do jobs placed in priority.crwl.
#Get next task
#NOTE JOBS IN PRIORITY.CRWL SHOULD BE ASSIGNED NEGATIVE PIDs TO AVOID COLLISIONS
f=open("priority.crwl","r")
tasks=f.read().split('\n')
f.close()
while tasks[-1]=="":
tasks = tasks[:-1]
queued=False
ready=False
while not ready: #Search for a job to run
for i in range(0,len(tasks)):
if tasks[i]!='':
if tasks[i][0]!="#":
task = tasks[i].split()
if int(task[3])==0:
queued=True
qpid=int(task[0])
taskmodel = task[1]
if running+(1.0/8.0)<=nnodes: #MODELS[taskmodel] #We might be at 1 cpu less than capacity,
mark=i #so a plasim job might put us over the limit.
f=open("tasklog.crwl","a")
f.write("\nFound job "+' '.join(task))
f.close()
break
else:
queued = False
ready=False
if queued:
f=open("tasklog.crwl","a")
f.write("\nSearching for header...")
f.close()
header=False
for i in range(mark,-1,-1): #Find header for the task
if tasks[i]!='':
if tasks[i][0]=="#":
header = tasks[i]
if (len(header.split())-1)==(len(task)): #first header with the right number of args
break
else:
header=False
if not header: #None match!
f=open("tasklog.crwl","a")
f.write("\nTask "+str(qpid)+" header mismatch; skipping")
f.close()
else: #We found the header. Onward!
ready=True
f=open("tasklog.crwl","a")
f.write("\nTask "+str(qpid)+" header found")
f.close()
else: #We didn't find an available task
f=open("tasklog.crwl","a")
f.write("\nNo open jobs in priority queue; all done!")
print("No open jobs in priority queue")
f.close()
#running=nnodes
priority = False
break
#Engage next task
if ready:
print("Engaging a priority task")
goahead = False
print("Creating a Job with header\n",header,"\n and arguments \n",' '.join(task))
newjob = Job(header,' '.join(task),-1) #Collect and organize the job parameters
for i in range(0,len(resources[taskmodel])):
if float(resources[taskmodel][i])==0.0:
rid = i
print(resources[taskmodel])
resources[taskmodel][i]=newjob.ncores/8.0#float(MODELS[taskmodel])
print(resources[taskmodel])
newjob.home = rid
goahead = True
break
if not goahead: #No open slot found for this model
if running+float(newjob.ncores)/8.0 <= nnodes: #MODELS[taskmodel] #We do have space for one more though
print(resources[taskmodel])
tmp = np.zeros(len(resources[taskmodel])+100)
tmp[:len(resources[taskmodel])] = resources[taskmodel][:]
n0 = len(resources[taskmodel])
resources[taskmodel] = tmp
resources[taskmodel][n0] = newjob.ncores/8.0#float(MODELS[taskmodel])
print(resources[taskmodel])
rid = n0
newjob.home = rid
goahead = True
else: #Nope, pack up and go home
print("At capacity.")
priority = False
if goahead: #Found a job slot
task[3] = '1'
task = ' '.join(task)
tasks[mark] = task
tasks='\n'.join(tasks)
f=open("priority.crwl","w")
f.write(tasks)
f.close()
crawlset.newtask(newjob,dryrun=dryrun) #Set up the job and submit it
np.save(taskmodel+'/job'+str(newjob.home)+'/job.npy',newjob)
if not dryrun:
newjob.getID()
else:
newjob.tag='xxxxx.doug'
newjob.write()
f=open("tasklog.crwl","a")
f.write("\nQueued job "+newjob.name+" in "+newjob.queue+" for "+str(newjob.ncores)+" cores.")
f.close()
running += float(newjob.ncores)/8.0#MODELS[taskmodel] #Note that we are *that* much closer to the limit.
print("Moving on to normal tasks")
print(running,nnodes)
capacityflag = False
nsofar = 0
while running < nnodes: #We are using less than our full allocation, and the priority list is empty.
#Get next task
f=open("tasks.crwl","r")
tasks=f.read().split('\n')
f.close()
while tasks[-1]=="":
tasks = tasks[:-1]
queued=False
ready=False
print((tasks[-3:]))
while not ready: #Search for a job to run
for i in range(0,len(tasks)):
if tasks[i]!='':
#print tasks[i]
if tasks[i][0]!="#":
task = tasks[i].split()
if int(task[3])==0:
queued=int(task[0])
print(("Queued job ",queued))
taskmodel = task[1]
if running+(1.0/8.0)<=nnodes: #MODELS[taskmodel] #We might be at 1 cpu less than capacity,
mark=i #so a plasim job might put us over the limit.
f=open("tasklog.crwl","a")
f.write("\nFound job "+' '.join(task))
f.close()
break
else:
queued = False
ready=False
if queued:
f=open("tasklog.crwl","a")
f.write("\nSearching for header...")
f.close()
header=False
for i in range(mark,-1,-1): #Find header for the task
if tasks[i][0]=="#":
header = tasks[i]
if (len(header.split())-1)==(len(task)): #first header with the right number of args
break
else:
header=False
if not header: #None match!
f=open("tasklog.crwl","a")
f.write("\nTask "+str(queued)+" header mismatch; skipping")
f.close()
else: #We found the header. Onward!
ready=True
f=open("tasklog.crwl","a")
f.write("\nTask "+str(queued)+" header found")
f.close()
else: #We didn't find an available task
f=open("tasklog.crwl","a")
f.write("\nNo open jobs in task queue; all done!")
f.close()
running=nnodes
print("No open jobs in task queue; all done!")
capacityflag = True
break
#Engage next task
if ready:
goahead = False
print("Creating a Job with header\n",header,"\n and arguments \n",' '.join(task))
newjob = Job(header,' '.join(task),-1) #Collect and organize the job parameters
for i in range(0,len(resources[taskmodel])):
if float(resources[taskmodel][i])==0.0:
rid = i
print(resources[taskmodel])
print(newjob.ncores,MODELS[taskmodel])
resources[taskmodel][i]=newjob.ncores/8.0#float(MODELS[taskmodel])
print(resources[taskmodel])
newjob.home = rid
goahead = True
break
if not goahead: #No open slot found for this model
if running+float(newjob.ncores)/8.0 <= nnodes: #MODELS[taskmodel] #We do have space for one more though
print(resources[taskmodel])
print(newjob.ncores,MODELS[taskmodel])
tmp = np.zeros(len(resources[taskmodel])+100)
tmp[:len(resources[taskmodel])] = resources[taskmodel][:]
n0 = len(resources[taskmodel])
resources[taskmodel] = tmp
resources[taskmodel][n0] = newjob.ncores/8.0#float(MODELS[taskmodel])
print(resources[taskmodel])
rid = n0
newjob.home = rid
goahead = True
else: #Nope, pack up and go home
print("At capacity.")
capacityflag = True
if goahead: #Found a job slot
task[3] = '1'
task = ' '.join(task)
tasks[mark] = task
tasks='\n'.join(tasks)
if not Set(task).issubset(Set(" \n")): #Make sure we have something to write!
f=open("tasks.crwl","w")
f.write(tasks)
f.close()
crawlset.newtask(newjob,dryrun=dryrun) #Set up the job and submit it
np.save(taskmodel+'/job'+str(newjob.home)+'/job.npy',newjob)
if not dryrun:
newjob.getID()
else:
newjob.tag='xxxxx.doug'
newjob.write()
running += float(newjob.ncores)/8.0#MODELS[taskmodel] #Note that we are *that* much closer to the limit.
nsofar+=1
if not dryrun:
if mode=="normal" or (mode=="batch" and nsofar%100==0):
if rude:
resources,running = getjobs(rude=rude)
else:
resources = getjobs(rude=rude)
if not rude:
running = 0
for r in list(resources.keys()):
running += np.sum(resources[r])
if capacityflag:
running = nnodes
#folders = []
##print MODELS.keys()
#for m in MODELS.keys():
#fr=open("running_"+m+".crwl","w")
##print resources[m]
##print '--'
#fr.write(' '.join(resources[m])+'\n')
#fr.close()
#folders+=sorted(glob.glob(m+"/job*/"))
##Keep track of the current jobs
#htext = "Current job array:\n"
#for f in folders:
#jf = open(f+"/job.crwl")
#jc = jf.read().split('\n')
#jf.close()
#htext+=f+" "+jc[3]+"\n"
#cjb = open("currentjobs.crwl","w")
#cjb.write(htext)
#cjb.close()
os.system("echo 'Relinquishing control'>>tasklog.crwl")
f=open(top+"/inuse.crwl","w") #Release ownership of crawler.py
f.write('0')
f.close()
os.system("echo 'Main status is now '$(cat "+top+"/inuse.crwl)>>tasklog.crwl")