Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite the init of bypass mecanism (tks Martin;)
[simgrid.git] / examples / msg / token_ring / token_bypass.c
1 /* Copyright (c) 2008, 2009, 2010. 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 "msg/msg.h"
10 #include "surf/surf_private.h"
11
12 int host(int argc, char *argv[]);
13 unsigned int task_comp_size = 50000000;
14 unsigned int task_comm_size = 1000000;
15
16 int nb_hosts; /* All declared hosts */
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
19                              "Messages specific for this msg example");
20
21 /** @addtogroup MSG_examples
22  * 
23  *  @section MSG_ex_apps Examples of full applications
24  * 
25  * - <b>token_ring/token_bypass.c</b>: Classical token ring with a bypass deployment.
26  *   A token is exchanged along a ring to reach every participant.
27  * 
28  */
29
30 int host(int argc, char *argv[])
31 {
32   int host_number = atoi(MSG_process_get_name(MSG_process_self()));
33   char mailbox[256];
34   msg_task_t task = NULL;
35   _XBT_GNUC_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 static int surf_parse_bypass_platform(void)
63 {
64   sg_platf_begin();
65   sg_platf_new_AS_begin("AS0", A_surfxml_AS_routing_Full);
66
67   s_sg_platf_host_cbarg_t bob = SG_PLATF_HOST_INITIALIZER;
68   bob.id = "bob";
69   bob.power_peak = 98095000;
70   sg_platf_new_host(&bob);
71
72   s_sg_platf_host_cbarg_t alice = SG_PLATF_HOST_INITIALIZER;
73   alice.id = "alice";
74   alice.power_peak = 98095000;
75   sg_platf_new_host(&alice);
76
77   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
78   link.id = "link1";
79   link.latency = 0.000278066;
80   link.bandwidth = 27946250;
81   sg_platf_new_link(&link);
82
83   s_sg_platf_route_cbarg_t route= SG_PLATF_ROUTE_INITIALIZER;
84   route.src = "bob";
85   route.dst = "alice";
86   sg_platf_route_begin(&route);
87   sg_platf_route_add_link("link1", &route);
88   sg_platf_route_end(&route);
89
90   sg_platf_new_AS_end();
91   sg_platf_end();
92   sg_platf_exit();
93   return 0;
94 }
95
96 int main(int argc, char **argv)
97 {
98   int i;
99   msg_error_t res = MSG_OK;
100
101   MSG_init(&argc, argv);
102   surf_parse = surf_parse_bypass_platform;
103   MSG_create_environment(NULL);
104
105   MSG_function_register("host", host);
106
107   xbt_dynar_t hosts = MSG_hosts_as_dynar();
108   nb_hosts =  xbt_dynar_length(hosts);
109
110   XBT_INFO("Number of host '%d'",nb_hosts);
111   for(i = 0 ; i<nb_hosts; i++)
112   {
113     char* name_host = bprintf("%d",i);
114     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
115     free(name_host);
116   }
117   xbt_dynar_free(&hosts);
118
119   res = MSG_main();
120   XBT_INFO("Simulation time %g", MSG_get_clock());
121   MSG_clean();
122   if (res == MSG_OK)
123     return 0;
124   else
125     return 1;
126
127 }