Logo AND Algorithmique Numérique Distribuée

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