Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / examples / cpp / exec-cpu-factors / s4u-exec-cpu-factors.cpp
1 /* Copyright (c) 2010-2023. 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 = (flops < host->get_speed() / 100) ? 0.5 : 1.0;
35   XBT_INFO("Host %s, task with %lf flops, new factor %lf", host->get_cname(), flops, factor);
36   return factor;
37 }
38
39 /** @brief Create a simple 1-host platform */
40 static void load_platform()
41 {
42   auto* zone        = sg4::create_empty_zone("Zone1");
43   auto* runner_host = zone->create_host("runner", 1e6);
44   runner_host->set_factor_cb(std::bind(&cpu_variability, runner_host, std::placeholders::_1))->seal();
45   zone->seal();
46
47   /* create actor runner */
48   sg4::Actor::create("runner", runner_host, runner);
49 }
50
51 /*************************************************************************************************/
52 int main(int argc, char* argv[])
53 {
54   sg4::Engine e(&argc, argv);
55
56   /* create platform */
57   load_platform();
58
59   /* runs the simulation */
60   e.run();
61
62   return 0;
63 }