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 static double factor_use(std::vector<s_smpi_factor_t>& factors, const char* name, size_t size)
38 /* fallback to smpi/or config */
39 double current = factors.empty() ? 0.0 : factors.front().values[0] + factors.front().values[1] * size;
41 // Iterate over all the sections that were specified and find the right value. (fact.factor represents the interval
42 // sizes; we want to find the section that has fact.factor <= size and no other such fact.factor <= size)
43 // Note: parse_factor() (used before) already sorts the vector we iterate over!
44 for (auto const& fact : factors) {
45 if (size <= fact.factor) { // Values already too large, use the previously computed value of current!
46 XBT_DEBUG("%s: %zu <= %zu return %.10f", name, size, fact.factor, current);
49 // If the next section is too large, the current section must be used.
50 // Hence, save the cost, as we might have to use it.
51 current=fact.values[0]+fact.values[1]*size;
54 XBT_DEBUG("%s: %zu is larger than largest boundary, return %.10f", name, size, current);
59 double Host::orecv(size_t size, s4u::Host* src, s4u::Host* dst)
61 /* return user's callback if available */
62 if (auto it = cost_cbs.find(SmpiOperation::RECV); it != cost_cbs.end())
63 return it->second(size, src, dst);
65 return factor_use(orecv_parsed_values, "smpi/or", size);
68 double Host::osend(size_t size, s4u::Host* src, s4u::Host* dst)
70 /* return user's callback if available */
71 if (auto it = cost_cbs.find(SmpiOperation::SEND); it != cost_cbs.end())
72 return it->second(size, src, dst);
74 return factor_use(osend_parsed_values, "smpi/os", size);
77 double Host::oisend(size_t size, s4u::Host* src, s4u::Host* dst)
79 /* return user's callback if available */
80 if (auto it = cost_cbs.find(SmpiOperation::ISEND); it != cost_cbs.end())
81 return it->second(size, src, dst);
83 return factor_use(oisend_parsed_values, "smpi/ois", size);
86 void Host::check_factor_configs(const std::string& op) const
88 static const std::unordered_map<std::string, SmpiOperation> name_to_op_enum{
89 {"smpi/or", SmpiOperation::RECV}, {"smpi/os", SmpiOperation::SEND}, {"smpi/ois", SmpiOperation::ISEND}};
90 if (cost_cbs.find(name_to_op_enum.at(op)) != cost_cbs.end() &&
91 (host->get_property(op) || not config::is_default(op.c_str()))) {
92 XBT_WARN("SMPI (host: %s): mismatch cost functions for %s. Only user's callback will be used.", host->get_cname(),
97 Host::Host(s4u::Host* ptr) : host(ptr)
99 if (not smpi::Host::EXTENSION_ID.valid())
100 smpi::Host::EXTENSION_ID = s4u::Host::extension_create<Host>();
102 check_factor_configs("smpi/or");
103 if (const char* orecv_string = host->get_property("smpi/or")) {
104 orecv_parsed_values = simgrid::smpi::utils::parse_factor(orecv_string);
106 orecv_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/or"));
109 check_factor_configs("smpi/os");
110 if (const char* osend_string = host->get_property("smpi/os")) {
111 osend_parsed_values = simgrid::smpi::utils::parse_factor(osend_string);
113 osend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/os"));
116 check_factor_configs("smpi/ois");
117 if (const char* oisend_string = host->get_property("smpi/ois")) {
118 oisend_parsed_values = simgrid::smpi::utils::parse_factor(oisend_string);
120 oisend_parsed_values = simgrid::smpi::utils::parse_factor(config::get_value<std::string>("smpi/ois"));
124 } // namespace simgrid::smpi