-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcube.py
140 lines (125 loc) · 2.91 KB
/
bcube.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
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
'''
Construct BCube topology.
n : number of servers connect to one switch
k : number of layers(count from 0)
'''
def construct(n,k):
numlayer = k+1
numsvr = n**(k+1)
numsw_p = n**k
numsw = numlayer * numsw_p
N = numsvr + numsw
numflow = 8
G = nx.Graph()
for i1 in range(numsvr):
i2 = i1 + 1
while(i2<numsvr):
if(hammingDistance(i1,i2,n,k)==1):
commonsw = getCommonSwitch(i1,i2,n,k)
G.add_edge(i1,commonsw)
G.add_edge(i2,commonsw)
i2 = i2 + 1
#draw_graph(G,n,numsvr,numsw_p)
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
#print numlink
#print N2L
C = np.ones((2 * numlink, 1))
length = numflow * numsvr**2
tm = np.zeros((length,1))
for i in range(length):
index = i % numsvr**2
i1 = index/numsvr
i2 = index%numsvr
if(hammingDistance(i1,i2,n,k)==k+1):
tm[i] = 0.1
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
#print shortest_paths[7]
np.savez("topo.npz",numsvr,numsw,N,numlink,Adj,N2L,C,tm,shortest_paths)
def hammingDistance(x,y,n,k):
x_addr = getSvrAddr(x,n,k) + []
y_addr = getSvrAddr(y,n,k) + []
d = 0
for i in range(k+1):
if x_addr[i]!=y_addr[i]:
d = d + 1
return d
def getSvrAddr(x,n,k):
num = x
mid = [0 for i in range(k+1)]
i = k
while i>=0:
if num==0:
mid[i] = 0
break
num,rem = divmod(num,n)
mid[i] = rem
i = i - 1
return mid + []
def getSwitchAddr(x,n,k):
temp = x - n**(k+1)
l,num = divmod(temp,n**k)
mid = [0 for i in range(k)]
i = k - 1
while i>=0:
if num==0:
mid[i] = 0
break
num,rem = divmod(num,n)
mid[i] = rem
i = i - 1
mid.insert(0,l)
return mid + []
def switchAddr2ID(addr,n,k):
a = addr + []
ID = n**(k+1)
ID = ID + a[0] * n**k
i = 1
while(i<=k):
ID = ID + a[i]*n**(k-i)
i = i + 1
return ID
def getCommonSwitch(x,y,n,k):
x_addr = getSvrAddr(x,n,k)
y_addr = getSvrAddr(y,n,k)
for i in range(k+1):
if(x_addr[i]!=y_addr[i]):
l = k-i # common switch layer
temp = x_addr + []
del temp[k-l]
commonsw_addr = []
commonsw_addr.append(l)
commonsw_addr = commonsw_addr + temp
commonsw = switchAddr2ID(commonsw_addr,n,k)
return commonsw
def draw_graph(G,n,numsvr,numsw_p):
pos = {}
for i in range(numsvr):
pos[i] = (i,0)
for i in range(numsw_p):
pos[i+numsvr] = (i*n+n/2,2)
for i in range(numsw_p):
pos[i+numsw_p+numsvr] = (i*n+n/2,4)
#print pos
nx.draw_networkx(G,pos)
plt.show()
if __name__ == '__main__':
construct(4,1)