Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1f046a77ae9f9b399b41b4a5aafc37af644004bb
[simgrid.git] / examples / msg / app-token-ring / app-token-ring.c
1 /* Copyright (c) 2008-2016. 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/msg.h"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_app_token_ring, "Messages specific for this msg example");
9
10 /* Main function of all processes used in this example */
11 static int relay_runner(int argc, char *argv[])
12 {
13   xbt_assert(argc==0, "The relay_runner function does not accept any parameter from the XML deployment file");
14   int rank = xbt_str_parse_int(MSG_process_get_name(MSG_process_self()), "Any process of this example must have a numerical name, not %s");
15   char mailbox[256];
16
17   if (rank == 0) {
18     /* The root process (rank 0) first sends the token then waits to receive it back */
19     snprintf(mailbox,255, "%d", rank+1);
20     unsigned int task_comm_size = 1000000; /* The token is 1MB long*/
21     msg_task_t task = MSG_task_create("Token", 0, task_comm_size, NULL);
22     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"", rank, task->name,mailbox);
23     MSG_task_send(task, mailbox);
24     task = NULL;
25     int res = MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
26     xbt_assert(res == MSG_OK, "MSG_task_get failed");
27     XBT_INFO("Host \"%d\" received \"%s\"", rank, MSG_task_get_name(task));
28     MSG_task_destroy(task);
29
30   } else {
31     /* The others processes receive from their left neighbor (rank-1) and send to their right neighbor (rank+1) */
32     msg_task_t task = NULL;
33     int res = MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
34     xbt_assert(res == MSG_OK, "MSG_task_get failed");
35     XBT_INFO("Host \"%d\" received \"%s\"",rank, MSG_task_get_name(task));
36
37     if (rank+1 == MSG_get_host_number())
38       /* But the last process, which sends the token back to rank 0 */
39       snprintf(mailbox,255, "0");
40     else
41       snprintf(mailbox,255, "%d", rank+1);
42     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",rank,task->name,mailbox);
43     MSG_task_send(task, mailbox);
44   }
45   return 0;
46 }
47
48 int main(int argc, char *argv[])
49 {
50   MSG_init(&argc, argv);
51   xbt_assert(argc>1, "Usage: %s platform.xml\n",argv[0]);
52   MSG_create_environment(argv[1]);       /* - Load the platform description */
53   xbt_dynar_t hosts = MSG_hosts_as_dynar();
54
55   XBT_INFO("Number of hosts '%d'", MSG_get_host_number());
56   unsigned int i;
57   msg_host_t h;
58   xbt_dynar_foreach (hosts, i, h) {      /* - Give a unique rank to each host and create a @ref relay_runner process on each */
59     char* name_host = bprintf("%u",i);
60     MSG_process_create(name_host, relay_runner, NULL, h);
61     free(name_host);
62   }
63   xbt_dynar_free(&hosts);
64
65   int res = MSG_main();                 /* - Run the simulation */
66   XBT_INFO("Simulation time %g", MSG_get_clock());
67   return res != MSG_OK;
68 }