Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
de7e7574ff5345b398e785968f855d1520982ccb
[simgrid.git] / examples / msg / app-token-ring / app-token-ring.c
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_app_token_ring, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  * - <b>Token Ring: app-token-ring/app-token-ring.c</b>. Classical token ring communication, where a token is exchanged
14  *   along a ring to reach every participant. The tesh file laying in the directory shows how to run the same example on
15  *   different platforms.
16  */
17
18 /** Single function for all hosts */
19 static int foo(int argc, char *argv[])
20 {
21   unsigned int task_comm_size = 1000000; /** The token is 1MB long*/
22   int rank = xbt_str_parse_int(MSG_process_get_name(MSG_process_self()), "Invalid process name: %s");
23   char mailbox[256];
24   msg_task_t task = NULL;
25   XBT_ATTRIB_UNUSED int res;
26   if (rank == 0){ /** - The root (rank 0) first sends the token then waits to receive it back */
27     sprintf(mailbox, "%d", rank+1);
28     task = MSG_task_create("Token", 0, task_comm_size, NULL);
29     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"", rank, task->name,mailbox);
30     MSG_task_send(task, mailbox);
31     task = NULL;
32     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
33     xbt_assert(res == MSG_OK, "MSG_task_get failed");
34     XBT_INFO("Host \"%d\" received \"%s\"", rank, MSG_task_get_name(task));
35     MSG_task_destroy(task);
36   } else{ /** - The others receive from their left neighbor (rank-1) and send to their right neighbor (rank+1) */
37     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
38     xbt_assert(res == MSG_OK, "MSG_task_get failed");
39     XBT_INFO("Host \"%d\" received \"%s\"",rank, MSG_task_get_name(task));
40
41     if(rank+1 == MSG_get_host_number()) /** - Except for the last one which sends the token back to rank 0 */
42       sprintf(mailbox, "0");
43     else
44       sprintf(mailbox, "%d", rank+1);
45     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",rank,task->name,mailbox);
46     MSG_task_send(task, mailbox);
47   }
48   return 0;
49 }
50
51 int main(int argc, char *argv[])
52 {
53   unsigned int i;
54   MSG_init(&argc, argv);
55   MSG_create_environment(argv[1]);       /** - Load the platform description */
56   xbt_dynar_t hosts = MSG_hosts_as_dynar();
57   msg_host_t h;
58
59   XBT_INFO("Number of host '%d'",MSG_get_host_number());
60   xbt_dynar_foreach (hosts, i, h){      /** - Give a unique rank to each host and create a @ref foo process on each */
61     char* name_host = bprintf("%u",i);
62     MSG_process_create(name_host, foo, NULL, h);
63     free(name_host);
64   }
65   xbt_dynar_free(&hosts);
66
67   int res = MSG_main();                 /** - Run the simulation */
68   XBT_INFO("Simulation time %g", MSG_get_clock());
69   return res != MSG_OK;
70 }