1 /* Copyright (c) 2013-2016. The SimGrid Team. All rights reserved. */
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. */
8 #include <simgrid/s4u/host.hpp>
10 #include "src/kernel/routing/AsVivaldi.hpp"
11 #include "src/kernel/routing/NetCard.hpp"
12 #include "src/surf/network_interface.hpp"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
20 simgrid::xbt::Extension<s4u::Host, Coords> Coords::EXTENSION_ID;
22 Coords::Coords(s4u::Host* host, const char* coordStr)
24 if (!Coords::EXTENSION_ID.valid()) {
25 Coords::EXTENSION_ID = s4u::Host::extension_create<Coords>();
31 xbt_dynar_t ctn_str = xbt_str_split_str(coordStr, " ");
32 xbt_assert(xbt_dynar_length(ctn_str) == 3, "Coordinates of %s must have 3 dimensions", host->name().c_str());
34 this->coords = xbt_dynar_new(sizeof(double), nullptr);
35 xbt_dynar_foreach (ctn_str, cursor, str) {
36 double val = xbt_str_parse_double(str, "Invalid coordinate: %s");
37 xbt_dynar_push(this->coords, &val);
39 xbt_dynar_free(&ctn_str);
40 xbt_dynar_shrink(this->coords, 0);
41 host->extension_set<Coords>(this);
45 xbt_dynar_free(&coords);
47 }; // namespace vivaldi
49 static inline double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
51 double src_coord = xbt_dynar_get_as(src, index, double);
52 double dst_coord = xbt_dynar_get_as(dst, index, double);
54 return (src_coord - dst_coord) * (src_coord - dst_coord);
57 static xbt_dynar_t getCoordsFromNetcard(NetCard *nc)
59 xbt_dynar_t res = nullptr;
63 tmp_name = bprintf("peer_%s", nc->name().c_str());
64 simgrid::s4u::Host *host = simgrid::s4u::Host::by_name_or_null(tmp_name);
66 host = simgrid::s4u::Host::by_name_or_null(nc->name());
68 res = host->extension<simgrid::kernel::routing::vivaldi::Coords>()->coords;
70 else if(nc->isRouter() || nc->isAS()){
71 tmp_name = bprintf("router_%s", nc->name().c_str());
72 res = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_name, COORD_ASR_LEVEL);
78 xbt_assert(res,"No coordinate found for element '%s'",tmp_name);
83 AsVivaldi::AsVivaldi(As* father, const char* name) : AsCluster(father, name)
86 void AsVivaldi::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
88 XBT_DEBUG("vivaldi getLocalRoute from '%s'[%d] '%s'[%d]", src->name().c_str(), src->id(), dst->name().c_str(),
92 char* srcName = bprintf("router_%s", src->name().c_str());
93 char* dstName = bprintf("router_%s", dst->name().c_str());
94 route->gw_src = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, srcName, ROUTING_ASR_LEVEL);
95 route->gw_dst = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, dstName, ROUTING_ASR_LEVEL);
100 /* Retrieve the private links */
101 if (privateLinks_.size() > src->id()) {
102 s_surf_parsing_link_up_down_t info = privateLinks_.at(src->id());
104 route->link_list->push_back(info.linkUp);
106 *lat += info.linkUp->latency();
109 if (privateLinks_.size() > dst->id()) {
110 s_surf_parsing_link_up_down_t info = privateLinks_.at(dst->id());
112 route->link_list->push_back(info.linkDown);
114 *lat += info.linkDown->latency();
118 /* Compute the extra latency due to the euclidean distance if needed */
120 xbt_dynar_t srcCoords = getCoordsFromNetcard(src);
121 xbt_dynar_t dstCoords = getCoordsFromNetcard(dst);
123 double euclidean_dist =
124 sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
125 fabs(xbt_dynar_get_as(srcCoords, 2, double)) + fabs(xbt_dynar_get_as(dstCoords, 2, double));
127 XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
128 *lat += euclidean_dist / 1000.0; // From .ms to .s