File size: 3,853 Bytes
5fed0fc |
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 |
# EVOLVE-BLOCK-START
import networkx as nx
import json
from typing import Dict, List
def search_algorithm(src, dsts, G, num_partitions):
h = G.copy()
h.remove_edges_from(list(h.in_edges(src)) + list(nx.selfloop_edges(h)))
bc_topology = BroadCastTopology(src, dsts, num_partitions)
for dst in dsts:
path = nx.dijkstra_path(h, src, dst, weight="cost")
for i in range(0, len(path) - 1):
s, t = path[i], path[i + 1]
for j in range(bc_topology.num_partitions):
bc_topology.append_dst_partition_path(dst, j, [s, t, G[s][t]])
return bc_topology
class SingleDstPath(Dict):
partition: int
edges: List[List] # [[src, dst, edge data]]
class BroadCastTopology:
def __init__(self, src: str, dsts: List[str], num_partitions: int = 4, paths: Dict[str, SingleDstPath] = None):
self.src = src # single str
self.dsts = dsts # list of strs
self.num_partitions = num_partitions
# dict(dst) --> dict(partition) --> list(nx.edges)
# example: {dst1: {partition1: [src->node1, node1->dst1], partition 2: [src->dst1]}}
if paths is not None:
self.paths = paths
self.set_graph()
else:
self.paths = {dst: {str(i): None for i in range(num_partitions)} for dst in dsts}
def get_paths(self):
print(f"now the set path is: {self.paths}")
return self.paths
def set_num_partitions(self, num_partitions: int):
self.num_partitions = num_partitions
def set_dst_partition_paths(self, dst: str, partition: int, paths: List[List]):
"""
Set paths for partition = partition to reach dst
"""
partition = str(partition)
self.paths[dst][partition] = paths
def append_dst_partition_path(self, dst: str, partition: int, path: List):
"""
Append path for partition = partition to reach dst
"""
partition = str(partition)
if self.paths[dst][partition] is None:
self.paths[dst][partition] = []
self.paths[dst][partition].append(path)
def make_nx_graph(cost_path=None, throughput_path=None, num_vms=1):
"""
Default graph with capacity constraints and cost info
nodes: regions, edges: links
per edge:
throughput: max tput achievable (gbps)
cost: $/GB
flow: actual flow (gbps), must be < throughput, default = 0
"""
if cost_path is None:
cost = pd.read_csv("profiles/cost.csv")
else:
cost = pd.read_csv(cost_path)
if throughput_path is None:
throughput = pd.read_csv("profiles/throughput.csv")
else:
throughput = pd.read_csv(throughput_path)
G = nx.DiGraph()
for _, row in throughput.iterrows():
if row["src_region"] == row["dst_region"]:
continue
G.add_edge(row["src_region"], row["dst_region"], cost=None, throughput=num_vms * row["throughput_sent"] / 1e9)
for _, row in cost.iterrows():
if row["src"] in G and row["dest"] in G[row["src"]]:
G[row["src"]][row["dest"]]["cost"] = row["cost"]
# some pairs not in the cost grid
no_cost_pairs = []
for edge in G.edges.data():
src, dst = edge[0], edge[1]
if edge[-1]["cost"] is None:
no_cost_pairs.append((src, dst))
print("Unable to get costs for: ", no_cost_pairs)
return G
# EVOLVE-BLOCK-END
# Helper functions that won't be evolved
def create_broadcast_topology(src: str, dsts: List[str], num_partitions: int = 4):
"""Create a broadcast topology instance"""
return BroadCastTopology(src, dsts, num_partitions)
def run_search_algorithm(src: str, dsts: List[str], G, num_partitions: int):
"""Run the search algorithm and return the topology"""
return search_algorithm(src, dsts, G, num_partitions)
|