-
Notifications
You must be signed in to change notification settings - Fork 1
/
topologies.py
295 lines (218 loc) · 11 KB
/
topologies.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
import networkx as nx
import random
from tqdm import tqdm
from matplotlib.pyplot import plt
from functions import *
class FatTreeTopology:
def __init__(self, n):
self.n = n
self.graph = self.create_topology()
self.servers = [node for node in self.graph.nodes if node.startswith("server")]
self.switches = [node for node in self.graph.nodes if not node.startswith("switch")]
def create_topology(self):
num_core_switches = (self.n // 2) ** 2 #
num_agg_switches = num_edge_switches = self.n * (self.n // 2) #
num_switches = num_core_switches + num_agg_switches + num_edge_switches
num_servers = num_edge_switches * (self.n // 2)
# Create the graph object
graph = nx.Graph()
# Add switches and servers to the graph
for i in range(num_switches):
if i < num_core_switches:
graph.add_node(f"core_{i}", level=0)
elif i < num_core_switches + num_agg_switches:
graph.add_node(f"agg_{i - num_core_switches}", level=1)
else:
graph.add_node(f"edge_{i - num_core_switches - num_agg_switches}", level=2)
for i in range(num_servers):
graph.add_node(f"server_{i}")
# Add linns between switches and servers
for i in range(num_edge_switches): ## for each edge switch
for j in range(self.n // 2): ## for each port of the edge switch
server_id = i * (self.n // 2) + j
graph.add_edge(f"edge_{i}", f"server_{server_id}")
cost = 0
for i in range(num_agg_switches):
if (i % (self.n//2)) == 0 and (i != 0):
cost = 0
for j in range(self.n//2):
core_id = j + cost
graph.add_edge(f"core_{core_id}", f"agg_{i}")
cost += self.n//2
cost = 0
# Add links between aggregate and edge switches
for i in range(num_agg_switches):
if (i % (self.n // 2)) == 0 and (i!= 0):
cost += self.n // 2
for j in range(self.n // 2):
edge_id = j + cost
graph.add_edge(f"agg_{i}", f"edge_{edge_id}")
return graph
def get_servers(self):
return self.servers
def get_switches(self):
return self.switches
def __str__(self):
return f"Fat-Tree topology (n={self.n})"
def get_closest_nodes(self, server_node, N):
"""
Returns the top N closest neighboring nodes to a specific server node in terms of number of hops.
"""
# Check if server_node is a valid server node in the topology
if server_node not in self.get_servers():
raise ValueError("Invalid server node provided")
shortest_paths = {}
# Get the shortest paths from the server node to all other nodes in the topology
for server in self.get_servers():
shortest_paths[server] = nx.shortest_path_length(self.graph, source=server_node, target = server)
closest_nodes = dict(sorted(shortest_paths.items(), key=lambda x:x[1]))
# Sort the nodes by shortest path length and return the top N closest neighboring nodes
closest_nodes = dict(list(closest_nodes.items())[1:(N+1)])
return closest_nodes
def draw(self):
# Create a dictionary to map node types to colors
node_colors = {
"core": "red",
"agg": "blue",
"edge": "green",
"server": "purple"
}
# Create a list of node colors for each node in the graph
colors = [node_colors[node.split("_")[0]] for node in self.graph.nodes]
# Set the positions of the nodes
pos = nx.spring_layout(self.graph, seed=42)
# Draw the graph with specified node colors and positions
nx.draw(self.graph, pos, node_color=colors, with_labels=True)
# Create a legend for the node colors
legend_elements = [
plt.Line2D([0], [0], marker="o", color="w", label="Core Switch", markersize=10, markerfacecolor="red"),
plt.Line2D([0], [0], marker="o", color="w", label="Aggregate Switch", markersize=10, markerfacecolor="blue"),
plt.Line2D([0], [0], marker="o", color="w", label="Edge Switch", markersize=10, markerfacecolor="green"),
plt.Line2D([0], [0], marker="o", color="w", label="Server", markersize=10, markerfacecolor="purple")
]
plt.legend(handles=legend_elements, loc="upper left")
# Display the graph
plt.show()
# Jellyfish implementation from scratch but it is really slow
class SlowJellyfishTopology:
def __init__(self, s, n, r):
self.s = s # number of switches
self.n = n # number of ports per switch
self.r = r # number of ports per switch connected to other switches
self.graph = self.create_topology()
def create_topology(self):
# Create the graph object
graph = nx.Graph()
# Create a list to keep track of the free ports on each switch
free_ports = {f"switch_{i}": self.n for i in range(self.s)}
# Create a list of all switch nodes
switches = [f"switch_{i}" for i in range(self.s)]
# Connect switches randomly until no further links can be added
while True:
# Get a list of switch pairs with free ports and not already connected
switch_pairs = [(i, j) for i in switches for j in switches
if (i != j) and (free_ports[i] > self.r) and (free_ports[j] > self.r) and (not graph.has_edge(i, j))]
# If there are no switch pairs available, exit the loop
if not switch_pairs:
break
# Choose a random switch pair
i, j = random.choice(switch_pairs)
# Connect the switches
graph.add_edge(i, j)
# Reduce the number of free ports on the connected switches
free_ports[i] -= 1
free_ports[j] -= 1
while True:
count = 0
for v in tqdm(free_ports):
switch_links = [(i, j) for i in switches for j in switches if (i != j) and graph.has_edge(i, j)]
if free_ports[v] < 2:
count += 1
elif free_ports[v] >= 2:
i, j = random.choice(switch_links)
while (i == v or j== v ):
i, j = random.choice(switch_links)
graph.remove_edge(i,j)
graph.add_edge(v,i)
graph.add_edge(v,j)
free_ports[v] -= 2
if count == len(free_ports):
print('sono qui')
break
# Add servers to the graph
for i in range(self.s):
switch = f"switch_{i}"
for j in range(self.n - self.r):
server = f"{switch}:{j}"
graph.add_node(server)
graph.add_edge(switch, server)
return graph
def get_closest_nodes(self, server_node, N):
"""Returns the top N closest neighboring nodes to a specific server node in terms of number of hops.
"""
# Check if server_node is a valid server node in the topology
if server_node not in self.get_servers():
raise ValueError("Invalid server node provided")
shortest_paths = {}
# Get the shortest paths from the server node to all other nodes in the topology
for server in self.get_servers():
shortest_paths[server] = nx.shortest_path_length(self.graph, source=server_node, target = server)
closest_nodes = dict(sorted(shortest_paths.items(), key=lambda x:x[1]))
# Sort the nodes by shortest path length and return the top N closest neighboring nodes
closest_nodes = dict(list(closest_nodes.items())[1:(N+1)])
return closest_nodes
# fast implementation if the Jellyfish with r-regular random graph
class JellyfishTopology:
def __init__(self, s, n, r):
self.n = n # number of ports per switch
self.s = s # number of switches
self.r = r # number of ports per switch connected to other switches
self.graph = self.create_topology()
def create_topology(self):
graph = create_graph(k=self.s, typ="r",r=self.r)
labels = []
nx.set_node_attributes(graph, labels, "labels")
labels.append("switch")
switches = list(graph.nodes())
for i in range(len(switches)):
for j in range(self.n-self.r):
graph.add_node(f"server{i}_{j}")
graph.add_edge(switches[i], f'server{i}_{j}')
return graph
def get_servers(self):
return [i for i in list(self.graph.nodes()) if not isinstance(i, int)]
def get_closest_nodes(self, server_node, N):
"""
Returns the top N closest neighboring nodes to a specific server node in terms of number of hops.
"""
# Check if server_node is a valid server node in the topology
if server_node not in self.get_servers():
raise ValueError("Invalid server node provided")
shortest_paths = {}
# Get the shortest paths from the server node to all other nodes in the topology
for server in self.get_servers():
shortest_paths[server] = nx.shortest_path_length(self.graph, source=server_node, target = server)
closest_nodes = dict(sorted(shortest_paths.items(), key=lambda x:x[1]))
# Sort the nodes by shortest path length and return the top N closest neighboring nodes
closest_nodes = dict(list(closest_nodes.items())[1:(N+1)])
return closest_nodes
def draw(self):
# Create a dictionary to map node types to colors
node_colors = {
"switch": "blue",
"server": "purple"
}
# Create a list of node colors for each node in the graph
colors = [node_colors["switch"] if isinstance(node, int) else node_colors["server"] for node in list(self.graph.nodes)]
# Set the positions of the nodes
pos = nx.spring_layout(self.graph, seed=42)
# Draw the graph with specified node colors and positions
nx.draw(self.graph, pos, node_color=colors, with_labels=True)
# Create a legend for the node colors
legend_elements = [
plt.Line2D([0], [0], marker="o", color="w", label="Switch", markersize=10, markerfacecolor="blue"),
plt.Line2D([0], [0], marker="o", color="w", label="Server", markersize=10, markerfacecolor="purple")
]
plt.legend(handles=legend_elements, loc="upper left")
# Display the graph
plt.show()