Logo AND Algorithmique Numérique Distribuée

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