Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further refacto smpi_factors to reduce dupplication
[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::smpi {
33
34 xbt::Extension<s4u::Host, smpi::Host> Host::EXTENSION_ID;
35
36 double Host::orecv(size_t size, s4u::Host* src, s4u::Host* dst)
37 {
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);
41
42   return orecv_(size);
43 }
44
45 double Host::osend(size_t size, s4u::Host* src, s4u::Host* dst)
46 {
47   /* return user's callback if available */
48   if (auto it = cost_cbs.find(SmpiOperation::SEND); it != cost_cbs.end())
49     return it->second(size, src, dst);
50
51   return osend_(size);
52 }
53
54 double Host::oisend(size_t size, s4u::Host* src, s4u::Host* dst)
55 {
56   /* return user's callback if available */
57   if (auto it = cost_cbs.find(SmpiOperation::ISEND); it != cost_cbs.end())
58     return it->second(size, src, dst);
59
60   return oisend_(size);
61 }
62
63 void Host::check_factor_configs(const std::string& op) const
64 {
65   static const std::unordered_map<std::string, SmpiOperation> name_to_op_enum{
66       {"smpi/or", SmpiOperation::RECV}, {"smpi/os", SmpiOperation::SEND}, {"smpi/ois", SmpiOperation::ISEND}};
67   if (cost_cbs.find(name_to_op_enum.at(op)) != cost_cbs.end() &&
68       (host->get_property(op) || not config::is_default(op.c_str()))) {
69     XBT_WARN("SMPI (host: %s): mismatch cost functions for %s. Only user's callback will be used.", host->get_cname(),
70              op.c_str());
71   }
72 }
73
74 Host::Host(s4u::Host* ptr) : host(ptr)
75 {
76   if (not smpi::Host::EXTENSION_ID.valid())
77     smpi::Host::EXTENSION_ID = s4u::Host::extension_create<Host>();
78
79   check_factor_configs("smpi/or");
80   if (const char* orecv_string = host->get_property("smpi/or"))
81     orecv_.parse(orecv_string);
82   else
83     orecv_.parse(config::get_value<std::string>("smpi/or"));
84
85   check_factor_configs("smpi/os");
86   if (const char* osend_string = host->get_property("smpi/os"))
87     osend_.parse(osend_string);
88   else
89     osend_.parse(config::get_value<std::string>("smpi/os"));
90
91   check_factor_configs("smpi/ois");
92   if (const char* oisend_string = host->get_property("smpi/ois"))
93     oisend_.parse(oisend_string);
94   else
95     oisend_.parse(config::get_value<std::string>("smpi/ois"));
96 }
97
98 } // namespace simgrid::smpi