Logo AND Algorithmique Numérique Distribuée

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