Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move code in simgrid::mc
[simgrid.git] / src / surf / 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/surf/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
15 namespace simgrid {
16 namespace surf {
17   AsCluster::AsCluster(const char*name)
18     : AsImpl(name)
19   {}
20
21 void AsCluster::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
22 {
23   s_surf_parsing_link_up_down_t info;
24   XBT_VERB("cluster_get_route_and_latency from '%s'[%d] to '%s'[%d]",
25             src->name(), src->id(), dst->name(), dst->id());
26
27   if (! src->isRouter()) {    // No specific link for router
28
29     if((src->id() == dst->id()) && has_loopback_  ){
30       info = xbt_dynar_get_as(upDownLinks, src->id() * nb_links_per_node_, s_surf_parsing_link_up_down_t);
31       route->link_list->push_back(info.link_up);
32       if (lat)
33         *lat += info.link_up->getLatency();
34       return;
35     }
36
37
38     if (has_limiter_){          // limiter for sender
39       info = xbt_dynar_get_as(upDownLinks, src->id() * nb_links_per_node_ + has_loopback_, s_surf_parsing_link_up_down_t);
40       route->link_list->push_back((Link*)info.link_up);
41     }
42
43     info = xbt_dynar_get_as(upDownLinks, src->id() * nb_links_per_node_ + has_loopback_ + has_limiter_, s_surf_parsing_link_up_down_t);
44     if (info.link_up) {         // link up
45       route->link_list->push_back(info.link_up);
46       if (lat)
47         *lat += info.link_up->getLatency();
48     }
49
50   }
51
52   if (backbone_) {
53     route->link_list->push_back(backbone_);
54     if (lat)
55       *lat += backbone_->getLatency();
56   }
57
58   if (! dst->isRouter()) {    // No specific link for router
59     info = xbt_dynar_get_as(upDownLinks, dst->id() * nb_links_per_node_ + has_loopback_ + has_limiter_, s_surf_parsing_link_up_down_t);
60
61     if (info.link_down) {       // link down
62       route->link_list->push_back(info.link_down);
63       if (lat)
64         *lat += info.link_down->getLatency();
65     }
66     if (has_limiter_){          // limiter for receiver
67         info = xbt_dynar_get_as(upDownLinks, dst->id() * nb_links_per_node_ + has_loopback_, s_surf_parsing_link_up_down_t);
68         route->link_list->push_back(info.link_up);
69     }
70   }
71 }
72
73 void AsCluster::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
74 {
75   int isrc;
76   int table_size = xbt_dynar_length(vertices_);
77
78   NetCard *src;
79   xbt_node_t current, previous, backboneNode = NULL, 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 (isrc = 0; isrc < table_size; isrc++) {
96     src = xbt_dynar_get_as(vertices_, isrc, NetCard*);
97
98     if (! src->isRouter()) {
99       previous = new_xbt_graph_node(graph, src->name(), nodes);
100
101       info = xbt_dynar_get_as(upDownLinks, src->id(), s_surf_parsing_link_up_down_t);
102
103       if (info.link_up) {     // link up
104
105         const char *link_name = static_cast<simgrid::surf::Resource*>(
106           info.link_up)->getName();
107         current = new_xbt_graph_node(graph, link_name, nodes);
108         new_xbt_graph_edge(graph, previous, current, edges);
109
110         if (backbone_) {
111           new_xbt_graph_edge(graph, current, backboneNode, edges);
112         } else {
113           new_xbt_graph_edge(graph, current, routerNode, edges);
114         }
115
116       }
117
118       if (info.link_down) {    // link down
119         const char *link_name = static_cast<simgrid::surf::Resource*>(
120           info.link_down)->getName();
121         current = new_xbt_graph_node(graph, link_name, nodes);
122         new_xbt_graph_edge(graph, previous, current, edges);
123
124         if (backbone_) {
125           new_xbt_graph_edge(graph, current, backboneNode, edges);
126         } else {
127           new_xbt_graph_edge(graph, current, routerNode, edges);
128         }
129       }
130     }
131
132   }
133 }
134
135 void AsCluster::create_links_for_node(sg_platf_cluster_cbarg_t cluster, int id, int , int position){
136   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
137   s_surf_parsing_link_up_down_t info;
138   char* link_id = bprintf("%s_link_%d", cluster->id, id);
139
140   memset(&link, 0, sizeof(link));
141   link.id = link_id;
142   link.bandwidth = cluster->bw;
143   link.latency = cluster->lat;
144   link.policy = cluster->sharing_policy;
145   sg_platf_new_link(&link);
146
147   if (link.policy == SURF_LINK_FULLDUPLEX) {
148     char *tmp_link = bprintf("%s_UP", link_id);
149     info.link_up = sg_link_by_name(tmp_link);
150     xbt_free(tmp_link);
151     tmp_link = bprintf("%s_DOWN", link_id);
152     info.link_down = sg_link_by_name(tmp_link);
153     xbt_free(tmp_link);
154   } else {
155     info.link_up = sg_link_by_name(link_id);
156     info.link_down = info.link_up;
157   }
158   xbt_dynar_set(upDownLinks, position, &info);
159   xbt_free(link_id);
160 }
161
162 }
163 }