Logo AND Algorithmique Numérique Distribuée

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