Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fd5f54064ea88192654aac019fc0341914e2ff78
[simgrid.git] / src / kernel / routing / AsCluster.cpp
1 /* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/routing/AsCluster.hpp"
7 #include "src/surf/network_interface.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
10
11 /* This routing is specifically setup to represent clusters, aka homogeneous sets of machines
12  * Note that a router is created, easing the interconnexion with the rest of the world. */
13
14 namespace simgrid {
15 namespace kernel {
16 namespace routing {
17   AsCluster::AsCluster(const char*name)
18     : AsImpl(name)
19   {}
20   AsCluster::~AsCluster()
21   {
22     xbt_dynar_free(&privateLinks_);
23   }
24
25 void AsCluster::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
26 {
27   s_surf_parsing_link_up_down_t info;
28   XBT_VERB("cluster_get_route_and_latency from '%s'[%d] to '%s'[%d]",
29             src->name(), src->id(), dst->name(), dst->id());
30   xbt_assert(!xbt_dynar_is_empty(privateLinks_), "Cluster routing : no links attached to the source node - did you use host_link tag?");
31   if (! src->isRouter()) {    // No specific link for router
32
33     if((src->id() == dst->id()) && hasLoopback_ ){
34       info = xbt_dynar_get_as(privateLinks_, src->id() * linkCountPerNode_, s_surf_parsing_link_up_down_t);
35       route->link_list->push_back(info.linkUp);
36       if (lat)
37         *lat += info.linkUp->getLatency();
38       return;
39     }
40
41
42     if (hasLimiter_){          // limiter for sender
43       info = xbt_dynar_get_as(privateLinks_, src->id() * linkCountPerNode_ + (hasLoopback_?1:0), s_surf_parsing_link_up_down_t);
44       route->link_list->push_back((Link*)info.linkUp);
45     }
46
47     info = xbt_dynar_get_as(privateLinks_, src->id() * linkCountPerNode_ + (hasLoopback_?1:0) + (hasLimiter_?1:0), s_surf_parsing_link_up_down_t);
48     if (info.linkUp) {         // link up
49       route->link_list->push_back(info.linkUp);
50       if (lat)
51         *lat += info.linkUp->getLatency();
52     }
53
54   }
55
56   if (backbone_) {
57     route->link_list->push_back(backbone_);
58     if (lat)
59       *lat += backbone_->getLatency();
60   }
61
62   if (! dst->isRouter()) {    // No specific link for router
63     info = xbt_dynar_get_as(privateLinks_, dst->id() * linkCountPerNode_ + hasLoopback_ + hasLimiter_, s_surf_parsing_link_up_down_t);
64
65     if (info.linkDown) {       // link down
66       route->link_list->push_back(info.linkDown);
67       if (lat)
68         *lat += info.linkDown->getLatency();
69     }
70     if (hasLimiter_){          // limiter for receiver
71         info = xbt_dynar_get_as(privateLinks_, dst->id() * linkCountPerNode_ + hasLoopback_, s_surf_parsing_link_up_down_t);
72         route->link_list->push_back(info.linkUp);
73     }
74   }
75 }
76
77 void AsCluster::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
78 {
79   xbt_node_t current, previous, backboneNode = nullptr, routerNode;
80   s_surf_parsing_link_up_down_t info;
81
82   xbt_assert(router_,"Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
83
84   /* create the router */
85   char *link_name = router_->name();
86   routerNode = new_xbt_graph_node(graph, link_name, nodes);
87
88   if(backbone_) {
89     const char *link_nameR = backbone_->getName();
90     backboneNode = new_xbt_graph_node(graph, link_nameR, nodes);
91
92     new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
93   }
94
95   for (auto src: vertices_){
96     if (! src->isRouter()) {
97       previous = new_xbt_graph_node(graph, src->name(), nodes);
98
99       info = xbt_dynar_get_as(privateLinks_, src->id(), s_surf_parsing_link_up_down_t);
100
101       if (info.linkUp) {     // link up
102         const char *link_name = static_cast<simgrid::surf::Resource*>(info.linkUp)->getName();
103         current = new_xbt_graph_node(graph, link_name, nodes);
104         new_xbt_graph_edge(graph, previous, current, edges);
105
106         if (backbone_) {
107           new_xbt_graph_edge(graph, current, backboneNode, edges);
108         } else {
109           new_xbt_graph_edge(graph, current, routerNode, edges);
110         }
111       }
112
113       if (info.linkDown) {    // link down
114         const char *link_name = static_cast<simgrid::surf::Resource*>(
115           info.linkDown)->getName();
116         current = new_xbt_graph_node(graph, link_name, nodes);
117         new_xbt_graph_edge(graph, previous, current, edges);
118
119         if (backbone_) {
120           new_xbt_graph_edge(graph, current, backboneNode, edges);
121         } else {
122           new_xbt_graph_edge(graph, current, routerNode, edges);
123         }
124       }
125     }
126   }
127 }
128
129 void AsCluster::create_links_for_node(sg_platf_cluster_cbarg_t cluster, int id, int , int position){
130   s_surf_parsing_link_up_down_t info;
131   char* link_id = bprintf("%s_link_%d", cluster->id, id);
132
133   s_sg_platf_link_cbarg_t link;
134   memset(&link, 0, sizeof(link));
135   link.id = link_id;
136   link.bandwidth = cluster->bw;
137   link.latency = cluster->lat;
138   link.policy = cluster->sharing_policy;
139   sg_platf_new_link(&link);
140
141   if (link.policy == SURF_LINK_FULLDUPLEX) {
142     char *tmp_link = bprintf("%s_UP", link_id);
143     info.linkUp = Link::byName(tmp_link);
144     xbt_free(tmp_link);
145     tmp_link = bprintf("%s_DOWN", link_id);
146     info.linkDown = Link::byName(tmp_link);
147     xbt_free(tmp_link);
148   } else {
149     info.linkUp = Link::byName(link_id);
150     info.linkDown = info.linkUp;
151   }
152   xbt_dynar_set(privateLinks_, position, &info);
153   xbt_free(link_id);
154 }
155
156 }}}