-
Notifications
You must be signed in to change notification settings - Fork 0
/
creek_blast_schedule.py
337 lines (277 loc) · 12.9 KB
/
creek_blast_schedule.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import copy
import math
import networkx as nx
from NetConfig import *
#the number of requests that can be supported by a sender port and a receiver port
SenderCapacity = [CAPACITY_PER_PORT/CAPACITY_SERVER_TO_RACK for i in range(PORTNUM)]
Receiverapacity = [CAPACITY_PER_PORT/CAPACITY_SERVER_TO_RACK for i in range(PORTNUM)]
class CP2MPC:
def __init__(self, src, sinks):
self.src = src
self.sinks = sinks
#Greedy_BLAST: greedy, no preemption
def BLAST(RequestList):
reconfig_delta = 0.1
reconfiguration_time = True
resultWriter = open("completion_time_blast_noslot.txt", "w")
resultWriter.writelines("release_time, completion_time, duration\n")
request_read_pos = 0
RequestList_unstart = [] #store requests that arrive but have yet finished
current_time = 0
reject_reqnum = 0
unprocessed_requestnum = len(RequestList)
while unprocessed_requestnum > 0:
SenderPortCapacity = [MAXREQUESTNUM_PER_PORT for i in range(PORTNUM)]
ReceiverPortCapacity = [MAXREQUESTNUM_PER_PORT for i in range(PORTNUM)]
max_time_duration = 0
#read requests from file
while request_read_pos < len(RequestList) and RequestList[request_read_pos].release_time <= current_time:
RequestList_unstart.append(RequestList[request_read_pos])
request_read_pos += 1
#sort RequestList_unstart
RequestList_unstart.sort(key=lambda x: x.score, reverse=True)
#check requests from the highest score
for request in RequestList_unstart:
_schedule = True
#fanout constraint
if len(request.sinks) > FANOUT_PER_PORT:
_schedule = False
reject_reqnum += 1
RequestList_unstart.remove(request)
unprocessed_requestnum -= 1
continue
#capacity constraint
if SenderPortCapacity[request.src] < 1:
_schedule = False
continue
for sink in request.sinks:
if ReceiverPortCapacity[sink] < 1:
_schedule = False
break
if _schedule == False:
continue
#if the capacity is enough to schedule a request, update remaining capacity
if _schedule == True:
unprocessed_requestnum -= 1
_completion_time = current_time + float(request.size)/CAPACITY_SERVER_TO_RACK
_completion_time_duration = _completion_time - request.release_time
resultWriter.writelines("%f %f %f\n" %(request.release_time, _completion_time, _completion_time_duration))
#update remaining capacity
SenderPortCapacity[request.src] -= 1
for sink in request.sinks:
ReceiverPortCapacity[sink] -= 1
#remove request form RequestList_unstart
RequestList_unstart.remove(request)
if max_time_duration < float(request.size)/CAPACITY_SERVER_TO_RACK:
max_time_duration = float(request.size)/CAPACITY_SERVER_TO_RACK
current_time += max_time_duration
if reconfiguration_time:
current_time += reconfig_delta
resultWriter.close()
def Blast_Preemption(SIMULATE_TIME, RequestList):
resultWriter = open("completion_time_preemption.txt", "w")
resultWriter.writelines("release_time, completion_time, duration\n")
request_read_pos = 0
RequestList_processing = [] #store requests that arrive but have yet finished
current_time = 0
PortCapacity = [[MAXREQUESTNUM_PER_PORT for t in range(BIG_SIMULATE_SLOT)] for i in range(PORTNUM)]
while current_time < SIMULATE_TIME or len(RequestList_processing) > 0:
#read requests from file
if current_time < SIMULATE_TIME:
while request_read_pos < len(RequestList) and RequestList[request_read_pos].release_time == current_time:
RequestList_processing.append(RequestList[request_read_pos])
request_read_pos += 1
# sort RequestList_unstart
#RequestList_processing.sort(key=lambda x: x.score, reverse=True)
current_slot = current_time/SLOT_DURATION
#schedule multicast requests to fill up the rack capacity
for request in RequestList_processing:
_schedule = True
#fanout constraint
if len(request.sinks) > FANOUT_PER_PORT:
_schedule = False
RequestList_processing.remove(request)
continue
#capacity constraint
if PortCapacity[request.src][current_slot] < 1:
_schedule = False
continue
for sink in request.sinks:
if PortCapacity[sink][current_slot] < 1:
_schedule = False
break
#if the capacity is enough to schedule a request, update remaining capacity
if _schedule == True:
request.size = max(0, request.size - CAPACITY_SERVER_TO_RACK*SLOT_DURATION)
if request.size <= 0:
RequestList_processing.remove(request)
completion_time = (current_slot+1)*SLOT_DURATION
resultWriter.writelines("%d %d %d\n" %(request.release_time, completion_time, completion_time-request.release_time))
#update remaining capacity
PortCapacity[request.src][current_slot] -= 1
for sink in request.sinks:
PortCapacity[sink][current_slot] -= 1
current_time += SLOT_DURATION
resultWriter.close()
def Creek(SIMULATE_TIME, RequestList):
resultWriter = open("completion_time_creek.txt", "w")
resultWriter.writelines("release_time, completion_time, duration\n")
request_read_pos = 0
RequestList_processing = [] #store requests that arrive but have yet finished
current_time = 0
PortCapacity = [[MAXREQUESTNUM_PER_PORT for t in range(BIG_SIMULATE_SLOT)] for i in range(PORTNUM)]
epoch_t = 0
while current_time < SIMULATE_TIME or len(RequestList_processing) > 0:
# read requests from file
if current_time < SIMULATE_TIME:
while request_read_pos < len(RequestList) and RequestList[request_read_pos].release_time == current_time:
RequestList_processing.append(RequestList[request_read_pos])
request_read_pos += 1
#create multicasting, serving requests
P2MPCListOfRequest, ServeRequestList, AllP2MPCList = CreateEpochSchedule(RequestList_processing)
epoch_t = DecideEpochDuration(ServeRequestList)
resultWriter.close()
def DecideEpochDuration(ServeRequestList):
ServeRequestList.sort(key=lambda x: x.size)
epoch_duration = ServeRequestList[0].size/CAPACITY_SERVER_TO_RACK
return epoch_duration
def CreateEpochSchedule(RequestList):
RequestList.sort(key=lambda x: x.size) # increasing order
P2MPCListOfRequest = [[] for r in range(len(RequestList))]
ServeRequestList = []
AllP2MPCList = {}
forward_graph = nx.DiGraph() #HyperGraph or multicast_graph
for reqindex, request in RequestList:
P2MPCList_use, forward_graph, AllP2MPCList = SolveConflict(forward_graph, request, copy.deepcopy(AllP2MPCList), False)
if P2MPCList_use:
#determine to serve demand request, so the capacity needs to update
for sender_key in P2MPCList_use:
SenderCapacity[sender_key] -= 1
for sink_value in P2MPCList_use[sender_key]:
Receiverapacity[sink_value] -= 1
ServeRequestList.append(request)
P2MPCListOfRequest[reqindex].append(P2MPCList_use)
for reqindex, request in RequestList:
if request not in ServeRequestList:
P2MPCList_use, forward_graph, AllP2MPCList = SolveConflict(forward_graph, request, copy.deepcopy(AllP2MPCList), True)
if P2MPCList_use:
#determine to serve demand request, so the capacity needs to update
for sender_key in P2MPCList_use:
SenderCapacity[sender_key] -= 1
for sink_value in P2MPCList_use[sender_key]:
Receiverapacity[sink_value] -= 1
ServeRequestList.append(request)
P2MPCListOfRequest[reqindex].append(P2MPCList_use)
#convert to C and R
#C is the P2MPCList, R is the served request list?
return P2MPCListOfRequest, ServeRequestList, AllP2MPCList
def SolveConflict(forward_graph, request, P2MPCList, loopfree):
src_port = request.src
sink_ports = request.sinks
sinks_hasinport = []
sinks_hasnoinport = []
for d in sink_ports:
if forward_graph.in_degree(d) == 1:
sinks_hasinport.append(d)
else:
sinks_hasnoinport.append(d)
rootportList = []
#the src port already has P2MPC, then extend current P2MPC to include all the receiver ports
if src_port in P2MPCList.keys():
for sink_port in sinks_hasinport:
#if forward_graph.has_edge(src_port, sink_port):
#check whether the src and the sink are connected
if nx.has_path(forward_graph, src_port, sink_port):
if hasfreeCapacity(forward_graph, src_port, sink_port) == False:
return None
else:
root_port = rootAncestor(forward_graph, sink_port)
if root_port == None or hasfreeCapacity(forward_graph, root_port, sink_port) == False:
return None
rootportList.append(root_port)
##todo: write funtion
#note: fanout limit
portnum_tobeconnected = len(P2MPCList[src_port]) + len(rootportList) + len(sinks_hasnoinport)
if portnum_tobeconnected > FANOUT_PER_PORT:
return None
extend_P2MPC, forward_graph = extendP2MPC(P2MPCList[src_port], src_port, rootportList, sinks_hasnoinport, forward_graph)
P2MPCList[src_port] = extend_P2MPC
else:
for sink_port in sinks_hasinport:
root_port = rootAncestor(forward_graph, sink_port)
if hasfreeCapacity(forward_graph, root_port, sink_port) == False:
return None
rootportList.append(root_port)
portnum_tobeconnected = len(P2MPCList[src_port]) + len(rootportList) + len(sinks_hasnoinport)
if portnum_tobeconnected > FANOUT_PER_PORT:
return None
new_P2MPC, forward_graph = addP2MPC(src_port, rootportList, sinks_hasnoinport, forward_graph)
if new_P2MPC == None:
return None
P2MPCList[src_port] = new_P2MPC #add new P2MPC
if loopfree and nx.simple_cycles(forward_graph):
return None
return getP2MPCList(src_port, sink_ports, P2MPCList, forward_graph), forward_graph, P2MPCList
def hasfreeCapacity(graph, src, sink):
path = nx.shortest_path(graph, src, sink)
for i in range(len(path)-1):
sender_port = path[i]
receiver_port = path[i+1]
if SenderCapacity[sender_port] <= 0:
return False
if Receiverapacity[receiver_port] <= 0:
return False
i += 1
return True
#root may be the multi-hop far away from the sink
def rootAncestor(graph, sink):
root = sink
while graph.predecessors(root):
root = graph.predecessors(sink)
if root == sink:
return None
return root
#add nodes in roots and sinks to P2MPC,
#add links between the sender of P2MPC, and every node in roots and sinks
def extendP2MPC(P2MPC, sender, roots, sinks, graph):
P2MPC.append(roots)
P2MPC.append(sinks)
graph.add_nodes_from(roots)
graph.add_nodes_from(sinks)
for v in roots:
graph.add_edge(sender, v)
for v in sinks:
graph.add_edge(sender, v)
return P2MPC, graph
def addP2MPC(sender, roots, sinks, graph):
new_P2MPC = []
new_P2MPC.append(roots)
new_P2MPC.append(sinks)
graph.add_nodes_from(roots)
graph.add_nodes_from(sinks)
for v in roots:
graph.add_edge(sender, v)
for v in sinks:
graph.add_edge(sender, v)
return new_P2MPC, graph
def getP2MPCList(sender, sinks, P2MPCList, graph):
traverse_P2MPCList = {}
sendersofP2MPC = []
sendersofP2MPC.append(sender)
for sink in sinks:
successor_port = sink
while graph.predecessors(successor_port) and graph.predecessors(successor_port)[0] != sender:
successor_port = graph.predecessors(successor_port)[0]
sendersofP2MPC.append(successor_port)
new_sendersofP2MPC = list(set(sendersofP2MPC))
for s in new_sendersofP2MPC:
if s not in traverse_P2MPCList.keys():
traverse_P2MPCList[s] = graph.successors(s)
#check whether we get the same P2MPC from the graph
print "\n s, traverse_P2MPCList[s]", s, traverse_P2MPCList[s]
print "s, P2MPCList[s]", s, P2MPCList[s]
return traverse_P2MPCList