Logo AND Algorithmique Numérique Distribuée

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