Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sonar code smells
[simgrid.git] / examples / smpi / comm_dynamic_costs / comm-dynamic-cost.cpp
1 /* Copyright (c) 2006-2021. 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/s4u.hpp>
7 #include <smpi/smpi.h>
8 namespace sg4 = simgrid::s4u;
9
10 /**
11  * @brief User's callback to set dynamic cost for MPI operations
12  *
13  * Note that src host can be a nullptr for some collective operations.
14  * This is due the creation of an special requests that aggregates sub-requests
15  * in collective operations. But it doesn't happen in this example
16  *
17  * @param op MPI Operation (set by user at cb registering below)
18  * @param size Message size (set by simgrid)
19  * @param src Source host (set by simgrid)
20  * @param dst Source host (set by simgrid)
21  */
22 static double smpi_cost_cb(SmpiOperation op, size_t /*size*/, const sg4::Host* src, const sg4::Host* dst)
23 {
24   /* some dummy cost that depends on the operation and host */
25   static std::unordered_map<std::string, double> read_cost  = {{"Tremblay", 1}, {"Jupiter", 2}};
26   static std::unordered_map<std::string, double> write_cost = {{"Tremblay", 5}, {"Jupiter", 10}};
27
28   if (op == SmpiOperation::RECV)
29     return read_cost[src->get_name()];
30
31   return write_cost[dst->get_name()];
32 }
33
34 /*
35  * Creates a platform for examples/smpi/simple-execute/simple-execute.c MPI program
36  *
37  * Sets specific cost for MPI_Send and MPI_Recv operations
38  */
39 extern "C" void load_platform(const sg4::Engine& e);
40 void load_platform(const sg4::Engine& /*e*/)
41 {
42   /* create a simple 2 host platform inspired from small_platform.xml */
43   auto* root = sg4::create_full_zone("zone0");
44
45   const sg4::Host* tremblay = root->create_host("Tremblay", "98.095Mf")->seal();
46   const sg4::Host* jupiter  = root->create_host("Jupiter", "76.296Mf")->seal();
47
48   const sg4::Link* link9 = root->create_split_duplex_link("9", "7.20975MBps")->set_latency("1.461517ms")->seal();
49
50   root->add_route(tremblay->get_netpoint(), jupiter->get_netpoint(), nullptr, nullptr,
51                   {{link9, sg4::LinkInRoute::Direction::UP}}, true);
52   root->seal();
53
54   /* set cost callback for MPI_Send and MPI_Recv */
55   smpi_register_op_cost_callback(SmpiOperation::SEND,
56                                  std::bind(&smpi_cost_cb, SmpiOperation::SEND, std::placeholders::_1,
57                                            std::placeholders::_2, std::placeholders::_3));
58
59   smpi_register_op_cost_callback(SmpiOperation::RECV,
60                                  std::bind(&smpi_cost_cb, SmpiOperation::RECV, std::placeholders::_1,
61                                            std::placeholders::_2, std::placeholders::_3));
62 }