Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add an example for Engine::run_until()
[simgrid.git] / examples / cpp / engine-run-partial / s4u-engine-run-partial.cpp
1 /* Copyright (c) 2007-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 is to show how to use Engine::run(date) to simulate only up to that point in time */
7
8 #include "simgrid/s4u.hpp"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
11
12 /* This actor simply executes and waits the activity it got as a parameter. */
13 static void runner(simgrid::s4u::ExecPtr activity)
14 {
15   activity->start();
16   activity->wait();
17
18   XBT_INFO("Goodbye now!");
19 }
20
21 /* This actor tests the ongoing execution until its completion, and don't wait before it's terminated. */
22 static void monitor()
23 {
24   double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
25   XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
26   simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_init(computation_amount);
27   activity->start();
28
29   while (not activity->test()) {
30     XBT_INFO("Remaining amount of flops: %g (%.0f%%)", activity->get_remaining(),
31              100 * activity->get_remaining_ratio());
32     simgrid::s4u::this_actor::sleep_for(0.3);
33   }
34
35   XBT_INFO("Goodbye now!");
36 }
37
38 /* This actor cancels the ongoing execution after a while. */
39 static void canceller()
40 {
41   double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
42
43   XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
44   simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_async(computation_amount);
45   simgrid::s4u::this_actor::sleep_for(0.5);
46   XBT_INFO("I changed my mind, cancel!");
47   activity->cancel();
48
49   XBT_INFO("Goodbye now!");
50 }
51
52 int main(int argc, char* argv[])
53 {
54   simgrid::s4u::Engine e(&argc, argv);
55   e.load_platform(argv[1]);
56
57   simgrid::s4u::Host* fafard = e.host_by_name("Fafard");
58
59   simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_init(fafard->get_speed() * 10.)->set_host(fafard);
60   simgrid::s4u::Actor::create("runner", fafard, runner, activity);
61
62   while (activity->get_remaining() > 0) {
63     XBT_INFO("Remaining amount of flops: %g (%.0f%%)", activity->get_remaining(),
64              100 * activity->get_remaining_ratio());
65     e.run_until(e.get_clock() + 1);
66   }
67
68   XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());
69
70   return 0;
71 }