Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
820614bbc91cd889e89611cf98e0540a1615fcde
[simgrid.git] / examples / cpp / exec-cpu-factors / s4u-exec-cpu-factors.cpp
1 /* Copyright (c) 2010-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 /* This example shows how to simulate variability for CPUs, using multiplicative factors
7  */
8
9 #include <simgrid/s4u.hpp>
10
11 namespace sg4 = simgrid::s4u;
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
14
15 /*************************************************************************************************/
16 static void runner()
17 {
18   double computation_amount = sg4::this_actor::get_host()->get_speed();
19
20   XBT_INFO("Executing 1 tasks of %g flops, should take 1 second.", computation_amount);
21   sg4::this_actor::execute(computation_amount);
22   XBT_INFO("Executing 1 tasks of %g flops, it would take .001s without factor. It'll take .002s",
23            computation_amount / 10);
24   sg4::this_actor::execute(computation_amount / 1000);
25
26   XBT_INFO("Finished executing. Goodbye now!");
27 }
28 /*************************************************************************************************/
29 /** @brief Variability for CPU */
30 static double cpu_variability(const sg4::Host* host, double flops)
31 {
32   /* creates variability for tasks smaller than 1% of CPU power.
33    * unrealistic, for learning purposes */
34   double factor = 1.0;
35   double speed  = host->get_speed();
36   if (flops < speed / 100) {
37     factor = 0.5;
38   }
39   XBT_INFO("Host %s, task with %lf flops, new factor %lf", host->get_cname(), flops, factor);
40   return factor;
41 }
42
43 /** @brief Create a simple 1-host platform */
44 static void load_platform()
45 {
46   auto* zone        = sg4::create_empty_zone("Zone1");
47   auto* runner_host = zone->create_host("runner", 1e6);
48   runner_host->set_factor_cb(std::bind(&cpu_variability, runner_host, std::placeholders::_1))->seal();
49   zone->seal();
50
51   /* create actor runner */
52   sg4::Actor::create("runner", runner_host, runner);
53 }
54
55 /*************************************************************************************************/
56 int main(int argc, char* argv[])
57 {
58   sg4::Engine e(&argc, argv);
59
60   /* create platform */
61   load_platform();
62
63   /* runs the simulation */
64   e.run();
65
66   return 0;
67 }