Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use homogeneous dicts which are more efficient.
[simgrid.git] / src / surf / surf_routing_cluster.c
1 /* Copyright (c) 2009-2013. 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 #include "surf_routing_private.h"
7 #include "xbt/graph.h"
8
9 /* Global vars */
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_cluster, surf, "Routing part of surf");
12
13 /* This routing is specifically setup to represent clusters, aka homogeneous sets of machines
14  * Note that a router is created, easing the interconnexion with the rest of the world.
15  */
16
17 /* Business methods */
18 static void cluster_get_route_and_latency(AS_t as,
19                                           sg_routing_edge_t src,
20                                           sg_routing_edge_t dst,
21                                           sg_platf_route_cbarg_t route,
22                                           double *lat)
23 {
24
25   s_surf_parsing_link_up_down_t info;
26   XBT_VERB("cluster_get_route_and_latency from '%s'[%d] to '%s'[%d]",
27             src->name, src->id, dst->name, dst->id);
28
29   if (src->rc_type != SURF_NETWORK_ELEMENT_ROUTER) {    // No specific link for router
30     info =
31         xbt_dynar_get_as(as->link_up_down_list, src->id,
32                          s_surf_parsing_link_up_down_t);
33                          
34     if((src->id == dst->id) && info.loopback_link  ){
35       xbt_dynar_push_as(route->link_list, void *, info.loopback_link);
36       if (lat)
37         *lat +=
38             surf_network_model->extension.network.get_link_latency(info.
39                                                                    loopback_link);
40       return;
41     }
42                          
43                          
44     if (info.limiter_link)          // limiter for sender
45       xbt_dynar_push_as(route->link_list, void *, info.limiter_link);
46     
47     if (info.link_up) {         // link up
48       xbt_dynar_push_as(route->link_list, void *, info.link_up);
49       if (lat)
50         *lat +=
51             surf_network_model->extension.network.get_link_latency(info.
52                                                                    link_up);
53     }
54   }
55
56   if (((as_cluster_t) as)->backbone) {
57     xbt_dynar_push_as(route->link_list, void *, ((as_cluster_t) as)->backbone);
58     if (lat)
59       *lat +=
60           surf_network_model->extension.network.
61           get_link_latency(((as_cluster_t) as)->backbone);
62   }
63
64   if (dst->rc_type != SURF_NETWORK_ELEMENT_ROUTER) {    // No specific link for router
65     info =
66         xbt_dynar_get_as(as->link_up_down_list, dst->id,
67                          s_surf_parsing_link_up_down_t);
68     if (info.link_down) {       // link down
69       xbt_dynar_push_as(route->link_list, void *, info.link_down);
70       if (lat)
71         *lat +=
72             surf_network_model->extension.network.get_link_latency(info.
73                                                                    link_down);
74     }
75     
76     if (info.limiter_link)          // limiter for receiver
77       xbt_dynar_push_as(route->link_list, void *, info.limiter_link);
78     
79   }
80 }
81
82 static void cluster_get_graph(xbt_graph_t graph, xbt_dict_t nodes,
83                               xbt_dict_t edges, AS_t rc)
84 {
85   int isrc;
86   int table_size = xbt_dynar_length(rc->index_network_elm);
87
88   sg_routing_edge_t src;
89   xbt_node_t current, previous, backboneNode = NULL, routerNode;
90   s_surf_parsing_link_up_down_t info;
91
92   xbt_assert(((as_cluster_t) rc)->router,"Malformed cluster");
93
94   /* create the router */
95   char *link_name =
96     ((sg_routing_edge_t) ((as_cluster_t) rc)->router)->name;
97   routerNode = new_xbt_graph_node(graph, link_name, nodes);
98
99   if(((as_cluster_t) rc)->backbone) {
100     char *link_nameR =
101       ((surf_resource_t) ((as_cluster_t) rc)->backbone)->name;
102     backboneNode = new_xbt_graph_node(graph, link_nameR, nodes);
103
104     new_xbt_graph_edge(graph, routerNode, backboneNode, edges);
105   }
106
107   for (isrc = 0; isrc < table_size; isrc++) {
108     src = xbt_dynar_get_as(rc->index_network_elm, isrc, sg_routing_edge_t);
109
110     if (src->rc_type != SURF_NETWORK_ELEMENT_ROUTER) {
111       previous = new_xbt_graph_node(graph, src->name, nodes);
112
113       info = xbt_dynar_get_as(rc->link_up_down_list, src->id,
114                               s_surf_parsing_link_up_down_t);
115
116       if (info.link_up) {     // link up
117
118         char *link_name = ((surf_resource_t) info.link_up)->name;
119         current = new_xbt_graph_node(graph, link_name, nodes);
120         new_xbt_graph_edge(graph, previous, current, edges);
121
122         if (((as_cluster_t) rc)->backbone) {
123           new_xbt_graph_edge(graph, current, backboneNode, edges);
124         } else {
125           new_xbt_graph_edge(graph, current, routerNode, edges);
126         }
127
128       }
129
130       if (info.link_down) {    // link down
131         char *link_name = ((surf_resource_t) info.link_down)->name;
132         current = new_xbt_graph_node(graph, link_name, nodes);
133         new_xbt_graph_edge(graph, previous, current, edges);
134
135         if (((as_cluster_t) rc)->backbone) {
136           new_xbt_graph_edge(graph, current, backboneNode, edges);
137         } else {
138           new_xbt_graph_edge(graph, current, routerNode, edges);
139         }
140       }
141     }
142
143   }
144 }
145
146 static void model_cluster_finalize(AS_t as)
147 {
148   model_none_finalize(as);
149 }
150
151 static int cluster_parse_PU(AS_t rc, sg_routing_edge_t elm) {
152   XBT_DEBUG("Load process unit \"%s\"", elm->name);
153   xbt_dynar_push_as(rc->index_network_elm,sg_routing_edge_t,elm);
154   return xbt_dynar_length(rc->index_network_elm)-1;
155 }
156
157 static int cluster_parse_AS(AS_t rc, sg_routing_edge_t elm) {
158   XBT_DEBUG("Load Autonomous system \"%s\"", elm->name);
159   xbt_dynar_push_as(rc->index_network_elm,sg_routing_edge_t,elm);
160   return xbt_dynar_length(rc->index_network_elm)-1;
161 }
162
163 /* Creation routing model functions */
164 AS_t model_cluster_create(void)
165 {
166   AS_t result = model_none_create_sized(sizeof(s_as_cluster_t));
167   result->get_route_and_latency = cluster_get_route_and_latency;
168   result->finalize = model_cluster_finalize;
169   result->get_graph = cluster_get_graph;
170   result->parse_AS = cluster_parse_AS;
171   result->parse_PU = cluster_parse_PU;
172
173   return (AS_t) result;
174 }