Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanups
[simgrid.git] / examples / msg / masterslave / masterslave_forwarder.c
1 /* Copyright (c) 2007-2015. 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 "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  *  - <b>masterslave/masterslave_forwarder.c: Master/slaves example</b>. This good old example is also very simple. Its
14  *    basic version is fully commented on this page: \ref MSG_ex_master_slave, but several variants can be found in the
15  *    same directory.
16  */
17
18 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
19
20 /** Emitter function  */
21 static int master(int argc, char *argv[])
22 {
23   int slaves_count = 0;
24   msg_host_t *slaves = NULL;
25   msg_task_t *todo = NULL;
26   int number_of_tasks = 0;
27   double task_comp_size = 0;
28   double task_comm_size = 0;
29
30   int i;
31
32   XBT_ATTRIB_UNUSED int res = sscanf(argv[1], "%d", &number_of_tasks);
33   xbt_assert(res,"Invalid argument %s\n", argv[1]);
34   res = sscanf(argv[2], "%lg", &task_comp_size);
35   xbt_assert(res, "Invalid argument %s\n", argv[2]);
36   res = sscanf(argv[3], "%lg", &task_comm_size);
37   xbt_assert(res, "Invalid argument %s\n", argv[3]);
38
39   {                             /*  Task creation */
40     char sprintf_buffer[64];
41
42     todo = xbt_new0(msg_task_t, number_of_tasks);
43
44     for (i = 0; i < number_of_tasks; i++) {
45       sprintf(sprintf_buffer, "Task_%d", i);
46       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
47     }
48   }
49
50   {                             /* Process organization */
51     slaves_count = argc - 4;
52     slaves = xbt_new0(msg_host_t, slaves_count);
53
54     for (i = 4; i < argc; i++) {
55       slaves[i - 4] = MSG_host_by_name(argv[i]);
56       xbt_assert(slaves[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
57     }
58   }
59
60   XBT_INFO("Got %d slaves and %d tasks to process", slaves_count, number_of_tasks);
61   for (i = 0; i < slaves_count; i++)
62     XBT_DEBUG("%s", MSG_host_get_name(slaves[i]));
63
64   for (i = 0; i < number_of_tasks; i++) {
65     XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(slaves[i % slaves_count]));
66     if (MSG_host_self() == slaves[i % slaves_count]) {
67       XBT_INFO("Hey ! It's me ! :)");
68     }
69
70     MSG_task_send(todo[i], MSG_host_get_name(slaves[i % slaves_count]));
71     XBT_INFO("Sent");
72   }
73
74   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
75   for (i = 0; i < slaves_count; i++) {
76     msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
77     MSG_task_send(finalize, MSG_host_get_name(slaves[i]));
78   }
79
80   XBT_INFO("Goodbye now!");
81   free(slaves);
82   free(todo);
83   return 0;
84 }
85
86 static int slave(int argc, char *argv[])
87 {
88   msg_task_t task = NULL;
89   XBT_ATTRIB_UNUSED int res;
90   while (1) {
91     res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
92     xbt_assert(res == MSG_OK, "MSG_task_get failed");
93
94     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
95     if (!strcmp(MSG_task_get_name(task), "finalize")) {
96       MSG_task_destroy(task);
97       break;
98     }
99
100     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
101     MSG_task_execute(task);
102     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
103     MSG_task_destroy(task);
104     task = NULL;
105   }
106   XBT_INFO("I'm done. See you!");
107   return 0;
108 }
109
110 static int forwarder(int argc, char *argv[])
111 {
112   int i;
113   int slaves_count;
114   msg_host_t *slaves;
115
116   {                             /* Process organization */
117     slaves_count = argc - 1;
118     slaves = xbt_new0(msg_host_t, slaves_count);
119
120     for (i = 1; i < argc; i++) {
121       slaves[i - 1] = MSG_host_by_name(argv[i]);
122       if (slaves[i - 1] == NULL) {
123         XBT_INFO("Unknown host %s. Stopping Now! ", argv[i]);
124         abort();
125       }
126     }
127   }
128
129   i = 0;
130   while (1) {
131     msg_task_t task = NULL;
132     int a;
133     a = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
134     if (a == MSG_OK) {
135       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
136       if (MSG_task_get_data(task) == FINALIZE) {
137         XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
138         for (i = 0; i < slaves_count; i++)
139           MSG_task_send(MSG_task_create("finalize", 0, 0, FINALIZE), MSG_host_get_name(slaves[i]));
140         MSG_task_destroy(task);
141         break;
142       }
143       XBT_INFO("Sending \"%s\" to \"%s\"", MSG_task_get_name(task), MSG_host_get_name(slaves[i % slaves_count]));
144       MSG_task_send(task, MSG_host_get_name(slaves[i % slaves_count]));
145       i++;
146     } else {
147       XBT_INFO("Hey ?! What's up ? ");
148       xbt_die("Unexpected behavior");
149     }
150   }
151   xbt_free(slaves);
152
153   XBT_INFO("I'm done. See you!");
154   return 0;
155 }
156
157 int main(int argc, char *argv[])
158 {
159   msg_error_t res = MSG_OK;
160
161   MSG_init(&argc, argv);
162   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
163              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
164
165   MSG_create_environment(argv[1]);
166
167   MSG_function_register("master", master);
168   MSG_function_register("slave", slave);
169   MSG_function_register("forwarder", forwarder);
170   MSG_launch_application(argv[2]);
171
172   res = MSG_main();
173
174   XBT_INFO("Simulation time %g", MSG_get_clock());
175
176   return res != MSG_OK;
177 }