-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjellyfish.py
More file actions
342 lines (300 loc) · 8.05 KB
/
jellyfish.py
File metadata and controls
342 lines (300 loc) · 8.05 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
import numpy as np
import networkx as nx
import random
import math
import matplotlib.pyplot as plt
'''
Construct Jellyfish topology with homogeneity:
All the switches has same number of ports.
All the switches connect to same number of servers.
n : number of switches
k : number of ports per switch
r : number of ports used to connect to other switches
s : seed to generate random number
Reference : "Jellyfish: Networking Data Centers Randomly"
'''
def construct1(n,k,r,s):
numsw = n
numsvr = n * (k-r)
N = numsvr + numsw
numhash = 10
G = nx.Graph()
for i in range(numsvr): # add edges between servers and switches
svr = i
sw = numsvr + i/(k-r)
G.add_edge(svr,sw)
openPorts = [r for i in range(numsw)]
switches_left = numsw
consecFails = 0 # If there are ten consecutive fails then we think the constrution period is finished
random.seed(s)
while switches_left > 1 and consecFails < 10:
s1 = random.randrange(numsw)
while openPorts[s1] == 0:
s1 = random.randrange(numsw)
s2 = random.randrange(numsw)
while openPorts[s2] == 0 or s2==s1:
s2 = random.randrange(numsw)
if(G.has_edge(s1+numsvr,s2+numsvr)):
consecFails += 1
else:
consecFails = 0
G.add_edge(s1+numsvr,s2+numsvr)
openPorts[s1] -= 1
openPorts[s2] -= 1
if(openPorts[s1]==0):
switches_left -= 1
if(openPorts[s2]==0):
switches_left -= 1
if switches_left > 0:
p1 = numsvr
p2 = numsvr
for i in range(numsw):
if openPorts[i] >= 1:
p1 = i + numsvr
openPorts[i] -= 1
break
for i in range(numsw):
if openPorts[i] >= 1:
p2 = i + numsvr
openPorts[i] -= 1
break
while True:
rLink = random.choice(list(set(G.edges())-set(range(numsvr))))
if p1==rLink[0] or p1==rLink[1]:
continue
if p2==rLink[0] or p2==rLink[1]:
continue
G.remove_edge(rLink[0],rLink[1])
G.add_edge(p1,rLink[0])
G.add_edge(p2,rLink[1])
break
#draw_graph1(G)
Adj = np.zeros((N,N),dtype=int)
N2L = np.zeros((N,N),dtype=int)
idx = 0
for e in G.edges():
Adj[e[0]][e[1]] = 1
Adj[e[1]][e[0]] = 1
N2L[e[0]][e[1]] = idx
N2L[e[1]][e[0]] = idx + 1
idx = idx + 2
numlink = np.sum(Adj) / 2
C = np.zeros((2 * numlink, 1))
idx = 0
for i in range(numsvr):
C[idx] = 1
C[idx + 1] = 1
idx += 2
for j in range(numsvr, numlink):
C[idx] = 0.01
C[idx + 1] = 0.01
idx += 2
numvlan = 4
VLAN = [np.zeros((N,N)) for v in range(numvlan)]
for v in range(numvlan):
root = random.randrange(numsw) + numsvr
g = generate_tree(root,G)
for e in g.edges():
VLAN[v][e[0]][e[1]] = 1
VLAN[v][e[1]][e[0]] = 1
length = numhash * numsvr**2
stride = numsvr/numhash
tm = np.zeros((length,1))
for i in range(length):
index = i % numsvr**2 # hash class
hash_id = i / numsvr**2
src = index/numsvr
des = index%numsvr
if(src!=des):
if src/stride==hash_id:
tm[i] = 0.01
shortest_paths = [[] for i in range(numsvr**2)]
for i in range(numsvr):
for j in range(numsvr):
paths_iter = nx.all_shortest_paths(G,i,j)
paths = []
for x in paths_iter:
paths.append(x)
shortest_paths[i*numsvr+j] = paths
np.savez("topo.npz",numsvr,numsw,N,numlink,Adj,N2L,C,tm,shortest_paths,numvlan,VLAN,numhash,k,r)
'''
Construct Jellyfish topology with heterogeneity:
Each switch has its own number of port.
Each switch has its own number of port used to connect to other switches.
n : number of switches
k : list of port number of every switch
r : list of number of ports used to connect to other switches
s : seed to generate random number
'''
def construct2(n,k,r,s):
numsw = n
numsvr = 0
for i in range(n):
numsvr += k-r[i]
N = numsvr + numsw
numhash = 8
G = nx.Graph()
for i in range(numsvr):# add edges between servers and switches
svr = i
sw = numsvr + i/(k-r[i])
G.add_edge(svr,sw)
openPorts = r+[]
switches_left = numsw
consecFails = 0
random.seed(s)
while switches_left > 1 and consecFails < 10:
s1 = random.randrange(numsw)
while openPorts[s1] == 0:
s1 = random.randrange(numsw)
s2 = random.randrange(numsw)
while openPorts[s2] == 0 or s2==s1:
s2 = random.randrange(numsw)
if(G.has_edge(s1+numsvr,s2+numsvr)):
consecFails += 1
else:
consecFails = 0
G.add_edge(s1+numsvr,s2+numsvr)
openPorts[s1] -= 1
openPorts[s2] -= 1
if(openPorts[s1]==0):
switches_left -= 1
if(openPorts[s2]==0):
switches_left -= 1
if switches_left > 0:
p1 = numsvr
p2 = numsvr
for i in range(numsw):
if openPorts[i] >= 1:
p1 = i + numsvr
openPorts[i] -= 1
break
for i in range(numsw):
if openPorts[i] >= 1:
p2 = i + numsvr
openPorts[i] -= 1
break
while True:
rLink = random.choice(list(set(G.edges())-set(range(numsvr))))
if p1==rLink[0] or p1==rLink[1]:
continue
if p2==rLink[0] or p2==rLink[1]:
continue
G.remove_edge(rLink[0],rLink[1])
G.add_edge(p1,rLink[0])
G.add_edge(p2,rLink[1])
break
#draw_graph(G)
Adj = np.zeros((N,N),dtype=int)
N2L = np.zeros((N,N),dtype=int)
idx = 0
for e in G.edges():
Adj[e[0]][e[1]] = 1
Adj[e[1]][e[0]] = 1
N2L[e[0]][e[1]] = idx
N2L[e[1]][e[0]] = idx + 1
idx = idx + 2
numlink = np.sum(Adj) / 2
C = np.zeros((2 * numlink, 1))
idx = 0
for i in range(numsvr):
C[idx] = 1
C[idx + 1] = 1
idx += 2
for j in range(numsvr, numlink):
C[idx] = 0.01
C[idx + 1] = 0.01
idx += 2
numvlan = 4
root = [32,33,34,35]
VLAN = [np.zeros((N,N)) for v in range(numvlan)]
for v in range(numvlan):
g = generate_tree(root[v],G)
#draw_graph(g,root[v])
for e in g.edges():
VLAN[v][e[0]][e[1]] = 1
VLAN[v][e[1]][e[0]] = 1
length = numhash * numsvr**2
stride = numsvr/numhash
tm = np.zeros((length,1))
for i in range(length):
index = i % numsvr**2
hash_id = i / numsvr**2
src = index/numsvr
des = index%numsvr
if(src!=des):
if src/stride==hash_id:
tm[i] = 0.01
shortest_paths = [[] for i in range(numsvr**2)]
for i in range(numsvr):
for j in range(numsvr):
paths_iter = nx.all_shortest_paths(G,i,j)
paths = []
for x in paths_iter:
paths.append(x)
shortest_paths[i*numsvr+j] = paths
np.savez("topo.npz",numsvr,numsw,N,numlink,Adj,N2L,C,tm,shortest_paths,numvlan,VLAN,numhash,k,r)
def generate_tree(root,G):
nodes_now = [root]
visited = {}
for n in G.nodes():
visited[n] = False
visited[root] = True
g = nx.Graph()
while len(nodes_now)!=0:
n1 = nodes_now[0]
del nodes_now[0]
for n2 in G.neighbors(n1):
if(not visited[n2]):
nodes_now.append(n2)
g.add_edge(n1,n2)
visited[n2] = True
return g
def draw_graph1(G):
deg = 360/20
pos = {}
for i in range(20):
pos[i] = (10 * math.sin(math.radians(i*deg)),10 * math.cos(math.radians(i*deg)))
pos[i+20] = (7 * math.sin(math.radians(i*deg)),7 * math.cos(math.radians(i*deg)))
color = []
size = []
for i in range(20):
color.append('yellow')
size.append(200)
for i in range(20):
color.append('red')
size.append(300)
nx.draw_networkx(G,pos,node_size=size,node_color=color)
plt.show()
def draw_graph2(G,rot=None):
deg = 360/20
degree_svr = [i*deg for i in range(16)]
degree_sw = [i*deg for i in range(20)]
pos = {}
for i in range(16):
pos[i] = (10 * math.sin(math.radians(degree_svr[i])),10 * math.cos(math.radians(degree_svr[i])))
for i in range(20):
pos[i+16] = (7 * math.sin(math.radians(degree_sw[i])),7 * math.cos(math.radians(degree_sw[i])))
node_size = []
node_color = []
for i in range(16):
node_size.append(400)
node_color.append('green')
for i in range(16):
node_size.append(500)
node_color.append('red')
for i in range(4):
node_size.append(500)
node_color.append('blue')
if(rot):
node_color[rot] = 'yellow'
nx.draw_networkx(G,pos,node_size=node_size,node_color=node_color)
plt.show()
if __name__ == '__main__':
construct1(20,4,3,0)
# n = 20
# k = 4
# r = [3 for i in range(16)]
# for i in range(4):
# r.append(4)
# s = 0
# construct2(n,k,r,s)