1 /* Copyright (c) 2006-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. */
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 NormalMixture : public Sampler {
17 std::vector<std::normal_distribution<double>> mixture_;
18 std::vector<double> prob_;
22 NormalMixture(std::mt19937& gen) : gen_(gen) {}
23 void append(double mean, double stddev, double prob)
25 mixture_.push_back(std::normal_distribution<double>(mean, stddev));
26 prob_.push_back(prob);
30 std::discrete_distribution<> d(prob_.begin(), prob_.end());
32 auto& normal = mixture_[index];
33 double value = normal(gen_);
39 * @brief Callback to set latency factor for a communication
41 * @param latency_base The base latency for this calibration (user-defined)
42 * @param seg Segmentation (user-defined)
43 * @param size Message size (simgrid)
45 static double latency_factor_cb(double latency_base, const SegmentedRegression& seg, double size,
46 const sg4::Host* /*src*/, const sg4::Host* /*dst*/,
47 const std::vector<sg4::Link*>& /*links*/,
48 const std::unordered_set<sg4::NetZone*>& /*netzones*/)
51 return 0.0; // no calibration for small messages
53 return seg.sample(size) / latency_base;
57 * @brief Callback to set bandwidth factor for a communication
59 * @param bw_base The base bandwidth for this calibration (user-defined)
60 * @param seg Segmentation (user-defined)
61 * @param size Message size (simgrid)
63 static double bw_factor_cb(double bw_base, const SegmentedRegression& seg, double size, const sg4::Host* /*src*/,
64 const sg4::Host* /*dst*/, const std::vector<sg4::Link*>& /*links*/,
65 const std::unordered_set<sg4::NetZone*>& /*netzones*/)
68 return 1.0; // no calibration for small messages
69 double est_bw = 1.0 / seg.get_coef(size);
70 return est_bw / bw_base;
73 static double smpi_cost_cb(const SegmentedRegression& seg, double size, sg4::Host* /*src*/, sg4::Host* /*dst*/)
75 return seg.sample(size);
78 static SegmentedRegression read_json_file(const std::string& jsonFile, std::mt19937& gen, bool read_coef = true)
80 boost::property_tree::ptree pt;
81 boost::property_tree::read_json(jsonFile, pt);
82 std::unordered_map<double, std::shared_ptr<NormalMixture>> mixtures;
83 std::unordered_map<double, double> coefs;
84 printf("Starting parsing file: %s\n", jsonFile.c_str());
85 pt = pt.get_child("seg"); // go to segments part
86 SegmentedRegression seg(read_coef);
87 for (const auto& it : pt) {
88 double max = it.second.get_child("max_x").get_value<double>();
89 coefs[max] = it.second.get_child("coefficient").get_value<double>();
90 auto& mixture = mixtures[max];
92 mixture = std::make_shared<NormalMixture>(gen);
93 mixture->append(it.second.get_child("mean").get_value<double>(), it.second.get_child("sd").get_value<double>(),
94 it.second.get_child("prob").get_value<double>());
96 for (const auto& i : mixtures) {
97 seg.append(i.first, coefs[i.first], i.second);
102 /** @brief Programmatic version of dahu */
103 extern "C" void load_platform(const sg4::Engine& e);
104 void load_platform(const sg4::Engine& e)
106 // setting threshold sync/async modes
107 e.set_config("smpi/async-small-thresh", 63305);
108 e.set_config("smpi/send-is-detached-thresh", 63305);
110 /* reading bandwidth and latency base value. It is the same for all regressions, get from first file */
111 boost::property_tree::ptree pt;
112 boost::property_tree::read_json("pingpong_ckmeans.json", pt);
113 double bw_base = pt.get_child("bandwidth_base").get_value<double>();
114 double lat_base = pt.get_child("latency_base").get_value<double>();
115 printf("Read bandwidth_base: %e latency_base: %e\n", bw_base, lat_base);
117 load_dahu_platform(e, bw_base, lat_base);
119 static std::mt19937 gen(42); // remove it from stack, since we need it after this this load_platform function is over
121 /* setting network factors callbacks */
122 simgrid::kernel::resource::NetworkModelIntf* model = e.get_netzone_root()->get_network_model();
124 SegmentedRegression seg = read_json_file("pingpong_ckmeans.json", gen, false);
125 model->set_lat_factor_cb(std::bind(&latency_factor_cb, lat_base, seg, std::placeholders::_1, std::placeholders::_2,
126 std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
128 model->set_bw_factor_cb(std::bind(&bw_factor_cb, bw_base, seg, std::placeholders::_1, std::placeholders::_2,
129 std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
131 seg = read_json_file("send_ckmeans.json", gen);
132 smpi_register_op_cost_callback(SmpiOperation::SEND, std::bind(&smpi_cost_cb, seg, std::placeholders::_1,
133 std::placeholders::_2, std::placeholders::_3));
135 seg = read_json_file("isend_ckmeans.json", gen);
136 smpi_register_op_cost_callback(SmpiOperation::ISEND, std::bind(&smpi_cost_cb, seg, std::placeholders::_1,
137 std::placeholders::_2, std::placeholders::_3));
138 seg = read_json_file("recv_ckmeans.json", gen);
139 smpi_register_op_cost_callback(SmpiOperation::RECV, std::bind(&smpi_cost_cb, seg, std::placeholders::_1,
140 std::placeholders::_2, std::placeholders::_3));