ruby: topology: refactor code.
This commit is contained in:
@@ -46,35 +46,15 @@ const int INFINITE_LATENCY = 10000; // Yes, this is a big hack
|
||||
// the second m_nodes set of SwitchIDs represent the the output queues
|
||||
// of the network.
|
||||
|
||||
// Helper functions based on chapter 29 of Cormen et al.
|
||||
void extend_shortest_path(Matrix& current_dist, Matrix& latencies,
|
||||
Matrix& inter_switches);
|
||||
Matrix shortest_path(const Matrix& weights, Matrix& latencies,
|
||||
Matrix& inter_switches);
|
||||
bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
|
||||
SwitchID final, const Matrix& weights, const Matrix& dist);
|
||||
NetDest shortest_path_to_node(SwitchID src, SwitchID next,
|
||||
const Matrix& weights, const Matrix& dist);
|
||||
|
||||
Topology::Topology(uint32_t num_routers, vector<BasicExtLink *> ext_links,
|
||||
vector<BasicIntLink *> int_links)
|
||||
: m_number_of_switches(num_routers)
|
||||
Topology::Topology(uint32_t num_routers,
|
||||
const vector<BasicExtLink *> &ext_links,
|
||||
const vector<BasicIntLink *> &int_links)
|
||||
: m_nodes(ext_links.size()), m_number_of_switches(num_routers),
|
||||
m_ext_link_vector(ext_links), m_int_link_vector(int_links)
|
||||
{
|
||||
|
||||
// initialize component latencies record
|
||||
m_component_latencies.resize(0);
|
||||
m_component_inter_switches.resize(0);
|
||||
|
||||
// Total nodes/controllers in network
|
||||
// Must make sure this is called after the State Machine constructors
|
||||
m_nodes = MachineType_base_number(MachineType_NUM);
|
||||
assert(m_nodes > 1);
|
||||
|
||||
if (m_nodes != ext_links.size()) {
|
||||
fatal("m_nodes (%d) != ext_links vector length (%d)\n",
|
||||
m_nodes, ext_links.size());
|
||||
}
|
||||
|
||||
// analyze both the internal and external links, create data structures
|
||||
// Note that the python created links are bi-directional, but that the
|
||||
// topology and networks utilize uni-directional links. Thus each
|
||||
@@ -85,9 +65,6 @@ Topology::Topology(uint32_t num_routers, vector<BasicExtLink *> ext_links,
|
||||
AbstractController *abs_cntrl = ext_link->params()->ext_node;
|
||||
BasicRouter *router = ext_link->params()->int_node;
|
||||
|
||||
// Store the ExtLink pointers for later
|
||||
m_ext_link_vector.push_back(ext_link);
|
||||
|
||||
int machine_base_idx = MachineType_base_number(abs_cntrl->getType());
|
||||
int ext_idx1 = machine_base_idx + abs_cntrl->getVersion();
|
||||
int ext_idx2 = ext_idx1 + m_nodes;
|
||||
@@ -133,28 +110,13 @@ Topology::createLinks(Network *net)
|
||||
}
|
||||
|
||||
// Initialize weight, latency, and inter switched vectors
|
||||
Matrix topology_weights;
|
||||
int num_switches = max_switch_id+1;
|
||||
topology_weights.resize(num_switches);
|
||||
m_component_latencies.resize(num_switches);
|
||||
m_component_inter_switches.resize(num_switches);
|
||||
|
||||
for (int i = 0; i < topology_weights.size(); i++) {
|
||||
topology_weights[i].resize(num_switches);
|
||||
m_component_latencies[i].resize(num_switches);
|
||||
m_component_inter_switches[i].resize(num_switches);
|
||||
|
||||
for (int j = 0; j < topology_weights[i].size(); j++) {
|
||||
topology_weights[i][j] = INFINITE_LATENCY;
|
||||
|
||||
// initialize to invalid values
|
||||
m_component_latencies[i][j] = -1;
|
||||
|
||||
// initially assume direct connections / no intermediate
|
||||
// switches between components
|
||||
m_component_inter_switches[i][j] = 0;
|
||||
}
|
||||
}
|
||||
Matrix topology_weights(num_switches,
|
||||
vector<int>(num_switches, INFINITE_LATENCY));
|
||||
Matrix component_latencies(num_switches,
|
||||
vector<int>(num_switches, -1));
|
||||
Matrix component_inter_switches(num_switches,
|
||||
vector<int>(num_switches, 0));
|
||||
|
||||
// Set identity weights to zero
|
||||
for (int i = 0; i < topology_weights.size(); i++) {
|
||||
@@ -168,13 +130,14 @@ Topology::createLinks(Network *net)
|
||||
BasicLink* link = (*i).second.link;
|
||||
int src = src_dest.first;
|
||||
int dst = src_dest.second;
|
||||
m_component_latencies[src][dst] = link->m_latency;
|
||||
component_latencies[src][dst] = link->m_latency;
|
||||
topology_weights[src][dst] = link->m_weight;
|
||||
}
|
||||
|
||||
// Walk topology and hookup the links
|
||||
Matrix dist = shortest_path(topology_weights, m_component_latencies,
|
||||
m_component_inter_switches);
|
||||
Matrix dist = shortest_path(topology_weights, component_latencies,
|
||||
component_inter_switches);
|
||||
|
||||
for (int i = 0; i < topology_weights.size(); i++) {
|
||||
for (int j = 0; j < topology_weights[i].size(); j++) {
|
||||
int weight = topology_weights[i][j];
|
||||
@@ -243,8 +206,8 @@ Topology::makeLink(Network *net, SwitchID src, SwitchID dest,
|
||||
// The following all-pairs shortest path algorithm is based on the
|
||||
// discussion from Cormen et al., Chapter 26.1.
|
||||
void
|
||||
extend_shortest_path(Matrix& current_dist, Matrix& latencies,
|
||||
Matrix& inter_switches)
|
||||
Topology::extend_shortest_path(Matrix ¤t_dist, Matrix &latencies,
|
||||
Matrix &inter_switches)
|
||||
{
|
||||
bool change = true;
|
||||
int nodes = current_dist.size();
|
||||
@@ -281,7 +244,8 @@ extend_shortest_path(Matrix& current_dist, Matrix& latencies,
|
||||
}
|
||||
|
||||
Matrix
|
||||
shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
|
||||
Topology::shortest_path(const Matrix &weights, Matrix &latencies,
|
||||
Matrix &inter_switches)
|
||||
{
|
||||
Matrix dist = weights;
|
||||
extend_shortest_path(dist, latencies, inter_switches);
|
||||
@@ -289,15 +253,16 @@ shortest_path(const Matrix& weights, Matrix& latencies, Matrix& inter_switches)
|
||||
}
|
||||
|
||||
bool
|
||||
link_is_shortest_path_to_node(SwitchID src, SwitchID next, SwitchID final,
|
||||
const Matrix& weights, const Matrix& dist)
|
||||
Topology::link_is_shortest_path_to_node(SwitchID src, SwitchID next,
|
||||
SwitchID final, const Matrix &weights,
|
||||
const Matrix &dist)
|
||||
{
|
||||
return weights[src][next] + dist[next][final] == dist[src][final];
|
||||
}
|
||||
|
||||
NetDest
|
||||
shortest_path_to_node(SwitchID src, SwitchID next, const Matrix& weights,
|
||||
const Matrix& dist)
|
||||
Topology::shortest_path_to_node(SwitchID src, SwitchID next,
|
||||
const Matrix &weights, const Matrix &dist)
|
||||
{
|
||||
NetDest result;
|
||||
int d = 0;
|
||||
|
||||
@@ -64,28 +64,38 @@ typedef std::map<std::pair<SwitchID, SwitchID>, LinkEntry> LinkMap;
|
||||
class Topology
|
||||
{
|
||||
public:
|
||||
Topology(uint32_t num_routers, std::vector<BasicExtLink *> ext_links,
|
||||
std::vector<BasicIntLink *> int_links);
|
||||
Topology(uint32_t num_routers, const std::vector<BasicExtLink *> &ext_links,
|
||||
const std::vector<BasicIntLink *> &int_links);
|
||||
|
||||
uint32_t numSwitches() const { return m_number_of_switches; }
|
||||
void createLinks(Network *net);
|
||||
void print(std::ostream& out) const { out << "[Topology]"; }
|
||||
|
||||
protected:
|
||||
private:
|
||||
void addLink(SwitchID src, SwitchID dest, BasicLink* link,
|
||||
LinkDirection dir);
|
||||
void makeLink(Network *net, SwitchID src, SwitchID dest,
|
||||
const NetDest& routing_table_entry);
|
||||
|
||||
NodeID m_nodes;
|
||||
uint32_t m_number_of_switches;
|
||||
// Helper functions based on chapter 29 of Cormen et al.
|
||||
void extend_shortest_path(Matrix ¤t_dist, Matrix &latencies,
|
||||
Matrix &inter_switches);
|
||||
|
||||
std::vector<std::vector<int>> shortest_path(const Matrix &weights,
|
||||
Matrix &latencies, Matrix &inter_switches);
|
||||
|
||||
bool link_is_shortest_path_to_node(SwitchID src, SwitchID next,
|
||||
SwitchID final, const Matrix &weights, const Matrix &dist);
|
||||
|
||||
NetDest shortest_path_to_node(SwitchID src, SwitchID next,
|
||||
const Matrix &weights, const Matrix &dist);
|
||||
|
||||
const uint32_t m_nodes;
|
||||
const uint32_t m_number_of_switches;
|
||||
|
||||
std::vector<BasicExtLink*> m_ext_link_vector;
|
||||
std::vector<BasicIntLink*> m_int_link_vector;
|
||||
|
||||
Matrix m_component_latencies;
|
||||
Matrix m_component_inter_switches;
|
||||
|
||||
LinkMap m_link_map;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user