1 /* Copyright (c) 2017-2022. 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. */
6 #include "smpi_host.hpp"
8 #include "simgrid/s4u/VirtualMachine.hpp"
10 #include "smpi_utils.hpp"
11 #include "xbt/config.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_host, smpi, "Logging specific to SMPI (host)");
19 /* save user's cost callbacks for SMPI operations */
20 static std::unordered_map<SmpiOperation, SmpiOpCostCb> cost_cbs;
22 void smpi_register_op_cost_callback(SmpiOperation op, const SmpiOpCostCb& cb)
27 void smpi_cleanup_op_cost_callback()
32 namespace simgrid::smpi {
34 xbt::Extension<s4u::Host, smpi::Host> Host::EXTENSION_ID;
36 double Host::orecv(size_t size, s4u::Host* src, s4u::Host* dst)
38 /* return user's callback if available */
39 if (auto it = cost_cbs.find(SmpiOperation::RECV); it != cost_cbs.end())
40 return it->second(size, src, dst);
42 /* fallback to smpi/or config */
43 double current = orecv_parsed_values.empty() ? 0.0 : orecv_parsed_values.front().values[0] +
44 orecv_parsed_values.front().values[1] * size;
46 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
47 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
48 // Note: parse_factor() (used before) already sorts the vector we iterate over!
49 for (auto const& fact : orecv_parsed_values) {
50 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
51 XBT_DEBUG("or : %zu <= %zu return %.10f", size, fact.factor, current);
54 // If the next section is too large, the current section must be used.
55 // Hence, save the cost, as we might have to use it.
56 current=fact.values[0]+fact.values[1]*size;
59 XBT_DEBUG("smpi_or: %zu is larger than largest boundary, return %.10f", size, current);
64 double Host::osend(size_t size, s4u::Host* src, s4u::Host* dst)
66 /* return user's callback if available */
67 if (auto it = cost_cbs.find(SmpiOperation::SEND); it != cost_cbs.end())
68 return it->second(size, src, dst);
70 /* fallback to smpi/os config */
72 osend_parsed_values.empty() ? 0.0 : osend_parsed_values[0].values[0] + osend_parsed_values[0].values[1] * size;
73 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
74 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
75 // Note: parse_factor() (used before) already sorts the vector we iterate over!
76 for (auto const& fact : osend_parsed_values) {
77 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
78 XBT_DEBUG("os : %zu <= %zu return %.10f", size, fact.factor, current);
81 // If the next section is too large, the current section must be used.
82 // Hence, save the cost, as we might have to use it.
83 current = fact.values[0] + fact.values[1] * size;
86 XBT_DEBUG("Searching for smpi/os: %zu is larger than the largest boundary, return %.10f", size, current);
91 double Host::oisend(size_t size, s4u::Host* src, s4u::Host* dst)
93 /* return user's callback if available */
94 if (auto it = cost_cbs.find(SmpiOperation::ISEND); it != cost_cbs.end())
95 return it->second(size, src, dst);
97 /* fallback to smpi/ois config */
99 oisend_parsed_values.empty() ? 0.0 : oisend_parsed_values[0].values[0] + oisend_parsed_values[0].values[1] * size;
101 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
102 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
103 // Note: parse_factor() (used before) already sorts the vector we iterate over!
104 for (auto const& fact : oisend_parsed_values) {
105 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
106 XBT_DEBUG("ois : %zu <= %zu return %.10f", size, fact.factor, current);
109 // If the next section is too large, the current section must be used.
110 // Hence, save the cost, as we might have to use it.
111 current = fact.values[0] + fact.values[1] * size;
114 XBT_DEBUG("Searching for smpi/ois: %zu is larger than the largest boundary, return %.10f", size, current);
119 void Host::check_factor_configs(const std::string& op) const
121 static const std::unordered_map<std::string, SmpiOperation> name_to_op_enum{
122 {"smpi/or", SmpiOperation::RECV}, {"smpi/os", SmpiOperation::SEND}, {"smpi/ois", SmpiOperation::ISEND}};
123 if (cost_cbs.find(name_to_op_enum.at(op)) != cost_cbs.end() &&
124 (host->get_property(op) || not config::is_default(op.c_str()))) {
125 XBT_WARN("SMPI (host: %s): mismatch cost functions for %s. Only user's callback will be used.", host->get_cname(),
130 Host::Host(s4u::Host* ptr) : host(ptr)
132 if (not smpi::Host::EXTENSION_ID.valid())
133 smpi::Host::EXTENSION_ID = s4u::Host::extension_create<Host>();
135 check_factor_configs("smpi/or");
136 if (const char* orecv_string = host->get_property("smpi/or")) {
137 orecv_parsed_values = simgrid::smpi::utils::parse_factor(orecv_string);
139 orecv_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/or"));
142 check_factor_configs("smpi/os");
143 if (const char* osend_string = host->get_property("smpi/os")) {
144 osend_parsed_values = simgrid::smpi::utils::parse_factor(osend_string);
146 osend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/os"));
149 check_factor_configs("smpi/ois");
150 if (const char* oisend_string = host->get_property("smpi/ois")) {
151 oisend_parsed_values = simgrid::smpi::utils::parse_factor(oisend_string);
153 oisend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/ois"));
157 } // namespace simgrid::smpi