1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/s4u.hpp"
8 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example");
10 /* This actor simply waits for its activity completion after starting it.
11 * That's exactly equivalent to synchronous execution. */
14 double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
15 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
16 simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_init(computation_amount);
20 XBT_INFO("Goodbye now!");
23 /* This actor tests the ongoing execution until its completion, and don't wait before it's terminated. */
26 double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
27 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
28 simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_init(computation_amount);
31 while (not activity->test()) {
32 XBT_INFO("Remaining amount of flops: %g (%.0f%%)", activity->get_remaining(),
33 100 * activity->get_remaining_ratio());
34 simgrid::s4u::this_actor::sleep_for(0.3);
37 XBT_INFO("Goodbye now!");
40 /* This actor cancels the ongoing execution after a while. */
41 static void canceller()
43 double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
45 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
46 simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_async(computation_amount);
47 simgrid::s4u::this_actor::sleep_for(0.5);
48 XBT_INFO("I changed my mind, cancel!");
51 XBT_INFO("Goodbye now!");
54 int main(int argc, char* argv[])
56 simgrid::s4u::Engine e(&argc, argv);
57 e.load_platform(argv[1]);
59 simgrid::s4u::Host* fafard = e.host_by_name("Fafard");
60 simgrid::s4u::Host* ginette = e.host_by_name("Ginette");
61 simgrid::s4u::Host* boivin = e.host_by_name("Boivin");
63 simgrid::s4u::Actor::create("wait", fafard, waiter);
64 simgrid::s4u::Actor::create("monitor", ginette, monitor);
65 simgrid::s4u::Actor::create("cancel", boivin, canceller);
69 XBT_INFO("Simulation time %g", simgrid::s4u::Engine::get_clock());