Logo AND Algorithmique Numérique Distribuée

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