1 /* Copyright (c) 2006-2021. 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. */
7 #include <boost/property_tree/json_parser.hpp>
8 #include <boost/property_tree/ptree.hpp>
11 #include <simgrid/kernel/resource/NetworkModelIntf.hpp>
12 #include <simgrid/s4u.hpp>
13 #include <smpi/smpi.h>
14 namespace sg4 = simgrid::s4u;
16 class DhistSampler : public Sampler {
18 std::vector<double> breaks_;
19 std::vector<double> heights_;
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)
29 std::piecewise_constant_distribution<double> d(breaks_.begin(), breaks_.end(), heights_.begin());
32 value = std::exp(value);
38 * @brief Callback to set latency factor for a communication
40 * @param latency_base The base latency for this calibration (user-defined)
41 * @param seg Segmentation (user-defined)
42 * @param size Message size (simgrid)
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*/)
50 return 0.0; // no calibration for small messages
52 return seg.sample(size) / latency_base;
56 * @brief Callback to set bandwidth factor for a communication
58 * @param bw_base The base bandwidth for this calibration (user-defined)
59 * @param seg Segmentation (user-defined)
60 * @param size Message size (simgrid)
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*/)
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;
72 static double smpi_cost_cb(const SegmentedRegression& seg, double size, sg4::Host* /*src*/, sg4::Host* /*dst*/)
74 return seg.sample(size);
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)
80 std::vector<double> v;
81 for (const auto& it : pt.get_child(path)) {
82 double value = it.second.get_value<double>();
88 static SegmentedRegression read_json_file(const std::string& jsonFile, std::mt19937& gen, bool read_coef = true)
90 boost::property_tree::ptree pt;
91 boost::property_tree::read_json(jsonFile, pt);
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>();
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")));
108 /** @brief Programmatic version of dahu */
109 extern "C" void load_platform(const sg4::Engine& e);
110 void load_platform(const sg4::Engine& e)
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);
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);
123 load_dahu_platform(e, bw_base, lat_base);
125 static std::mt19937 gen(42); // remove it from stack, since we need it after this this load_platform function is over
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));
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));
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));
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));