1 /* Copyright (c) 2008-2010, 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include "surf/surf_private.h"
12 int host(int argc, char *argv[]);
13 unsigned int task_comp_size = 50000000;
14 unsigned int task_comm_size = 1000000;
16 int nb_hosts; /* All declared hosts */
18 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
19 "Messages specific for this msg example");
21 /** @addtogroup MSG_examples
23 * @section MSG_ex_apps Examples of full applications
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.
30 int host(int argc, char *argv[])
32 int host_number = atoi(MSG_process_get_name(MSG_process_self()));
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);
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);
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));
52 if(host_number+1 == nb_hosts)
53 sprintf(mailbox, "0");
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);
62 static int surf_parse_bypass_platform(void)
65 s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
67 AS.routing = A_surfxml_AS_routing_Full;
68 sg_platf_new_AS_begin(&AS);
70 s_sg_platf_host_cbarg_t bob = SG_PLATF_HOST_INITIALIZER;
72 bob.power_peak = xbt_dynar_new(sizeof(double), NULL);
73 xbt_dynar_push_as(bob.power_peak, double, 98095000.0);
74 sg_platf_new_host(&bob);
76 s_sg_platf_host_cbarg_t alice = SG_PLATF_HOST_INITIALIZER;
78 alice.power_peak = xbt_dynar_new(sizeof(double), NULL);
79 xbt_dynar_push_as(alice.power_peak, double, 98095000.0);
80 sg_platf_new_host(&alice);
82 s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
84 link.latency = 0.000278066;
85 link.bandwidth = 27946250;
86 sg_platf_new_link(&link);
88 s_sg_platf_route_cbarg_t route= SG_PLATF_ROUTE_INITIALIZER;
91 sg_platf_route_begin(&route);
92 sg_platf_route_add_link("link1", &route);
93 sg_platf_route_end(&route);
95 sg_platf_new_AS_end();
101 int main(int argc, char **argv)
104 msg_error_t res = MSG_OK;
106 MSG_init(&argc, argv);
107 surf_parse = surf_parse_bypass_platform;
108 MSG_create_environment(NULL);
110 MSG_function_register("host", host);
112 xbt_dynar_t hosts = MSG_hosts_as_dynar();
113 nb_hosts = xbt_dynar_length(hosts);
115 XBT_INFO("Number of host '%d'",nb_hosts);
116 for(i = 0 ; i<nb_hosts; i++)
118 char* name_host = bprintf("%d",i);
119 MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,msg_host_t) );
122 xbt_dynar_free(&hosts);
125 XBT_INFO("Simulation time %g", MSG_get_clock());