Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d8e5866db85d9094a78c4ee19991c07f3f6accc9
[simgrid.git] / examples / s4u / engine-filtering / s4u-engine-filtering.cpp
1 /* Copyright (c) 2017-2018. 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 "xbt/log.h"
7 #include <simgrid/s4u.hpp>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_engine_filtering, "Messages specific for this s4u example");
10
11 namespace filter {
12 class FrequencyChanged {
13   std::map<simgrid::s4u::Host*, int> host_list;
14
15 public:
16   explicit FrequencyChanged(simgrid::s4u::Engine& e)
17   {
18     std::vector<simgrid::s4u::Host*> list = e.get_all_hosts();
19     for (auto& host : list) {
20       host_list.insert({host, host->get_pstate()});
21     }
22   }
23
24   bool operator()(simgrid::s4u::Host* host) { return host->get_pstate() != host_list.at(host); }
25
26   double get_old_speed(simgrid::s4u::Host* host) { return host_list.at(host); }
27 };
28 class SingleCore {
29 public:
30   bool operator()(simgrid::s4u::Host* host) { return host->get_core_count() == 1; }
31 };
32
33 bool filter_speed_more_than_50Mf(simgrid::s4u::Host* host);
34 bool filter_speed_more_than_50Mf(simgrid::s4u::Host* host)
35 {
36   return host->getSpeed() > 50E6;
37 }
38 }
39 int main(int argc, char* argv[])
40 {
41   simgrid::s4u::Engine e(&argc, argv);
42   e.load_platform(argv[1]);
43
44   /**
45    * Use a lambda function to filter hosts: We only want multicore hosts
46    */
47   XBT_INFO("Hosts currently registered with this engine: %zu", e.get_host_count());
48   std::vector<simgrid::s4u::Host*> list =
49       e.get_filtered_hosts([](simgrid::s4u::Host* host) { return host->get_core_count() > 1; });
50
51   for (auto& host : list)
52     XBT_INFO("The following hosts have more than one core: %s", host->get_cname());
53
54   xbt_assert(list.size() == 1);
55
56   /*
57    * Use a function object (functor) without memory
58    */
59   list = e.get_filtered_hosts(filter::SingleCore());
60
61   for (auto& host : list)
62     XBT_INFO("The following hosts are SingleCore: %s", host->get_cname());
63
64   /**
65    * This just shows how to use a function object that uses
66    * memory to filter.
67    */
68   XBT_INFO("A simple example: Let's retrieve all hosts that changed their frequency");
69   filter::FrequencyChanged filter(e);
70   e.host_by_name("MyHost2")->set_pstate(2);
71   list = e.get_filtered_hosts(filter);
72
73   for (auto& host : list)
74     XBT_INFO("The following hosts changed their frequency: %s (from %.1ff to %.1ff)", host->get_cname(), host->getPstateSpeed(filter.get_old_speed(host)), host->getSpeed());
75
76   /*
77    * You can also just use any normal function (namespaced as well, if you want) to filter
78    */
79   list = e.get_filtered_hosts(filter::filter_speed_more_than_50Mf);
80
81   for (auto& host : list)
82     XBT_INFO("The following hosts have a frequency > 50Mf: %s", host->get_cname());
83
84   return 0;
85 }