Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d60e2d2dc5ba37af55ce76cc3470dd0e1d463ee4
[simgrid.git] / examples / c / exec-async / exec-async.c
1 /* Copyright (c) 2007-2020. 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 #include "simgrid/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/exec.h"
9 #include "simgrid/host.h"
10
11 #include "xbt/asserts.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(exec_async, "Messages specific for this example");
15
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[])
19 {
20   double computation_amount = sg_host_get_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);
25
26   XBT_INFO("Goodbye now!");
27 }
28
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[])
31 {
32   double computation_amount = sg_host_get_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);
36
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);
41   }
42
43   XBT_INFO("Goodbye now!");
44 }
45
46 /* This actor cancels the ongoing execution after a while. */
47 static void canceller(int argc, char* argv[])
48 {
49   double computation_amount = sg_host_get_speed(sg_host_self());
50
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);
57
58   XBT_INFO("Goodbye now!");
59 }
60
61 int main(int argc, char* argv[])
62 {
63   simgrid_init(&argc, argv);
64   simgrid_load_platform(argv[1]);
65
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");
69
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);
73
74   simgrid_run();
75
76   XBT_INFO("Simulation time %g", simgrid_get_clock());
77
78   return 0;
79 }