Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / examples / c / actor-migrate / actor-migrate.c
1 /* Copyright (c) 2009-2023. 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/barrier.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/host.h"
10
11 #include "xbt/asserts.h"
12 #include "xbt/log.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(actor_migrate, "Messages specific for this example");
15
16 static void worker(int argc, char* argv[])
17 {
18   xbt_assert(argc > 2);
19   sg_host_t first  = sg_host_by_name(argv[1]);
20   const_sg_host_t second = sg_host_by_name(argv[2]);
21
22   double flopAmount = sg_host_get_speed(first) * 5 + sg_host_get_speed(second) * 5;
23
24   XBT_INFO("Let's move to %s to execute %.2f Mflops (5sec on %s and 5sec on %s)", argv[1], flopAmount / 1e6, argv[1],
25            argv[2]);
26
27   sg_actor_set_host(sg_actor_self(), first);
28   sg_actor_execute(flopAmount);
29
30   XBT_INFO("I wake up on %s. Let's suspend a bit", sg_host_get_name(sg_host_self()));
31
32   sg_actor_suspend(sg_actor_self());
33
34   XBT_INFO("I wake up on %s", sg_host_get_name(sg_host_self()));
35   XBT_INFO("Done");
36 }
37
38 static void monitor(int argc, char* argv[])
39 {
40   sg_host_t jacquelin = sg_host_by_name("Jacquelin");
41   sg_host_t fafard    = sg_host_by_name("Fafard");
42
43   int actor_argc           = 3;
44   const char* actor_argv[] = {"worker", "Boivin", "Jacquelin", NULL};
45   sg_actor_t actor         = sg_actor_create_("worker", sg_host_by_name("Fafard"), worker, actor_argc, actor_argv);
46
47   sg_actor_sleep_for(5);
48
49   XBT_INFO("After 5 seconds, move the actor to %s", sg_host_get_name(jacquelin));
50   sg_actor_set_host(actor, jacquelin);
51
52   sg_actor_sleep_until(15);
53   XBT_INFO("At t=15, move the actor to %s and resume it.", sg_host_get_name(fafard));
54   sg_actor_set_host(actor, fafard);
55   sg_actor_resume(actor);
56 }
57
58 int main(int argc, char* argv[])
59 {
60   simgrid_init(&argc, argv);
61   xbt_assert(argc == 2, "Usage: %s platform_file\n\tExample: %s platform.xml\n", argv[0], argv[0]);
62
63   simgrid_load_platform(argv[1]); /* - Load the platform description */
64   sg_actor_create("monitor", sg_host_by_name("Boivin"), monitor, 0, NULL);
65
66   simgrid_run();
67
68   return 0;
69 }