Logo AND Algorithmique Numérique Distribuée

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