Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / docs / source / tuto_network_calibration / Utils.hpp
1 /* Copyright (c) 2006-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 <map>
7 #include <memory>
8 #include <simgrid/s4u.hpp>
9 #include <vector>
10
11 /** @brief Creates a platform based on dahu cluster in Grid'5000 */
12 void load_dahu_platform(const simgrid::s4u::Engine& e, double bw, double lat);
13
14 /** @brief Noise generation interface */
15 class Sampler {
16 public:
17   virtual double sample() = 0;
18 };
19
20 /** @brief Segmented noise generation. Use different sampler for each message size */
21 class SegmentedRegression {
22   std::map<double, std::shared_ptr<Sampler>> segments_;
23   std::map<double, double> coef_;
24   bool use_coef_;
25   std::shared_ptr<Sampler> get_sampler(double x) const
26   {
27     auto sampler = segments_.upper_bound(x);
28     if (sampler->first >= x)
29       return sampler->second;
30     THROW_IMPOSSIBLE;
31   }
32
33 public:
34   explicit SegmentedRegression(bool use = true) : use_coef_(use) {}
35   void append(double max, double coef, std::shared_ptr<Sampler> sampler)
36   {
37     coef_[max]     = coef;
38     segments_[max] = std::move(sampler);
39   }
40
41   double sample(double x) const
42   {
43     double value = get_sampler(x)->sample();
44     if (use_coef_)
45       value += x * coef_.upper_bound(x)->second;
46     return value;
47   }
48
49   double get_coef(double x) const { return coef_.upper_bound(x)->second; }
50 };