1 /* Copyright (c) 2007-2020. 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/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/exec.h"
9 #include "simgrid/host.h"
11 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(exec_async, "Messages specific for this example");
16 /* This actor simply waits for its task completion after starting it.
17 * That's exactly equivalent to synchronous execution. */
18 static void waiter(int argc, char* argv[])
20 double computation_amount = sg_host_speed(sg_host_self());
21 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
22 sg_exec_t activity = sg_actor_exec_init(computation_amount);
23 sg_exec_start(activity);
24 sg_exec_wait(activity);
26 XBT_INFO("Goodbye now!");
29 /* This actor tests the ongoing execution until its completion, and don't wait before it's terminated. */
30 static void monitor(int argc, char* argv[])
32 double computation_amount = sg_host_speed(sg_host_self());
33 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
34 sg_exec_t activity = sg_actor_exec_init(computation_amount);
35 sg_exec_start(activity);
37 while (!sg_exec_test(activity)) {
38 XBT_INFO("Remaining amount of flops: %g (%.0f%%)", sg_exec_get_remaining(activity),
39 100 * sg_exec_get_remaining_ratio(activity));
40 sg_actor_sleep_for(0.3);
43 XBT_INFO("Goodbye now!");
46 /* This actor cancels the ongoing execution after a while. */
47 static void canceller(int argc, char* argv[])
49 double computation_amount = sg_host_speed(sg_host_self());
51 XBT_INFO("Execute %g flops, should take 1 second.", computation_amount);
52 sg_exec_t activity = sg_actor_exec_init(computation_amount);
53 sg_exec_start(activity);
54 sg_actor_sleep_for(0.5);
55 XBT_INFO("I changed my mind, cancel!");
56 sg_exec_cancel(activity);
58 XBT_INFO("Goodbye now!");
61 int main(int argc, char* argv[])
63 simgrid_init(&argc, argv);
64 simgrid_load_platform(argv[1]);
66 sg_host_t fafard = sg_host_by_name("Fafard");
67 sg_host_t ginette = sg_host_by_name("Ginette");
68 sg_host_t boivin = sg_host_by_name("Boivin");
70 sg_actor_create("wait", fafard, waiter, 0, NULL);
71 sg_actor_create("monitor", ginette, monitor, 0, NULL);
72 sg_actor_create("cancel", boivin, canceller, 0, NULL);
76 XBT_INFO("Simulation time %g", simgrid_get_clock());