Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
prefer returning a NetZone than a pair of NetPoints and adapt callbacks for fancy...
[simgrid.git] / src / kernel / routing / ClusterZone.cpp
1 /* Copyright (c) 2009-2023. 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 "simgrid/s4u/Host.hpp"
7 #include "simgrid/kernel/routing/ClusterZone.hpp"
8 #include "simgrid/kernel/routing/NetPoint.hpp"
9 #include "src/kernel/resource/StandardLinkImpl.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_routing_cluster, ker_platform, "Kernel Cluster Routing");
12
13 /* This routing is specifically setup to represent clusters, aka homogeneous sets of machines
14  * Note that a router is created, easing the interconnection with the rest of the world. */
15
16 namespace simgrid::kernel::routing {
17
18 void ClusterBase::set_loopback()
19 {
20   if (not has_loopback_) {
21     num_links_per_node_++;
22     has_loopback_ = true;
23   }
24 }
25
26 void ClusterBase::set_limiter()
27 {
28   if (not has_limiter_) {
29     num_links_per_node_++;
30     has_limiter_ = true;
31   }
32 }
33
34 void ClusterBase::set_link_characteristics(double bw, double lat, s4u::Link::SharingPolicy sharing_policy)
35 {
36   link_sharing_policy_ = sharing_policy;
37   link_bw_             = bw;
38   link_lat_            = lat;
39 }
40
41 void ClusterBase::add_private_link_at(unsigned long position,
42                                       std::pair<resource::StandardLinkImpl*, resource::StandardLinkImpl*> link)
43 {
44   private_links_.try_emplace(position, link);
45 }
46
47 void ClusterBase::set_gateway(unsigned long position, NetPoint* gateway)
48 {
49   xbt_assert(not gateway || not gateway->is_netzone(), "ClusterBase: gateway cannot be another netzone %s",
50              gateway->get_cname());
51   gateways_[position] = gateway;
52 }
53
54 NetPoint* ClusterBase::get_gateway(unsigned long position)
55 {
56   auto it = gateways_.find(position);
57   return it == gateways_.end() ? nullptr : it->second;
58 }
59
60 void ClusterBase::fill_leaf_from_cb(unsigned long position, const std::vector<unsigned long>& dimensions,
61                                     const s4u::ClusterCallbacks& set_callbacks, NetPoint** node_netpoint,
62                                     s4u::Link** lb_link, s4u::Link** limiter_link)
63 {
64   xbt_assert(node_netpoint, "Invalid node_netpoint parameter");
65   xbt_assert(lb_link, "Invalid lb_link parameter");
66   xbt_assert(limiter_link, "Invalid limiter_link paramater");
67   *lb_link      = nullptr;
68   *limiter_link = nullptr;
69
70   // auxiliary function to get dims from index
71   auto index_to_dims = [&dimensions](unsigned long index) {
72     std::vector<unsigned long> dims_array(dimensions.size());
73     for (auto i = static_cast<int>(dimensions.size() - 1); i >= 0; --i) {
74       if (index == 0)
75         break;
76       unsigned long value = index % dimensions[i];
77       dims_array[i]      = value;
78       index              = (index / dimensions[i]);
79     }
80     return dims_array;
81   };
82
83   kernel::routing::NetPoint* netpoint = nullptr;
84   kernel::routing::NetPoint* gw       = nullptr;
85   auto dims                           = index_to_dims(position);
86   if (set_callbacks.is_by_netpoint()) { // XBT_ATTRIB_DEPRECATED_v339
87     std::tie(netpoint, gw) = set_callbacks.netpoint(get_iface(), dims, position); // XBT_ATTRIB_DEPRECATED_v339
88   } else if (set_callbacks.is_by_netzone()) {
89     s4u::NetZone* netzone = set_callbacks.netzone(get_iface(), dims, position);
90     netpoint              = netzone->get_netpoint();
91     gw                    = netzone->get_gateway();
92   } else {
93     s4u::Host* host = set_callbacks.host(get_iface(), dims, position);
94     netpoint        = host->get_netpoint();
95   }
96
97   xbt_assert(netpoint, "set_netpoint(elem=%lu): Invalid netpoint (nullptr)", position);
98   if (netpoint->is_netzone()) {
99     xbt_assert(gw && not gw->is_netzone(),
100                "set_netpoint(elem=%lu): Netpoint (%s) is a netzone, but gateway (%s) is invalid", position,
101                netpoint->get_cname(), gw ? gw->get_cname() : "nullptr");
102   } else {
103     xbt_assert(not gw, "set_netpoint: Netpoint (%s) isn't netzone, gateway must be nullptr", netpoint->get_cname());
104   }
105   // setting gateway
106   set_gateway(position, gw);
107
108   if (set_callbacks.loopback) {
109     s4u::Link* loopback = set_callbacks.loopback(get_iface(), dims, position);
110     xbt_assert(loopback, "set_loopback: Invalid loopback link (nullptr) for element %lu", position);
111     set_loopback();
112     add_private_link_at(node_pos(netpoint->id()), {loopback->get_impl(), loopback->get_impl()});
113     *lb_link = loopback;
114   }
115
116   if (set_callbacks.limiter) {
117     s4u::Link* limiter = set_callbacks.limiter(get_iface(), dims, position);
118     xbt_assert(limiter, "set_limiter: Invalid limiter link (nullptr) for element %lu", position);
119     set_limiter();
120     add_private_link_at(node_pos_with_loopback(netpoint->id()), {limiter->get_impl(), limiter->get_impl()});
121     *limiter_link = limiter;
122   }
123   *node_netpoint = netpoint;
124 }
125
126 } // namespace simgrid::kernel::routing