Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove unused call to MSG_process_sleep(1).
[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 extern routing_global_t global_routing;
13 int totalHosts= 0;
14 const m_host_t *hosts;
15
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(ring,
20                              "Messages specific for this msg example");
21
22 int master(int argc, char *argv[])
23 {
24         m_task_t task_s = NULL;
25         m_task_t task_r = NULL;
26         unsigned int task_comp_size = 50000000;
27         unsigned int task_comm_size = 1000000;
28         char mailbox[80];
29         char buffer[20];
30         int num = atoi(argv[1]);
31
32     sprintf(mailbox, "host%d", num+1);
33     if(num == totalHosts-1)
34         sprintf(mailbox, "host%d", 0);
35     sprintf(buffer, "Token");
36
37     task_s = MSG_task_create(buffer,
38                                                         task_comp_size,
39                                                         task_comm_size,
40                                                         NULL);
41     MSG_task_send(task_s,mailbox);
42     INFO1("Send Data to \"%s\"", mailbox);
43
44         sprintf(mailbox, "host%d", num);
45         MSG_task_receive(&(task_r), mailbox);
46         INFO1("Received \"%s\"", MSG_task_get_name(task_r));
47         return 0;
48 }
49
50 int slave(int argc, char *argv[])
51 {
52         m_task_t task_s = NULL;
53         m_task_t task_r = NULL;
54         unsigned int task_comp_size = 50000000;
55         unsigned int task_comm_size = 1000000;
56         char mailbox[80];
57         char buffer[20];
58         int num = atoi(argv[1]);
59
60         sprintf(mailbox, "host%d", num);
61         MSG_task_receive(&(task_r), mailbox);
62         INFO1("Received \"%s\"", MSG_task_get_name(task_r));
63         sprintf(mailbox, "host%d", num+1);
64         if(num == totalHosts-1)
65                 sprintf(mailbox, "host%d", 0);
66         sprintf(buffer, "Token");
67         task_s = MSG_task_create(buffer,
68                                                         task_comp_size,
69                                                         task_comm_size,
70                                                         NULL);
71         MSG_task_send(task_s, mailbox);
72         INFO1("Send Data to \"%s\"", mailbox);
73
74         return 0;
75 }
76
77 static int surf_parse_bypass_application(void)
78 {
79         int i;
80         static int AX_ptr;
81         static int surfxml_bufferstack_size = 2048;
82         static int surfxml_buffer_stack_stack_ptr = 0;
83         static int surfxml_buffer_stack_stack[1024];
84         /* allocating memory to the buffer, I think 2MB should be enough */
85         surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
86
87         totalHosts = MSG_get_host_number();
88         hosts = MSG_get_host_table();
89
90         /* <platform> */
91         SURFXML_BUFFER_SET(platform_version, "3");
92
93         SURFXML_START_TAG(platform);
94
95         DEBUG1("process : %s en master",MSG_host_get_name(hosts[0]));
96         /*   <process host="host A" function="master"> */
97         SURFXML_BUFFER_SET(process_host, MSG_host_get_name(hosts[0]));
98         SURFXML_BUFFER_SET(process_function, "master");
99         SURFXML_BUFFER_SET(process_start_time, "-1.0");
100         SURFXML_BUFFER_SET(process_kill_time, "-1.0");
101         SURFXML_START_TAG(process);
102
103         /*      <argument value="0"/> */
104         SURFXML_BUFFER_SET(argument_value, "0");
105         SURFXML_START_TAG(argument);
106         SURFXML_END_TAG(argument);
107         SURFXML_END_TAG(process);
108
109         for(i=1;i<totalHosts;i++)
110         {
111         DEBUG1("process : %s en slave",MSG_host_get_name(hosts[i]));
112         /*   <process host="host A" function="slave"> */
113         SURFXML_BUFFER_SET(process_host,MSG_host_get_name(hosts[i]) );
114         SURFXML_BUFFER_SET(process_function, "slave");
115         SURFXML_BUFFER_SET(process_start_time, "-1.0");
116         SURFXML_BUFFER_SET(process_kill_time, "-1.0");
117         SURFXML_START_TAG(process);
118
119         /*      <argument value="num"/> */
120         SURFXML_BUFFER_SET(argument_value, bprintf("%d",i));
121         SURFXML_START_TAG(argument);
122         SURFXML_END_TAG(argument);
123         SURFXML_END_TAG(process);
124         }
125         /* </platform> */
126         SURFXML_END_TAG(platform);
127
128         free(surfxml_bufferstack);
129         return 0;
130 }
131
132 typedef enum {
133   PORT_22 = 20,
134   MAX_CHANNEL
135 } channel_t;
136
137 int main(int argc, char **argv)
138 {
139         int res;
140   MSG_global_init(&argc, argv);
141   MSG_set_channel_number(MAX_CHANNEL);
142   MSG_create_environment(argv[1]);
143
144   MSG_function_register("master", master);
145   MSG_function_register("slave", slave);
146   surf_parse = surf_parse_bypass_application;
147   MSG_launch_application(NULL);
148
149   res = MSG_main();
150
151   INFO1("Simulation time %g", MSG_get_clock());
152
153   MSG_clean();
154
155   if (res == MSG_OK)
156     return 0;
157   else
158     return 1;
159
160 }