-
Notifications
You must be signed in to change notification settings - Fork 0
/
workthief.py
executable file
·418 lines (290 loc) · 13.4 KB
/
workthief.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
#!/usr/bin/env python
import shutil
import numpy as np
import tarfile
import os, sys, copy
from random import randint, seed
from stat import *
from write_rand_data import *
from collections import defaultdict
from time import sleep
from mpi4py import MPI
from mpiclass import MPIClass
np.set_printoptions(threshold=7)
################################################################################
class WorkThief(MPIClass):
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def __init__(self):
MPIClass.__init__(self,initdirs=False)
self.rank_up = self.rank+1 % self.nranks
self.rank_down = (self.nranks-1) if self.i_am_root else (self.rank-1)
self.last_steal = self.rank_up
self.instruct = None;
self.queue = []
self.dirs = []
self.files = []
self.excess_threshold = 2
self.starve_threshold = 0
self.sendvals = defaultdict(list)
self.requests = [MPI.REQUEST_NULL for p in range(0,self.nranks) ]
self.init_queue()
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def next_steal(self):
self.last_steal = (self.last_steal + 1) % self.nranks
if self.last_steal == self.rank:
self.last_steal = (self.last_steal + 1) % self.nranks
return self.last_steal
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def summary(self):
self.comm.Barrier()
sys.stdout.flush()
sep="-"*80
assert len(self.queue) == 0
nfiles = len(self.files)
ndirs = len(self.dirs)
# print end message
for p in range(0,self.nranks):
self.comm.Barrier()
sys.stdout.flush()
if p == self.rank:
if self.i_am_root: print(sep)
#print(nrank {}, found {} files, {} dirs".format(self.rank,self.files,self.dirs))
print("rank {}, found {} files, {} dirs".format(self.rank, nfiles, ndirs))
nfiles_tot = self.comm.allreduce(nfiles, MPI.SUM)
ndirs_tot = self.comm.allreduce(ndirs, MPI.SUM)
self.comm.Barrier()
sys.stdout.flush()
if self.i_am_root:
print("{}\nTotal found {} files, {} dirs".format(sep,nfiles_tot,ndirs_tot))
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def recurse(self, top, maxdepth=10**9, depth=0):
self.dirs.append(top)
for f in os.listdir(top):
pathname = os.path.join(top, f)
statinfo = os.stat(pathname)
if S_ISDIR(statinfo.st_mode):
if (depth < maxdepth):
self.recurse(pathname, maxdepth, depth=depth+1)
else:
self.queue.append(pathname)
else:
#print(statinfo)
self.files.append(pathname)
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def init_queue(self):
if self.i_am_root:
rootdir='.'
self.recurse(rootdir, maxdepth=0)
sep="-s"*40
print("{}\ndir queue, {} items=\n{}".format(sep, len(self.queue), self.queue))
print("{}\ndirs found {} items=\n{}".format(sep, len(self.dirs), self.dirs))
print("{}\nfiles found {} items=\n{}".format(sep,len(self.files), self.files))
# populate initial tasks for other ranks
excess = self.excess_work()
while excess:
for dest in range(1,self.nranks):
if excess:
self.sendvals[dest].append(self.queue.pop())
excess = self.excess_work() # still?
for dest in range(1,self.nranks):
if self.sendvals[dest]:
print("sending {} entries '{}' to rank {}".format(len(self.sendvals[dest]),self.sendvals[dest],dest))
self.requests[dest] = self.comm.issend(self.sendvals[dest], dest=dest, tag=self.tags['work_reply'])
print("{}\ndir queue, {} items=\n{}".format(sep, len(self.queue), self.queue))
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def excess_work(self):
if self.nranks == 1: return False # no excess work with no helpers!
if len(self.queue) > self.excess_threshold:
return (len(self.queue) - self.excess_threshold)
return False
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def need_work(self):
if len(self.queue) <= self.starve_threshold:
return True
return False
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def split_queue(self):
curlen = len(self.queue)
if curlen < 2: return None
mid = int(curlen/2)
if mid == 0: return None
front = self.queue[0:mid]
back = self.queue[mid:]
if (len(front) + len(back)) != curlen:
print ("q={},\nf={},\nb={}".format(self.queue, front, back))
print (len(front),len(back),(len(front)+len(back)),curlen)
raise Exception('error splitting queue!')
self.queue = back
return front
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def progress(self,nsteps=1):
for step in range(0,nsteps):
if self.queue:
self.recurse(self.queue.pop(), maxdepth=1)
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def random_rank (self):
if self.nranks == 1: return 0
rrank = self.rank
while rrank != self.rank:
rrank = randint(0,(self.nranks-1))
return rrank
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def random_rank_in_range (self, nentries=None):
if not nentries:
seed(self.rank+self.nranks)
nentries = randint(0, 10*self.nranks)
if 'rand_initialized' not in WorkThief.__dict__:
seed(self.rank)
WorkThief.rand_initialized = True
vals=np.empty(nentries, dtype=np.int)
for idx in range(0,nentries):
vals[idx] = randint(0,10**9) % self.nranks
return vals
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def execute(self):
srcs=set()
# intialiaze acounting & misc vals
my_size = np.full(1, 1, dtype=np.int)
gloabl_size = np.full(1, 1, dtype=np.int)
recv_cnt = 0
recv_loop = 0
inner_loop = 0
outer_loop = 0
# lists of ranks
i_denied = set()
denied_me = set()
tstart = MPI.Wtime()
status = MPI.Status()
# idx, flag, msg = MPI.Request.testany(self.requests)
# print(idx, flag, msg)
# assert not flag
# assert idx == MPI.UNDEFINED
# self.comm.Barrier()
# enter nonzero size loop
while gloabl_size[0]:
# loop specific
outer_loop += 1
barrier = None
done = False
inner_loop = 0
i_requested_work = False
i_denied.clear()
denied_me.clear()
# enter work loop
while not done:
inner_loop += 1
recv_loop += 1
# single rank optimization
if self.nranks == 1:
self.progress(10**6)
gloabl_size[0] = len(self.queue)
done = False if gloabl_size[0] else True
continue
# work reply?
if self.comm.iprobe(source=MPI.ANY_SOURCE, tag=self.tags['work_reply'], status=status):
# probe the source and tag before receiving
source = status.Get_source()
srcs.add(source)
# complete the receive
recvval = self.comm.recv(source=source, tag=self.tags['work_reply'])
recv_cnt += 1
if recvval:
#print("{:3d} rank got '{}' from rank {:3d}".format(self.rank,recvval,source))
self.queue.extend(recvval)
i_requested_work = False
# make progress on our own work
self.progress(10)
# work request?
if self.comm.iprobe(source=MPI.ANY_SOURCE, tag=self.tags['work_request'], status=status):
# probe the source and tag before receiving
source = status.Get_source()
srcs.add(source)
recv_cnt += 1
# Reply first. This makes sure the
# new message is in flight before the sending request completes
MPI.Request.Wait(self.requests[source])
if self.excess_work():
self.sendvals[source] = self.split_queue()
print("rank {:3d} satisfying work request from {}".format(self.rank, source))
self.requests[source] = self.comm.issend(self.sendvals[source],
dest=source,
tag=self.tags['work_reply'])
else:
i_denied.add(source)
self.requests[source] = self.comm.issend(None,
dest=source,
tag=self.tags['work_deny'])
# complete the receive (empty message)
self.comm.recv(source=source, tag=self.tags['work_request'])
# work deny?
if self.comm.iprobe(source=MPI.ANY_SOURCE, tag=self.tags['work_deny'], status=status):
# probe the source and tag before receiving
source = status.Get_source()
srcs.add(source)
recv_cnt += 1
denied_me.add(source)
# complete the receive (empty message)
self.comm.recv(source=source, tag=self.tags['work_deny'])
# Do I need more work?
if self.need_work() and not i_requested_work:
stealfrom = self.next_steal()
print("rank {:3d} requesing work from {:3d}".format(self.rank, stealfrom))
# abuse requests[self.rank]
MPI.Request.Wait(self.requests[self.rank])
self.requests[self.rank] = self.comm.issend(None, dest=stealfrom, tag=self.tags['work_request'])
i_requested_work = True
# ibarrier bits
if not barrier:
# activate barrier when all my sends complete
if MPI.Request.Testall(self.requests):
barrier = self.comm.Ibarrier()
# otherwise see if ibarrier completed
else: done = MPI.Request.Test(barrier)
# done with NBC, get current size
my_size[0] = len(self.queue)
self.comm.Allreduce(my_size, gloabl_size)
# complete
tstop = MPI.Wtime()
max_steps = self.comm.allreduce(recv_loop, MPI.MAX)
# idx, flag, msg = MPI.Request.testany(self.requests)
# print(idx, flag)
# assert idx == MPI.UNDEFINED
# assert flag
# print end message
for p in range(0,self.nranks):
self.comm.Barrier()
sys.stdout.flush()
if p == self.rank and (recv_cnt or self.i_am_root):
if self.i_am_root:
print("-"*80)
print("{} outer loops completed".format(outer_loop))
print("Completed in {:4f} seconds on {} ranks in max {} steps".format(tstop-tstart,
self.nranks,
max_steps))
print("-"*80)
print("-r-> rank {:3d} received {:3d} messages from {:3d} ranks in {:5d} steps from {}".format(self.rank,
recv_cnt,
len(srcs),
recv_loop,
np.array(list(srcs))))
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def run_serial_task(self):
return
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def run(self):
#self.process()
self.comm.Barrier()
sys.stdout.flush()
self.execute()
return
################################################################################
if __name__ == "__main__":
wt = WorkThief()
wt.run()
wt.summary()