Logo AND Algorithmique Numérique Distribuée

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