Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / smpi / internals / smpi_host.cpp
1 /* Copyright (c) 2017-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 "smpi_host.hpp"
7 #include "private.hpp"
8 #include "simgrid/s4u/VirtualMachine.hpp"
9 #include "smpi/smpi.h"
10 #include "smpi_utils.hpp"
11 #include "xbt/config.hpp"
12
13 #include <string>
14 #include <vector>
15 #include <xbt/log.h>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_host, smpi, "Logging specific to SMPI (host)");
18
19 /* save user's cost callbacks for SMPI operations */
20 static std::unordered_map<SmpiOperation, SmpiOpCostCb> cost_cbs;
21
22 void smpi_register_op_cost_callback(SmpiOperation op, const SmpiOpCostCb& cb)
23 {
24   cost_cbs[op] = cb;
25 }
26
27 void smpi_cleanup_op_cost_callback()
28 {
29   cost_cbs.clear();
30 }
31
32 namespace simgrid {
33 namespace smpi {
34
35 xbt::Extension<s4u::Host, smpi::Host> Host::EXTENSION_ID;
36
37 double Host::orecv(size_t size, s4u::Host* src, s4u::Host* dst)
38 {
39   /* return user's callback if available */
40   auto it = cost_cbs.find(SmpiOperation::RECV);
41   if (it != cost_cbs.end())
42     return it->second(size, src, dst);
43
44   /* fallback to smpi/or config */
45   double current = orecv_parsed_values.empty() ? 0.0 : orecv_parsed_values.front().values[0] +
46                                                            orecv_parsed_values.front().values[1] * size;
47
48   // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
49   // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
50   // Note: parse_factor() (used before) already sorts the vector we iterate over!
51   for (auto const& fact : orecv_parsed_values) {
52     if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
53       XBT_DEBUG("or : %zu <= %zu return %.10f", size, fact.factor, current);
54       return current;
55     } else {
56       // If the next section is too large, the current section must be used.
57       // Hence, save the cost, as we might have to use it.
58       current=fact.values[0]+fact.values[1]*size;
59     }
60   }
61   XBT_DEBUG("smpi_or: %zu is larger than largest boundary, return %.10f", size, current);
62
63   return current;
64 }
65
66 double Host::osend(size_t size, s4u::Host* src, s4u::Host* dst)
67 {
68   /* return user's callback if available */
69   auto it = cost_cbs.find(SmpiOperation::SEND);
70   if (it != cost_cbs.end())
71     return it->second(size, src, dst);
72
73   /* fallback to smpi/os config */
74   double current =
75       osend_parsed_values.empty() ? 0.0 : osend_parsed_values[0].values[0] + osend_parsed_values[0].values[1] * size;
76   // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
77   // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
78   // Note: parse_factor() (used before) already sorts the vector we iterate over!
79   for (auto const& fact : osend_parsed_values) {
80     if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
81       XBT_DEBUG("os : %zu <= %zu return %.10f", size, fact.factor, current);
82       return current;
83     } else {
84       // If the next section is too large, the current section must be used.
85       // Hence, save the cost, as we might have to use it.
86       current = fact.values[0] + fact.values[1] * size;
87     }
88   }
89   XBT_DEBUG("Searching for smpi/os: %zu is larger than the largest boundary, return %.10f", size, current);
90
91   return current;
92 }
93
94 double Host::oisend(size_t size, s4u::Host* src, s4u::Host* dst)
95 {
96   /* return user's callback if available */
97   auto it = cost_cbs.find(SmpiOperation::ISEND);
98   if (it != cost_cbs.end())
99     return it->second(size, src, dst);
100
101   /* fallback to smpi/ois config */
102   double current =
103       oisend_parsed_values.empty() ? 0.0 : oisend_parsed_values[0].values[0] + oisend_parsed_values[0].values[1] * size;
104
105   // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
106   // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
107   // Note: parse_factor() (used before) already sorts the vector we iterate over!
108   for (auto const& fact : oisend_parsed_values) {
109     if (size <= fact.factor) { // Values already too large, use the previously  computed value of current!
110       XBT_DEBUG("ois : %zu <= %zu return %.10f", size, fact.factor, current);
111       return current;
112     } else {
113       // If the next section is too large, the current section must be used.
114       // Hence, save the cost, as we might have to use it.
115       current = fact.values[0] + fact.values[1] * size;
116     }
117   }
118   XBT_DEBUG("Searching for smpi/ois: %zu is larger than the largest boundary, return %.10f", size, current);
119
120   return current;
121 }
122
123 void Host::check_factor_configs(const std::string& op) const
124 {
125   static const std::unordered_map<std::string, SmpiOperation> name_to_op_enum{
126       {"smpi/or", SmpiOperation::RECV}, {"smpi/os", SmpiOperation::SEND}, {"smpi/ois", SmpiOperation::ISEND}};
127   if (cost_cbs.find(name_to_op_enum.at(op)) != cost_cbs.end() &&
128       (host->get_property(op) || not config::is_default(op.c_str()))) {
129     XBT_WARN("SMPI (host: %s): mismatch cost functions for %s. Only user's callback will be used.", host->get_cname(),
130              op.c_str());
131   }
132 }
133
134 Host::Host(s4u::Host* ptr) : host(ptr)
135 {
136   if (not smpi::Host::EXTENSION_ID.valid())
137     smpi::Host::EXTENSION_ID = s4u::Host::extension_create<Host>();
138
139   check_factor_configs("smpi/or");
140   const char* orecv_string = host->get_property("smpi/or");
141   if (orecv_string != nullptr) {
142     orecv_parsed_values = simgrid::smpi::utils::parse_factor(orecv_string);
143   } else {
144     orecv_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/or"));
145   }
146
147   check_factor_configs("smpi/os");
148   const char* osend_string = host->get_property("smpi/os");
149   if (osend_string != nullptr) {
150     osend_parsed_values = simgrid::smpi::utils::parse_factor(osend_string);
151   } else {
152     osend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/os"));
153   }
154
155   check_factor_configs("smpi/ois");
156   const char* oisend_string = host->get_property("smpi/ois");
157   if (oisend_string != nullptr) {
158     oisend_parsed_values = simgrid::smpi::utils::parse_factor(oisend_string);
159   } else {
160     oisend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/ois"));
161   }
162 }
163
164 } // namespace smpi
165 } // namespace simgrid