1 /* Copyright (c) 2007-2019. 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 task 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);
38 XBT_INFO("Goodbye now!");
41 /* This actor cancels the ongoing execution after a while. */
42 static void canceller()
44 double computation_amount = simgrid::s4u::this_actor::get_host()->get_speed();
46 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
47 simgrid::s4u::ExecPtr activity = simgrid::s4u::this_actor::exec_async(computation_amount);
48 simgrid::s4u::this_actor::sleep_for(0.5);
49 XBT_INFO("I changed my mind, cancel!");
52 XBT_INFO("Goodbye now!");
55 int main(int argc, char* argv[])
57 simgrid::s4u::Engine e(&argc, argv);
58 e.load_platform(argv[1]);
60 simgrid::s4u::Host* fafard = simgrid::s4u::Host::by_name("Fafard");
61 simgrid::s4u::Host* ginette = simgrid::s4u::Host::by_name("Ginette");
62 simgrid::s4u::Host* boivin = simgrid::s4u::Host::by_name("Boivin");
64 simgrid::s4u::Actor::create("wait", fafard, waiter);
65 simgrid::s4u::Actor::create("monitor", ginette, monitor);
66 simgrid::s4u::Actor::create("cancel", boivin, canceller);
70 XBT_INFO("Simulation time %g", e.get_clock());