Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Missing include.
[simgrid.git] / examples / msg / token_ring / ring_call.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 xbt_dynar_t hosts; /* All declared hosts */
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
19                              "Messages specific for this msg example");
20
21 int host(int argc, char *argv[])
22 {
23   int host_number = atoi(MSG_process_get_name(MSG_process_self()));
24   char mailbox[256];
25   m_task_t task = NULL;
26   _XBT_GNUC_UNUSED int res;
27
28   if (host_number == 0){ //master  send then receive
29     sprintf(mailbox, "%d", host_number+1);
30     task = MSG_task_create("Token", task_comp_size, task_comm_size, NULL);
31     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
32     MSG_task_send(task, mailbox);
33     task = NULL;
34     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
35     xbt_assert(res == MSG_OK, "MSG_task_get failed");
36     XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
37     MSG_task_destroy(task);
38   }
39   else{ //slave receive then send
40     res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
41     xbt_assert(res == MSG_OK, "MSG_task_get failed");
42     XBT_INFO("Host \"%d\" received \"%s\"",host_number, MSG_task_get_name(task));
43
44     if(host_number+1 == xbt_dynar_length(hosts) )
45       sprintf(mailbox, "0");
46     else
47       sprintf(mailbox, "%d", host_number+1);
48     XBT_INFO("Host \"%d\" send '%s' to Host \"%s\"",host_number,task->name,mailbox);
49     MSG_task_send(task, mailbox);
50   }
51   return 0;
52 }
53
54 int main(int argc, char **argv)
55 {
56         int i,res;
57   MSG_global_init(&argc, argv);
58   MSG_create_environment(argv[1]);
59   hosts =  MSG_hosts_as_dynar();
60   MSG_function_register("host", host);
61
62   XBT_INFO("Number of host '%zu'",xbt_dynar_length(hosts));
63   for(i = 0 ; i<xbt_dynar_length(hosts); i++)
64   {
65     char* name_host = bprintf("%d",i);
66     MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,i,m_host_t) );
67     free(name_host);
68   }
69   xbt_dynar_free(&hosts);
70
71   res = MSG_main();
72   XBT_INFO("Simulation time %g", MSG_get_clock());
73   MSG_clean();
74   if (res == MSG_OK)
75     return 0;
76   else
77     return 1;
78
79 }