Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The next day sonar commit
[simgrid.git] / examples / cpp / exec-cpu-nonlinear / s4u-exec-cpu-nonlinear.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 a non-linear resource sharing for
7  * CPUs.
8  */
9
10 #include <simgrid/s4u.hpp>
11
12 namespace sg4 = simgrid::s4u;
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_network_nonlinear, "Messages specific for this s4u example");
15
16 /*************************************************************************************************/
17 static void runner()
18 {
19   double computation_amount = sg4::this_actor::get_host()->get_speed();
20   int n_task                = 10;
21   std::vector<sg4::ExecPtr> tasks;
22
23   XBT_INFO(
24       "Execute %d tasks of %g flops, should take %d second in a CPU without degradation. It will take the double here.",
25       n_task, computation_amount, n_task);
26   for (int i = 0; i < n_task; i++) {
27     tasks.emplace_back(sg4::this_actor::exec_async(computation_amount));
28   }
29   XBT_INFO("Waiting for all tasks to be done!");
30   for (const auto& task : tasks)
31     task->wait();
32
33   XBT_INFO("Finished executing. Goodbye now!");
34 }
35 /*************************************************************************************************/
36 /** @brief Non-linear resource sharing for CPU */
37 static double cpu_nonlinear(const sg4::Host* host, double capacity, int n)
38 {
39   /* emulates a degradation in link according to the number of flows
40    * totally unrealistic but for learning purposes */
41   capacity = n > 1 ? capacity / 2 : capacity;
42   XBT_INFO("Host %s, %d concurrent tasks, new capacity %lf", host->get_cname(), n, capacity);
43   return capacity;
44 }
45
46 /** @brief Create a simple 1-host platform */
47 static void load_platform()
48 {
49   auto* zone        = sg4::create_empty_zone("Zone1");
50   auto* runner_host = zone->create_host("runner", 1e6);
51   runner_host->set_sharing_policy(sg4::Host::SharingPolicy::NONLINEAR,
52                                   std::bind(&cpu_nonlinear, runner_host, std::placeholders::_1, std::placeholders::_2));
53   runner_host->seal();
54   zone->seal();
55
56   /* create actor runner */
57   sg4::Actor::create("runner", runner_host, runner);
58 }
59
60 /*************************************************************************************************/
61 int main(int argc, char* argv[])
62 {
63   sg4::Engine e(&argc, argv);
64
65   /* create platform */
66   load_platform();
67
68   /* runs the simulation */
69   e.run();
70
71   return 0;
72 }