Logo AND Algorithmique Numérique Distribuée

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