Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
have uniform log format for the same example
[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   xbt_dynar_t isends;           /* of msg_comm_t */
39   /* Used to implement irecv+wait */
40   xbt_dynar_t irecvs;           /* of msg_comm_t */
41   xbt_dynar_t tasks;            /* of msg_task_t */
42 } s_process_globals_t, *process_globals_t;
43
44 /* Helper function */
45 static double parse_double(const char *string)
46 {
47   double value;
48   char *endptr;
49
50   value = strtod(string, &endptr);
51   if (*endptr != '\0')
52     THROWF(unknown_error, 0, "%s is not a double", string);
53   return value;
54 }
55
56 #define ACT_DEBUG(...) \
57   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {  \
58     char *NAME = xbt_str_join_array(action, " ");              \
59     XBT_DEBUG(__VA_ARGS__);                                    \
60     xbt_free(NAME);                                            \
61   } else ((void)0)
62
63 static void log_action(const char *const *action, double date)
64 {
65   if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
66     char *name = xbt_str_join_array(action, " ");
67     XBT_VERB("%s %f", name, date);
68     xbt_free(name);
69   }
70 }
71
72 static void asynchronous_cleanup(void)
73 {
74   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
75
76   /* Destroy any isend which correspond to completed communications */
77   int found;
78   msg_comm_t comm;
79   while ((found = MSG_comm_testany(globals->isends)) != -1) {
80     xbt_dynar_remove_at(globals->isends, found, &comm);
81     MSG_comm_destroy(comm);
82   }
83 }
84
85 /* My actions */
86 static void action_send(const char *const *action)
87 {
88   char to[250];
89   const char *size_str = action[3];
90   double size = parse_double(size_str);
91   double clock = MSG_get_clock();
92
93   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
94
95   ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
96   if (size < 65536) {
97     action_Isend(action);
98   } else {
99     MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
100   }
101
102   log_action(action, MSG_get_clock() - clock);
103   asynchronous_cleanup();
104 }
105
106 static void action_Isend(const char *const *action)
107 {
108   char to[250];
109   const char *size = action[3];
110   double clock = MSG_get_clock();
111   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
112
113   sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
114   msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
115   xbt_dynar_push(globals->isends, &comm);
116
117   XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
118   log_action(action, MSG_get_clock() - clock);
119   asynchronous_cleanup();
120 }
121
122 static void action_recv(const char *const *action)
123 {
124   char mailbox_name[250];
125   msg_task_t task = NULL;
126   double clock = MSG_get_clock();
127
128   sprintf(mailbox_name, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
129
130   ACT_DEBUG("Receiving: %s", NAME);
131   msg_error_t res = MSG_task_receive(&task, mailbox_name);
132   log_action(action, MSG_get_clock() - clock);
133
134   if (res == MSG_OK) {
135     MSG_task_destroy(task);
136   }
137   asynchronous_cleanup();
138 }
139
140 static void action_Irecv(const char *const *action)
141 {
142   char mailbox[250];
143   double clock = MSG_get_clock();
144   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
145
146   XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
147
148   sprintf(mailbox, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
149   msg_task_t t = NULL;
150   xbt_dynar_push(globals->tasks, &t);
151   msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
152   xbt_dynar_push(globals->irecvs, &c);
153
154   log_action(action, MSG_get_clock() - clock);
155   asynchronous_cleanup();
156 }
157
158 static void action_wait(const char *const *action)
159 {
160   msg_task_t task = NULL;
161   msg_comm_t comm;
162   double clock = MSG_get_clock();
163   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
164
165   xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
166              xbt_str_join_array(action, " "));
167
168   ACT_DEBUG("Entering %s", NAME);
169   comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
170   MSG_comm_wait(comm, -1);
171   task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
172   MSG_comm_destroy(comm);
173   MSG_task_destroy(task);
174
175   log_action(action, MSG_get_clock() - clock);
176 }
177
178 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
179 static void action_barrier(const char *const *action)
180 {
181   static smx_mutex_t mutex = NULL;
182   static smx_cond_t cond = NULL;
183   static int processes_arrived_sofar = 0;
184
185   if (mutex == NULL) {          // first arriving on the barrier
186     mutex = simcall_mutex_init();
187     cond = simcall_cond_init();
188     processes_arrived_sofar = 0;
189   }
190   ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
191
192   simcall_mutex_lock(mutex);
193   if (++processes_arrived_sofar == communicator_size) {
194     simcall_cond_broadcast(cond);
195     simcall_mutex_unlock(mutex);
196   } else {
197     simcall_cond_wait(cond, mutex);
198     simcall_mutex_unlock(mutex);
199   }
200
201   ACT_DEBUG("Exiting barrier: %s", NAME);
202
203   processes_arrived_sofar--;
204   if (!processes_arrived_sofar) {
205     SIMIX_cond_destroy(cond);
206     SIMIX_mutex_destroy(mutex);
207     mutex = NULL;
208   }
209 }
210
211 static void action_bcast(const char *const *action)
212 {
213   int i;
214   char *bcast_identifier;
215   char mailbox[80];
216   double comm_size = parse_double(action[2]);
217   msg_task_t task = NULL;
218   const char *process_name;
219   double clock = MSG_get_clock();
220
221   process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
222
223   xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
224
225   process_name = MSG_process_get_name(MSG_process_self());
226
227   bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
228
229   if (!strcmp(process_name, "p0")) {
230     XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
231
232     msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
233
234     for (i = 1; i < communicator_size; i++) {
235       sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
236       comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
237     }
238     MSG_comm_waitall(comms, communicator_size - 1, -1);
239     for (i = 1; i < communicator_size; i++)
240       MSG_comm_destroy(comms[i - 1]);
241     xbt_free(comms);
242
243     XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
244   } else {
245     sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
246     MSG_task_receive(&task, mailbox);
247     MSG_task_destroy(task);
248     XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
249   }
250
251   log_action(action, MSG_get_clock() - clock);
252   xbt_free(bcast_identifier);
253 }
254
255 static void action_comm_size(const char *const *action)
256 {
257   const char *size = action[2];
258   double clock = MSG_get_clock();
259
260   communicator_size = parse_double(size);
261   log_action(action, MSG_get_clock() - clock);
262 }
263
264 static void action_compute(const char *const *action)
265 {
266   const char *amount = action[2];
267   msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
268   double clock = MSG_get_clock();
269
270   ACT_DEBUG("Entering %s", NAME);
271   MSG_task_execute(task);
272   MSG_task_destroy(task);
273   log_action(action, MSG_get_clock() - clock);
274 }
275
276 static void action_init(const char *const *action)
277 {
278   XBT_DEBUG("Initialize the counters");
279   process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
280   globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
281   globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
282   globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
283   MSG_process_set_data(MSG_process_self(), globals);
284 }
285
286 static void action_finalize(const char *const *action)
287 {
288   process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
289   if (globals) {
290     asynchronous_cleanup();
291     xbt_dynar_free_container(&(globals->isends));
292     xbt_dynar_free_container(&(globals->irecvs));
293     xbt_dynar_free_container(&(globals->tasks));
294     xbt_free(globals);
295   }
296 }
297
298 /** Main function */
299 int main(int argc, char *argv[])
300 {
301   msg_error_t res = MSG_OK;
302
303   /* Check the given arguments */
304   MSG_init(&argc, argv);
305   /* Explicit initialization of the action module is required now*/
306   MSG_action_init();
307
308   xbt_assert(argc > 2,
309        "Usage: %s platform_file deployment_file [action_files]\n"
310        "\t# if all actions are in the same file\n"
311        "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
312        "\t# if actions are in separate files, specified in deployment\n"
313        "\tExample: %s msg_platform.xml msg_deployment.xml ",
314        argv[0],argv[0],argv[0]);
315
316   MSG_create_environment(argv[1]);
317   MSG_launch_application(argv[2]);
318
319   /*   Action registration */
320   xbt_replay_action_register("init", action_init);
321   xbt_replay_action_register("finalize", action_finalize);
322   xbt_replay_action_register("comm_size", action_comm_size);
323   xbt_replay_action_register("send", action_send);
324   xbt_replay_action_register("Isend", action_Isend);
325   xbt_replay_action_register("recv", action_recv);
326   xbt_replay_action_register("Irecv", action_Irecv);
327   xbt_replay_action_register("wait", action_wait);
328   xbt_replay_action_register("barrier", action_barrier);
329   xbt_replay_action_register("bcast", action_bcast);
330   xbt_replay_action_register("compute", action_compute);
331
332   /* Actually do the simulation using MSG_action_trace_run */
333   res = MSG_action_trace_run(argv[3]);  // it's ok to pass a NULL argument here
334
335   XBT_INFO("Simulation time %g", MSG_get_clock());
336
337   /* Explicit finalization of the action module is required now*/
338   MSG_action_exit();
339
340   return res != MSG_OK;
341 }