Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4b8f430f559a517b359522c1dd5b3936dbabe3e0
[simgrid.git] / docs / source / tuto_network_calibration / dahu_platform_dhist.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 "Utils.hpp"
7 #include <boost/property_tree/json_parser.hpp>
8 #include <boost/property_tree/ptree.hpp>
9 #include <map>
10 #include <random>
11 #include <simgrid/kernel/resource/NetworkModelIntf.hpp>
12 #include <simgrid/s4u.hpp>
13 #include <smpi/smpi.h>
14 namespace sg4 = simgrid::s4u;
15
16 class DhistSampler : public Sampler {
17   bool log_;
18   std::vector<double> breaks_;
19   std::vector<double> heights_;
20   std::mt19937& gen_;
21
22 public:
23   DhistSampler(bool log, std::mt19937& gen, const std::vector<double>& b, const std::vector<double> h)
24       : log_(log_), breaks_(b), heights_(h), gen_(gen)
25   {
26   }
27   double sample()
28   {
29     std::piecewise_constant_distribution<double> d(breaks_.begin(), breaks_.end(), heights_.begin());
30     auto value = d(gen_);
31     if (log)
32       value = std::exp(value);
33     return value;
34   }
35 };
36
37 /**
38  * @brief Callback to set latency factor for a communication
39  *
40  * @param latency_base The base latency for this calibration (user-defined)
41  * @param seg Segmentation (user-defined)
42  * @param size Message size (simgrid)
43  */
44 static double latency_factor_cb(double latency_base, const SegmentedRegression& seg, double size,
45                                 const sg4::Host* /*src*/, const sg4::Host* /*dst*/,
46                                 const std::vector<sg4::Link*>& /*links*/,
47                                 const std::unordered_set<sg4::NetZone*>& /*netzones*/)
48 {
49   if (size < 63305)
50     return 0.0; // no calibration for small messages
51
52   return seg.sample(size) / latency_base;
53 }
54
55 /**
56  * @brief Callback to set bandwidth factor for a communication
57  *
58  * @param bw_base The base bandwidth for this calibration (user-defined)
59  * @param seg Segmentation (user-defined)
60  * @param size Message size (simgrid)
61  */
62 static double bw_factor_cb(double bw_base, const SegmentedRegression& seg, double size, const sg4::Host* /*src*/,
63                            const sg4::Host* /*dst*/, const std::vector<sg4::Link*>& /*links*/,
64                            const std::unordered_set<sg4::NetZone*>& /*netzones*/)
65 {
66   if (size < 63305)
67     return 1.0; // no calibration for small messages
68   double est_bw = 1.0 / seg.get_coef(size);
69   return est_bw / bw_base;
70 }
71
72 static double smpi_cost_cb(const SegmentedRegression& seg, double size, sg4::Host* /*src*/, sg4::Host* /*dst*/)
73 {
74   return seg.sample(size);
75 }
76
77 /** @brief Auxiliary method to get list of values from json in a vector */
78 static std::vector<double> get_list_from_json(const boost::property_tree::ptree& pt, const std::string& path)
79 {
80   std::vector<double> v;
81   for (const auto& it : pt.get_child(path)) {
82     double value = it.second.get_value<double>();
83     v.push_back(value);
84   }
85   return v;
86 }
87
88 static SegmentedRegression read_json_file(const std::string& jsonFile, std::mt19937& gen, bool read_coef = true)
89 {
90   boost::property_tree::ptree pt;
91   boost::property_tree::read_json(jsonFile, pt);
92
93   printf("Starting parsing file: %s\n", jsonFile.c_str());
94   pt = pt.get_child("seg"); // go to segments part
95   SegmentedRegression seg(read_coef);
96   for (const auto& it : pt) {
97     double max  = it.second.get_child("max_x").get_value<double>();
98     double coef = it.second.get_child("coeff").get_value<double>();
99
100     seg.append(max, coef,
101                std::make_shared<DhistSampler>(it.second.get_child("log").get_value<bool>(), gen,
102                                               get_list_from_json(it.second, "xbr"),
103                                               get_list_from_json(it.second, "height")));
104   }
105   return seg;
106 }
107
108 /** @brief Programmatic version of dahu */
109 extern "C" void load_platform(const sg4::Engine& e);
110 void load_platform(const sg4::Engine& e)
111 {
112   // setting threshold sync/async modes
113   e.set_config("smpi/async-small-thresh", 63305);
114   e.set_config("smpi/send-is-detached-thresh", 63305);
115
116   /* reading bandwidth and latency base value. It is the same for all regressions, get from first file */
117   boost::property_tree::ptree pt;
118   boost::property_tree::read_json("pingpong_dhist.json", pt);
119   double bw_base  = pt.get_child("bandwidth_base").get_value<double>();
120   double lat_base = pt.get_child("latency_base").get_value<double>();
121   printf("Read bandwidth_base: %e latency_base: %e\n", bw_base, lat_base);
122
123   load_dahu_platform(e, bw_base, lat_base);
124
125   static std::mt19937 gen(42); // remove it from stack, since we need it after this this load_platform function is over
126
127   /* setting network factors callbacks */
128   simgrid::kernel::resource::NetworkModelIntf* model = e.get_netzone_root()->get_network_model();
129   SegmentedRegression seg                            = read_json_file("pingpong_dhist.json", gen, false);
130   model->set_lat_factor_cb(std::bind(&latency_factor_cb, lat_base, seg, std::placeholders::_1, std::placeholders::_2,
131                                      std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
132
133   model->set_bw_factor_cb(std::bind(&bw_factor_cb, bw_base, seg, std::placeholders::_1, std::placeholders::_2,
134                                     std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
135
136   seg = read_json_file("send_dhist.json", gen);
137   smpi_register_op_cost_callback(SmpiOperation::SEND, std::bind(&smpi_cost_cb, seg, std::placeholders::_1,
138                                                                 std::placeholders::_2, std::placeholders::_3));
139
140   seg = read_json_file("isend_dhist.json", gen);
141   smpi_register_op_cost_callback(SmpiOperation::ISEND, std::bind(&smpi_cost_cb, seg, std::placeholders::_1,
142                                                                  std::placeholders::_2, std::placeholders::_3));
143   seg = read_json_file("recv_dhist.json", gen);
144   smpi_register_op_cost_callback(SmpiOperation::RECV, std::bind(&smpi_cost_cb, seg, std::placeholders::_1,
145                                                                 std::placeholders::_2, std::placeholders::_3));
146 }