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/surf/network_interface.hpp"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_vivaldi, surf, "Routing part of surf");
19 simgrid::xbt::Extension<s4u::Host, Coords> Coords::EXTENSION_ID;
21 Coords::Coords(s4u::Host* host, const char* coordStr)
23 if (!Coords::EXTENSION_ID.valid()) {
24 Coords::EXTENSION_ID = s4u::Host::extension_create<Coords>();
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", host->name().c_str());
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);
38 xbt_dynar_free(&ctn_str);
39 xbt_dynar_shrink(this->coords, 0);
40 host->extension_set<Coords>(this);
44 xbt_dynar_free(&coords);
46 }; // namespace vivaldi
48 static inline double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
50 double src_coord = xbt_dynar_get_as(src, index, double);
51 double dst_coord = xbt_dynar_get_as(dst, index, double);
53 return (src_coord - dst_coord) * (src_coord - dst_coord);
56 static xbt_dynar_t getCoordsFromNetcard(NetCard *nc)
58 xbt_dynar_t res = nullptr;
62 tmp_name = bprintf("peer_%s", nc->name().c_str());
63 simgrid::s4u::Host *host = simgrid::s4u::Host::by_name_or_null(tmp_name);
65 host = simgrid::s4u::Host::by_name_or_null(nc->name());
67 res = host->extension<simgrid::kernel::routing::vivaldi::Coords>()->coords;
69 else if(nc->isRouter() || nc->isAS()){
70 tmp_name = bprintf("router_%s", nc->name().c_str());
71 res = (xbt_dynar_t) xbt_lib_get_or_null(as_router_lib, tmp_name, COORD_ASR_LEVEL);
77 xbt_assert(res,"No coordinate found for element '%s'",tmp_name);
82 AsVivaldi::AsVivaldi(As* father, const char* name) : AsCluster(father, name)
85 void AsVivaldi::getLocalRoute(NetCard* src, NetCard* dst, sg_platf_route_cbarg_t route, double* lat)
87 XBT_DEBUG("vivaldi getLocalRoute from '%s'[%d] '%s'[%d]", src->name().c_str(), src->id(), dst->name().c_str(),
91 char* srcName = bprintf("router_%s", src->name().c_str());
92 char* dstName = bprintf("router_%s", dst->name().c_str());
93 route->gw_src = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, srcName, ROUTING_ASR_LEVEL);
94 route->gw_dst = (sg_netcard_t)xbt_lib_get_or_null(as_router_lib, dstName, ROUTING_ASR_LEVEL);
99 /* Retrieve the private links */
100 if (privateLinks_.size() > src->id()) {
101 s_surf_parsing_link_up_down_t info = privateLinks_.at(src->id());
103 route->link_list->push_back(info.linkUp);
105 *lat += info.linkUp->latency();
108 if (privateLinks_.size() > dst->id()) {
109 s_surf_parsing_link_up_down_t info = privateLinks_.at(dst->id());
111 route->link_list->push_back(info.linkDown);
113 *lat += info.linkDown->latency();
117 /* Compute the extra latency due to the euclidean distance if needed */
119 xbt_dynar_t srcCoords = getCoordsFromNetcard(src);
120 xbt_dynar_t dstCoords = getCoordsFromNetcard(dst);
122 double euclidean_dist =
123 sqrt(euclidean_dist_comp(0, srcCoords, dstCoords) + euclidean_dist_comp(1, srcCoords, dstCoords)) +
124 fabs(xbt_dynar_get_as(srcCoords, 2, double)) + fabs(xbt_dynar_get_as(dstCoords, 2, double));
126 XBT_DEBUG("Updating latency %f += %f", *lat, euclidean_dist);
127 *lat += euclidean_dist / 1000.0; // From .ms to .s