Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
085d574a9e0d09c08718a7ed94f2ef95adc0bdda
[simgrid.git] / examples / msg / masterslave / masterslave_cluster.c
1 /* Copyright (c) 2010-2014. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
18
19 msg_error_t test_all(const char *platform_file);
20
21
22 int master(int argc, char *argv[]);
23 int slave(int argc, char *argv[]);
24
25 /** Emitter function  */
26 int master(int argc, char *argv[])
27 {
28   int slaves_count = 0;
29   msg_host_t *slaves = NULL;
30   msg_task_t *todo = NULL;
31   int number_of_tasks = 0;
32   double task_comp_size = 0;
33   double task_comm_size = 0;
34   int i;
35   _XBT_GNUC_UNUSED int read;
36
37   read = sscanf(argv[1], "%d", &number_of_tasks);
38   xbt_assert(read, "Invalid argument %s\n", argv[1]);
39   read = sscanf(argv[2], "%lg", &task_comp_size);
40   xbt_assert(read, "Invalid argument %s\n", argv[2]);
41   read = sscanf(argv[3], "%lg", &task_comm_size);
42   xbt_assert(read, "Invalid argument %s\n", argv[3]);
43
44   {                             /*  Task creation */
45     char sprintf_buffer[64];
46
47     todo = xbt_new0(msg_task_t, number_of_tasks);
48
49     for (i = 0; i < number_of_tasks; i++) {
50       sprintf(sprintf_buffer, "Task_%d", i);
51       todo[i] =
52           MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
53                           NULL);
54     }
55   }
56
57   {                             /* Process organisation */
58     slaves_count = argc - 4;
59     slaves = xbt_new0(msg_host_t, slaves_count);
60
61     for (i = 4; i < argc; i++) {
62       slaves[i - 4] = MSG_get_host_by_name(argv[i]);
63       if (slaves[i - 4] == NULL) {
64         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
65         abort();
66       }
67     }
68   }
69
70   XBT_INFO("Got %d slave(s) :", slaves_count);
71   for (i = 0; i < slaves_count; i++)
72     XBT_INFO("\t %s", MSG_host_get_name(slaves[i]));
73
74   XBT_INFO("Got %d task to process :", number_of_tasks);
75
76   for (i = 0; i < number_of_tasks; i++)
77     XBT_INFO("\t\"%s\"", todo[i]->name);
78
79   for (i = 0; i < number_of_tasks; i++) {
80     XBT_INFO("Sending \"%s\" to \"%s\"",
81           todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
82     if (MSG_host_self() == slaves[i % slaves_count]) {
83       XBT_INFO("Hey ! It's me ! :)");
84     }
85     MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
86     XBT_INFO("Send completed");
87   }
88
89   XBT_INFO
90       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
91   for (i = 0; i < slaves_count; i++)
92     MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE),
93         MSG_host_get_name(slaves[i]));
94
95   XBT_INFO("Goodbye now!");
96   free(slaves);
97   free(todo);
98   return 0;
99 }                               /* end_of_master */
100
101 /** Receiver function  */
102 int slave(int argc, char *argv[])
103 {
104   XBT_INFO("I'm a slave");
105   while (1) {
106     msg_task_t task = NULL;
107     int a;
108     a = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
109     if (a == MSG_OK) {
110       XBT_INFO("Received \"%s\" ", MSG_task_get_name(task));
111       if (MSG_task_get_data(task) == FINALIZE) {
112         MSG_task_destroy(task);
113         break;
114       }
115       XBT_INFO("Processing \"%s\" ", MSG_task_get_name(task));
116       MSG_task_execute(task);
117       XBT_INFO("\"%s\" done ", MSG_task_get_name(task));
118       MSG_task_destroy(task);
119     } else {
120       XBT_INFO("Hey ?! What's up ? ");
121       xbt_die("Unexpected behavior");
122             }
123   }
124   XBT_INFO("I'm done. See you!");
125   return 0;
126 }                               /* end_of_slave */
127
128
129 /** Bypass deployment **/
130 static int bypass_deployment(void)
131 {
132   int nb_host,i;
133   static int AX_ptr;
134   static int surfxml_bufferstack_size = 2048;
135   static int surfxml_buffer_stack_stack_ptr = 0;
136   static int surfxml_buffer_stack_stack[1024];
137   xbt_dynar_t hosts = MSG_hosts_as_dynar();
138   /* allocating memory to the buffer, I think 2MB should be enough */
139   surfxml_bufferstack = xbt_new0(char, surfxml_bufferstack_size);
140
141   nb_host = xbt_dynar_length(hosts);
142   xbt_dynar_free(&hosts);
143
144   /* <platform> */
145   SURFXML_BUFFER_SET(platform_version, "3");
146   SURFXML_START_TAG(platform);
147   XBT_DEBUG("<platform version=\"3\">");
148
149     XBT_DEBUG("  <process host=\"c-0.me\" function=\"master\">");
150     SURFXML_BUFFER_SET(process_host, "c-0.me");
151     SURFXML_BUFFER_SET(process_function, "master");
152     SURFXML_BUFFER_SET(process_start___time, "-1.0");
153     SURFXML_BUFFER_SET(process_kill___time, "-1.0");
154     SURFXML_START_TAG(process);
155
156     XBT_DEBUG("    <argument value=\"%s\"/>",bprintf("%d",nb_host-1));
157     SURFXML_BUFFER_SET(argument_value, bprintf("%d",nb_host-1));
158     SURFXML_START_TAG(argument);
159     SURFXML_END_TAG(argument);
160
161     XBT_DEBUG("    <argument value=\"5000000\"/>");
162     SURFXML_BUFFER_SET(argument_value, "5000000");
163     SURFXML_START_TAG(argument);
164     SURFXML_END_TAG(argument);
165
166     XBT_DEBUG("    <argument value=\"100000\"/>");
167     SURFXML_BUFFER_SET(argument_value, "100000");
168     SURFXML_START_TAG(argument);
169     SURFXML_END_TAG(argument);
170
171   for(i=1 ; i<nb_host ; i++)
172   {
173     XBT_DEBUG("    <argument value=\"%s.me\"/>",bprintf("c-%d",i));
174     SURFXML_BUFFER_SET(argument_value, bprintf("c-%d.me",i));
175     SURFXML_START_TAG(argument);
176     SURFXML_END_TAG(argument);
177   }
178   XBT_DEBUG("  </process>");
179   SURFXML_END_TAG(process);
180
181   for(i=1 ; i<nb_host ; i++)
182   {
183     XBT_DEBUG("  <process host=\"%s.me\" function=\"slave\"/>",bprintf("c-%d",i));
184     SURFXML_BUFFER_SET(process_host, bprintf("c-%d.me",i));
185     SURFXML_BUFFER_SET(process_function, "slave");
186     SURFXML_BUFFER_SET(process_start___time, "-1.0");
187     SURFXML_BUFFER_SET(process_kill___time, "-1.0");
188     SURFXML_START_TAG(process);
189     SURFXML_END_TAG(process);
190   }
191
192   XBT_DEBUG("</platform>");
193   SURFXML_END_TAG(platform);
194
195   free(surfxml_bufferstack);
196   return 0;
197 }
198
199 /** Test function */
200 msg_error_t test_all(const char *platform_file)
201 {
202   msg_error_t res = MSG_OK;
203   MSG_create_environment(platform_file);
204   MSG_function_register("master", master);
205   MSG_function_register("slave", slave);
206   surf_parse = bypass_deployment;
207   MSG_launch_application(NULL);
208
209   res = MSG_main();
210
211   XBT_INFO("Simulation time %g", MSG_get_clock());
212   return res;
213 }                               /* end_of_test_all */
214
215
216 /** Main function */
217 int main(int argc, char *argv[])
218 {
219   msg_error_t res = MSG_OK;
220
221   MSG_init(&argc, argv);
222   res = test_all(argv[1]);
223
224   if (res == MSG_OK)
225     return 0;
226   else
227     return 1;
228 }                               /* end_of_main */