Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2c17529ae8d073721850fe909f570a04d228fa29
[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     MSG_process_sleep(1);
43     //MSG_comm_wait(comm, -1);
44     INFO1("Send Data to \"%s\"", mailbox);
45
46         sprintf(mailbox, "host%d", num);
47         MSG_task_receive(&(task_r), mailbox);
48         //res = MSG_comm_wait(res_irecv, -1);
49         INFO1("Received \"%s\"", MSG_task_get_name(task_r));
50         //MSG_comm_destroy(res_irecv);
51         return 0;
52 }
53
54 int slave(int argc, char *argv[])
55 {
56         m_task_t task_s = NULL;
57         m_task_t task_r = NULL;
58         unsigned int task_comp_size = 50000000;
59         unsigned int task_comm_size = 1000000;
60         char mailbox[80];
61         char buffer[20];
62         int num = atoi(argv[1]);
63
64         sprintf(mailbox, "host%d", num);
65         MSG_task_receive(&(task_r), mailbox);
66         //res = MSG_comm_wait(res_irecv, -1);
67         INFO1("Received \"%s\"", MSG_task_get_name(task_r));
68         MSG_process_sleep(1);
69         //MSG_comm_destroy(res_irecv);
70         //Receive something now need to tell it!
71
72         sprintf(mailbox, "host%d", num+1);
73         if(num == totalHosts-1)
74                 sprintf(mailbox, "host%d", 0);
75         sprintf(buffer, "Token");
76         task_s = MSG_task_create(buffer,
77                                                         task_comp_size,
78                                                         task_comm_size,
79                                                         NULL);
80         MSG_task_send(task_s, mailbox);
81         //MSG_comm_wait(comm, -1);
82         INFO1("Send Data to \"%s\"", mailbox);
83
84         return 0;
85 }
86
87 static int surf_parse_bypass_application(void)
88 {
89         int i;
90         static int AX_ptr;
91         static int surfxml_bufferstack_size = 2048;
92         static int surfxml_buffer_stack_stack_ptr = 0;
93         static int surfxml_buffer_stack_stack[1024];
94         /* allocating memory to the buffer, I think 2MB should be enough */
95         surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
96
97         totalHosts = MSG_get_host_number();
98         hosts = MSG_get_host_table();
99
100         /* <platform> */
101         SURFXML_BUFFER_SET(platform_version, "3");
102
103         SURFXML_START_TAG(platform);
104
105         DEBUG1("process : %s en master",MSG_host_get_name(hosts[0]));
106         /*   <process host="host A" function="master"> */
107         SURFXML_BUFFER_SET(process_host, MSG_host_get_name(hosts[0]));
108         SURFXML_BUFFER_SET(process_function, "master");
109         SURFXML_BUFFER_SET(process_start_time, "-1.0");
110         SURFXML_BUFFER_SET(process_kill_time, "-1.0");
111         SURFXML_START_TAG(process);
112
113         /*      <argument value="0"/> */
114         SURFXML_BUFFER_SET(argument_value, "0");
115         SURFXML_START_TAG(argument);
116         SURFXML_END_TAG(argument);
117         SURFXML_END_TAG(process);
118
119         for(i=1;i<totalHosts;i++)
120         {
121         DEBUG1("process : %s en slave",MSG_host_get_name(hosts[i]));
122         /*   <process host="host A" function="slave"> */
123         SURFXML_BUFFER_SET(process_host,MSG_host_get_name(hosts[i]) );
124         SURFXML_BUFFER_SET(process_function, "slave");
125         SURFXML_BUFFER_SET(process_start_time, "-1.0");
126         SURFXML_BUFFER_SET(process_kill_time, "-1.0");
127         SURFXML_START_TAG(process);
128
129         /*      <argument value="num"/> */
130         SURFXML_BUFFER_SET(argument_value, bprintf("%d",i));
131         SURFXML_START_TAG(argument);
132         SURFXML_END_TAG(argument);
133         SURFXML_END_TAG(process);
134         }
135         /* </platform> */
136         SURFXML_END_TAG(platform);
137
138         free(surfxml_bufferstack);
139         return 0;
140 }
141
142 typedef enum {
143   PORT_22 = 20,
144   MAX_CHANNEL
145 } channel_t;
146
147 int main(int argc, char **argv)
148 {
149         int res;
150   MSG_global_init(&argc, argv);
151   MSG_set_channel_number(MAX_CHANNEL);
152   MSG_create_environment(argv[1]);
153
154   MSG_function_register("master", master);
155   MSG_function_register("slave", slave);
156   surf_parse = surf_parse_bypass_application;
157   MSG_launch_application(NULL);
158
159   res = MSG_main();
160
161   INFO1("Simulation time %g", MSG_get_clock());
162
163   MSG_clean();
164
165   if (res == MSG_OK)
166     return 0;
167   else
168     return 1;
169
170 }