Logo AND Algorithmique Numérique Distribuée

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