-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
464 lines (370 loc) · 13.6 KB
/
Copy pathnode.py
File metadata and controls
464 lines (370 loc) · 13.6 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
'''
Created on Apr 18, 2016
@author: group09
'''
import socket
import threading
import argparse
import json
import re
import select
import sys
import time
import signal
import messaging
def signal_handler(signal, frame):
print 'You pressed Ctrl+C!'
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
SIZE = 1024 * 1024 * 4 # 4194304
num_jobs = 512
# SIZE = 20
# num_jobs = 4
a = [0.0] * SIZE
# multiplier constant
# this is a blind guess
k = 4
# only do a balance transfer if there's a 5 job imbalance
LOAD_BALANCE_THRESHOLD = 5
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--remote", help="Add this flag if this is the remote node", action="store_true")
# maybe later
parser.add_argument("remote_port", help="Port for client to connect to remote node, or listen port on remote node", type=int)
args = parser.parse_args()
def job_slice(left_index, right_index):
return ((left_index, right_index), a[left_index:right_index])
def send_some_jobs(sock, jobs, finished = False):
data = "#finished#" if finished else '#jobs#'
data += json.dumps(jobs)
messaging.send_msg(sock, data)
# print "sent %d bytes" % len(data)
def receive_data(sock):
# data = sock.recv(512) # TODO, pick a smarter packet size
# packr = re.match(r"#([a-z]+)#([0-9]+)#(.*)", data)
# type = packr.group(1)
# length = int(packr.group(2))
# print "type: %s, length: %d" % (type, length)
# i = 0
# while len(data) < length:
# print "get another packet %d" % i
# packet = sock.recv(512)
# data += packet
# i += 1
# print "length received"
# print len(data)
# regex = re.match(r"#([a-z]+)#([0-9]+)#(.*)", data)
data = messaging.recv_msg(sock)
# print data
regex = re.match(r"#([a-z]+)#(.*)", data)
type = regex.group(1)
message = regex.group(2)
if type == "jobs":
return receive_some_jobs(message)
elif type == "finished":
return receive_finished_jobs(message)
elif type == "state":
return receive_state(message)
else:
pass
def receive_state(message):
print "receiving state"
pats = re.match(r"(.*)#(.*)#(.*)", message)
other_length = int(pats.group(1))
other_finished_length = int(pats.group(2))
other_load_policy = float(pats.group(3))
other_state = (other_length, other_finished_length, other_load_policy)
state_manager.handle_received_state(other_state)
return other_state
f = open("a", "w+")
def receive_some_jobs(message):
try:
jobs = json.loads(message)
job_queue_lock.acquire()
job_queue.extend(jobs)
job_queue_lock.release()
print "I got %d jobs from the client" % (len(jobs))
((first_l, first_r), last_arr) = jobs[0]
((last_l, last_r), last_arr) = jobs[-1]
print "First job is indices %d to %d\nLast job is indices %d to %d" % (first_l, first_r, last_l, last_r)
work_event.set()
return jobs
except Exception, e:
f.write(message)
f.write("\n")
print "written to file"
def receive_finished_jobs(message):
print "receiving finished jobs"
try:
jobs = json.loads(message)
finished_queue_lock.acquire()
finished_queue.extend(jobs)
finished_queue_lock.release()
print "Finished queue adds %d jobs from the client" % (len(jobs))
((first_l, first_r), last_arr) = jobs[0]
((last_l, last_r), last_arr) = jobs[-1]
print "First job is indices %d to %d\nLast job is indices %d to %d" % (first_l, first_r, last_l, last_r)
work_event.set()
return jobs
except Exception, e:
f.write(message)
f.write("\n")
print "written to file"
job_queue = []
finished_queue = []
job_queue_lock = threading.Lock()
finished_queue_lock = threading.Lock()
def remove_n_jobs(n):
# WARNING: NOT THREAD SAFE
global job_queue
to_transfer = job_queue[-n:]
job_queue = job_queue[:-n]
return to_transfer
def add_finished_job(job):
global finished_queue
finished_queue.append(job)
def remove_finished_jobs():
global finished_queue
new_finished_queue = finished_queue[:]
finished_queue = []
return new_finished_queue
work_event = threading.Event()
load_policy_enforcer_event = threading.Event()
load_policy_enforcer_event.set()
if not args.remote:
print "I am local node"
print "remote port %d" % (args.remote_port)
comm_sock = socket.socket()
comm_sock.connect(("", args.remote_port))
# print "connected"
# response = comm_sock.recv(1024)
# print response
# comm_sock.send("Hi!222")
for i in range(num_jobs):
job_size = (SIZE / num_jobs)
left_index = i * job_size
right_index = left_index + job_size
# print (i, (left_index,right_index))
job_queue.append(job_slice(left_index, right_index))
# print job_queue
half = num_jobs / 2
send_jobs = job_queue[half:]
job_queue = job_queue[:half]
# print "sending"
send_some_jobs(comm_sock, send_jobs)
print "should have sent"
else:
print "I am remote node"
print "listening on port %d" % (args.remote_port)
temp_socket = socket.socket()
temp_socket.bind(("", args.remote_port))
temp_socket.listen(20)
# print "block until accept"
(comm_sock, addr_info) = temp_socket.accept()
# print "after accept"
# bootstrap phase
jobs = receive_data(comm_sock)
# print "after recv"
# print next
class StateManager(threading.Thread):
def __init__(self, sock):
'''
Constructor
'''
threading.Thread.__init__(self)
self.running = True
self.sock = sock
def handle_received_state(self, (other_length, other_finished_length, other_load_policy)):
# print "i am being asked to handle other state"
# print "other_length", other_length, "other_lp", other_load_policy
job_queue_lock.acquire()
finished_queue_lock.acquire()
my_length = len(job_queue)
my_finished_length = len(finished_queue)
job_queue_lock.release()
finished_queue_lock.release()
my_load_policy = hardware_monitor.load_policy
bundle = (my_length, my_finished_length, my_load_policy, other_length, other_finished_length, other_load_policy)
# send bundle to adapter
adaptor_thread.request_calculation(bundle)
def run(self):
while (self.running):
time.sleep(30)
# data = "#%f#%d#" % (load_policy, len(job_queue))
# send_msg(self.sock, data)
hardware_monitor.send_state(self.sock)
class AdaptorThread(threading.Thread):
def __init__(self, sock, remote, job_queue):
'''
Constructor
'''
threading.Thread.__init__(self)
self.running = True
self.sock = sock
self.bundle = ()
self.wake = threading.Event()
self.remote = remote
self.job_queue = job_queue
def request_calculation(self, bundle):
self.bundle = bundle
self.wake.set()
def run(self):
while (self.running):
self.wake.wait()
# do some work
(my_length, my_finished_length, my_load_policy, other_length, other_finished_length, other_load_policy) = self.bundle
if (my_length == 0 and my_finished_length == 0 and other_length == 0 and other_finished_length == 0):
shutdown()
print "i'm performing a calculation on ", my_length, my_load_policy, other_length, other_load_policy
if not self.remote:
s = (k * other_load_policy * my_length - my_load_policy * other_length) / (k * other_load_policy + my_load_policy)
else:
s = (other_load_policy * my_length - k * my_load_policy * other_length) / (other_load_policy + k * my_load_policy)
if (s > LOAD_BALANCE_THRESHOLD):
print "i think we should transfer ", s, " jobs"
job_queue_lock.acquire()
count = int(s)
to_transfer = remove_n_jobs(count)
job_queue_lock.release()
transfer_manager.request_transfer(to_transfer)
self.jobs_to_send = ()
self.wake.clear()
# sole job is to enforce the work time policy
class HardwareMonitor(threading.Thread):
def __init__(self, load_policy):
'''
Constructor
'''
threading.Thread.__init__(self)
self.running = True
self.load_policy = 0.7
self.update_work_interval()
def send_state(self, sock):
job_queue_lock.acquire()
finished_queue_lock.acquire()
data = "#state#%d#%d#%f" % (len(job_queue),(len(finished_queue)), self.load_policy)
job_queue_lock.release()
finished_queue_lock.release()
messaging.send_msg(sock, data)
def set_load_policy(self, new_load_policy):
if new_load_policy < 1 and new_load_policy > 0:
self.load_policy = new_load_policy
print "New load policy is set to: ", self.load_policy
else:
print "The load policy is must be in range (0,1)!!!!"
self.update_work_interval()
def update_work_interval(self):
self.work_interval = self.load_policy * 100.0 / 1000.0
def run(self):
while (self.running):
time.sleep(self.work_interval) # after 70 ms
load_policy_enforcer_event.clear() # put the worker to sleep
time.sleep(0.1 - self.work_interval) # after 30 ms
load_policy_enforcer_event.set() # wake it up
# time.sleep(20) # after 70 ms
# load_policy_enforcer_event.clear() # put the worker to sleep
# print "putting worker to sleep"
# time.sleep(10) # after 30 ms
# load_policy_enforcer_event.set() # wake it up
# print "waking worker up"
class WorkerThread(threading.Thread):
def __init__(self, transfer_manager):
'''
Constructor
'''
threading.Thread.__init__(self)
self.running = True
self.transfer_manager = transfer_manager
@staticmethod
def run_job(job):
(_, array) = job
for i in range(len(array)):
for _ in range(200):
load_policy_enforcer_event.wait()
array[i] += 1.111111
def run(self):
while (self.running):
if len(job_queue) == 0:
work_event.clear()
print "done with jobs"
finished_queue_lock.acquire()
new_finished_queue = remove_finished_jobs()
finished_queue_lock.release()
if len(new_finished_queue) > 0:
if (not args.remote):
print "Applying ", len(new_finished_queue), " completed jobs"
for i in range(len(new_finished_queue)):
((first_l, first_r), last_arr) = new_finished_queue[i]
a[first_l:first_r] = last_arr
else:
print "sending", len(new_finished_queue), " jobs"
self.transfer_manager.request_transfer(new_finished_queue, True)
work_event.wait()
else:
job_queue_lock.acquire()
job = job_queue.pop()
job_queue_lock.release()
((left_index, right_index), array) = job
print "Popped new job with starting index: %d and end index: %d" %(left_index, right_index)
# WorkerThread.run_job(job)
(_, array) = job
for i in range(len(array)):
for _ in range(200):
load_policy_enforcer_event.wait()
array[i] += 1.111111
add_finished_job(job)
# a[left_index:right_index] = array
class TransferManager(threading.Thread):
def __init__(self, sock):
'''
Constructor
'''
threading.Thread.__init__(self)
self.running = True
self.sock = sock
self.wake = threading.Event()
self.jobs_to_send = []
def request_transfer(self, jobs, finished = False):
self.wake.set()
self.jobs_to_send = jobs
self.finished = finished
def run(self):
while (self.running):
self.wake.wait()
# do some work
send_some_jobs(self.sock, self.jobs_to_send, self.finished)
self.jobs_to_send = []
self.wake.clear()
running = True
adaptor_thread = AdaptorThread(comm_sock, args.remote, job_queue)
adaptor_thread.start()
hardware_monitor = HardwareMonitor(comm_sock)
hardware_monitor.start()
transfer_manager = TransferManager(comm_sock)
transfer_manager.start()
worker_thread = WorkerThread(transfer_manager)
worker_thread.start()
state_manager = StateManager(comm_sock)
state_manager.start()
read_fds = [sys.stdin, comm_sock]
def shutdown():
global running
running = False
worker_thread.running = False
adaptor_thread.running = False
hardware_monitor.running = False
transfer_manager.running = False
state_manager.running = False
work_event.set()
while running:
# block until we receive packets or keyboard read_fds
(senders, _, _) = select.select(read_fds, [], [])
for sender in senders:
# received packets
if sender == comm_sock:
receive_data(comm_sock)
# handle keyboard input
elif sender == sys.stdin:
thing = sys.stdin.readline().rstrip('\n')
new_load_policy = float(thing)
print "New load policy will be set to ", new_load_policy
hardware_monitor.set_load_policy(new_load_policy)