Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / examples / msg / token_ring / ring_call.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 <stdio.h>
8 #include <stdlib.h>
9 #include "simgrid/msg.h"
10
11 int host(int argc, char *argv[]);
12 unsigned int task_comp_size = 50000000;
13 unsigned int task_comm_size = 1000000;
14
15 int nb_hosts; /* All declared hosts */
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
18                              "Messages specific for this msg example");
19
20 /** @addtogroup MSG_examples
21  * 
22  *  @section MSG_ex_apps Examples of full applications
23  * 
24  * - <b>token_ring/ring_call.c</b>: Classical token ring
25  *   communication, where a token is exchanged along a ring to reach
26  *   every participant.
27  * 
28  */
29
30 int host(int argc, char *argv[])
31 {
32   int host_number = xbt_str_parse_int(MSG_process_get_name(MSG_process_self()), "Process name must be an integer but is: %s");
33   char mailbox[256];
34   msg_task_t task = NULL;
35   XBT_ATTRIB_UNUSED int res;
36   if (host_number == 0){ //master  send then receive
37     sprintf(mailbox, "%d", host_number+1);
38     task = MSG_task_create("Token", task_comp_size, task_comm_size, NULL);
39     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
40     MSG_task_send(task, mailbox);
41     task = NULL;
42     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
43     xbt_assert(res == MSG_OK, "MSG_task_get failed");
44     XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
45     MSG_task_destroy(task);
46   }
47   else{ //slave receive then send
48     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
49     xbt_assert(res == MSG_OK, "MSG_task_get failed");
50     XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
51
52     if(host_number+1 == nb_hosts)
53       sprintf(mailbox, "0");
54     else
55       sprintf(mailbox, "%d", host_number+1);
56     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
57     MSG_task_send(task, mailbox);
58   }
59   return 0;
60 }
61
62 int main(int argc, char **argv)
63 {
64   int i,res;
65   MSG_init(&argc, argv);
66   MSG_create_environment(argv[1]);
67   xbt_dynar_t hosts = MSG_hosts_as_dynar();
68   nb_hosts =  xbt_dynar_length(hosts);
69   MSG_function_register("host", host);
70
71   XBT_INFO("Number of host '%d'",nb_hosts);
72   for(i = 0 ; i<nb_hosts; i++)
73   {
74     char* name_host = bprintf("%d",i);
75     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
76     free(name_host);
77   }
78   xbt_dynar_free(&hosts);
79
80   res = MSG_main();
81   XBT_INFO("Simulation time %g", MSG_get_clock());
82   return res != MSG_OK;
83 }