Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
69c6fc177f561ab07e9b595f4cd426648157691a
[simgrid.git] / examples / c / app-token-ring / app-token-ring.c
1 /* Copyright (c) 2008-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/host.h"
9 #include "simgrid/mailbox.h"
10
11 #include "xbt/log.h"
12 #include "xbt/str.h"
13 #include "xbt/sysdep.h"
14
15 #include <stddef.h>
16 #include <stdio.h>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(app_token_ring, "Messages specific for this msg example");
19
20 /* Main function of all actors used in this example */
21 static void relay_runner(int argc, char* argv[])
22 {
23   xbt_assert(argc == 0, "The relay_runner function does not accept any parameter from the XML deployment file");
24
25   const char* name = sg_actor_self_get_name();
26   int rank         = (int)xbt_str_parse_int(name, "Any actor of this example must have a numerical name, not %s");
27
28   sg_mailbox_t my_mailbox = sg_mailbox_by_name(name);
29
30   /* The last actor sends the token back to rank 0, the others send to their right neighbor (rank+1) */
31   char neighbor_mailbox_name[256];
32   snprintf(neighbor_mailbox_name, 255, "%d", rank + 1 == sg_host_count() ? 0 : rank + 1);
33
34   sg_mailbox_t neighbor_mailbox = sg_mailbox_by_name(neighbor_mailbox_name);
35
36   char* res;
37   if (rank == 0) {
38     /* The root actor (rank 0) first sends the token then waits to receive it back */
39     XBT_INFO("Host \"%d\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox_name);
40     sg_mailbox_put(neighbor_mailbox, xbt_strdup("Token"), 1000000);
41
42     res = (char*)sg_mailbox_get(my_mailbox);
43     XBT_INFO("Host \"%d\" received \"%s\"", rank, res);
44   } else {
45     /* The others actors receive from their left neighbor (rank-1) and send to their right neighbor (rank+1) */
46     res = (char*)sg_mailbox_get(my_mailbox);
47     XBT_INFO("Host \"%d\" received \"%s\"", rank, res);
48     XBT_INFO("Host \"%d\" send 'Token' to Host \"%s\"", rank, neighbor_mailbox_name);
49     sg_mailbox_put(neighbor_mailbox, xbt_strdup("Token"), 1000000);
50   }
51   free(res);
52 }
53
54 int main(int argc, char* argv[])
55 {
56   simgrid_init(&argc, argv);
57   xbt_assert(argc > 1, "Usage: %s platform.xml\n", argv[0]);
58   simgrid_load_platform(argv[1]); /* - Load the platform description */
59
60   size_t host_count = sg_host_count();
61   sg_host_t* hosts  = sg_host_list();
62
63   XBT_INFO("Number of hosts '%zu'", host_count);
64   for (size_t i = 0; i < host_count; i++) {
65     /* - Give a unique rank to each host and create a @ref relay_runner actor on each */
66     char* name_host  = bprintf("%zu", i);
67     sg_actor_create(name_host, hosts[i], relay_runner, 0, NULL);
68     free(name_host);
69   }
70   free(hosts);
71
72   simgrid_run();
73
74   XBT_INFO("Simulation time %g", simgrid_get_clock());
75   return 0;
76 }