Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
better leaking than segfaulting
[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 #include "surf_routing_private.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
16 AS_t model_cluster_create(void)
17 {
18   return new simgrid::surf::AsCluster();
19 }
20
21 namespace simgrid {
22 namespace surf {
23
24 /* Creation routing model functions */
25 AsCluster::AsCluster() : AsNone()
26 {
27   p_backbone = 0;
28   p_loopback = 0;
29   p_router = 0;
30   p_has_limiter = 0;
31   p_has_loopback = 0;
32   p_nb_links_per_node = 1;
33 }
34
35 /* Business methods */
36 void AsCluster::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t route, double *lat)
37 {
38   s_surf_parsing_link_up_down_t info;
39   XBT_VERB("cluster_get_route_and_latency from '%s'[%d] to '%s'[%d]",
40             src->getName(), src->getId(), dst->getName(), dst->getId());
41
42   if (src->getRcType() != SURF_NETWORK_ELEMENT_ROUTER) {    // No specific link for router
43
44     if((src->getId() == dst->getId()) && p_has_loopback  ){
45       info = xbt_dynar_get_as(p_linkUpDownList, src->getId() * p_nb_links_per_node, s_surf_parsing_link_up_down_t);
46       xbt_dynar_push_as(route->link_list, void *, info.link_up);
47       if (lat)
48         *lat += static_cast<Link*>(info.link_up)->getLatency();
49       return;
50     }
51
52
53     if (p_has_limiter){          // limiter for sender
54       info = xbt_dynar_get_as(p_linkUpDownList, src->getId() * p_nb_links_per_node + p_has_loopback, s_surf_parsing_link_up_down_t);
55       xbt_dynar_push_as(route->link_list, void *, info.link_up);
56     }
57
58     info = xbt_dynar_get_as(p_linkUpDownList, src->getId() * p_nb_links_per_node + p_has_loopback + p_has_limiter, s_surf_parsing_link_up_down_t);
59     if (info.link_up) {         // link up
60       xbt_dynar_push_as(route->link_list, void *, info.link_up);
61       if (lat)
62         *lat += static_cast<Link*>(info.link_up)->getLatency();
63     }
64
65   }
66
67   if (p_backbone) {
68     xbt_dynar_push_as(route->link_list, void *,
69       static_cast<simgrid::surf::Resource*>(p_backbone));
70     if (lat)
71       *lat += p_backbone->getLatency();
72   }
73
74   if (dst->getRcType() != SURF_NETWORK_ELEMENT_ROUTER) {    // No specific link for router
75     info = xbt_dynar_get_as(p_linkUpDownList, dst->getId() * p_nb_links_per_node + p_has_loopback + p_has_limiter, s_surf_parsing_link_up_down_t);
76
77     if (info.link_down) {       // link down
78       xbt_dynar_push_as(route->link_list, void *, info.link_down);
79       if (lat)
80         *lat += static_cast<Link*>(info.link_down)->getLatency();
81     }
82     if (p_has_limiter){          // limiter for receiver
83         info = xbt_dynar_get_as(p_linkUpDownList, dst->getId() * p_nb_links_per_node + p_has_loopback, s_surf_parsing_link_up_down_t);
84         xbt_dynar_push_as(route->link_list, void *, info.link_up);
85     }
86   }
87 }
88
89 void AsCluster::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
90 {
91   int isrc;
92   int table_size = xbt_dynar_length(p_indexNetworkElm);
93
94   NetCard *src;
95   xbt_node_t current, previous, backboneNode = NULL, routerNode;
96   s_surf_parsing_link_up_down_t info;
97
98   xbt_assert(p_router,"Malformed cluster. This may be because your platform file is a hypergraph while it must be a graph.");
99
100   /* create the router */
101   char *link_name = p_router->getName();
102   routerNode = new_xbt_graph_node(graph, link_name, nodes);
103
104   if(p_backbone) {
105     const char *link_nameR = p_backbone->getName();
106     backboneNode = new_xbt_graph_node(graph, link_nameR, nodes);
107
108     new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
109   }
110
111   for (isrc = 0; isrc < table_size; isrc++) {
112     src = xbt_dynar_get_as(p_indexNetworkElm, isrc, NetCard*);
113
114     if (src->getRcType() != SURF_NETWORK_ELEMENT_ROUTER) {
115       previous = new_xbt_graph_node(graph, src->getName(), nodes);
116
117       info = xbt_dynar_get_as(p_linkUpDownList, src->getId(), s_surf_parsing_link_up_down_t);
118
119       if (info.link_up) {     // link up
120
121         const char *link_name = static_cast<simgrid::surf::Resource*>(
122           info.link_up)->getName();
123         current = new_xbt_graph_node(graph, link_name, nodes);
124         new_xbt_graph_edge(graph, previous, current, edges);
125
126         if (p_backbone) {
127           new_xbt_graph_edge(graph, current, backboneNode, edges);
128         } else {
129           new_xbt_graph_edge(graph, current, routerNode, edges);
130         }
131
132       }
133
134       if (info.link_down) {    // link down
135         const char *link_name = static_cast<simgrid::surf::Resource*>(
136           info.link_down)->getName();
137         current = new_xbt_graph_node(graph, link_name, nodes);
138         new_xbt_graph_edge(graph, previous, current, edges);
139
140         if (p_backbone) {
141           new_xbt_graph_edge(graph, current, backboneNode, edges);
142         } else {
143           new_xbt_graph_edge(graph, current, routerNode, edges);
144         }
145       }
146     }
147
148   }
149 }
150
151 void AsCluster::create_links_for_node(sg_platf_cluster_cbarg_t cluster, int id, int , int position){
152   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
153   s_surf_parsing_link_up_down_t info;
154   char* link_id = bprintf("%s_link_%d", cluster->id, id);
155
156   memset(&link, 0, sizeof(link));
157   link.id = link_id;
158   link.bandwidth = cluster->bw;
159   link.latency = cluster->lat;
160   link.initiallyOn = 1;
161   link.policy = cluster->sharing_policy;
162   sg_platf_new_link(&link);
163
164   if (link.policy == SURF_LINK_FULLDUPLEX) {
165     char *tmp_link = bprintf("%s_UP", link_id);
166     info.link_up = sg_link_by_name(tmp_link);
167     xbt_free(tmp_link);
168     tmp_link = bprintf("%s_DOWN", link_id);
169     info.link_down = sg_link_by_name(tmp_link);
170     xbt_free(tmp_link);
171   } else {
172     info.link_up = sg_link_by_name(link_id);
173     info.link_down = info.link_up;
174   }
175   xbt_dynar_set(p_linkUpDownList, position, &info);
176   xbt_free(link_id);
177 }
178
179 int AsCluster::parsePU(NetCard *elm) {
180   XBT_DEBUG("Load process unit \"%s\"", elm->getName());
181   xbt_dynar_push_as(p_indexNetworkElm, NetCard*, elm);
182   return xbt_dynar_length(p_indexNetworkElm)-1;
183 }
184
185 int AsCluster::parseAS(NetCard *elm) {
186   XBT_DEBUG("Load Autonomous system \"%s\"", elm->getName());
187   xbt_dynar_push_as(p_indexNetworkElm, NetCard*, elm);
188   return xbt_dynar_length(p_indexNetworkElm)-1;
189 }
190
191 }
192 }