Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
small cleanups
[simgrid.git] / src / surf / AsFull.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/AsFull.hpp"
7 #include "src/surf/network_interface.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
10
11 #define TO_ROUTE_FULL(i,j) routingTable_[(i)+(j)*table_size]
12
13 namespace simgrid {
14 namespace surf {
15   AsFull::AsFull(const char*name)
16     : AsRoutedGraph(name)
17   {
18   }
19
20 void AsFull::seal() {
21   int i;
22   sg_platf_route_cbarg_t e_route;
23
24   /* set utils vars */
25   int table_size = (int)xbt_dynar_length(vertices_);
26
27   /* Create table if necessary */
28   if (!routingTable_)
29     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
30
31   /* Add the loopback if needed */
32   if (routing_platf->loopback_ && hierarchy_ == RoutingMode::base) {
33     for (i = 0; i < table_size; i++) {
34       e_route = TO_ROUTE_FULL(i, i);
35       if (!e_route) {
36         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
37         e_route->gw_src = NULL;
38         e_route->gw_dst = NULL;
39         e_route->link_list = new std::vector<Link*>();
40         e_route->link_list->push_back(routing_platf->loopback_);
41         TO_ROUTE_FULL(i, i) = e_route;
42       }
43     }
44   }
45 }
46
47 AsFull::~AsFull(){
48   if (routingTable_) {
49     int table_size = (int)xbt_dynar_length(vertices_);
50     int i, j;
51     /* Delete routing table */
52     for (i = 0; i < table_size; i++)
53       for (j = 0; j < table_size; j++) {
54         if (TO_ROUTE_FULL(i,j)){
55           delete TO_ROUTE_FULL(i,j)->link_list;
56           xbt_free(TO_ROUTE_FULL(i,j));
57         }
58       }
59     xbt_free(routingTable_);
60   }
61 }
62
63 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
64 {
65   XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
66       src->name(), src->id(), dst->name(), dst->id());
67
68   /* set utils vars */
69   size_t table_size = xbt_dynar_length(vertices_);
70
71   sg_platf_route_cbarg_t e_route = NULL;
72
73   e_route = TO_ROUTE_FULL(src->id(), dst->id());
74
75   if (e_route) {
76     res->gw_src = e_route->gw_src;
77     res->gw_dst = e_route->gw_dst;
78     for (auto link : *e_route->link_list) {
79       res->link_list->push_back(link);
80       if (lat)
81         *lat += static_cast<Link*>(link)->getLatency();
82     }
83   }
84 }
85
86 void AsFull::addRoute(sg_platf_route_cbarg_t route)
87 {
88   const char *src = route->src;
89   const char *dst = route->dst;
90   NetCard *src_net_elm = sg_netcard_by_name_or_null(src);
91   NetCard *dst_net_elm = sg_netcard_by_name_or_null(dst);
92
93   addRouteCheckParams(route);
94
95   size_t table_size = xbt_dynar_length(vertices_);
96
97   if (!routingTable_)
98     routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
99
100   /* Check that the route does not already exist */
101   if (route->gw_dst) // AS route (to adapt the error message, if any)
102     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
103         "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
104         src,route->gw_src->name(),dst,route->gw_dst->name());
105   else
106     xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
107         "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src,dst);
108
109   /* Add the route to the base */
110   TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()) = newExtendedRoute(hierarchy_, route, 1);
111   TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id())->link_list->shrink_to_fit();
112
113   if (route->symmetrical == true && src_net_elm != dst_net_elm) {
114     if (route->gw_dst && route->gw_src) {
115       NetCard* gw_tmp = route->gw_src;
116       route->gw_src = route->gw_dst;
117       route->gw_dst = gw_tmp;
118     }
119     if (route->gw_dst) // AS route (to adapt the error message, if any)
120       xbt_assert(nullptr == TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()),
121           "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
122           dst,route->gw_dst->name(),src,route->gw_src->name());
123     else
124       xbt_assert(nullptr == TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()),
125           "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.", dst,src);
126
127     TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()) = newExtendedRoute(hierarchy_, route, 0);
128     TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list->shrink_to_fit();
129   }
130 }
131
132 }
133 }