Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
save some hidden calls to Engine::get_instance
[simgrid.git] / teshsuite / s4u / host-multicore-speed-file / host-multicore-speed-file.cpp
1 /* Copyright (c) 2010-2021. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/kernel/ProfileBuilder.hpp"
8 #include "simgrid/s4u.hpp"
9
10 namespace sg4 = simgrid::s4u;
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
13
14 static void run_activities(int n, double size, double rate = -1.0)
15 {
16   double start = sg4::Engine::get_clock();
17   std::vector<sg4::ExecPtr> activities;
18
19   // initialize exec activities
20   for (int i = 0; i < n; i++) {
21     sg4::ExecPtr op = sg4::this_actor::exec_init(size);
22     if (rate != -1.0)
23       op->set_bound(rate);
24     op->start();
25     activities.emplace_back(op);
26   }
27
28   // waiting for executions
29   for (const auto& act : activities)
30     act->wait();
31   XBT_INFO("Finished running 2 activities, elapsed %lf", sg4::Engine::get_clock() - start);
32 }
33
34 static void worker()
35 {
36   XBT_INFO("Running 1 tasks (1.5e6) in a 2*(1e6) speed host. Should take 2s (1 (limited by core) + 1 (limited by speed "
37            "file)");
38   run_activities(1, 1.5e6);
39
40   XBT_INFO("Running 1 tasks (.8e6) in a 2*(1e6) speed host with limited rate (.4e6). Should take 2s (limited by rate)");
41   run_activities(1, .8e6, .4e6);
42
43   XBT_INFO("Running 1 tasks (1.1e6) in a 2*(1e6) speed host with limited rate (.6e6). Should take 2s (.6 limited by "
44            "rate + .5 limited by speed file)");
45   run_activities(1, 1.1e6, .6e6);
46
47   XBT_INFO("Running 2 tasks (1e6) in a 2*(1e6) speed host. Should take 1s");
48   run_activities(2, 1e6);
49
50   XBT_INFO("Running 2 tasks (.5e6) in a .5*2*(1e6) speed host. Should take 1s");
51   run_activities(2, .5e6);
52
53   XBT_INFO("Running 2 tasks (1.1e6) with limited rate (.6e6). Should take 2s (0.6 limited by rate + 0.5 limited by "
54            "speed file)");
55   run_activities(2, 1.1e6, .6e6);
56
57   XBT_INFO("Running 2 tasks (.8e6) with limited rate (.4e6). Should take 2s (limited by rate)");
58   run_activities(2, .8e6, .4e6);
59
60   XBT_INFO("I'm done. See you!");
61 }
62
63 static void failed_worker()
64 {
65   XBT_INFO("Running a 2 tasks: a small .5e6 and a big 2e6.");
66   sg4::ExecPtr ok   = sg4::this_actor::exec_init(.5e6);
67   sg4::ExecPtr fail = sg4::this_actor::exec_init(2e6);
68   ok->wait();
69   XBT_INFO("Finished the small task");
70   try {
71     XBT_INFO("Waiting big task to finish");
72     fail->wait();
73   } catch (const simgrid::ForcefulKillException&) {
74     XBT_INFO("Unable to finish big task, host went down");
75   }
76 }
77
78 int main(int argc, char* argv[])
79 {
80   sg4::Engine e(&argc, argv);
81
82   /* speed and state file description */
83   const char* erin_state_file  = R"(
84 0 1
85 1 0
86 50 1
87 100 1
88 )";
89   const char* carol_speed_file = R"(
90 0 1.0
91 1 0.5
92 )";
93   /* simple platform containing 1 host and 2 disk */
94   auto* zone = sg4::create_full_zone("red");
95   zone->create_host("erin", 1e6)
96       ->set_core_count(2)
97       ->set_state_profile(simgrid::kernel::profile::ProfileBuilder::from_string("erin_state", erin_state_file, 0))
98       ->seal();
99   zone->create_host("carol", 1e6)
100       ->set_core_count(2)
101       ->set_speed_profile(simgrid::kernel::profile::ProfileBuilder::from_string("carol_speed", carol_speed_file, 1))
102       ->seal();
103   zone->seal();
104
105   sg4::Actor::create("carol", e.host_by_name("carol"), worker);
106   sg4::Actor::create("erin", e.host_by_name("erin"), failed_worker)->set_auto_restart(true);
107
108   e.run();
109
110   XBT_INFO("Simulation time %g", e.get_clock());
111
112   return 0;
113 }