Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use NetCard extension to store vivaldi coordinates
[simgrid.git] / src / kernel / routing / AsVivaldi.cpp
1 /* Copyright (c) 2013-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 <xbt/dynar.h>
7
8 #include <simgrid/s4u/host.hpp>
9
10 #include "src/kernel/routing/AsVivaldi.hpp"
11 #include "src/kernel/routing/NetCard.hpp"
12 #include "src/surf/network_interface.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
15
16 namespace simgrid {
17 namespace kernel {
18 namespace routing {
19 namespace vivaldi {
20 simgrid::xbt::Extension<NetCard, Coords> Coords::EXTENSION_ID;
21
22 Coords::Coords(NetCard* netcard, const char* coordStr)
23 {
24   if (!Coords::EXTENSION_ID.valid())
25     Coords::EXTENSION_ID = NetCard::extension_create<Coords>();
26
27   unsigned int cursor;
28   char* str;
29
30   xbt_dynar_t ctn_str = xbt_str_split_str(coordStr, " ");
31   xbt_assert(xbt_dynar_length(ctn_str) == 3, "Coordinates of %s must have 3 dimensions", netcard->cname());
32
33   this->coords = xbt_dynar_new(sizeof(double), nullptr);
34   xbt_dynar_foreach (ctn_str, cursor, str) {
35     double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
36     xbt_dynar_push(this->coords, &val);
37   }
38   xbt_dynar_free(&ctn_str);
39   xbt_dynar_shrink(this->coords, 0);
40   netcard->extension_set<Coords>(this);
41   XBT_DEBUG("Coords of %s %p: %s", netcard->cname(), netcard, coordStr);
42 }
43 Coords::~Coords()
44 {
45   xbt_dynar_free(&coords);
46 }
47 }; // namespace vivaldi
48
49 static inline double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
50 {
51   double src_coord = xbt_dynar_get_as(src, index, double);
52   double dst_coord = xbt_dynar_get_as(dst, index, double);
53
54   return (src_coord - dst_coord) * (src_coord - dst_coord);
55 }
56
57 static xbt_dynar_t getCoordsFromNetcard(NetCard* nc)
58 {
59   simgrid::kernel::routing::vivaldi::Coords* coords = nc->extension<simgrid::kernel::routing::vivaldi::Coords>();
60   xbt_assert(coords, "Please specify the Vivaldi coordinates of %s %s (%p)",
61              (nc->isAS() ? "AS" : (nc->isHost() ? "Host" : "Router")), nc->cname(), nc);
62   return coords->coords;
63 }
64 AsVivaldi::AsVivaldi(As* father, const char* name) : AsCluster(father, name)
65 {
66 }
67
68 void AsVivaldi::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
69 {
70   XBT_DEBUG("vivaldi getLocalRoute from '%s'[%d] '%s'[%d]", src->cname(), src->id(), dst->cname(), dst->id());
71
72   if (src->isAS()) {
73     char* srcName = bprintf("router_%s", src->cname());
74     char* dstName = bprintf("router_%s", dst->cname());
75     route->gw_src = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, srcName, ROUTING_ASR_LEVEL);
76     route->gw_dst = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, dstName, ROUTING_ASR_LEVEL);
77     xbt_free(srcName);
78     xbt_free(dstName);
79   }
80
81   /* Retrieve the private links */
82   if (privateLinks_.size() > src->id()) {
83     s_surf_parsing_link_up_down_t info = privateLinks_.at(src->id());
84     if (info.linkUp) {
85       route->link_list->push_back(info.linkUp);
86       if (lat)
87         *lat += info.linkUp->latency();
88     }
89   }
90   if (privateLinks_.size() > dst->id()) {
91     s_surf_parsing_link_up_down_t info = privateLinks_.at(dst->id());
92     if (info.linkDown) {
93       route->link_list->push_back(info.linkDown);
94       if (lat)
95         *lat += info.linkDown->latency();
96     }
97   }
98
99   /* Compute the extra latency due to the euclidean distance if needed */
100   if (lat) {
101     xbt_dynar_t srcCoords = getCoordsFromNetcard(src);
102     xbt_dynar_t dstCoords = getCoordsFromNetcard(dst);
103
104     double euclidean_dist =
105         sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
106         fabs(xbt_dynar_get_as(srcCoords, 2, double)) + fabs(xbt_dynar_get_as(dstCoords, 2, double));
107
108     XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
109     *lat += euclidean_dist / 1000.0; // From .ms to .s
110   }
111 }
112 }
113 }
114 }