Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
be5d60692bf3752b34e3b283f8f64e12e6880c1d
[simgrid.git] / src / surf / surf_routing_cluster.cpp
1 /* Copyright (c) 2009-2011, 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_routing_cluster.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     : As(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->getRcType() != SURF_NETWORK_ELEMENT_ROUTER) {    // 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       xbt_dynar_push_as(route->link_list, void *, info.link_up);
32       if (lat)
33         *lat += static_cast<Link*>(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       xbt_dynar_push_as(route->link_list, void *, 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       xbt_dynar_push_as(route->link_list, void *, info.link_up);
46       if (lat)
47         *lat += static_cast<Link*>(info.link_up)->getLatency();
48     }
49
50   }
51
52   if (backbone_) {
53     xbt_dynar_push_as(route->link_list, void *,
54       static_cast<simgrid::surf::Resource*>(backbone_));
55     if (lat)
56       *lat += backbone_->getLatency();
57   }
58
59   if (dst->getRcType() != SURF_NETWORK_ELEMENT_ROUTER) {    // No specific link for router
60     info = xbt_dynar_get_as(upDownLinks, dst->id() * nb_links_per_node_ + has_loopback_ + has_limiter_, s_surf_parsing_link_up_down_t);
61
62     if (info.link_down) {       // link down
63       xbt_dynar_push_as(route->link_list, void *, info.link_down);
64       if (lat)
65         *lat += static_cast<Link*>(info.link_down)->getLatency();
66     }
67     if (has_limiter_){          // limiter for receiver
68         info = xbt_dynar_get_as(upDownLinks, dst->id() * nb_links_per_node_ + has_loopback_, s_surf_parsing_link_up_down_t);
69         xbt_dynar_push_as(route->link_list, void *, info.link_up);
70     }
71   }
72 }
73
74 void AsCluster::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
75 {
76   int isrc;
77   int table_size = xbt_dynar_length(vertices_);
78
79   NetCard *src;
80   xbt_node_t current, previous, backboneNode = NULL, routerNode;
81   s_surf_parsing_link_up_down_t info;
82
83   xbt_assert(router_,"Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
84
85   /* create the router */
86   char *link_name = router_->name();
87   routerNode = new_xbt_graph_node(graph, link_name, nodes);
88
89   if(backbone_) {
90     const char *link_nameR = backbone_->getName();
91     backboneNode = new_xbt_graph_node(graph, link_nameR, nodes);
92
93     new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
94   }
95
96   for (isrc = 0; isrc < table_size; isrc++) {
97     src = xbt_dynar_get_as(vertices_, isrc, NetCard*);
98
99     if (src->getRcType() != SURF_NETWORK_ELEMENT_ROUTER) {
100       previous = new_xbt_graph_node(graph, src->name(), nodes);
101
102       info = xbt_dynar_get_as(upDownLinks, src->id(), s_surf_parsing_link_up_down_t);
103
104       if (info.link_up) {     // link up
105
106         const char *link_name = static_cast<simgrid::surf::Resource*>(
107           info.link_up)->getName();
108         current = new_xbt_graph_node(graph, link_name, nodes);
109         new_xbt_graph_edge(graph, previous, current, edges);
110
111         if (backbone_) {
112           new_xbt_graph_edge(graph, current, backboneNode, edges);
113         } else {
114           new_xbt_graph_edge(graph, current, routerNode, edges);
115         }
116
117       }
118
119       if (info.link_down) {    // link down
120         const char *link_name = static_cast<simgrid::surf::Resource*>(
121           info.link_down)->getName();
122         current = new_xbt_graph_node(graph, link_name, nodes);
123         new_xbt_graph_edge(graph, previous, current, edges);
124
125         if (backbone_) {
126           new_xbt_graph_edge(graph, current, backboneNode, edges);
127         } else {
128           new_xbt_graph_edge(graph, current, routerNode, edges);
129         }
130       }
131     }
132
133   }
134 }
135
136 void AsCluster::create_links_for_node(sg_platf_cluster_cbarg_t cluster, int id, int , int position){
137   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
138   s_surf_parsing_link_up_down_t info;
139   char* link_id = bprintf("%s_link_%d", cluster->id, id);
140
141   memset(&link, 0, sizeof(link));
142   link.id = link_id;
143   link.bandwidth = cluster->bw;
144   link.latency = cluster->lat;
145   link.policy = cluster->sharing_policy;
146   sg_platf_new_link(&link);
147
148   if (link.policy == SURF_LINK_FULLDUPLEX) {
149     char *tmp_link = bprintf("%s_UP", link_id);
150     info.link_up = sg_link_by_name(tmp_link);
151     xbt_free(tmp_link);
152     tmp_link = bprintf("%s_DOWN", link_id);
153     info.link_down = sg_link_by_name(tmp_link);
154     xbt_free(tmp_link);
155   } else {
156     info.link_up = sg_link_by_name(link_id);
157     info.link_down = info.link_up;
158   }
159   xbt_dynar_set(upDownLinks, position, &info);
160   xbt_free(link_id);
161 }
162
163 }
164 }