Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3 more renamed tests
[simgrid.git] / examples / msg / actions-comm / actions-comm.c
1 /* Copyright (c) 2009-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 #include "simgrid/simix.h"      /* semaphores for the barrier */
9 #include <xbt/replay.h>
10
11 /** @addtogroup MSG_examples
12  *
13  *  @section MSG_ex_actions Trace driven simulations
14  * 
15  *  The <b>actions/actions.c</b> example demonstrates how to run trace-driven simulations. It is very handy when you
16  *  want to test an algorithm or protocol that does nothing unless it receives some events from outside. For example,
17  *  a P2P protocol reacts to requests from the user, but does nothing if there is no such event.
18  * 
19  *  In such situations, SimGrid allows to write your protocol in your C file, and the events to react to in a separate
20  *  text file. Declare a function handling each of the events that you want to accept in your trace files, register
21  *  them using \ref xbt_replay_action_register in your main, and then use \ref MSG_action_trace_run to launch the
22  *  simulation. You can either have one trace file containing all your events, or a file per simulated process. Check
23  *  the tesh files in the example directory for details on how to do it.
24  *
25  *  This example uses this approach to replay MPI-like traces. It comes with a set of event handlers reproducing MPI
26  *  events. This is somehow similar to SMPI, yet differently implemented. This code should probably be changed to use
27  *  SMPI internals instead, but wasn't, so far.
28  */
29
30 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
31 int communicator_size = 0;
32
33 static void action_Isend(const char *const *action);
34
35 typedef struct {
36   int last_Irecv_sender_id;
37   int bcast_counter;
38   int reduce_counter;
39   int allReduce_counter;
40   xbt_dynar_t isends;           /* of msg_comm_t */
41   /* Used to implement irecv+wait */
42   xbt_dynar_t irecvs;           /* of msg_comm_t */
43   xbt_dynar_t tasks;            /* of msg_task_t */
44 } s_process_globals_t, *process_globals_t;
45
46 /* Helper function */
47 static double parse_double(const char *string)
48 {
49   double value;
50   char *endptr;
51
52   value = strtod(string, &endptr);
53   if (*endptr != '\0')
54     THROWF(unknown_error, 0, "%s is not a double", string);
55   return value;
56 }
57
58 #define ACT_DEBUG(...) \
59   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {  \
60     char *NAME = xbt_str_join_array(action, " ");              \
61     XBT_DEBUG(__VA_ARGS__);                                    \
62     xbt_free(NAME);                                            \
63   } else ((void)0)
64
65 static void log_action(const char *const *action, double date)
66 {
67   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
68     char *name = xbt_str_join_array(action, " ");
69     XBT_VERB("%s %f", name, date);
70     xbt_free(name);
71   }
72 }
73
74 static void asynchronous_cleanup(void)
75 {
76   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
77
78   /* Destroy any isend which correspond to completed communications */
79   int found;
80   msg_comm_t comm;
81   while ((found = MSG_comm_testany(globals->isends)) != -1) {
82     xbt_dynar_remove_at(globals->isends, found, &comm);
83     MSG_comm_destroy(comm);
84   }
85 }
86
87 /* My actions */
88 static void action_send(const char *const *action)
89 {
90   char to[250];
91   const char *size_str = action[3];
92   double size = parse_double(size_str);
93   double clock = MSG_get_clock();
94
95   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
96
97   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
98   if (size < 65536) {
99     action_Isend(action);
100   } else {
101     MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
102   }
103
104   log_action(action, MSG_get_clock() - clock);
105   asynchronous_cleanup();
106 }
107
108 static void action_Isend(const char *const *action)
109 {
110   char to[250];
111   const char *size = action[3];
112   double clock = MSG_get_clock();
113   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
114
115   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
116   msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
117   xbt_dynar_push(globals->isends, &comm);
118
119   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
120   log_action(action, MSG_get_clock() - clock);
121   asynchronous_cleanup();
122 }
123
124 static void action_recv(const char *const *action)
125 {
126   char mailbox_name[250];
127   msg_task_t task = NULL;
128   double clock = MSG_get_clock();
129
130   sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
131
132   ACT_DEBUG("Receiving: %s", NAME);
133   msg_error_t res = MSG_task_receive(&task, mailbox_name);
134   log_action(action, MSG_get_clock() - clock);
135
136   if (res == MSG_OK) {
137     MSG_task_destroy(task);
138   }
139   asynchronous_cleanup();
140 }
141
142 static void action_Irecv(const char *const *action)
143 {
144   char mailbox[250];
145   double clock = MSG_get_clock();
146   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
147
148   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
149
150   sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
151   msg_task_t t = NULL;
152   xbt_dynar_push(globals->tasks, &t);
153   msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
154   xbt_dynar_push(globals->irecvs, &c);
155
156   log_action(action, MSG_get_clock() - clock);
157   asynchronous_cleanup();
158 }
159
160 static void action_wait(const char *const *action)
161 {
162   msg_task_t task = NULL;
163   msg_comm_t comm;
164   double clock = MSG_get_clock();
165   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
166
167   xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
168              xbt_str_join_array(action, " "));
169
170   ACT_DEBUG("Entering %s", NAME);
171   comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
172   MSG_comm_wait(comm, -1);
173   task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
174   MSG_comm_destroy(comm);
175   MSG_task_destroy(task);
176
177   log_action(action, MSG_get_clock() - clock);
178 }
179
180 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
181 static void action_barrier(const char *const *action)
182 {
183   static smx_mutex_t mutex = NULL;
184   static smx_cond_t cond = NULL;
185   static int processes_arrived_sofar = 0;
186
187   if (mutex == NULL) {          // first arriving on the barrier
188     mutex = simcall_mutex_init();
189     cond = simcall_cond_init();
190     processes_arrived_sofar = 0;
191   }
192   ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
193
194   simcall_mutex_lock(mutex);
195   if (++processes_arrived_sofar == communicator_size) {
196     simcall_cond_broadcast(cond);
197     simcall_mutex_unlock(mutex);
198   } else {
199     simcall_cond_wait(cond, mutex);
200     simcall_mutex_unlock(mutex);
201   }
202
203   ACT_DEBUG("Exiting barrier: %s", NAME);
204
205   processes_arrived_sofar--;
206   if (!processes_arrived_sofar) {
207     SIMIX_cond_destroy(cond);
208     SIMIX_mutex_destroy(mutex);
209     mutex = NULL;
210   }
211 }
212
213 static void action_bcast(const char *const *action)
214 {
215   int i;
216   char *bcast_identifier;
217   char mailbox[80];
218   double comm_size = parse_double(action[2]);
219   msg_task_t task = NULL;
220   const char *process_name;
221   double clock = MSG_get_clock();
222
223   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
224
225   xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
226
227   process_name = MSG_process_get_name(MSG_process_self());
228
229   bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
230
231   if (!strcmp(process_name, "p0")) {
232     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
233
234     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
235
236     for (i = 1; i < communicator_size; i++) {
237       sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
238       comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
239     }
240     MSG_comm_waitall(comms, communicator_size - 1, -1);
241     for (i = 1; i < communicator_size; i++)
242       MSG_comm_destroy(comms[i - 1]);
243     xbt_free(comms);
244
245     XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
246   } else {
247     sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
248     MSG_task_receive(&task, mailbox);
249     MSG_task_destroy(task);
250     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
251   }
252
253   log_action(action, MSG_get_clock() - clock);
254   xbt_free(bcast_identifier);
255 }
256
257 static void action_comm_size(const char *const *action)
258 {
259   const char *size = action[2];
260   double clock = MSG_get_clock();
261
262   communicator_size = parse_double(size);
263   log_action(action, MSG_get_clock() - clock);
264 }
265
266 static void action_compute(const char *const *action)
267 {
268   const char *amount = action[2];
269   msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
270   double clock = MSG_get_clock();
271
272   ACT_DEBUG("Entering %s", NAME);
273   MSG_task_execute(task);
274   MSG_task_destroy(task);
275   log_action(action, MSG_get_clock() - clock);
276 }
277
278 static void action_init(const char *const *action)
279 {
280   XBT_DEBUG("Initialize the counters");
281   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
282   globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
283   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
284   globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
285   MSG_process_set_data(MSG_process_self(), globals);
286 }
287
288 static void action_finalize(const char *const *action)
289 {
290   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
291   if (globals) {
292     asynchronous_cleanup();
293     xbt_dynar_free_container(&(globals->isends));
294     xbt_dynar_free_container(&(globals->irecvs));
295     xbt_dynar_free_container(&(globals->tasks));
296     xbt_free(globals);
297   }
298 }
299
300 /** Main function */
301 int main(int argc, char *argv[])
302 {
303   msg_error_t res = MSG_OK;
304
305   /* Check the given arguments */
306   MSG_init(&argc, argv);
307   /* Explicit initialization of the action module is required now*/
308   MSG_action_init();
309
310   xbt_assert(argc > 2,
311        "Usage: %s platform_file deployment_file [action_files]\n"
312        "\t# if all actions are in the same file\n"
313        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
314        "\t# if actions are in separate files, specified in deployment\n"
315        "\tExample: %s msg_platform.xml msg_deployment.xml ",
316        argv[0],argv[0],argv[0]);
317
318   MSG_create_environment(argv[1]);
319   MSG_launch_application(argv[2]);
320
321   /*   Action registration */
322   xbt_replay_action_register("init", action_init);
323   xbt_replay_action_register("finalize", action_finalize);
324   xbt_replay_action_register("comm_size", action_comm_size);
325   xbt_replay_action_register("send", action_send);
326   xbt_replay_action_register("Isend", action_Isend);
327   xbt_replay_action_register("recv", action_recv);
328   xbt_replay_action_register("Irecv", action_Irecv);
329   xbt_replay_action_register("wait", action_wait);
330   xbt_replay_action_register("barrier", action_barrier);
331   xbt_replay_action_register("bcast", action_bcast);
332   xbt_replay_action_register("compute", action_compute);
333
334   /* Actually do the simulation using MSG_action_trace_run */
335   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
336
337   XBT_INFO("Simulation time %g", MSG_get_clock());
338
339   /* Explicit finalization of the action module is required now*/
340   MSG_action_exit();
341
342   return res != MSG_OK;
343 }