Logo AND Algorithmique Numérique Distribuée

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