1 /* Copyright (c) 2009-2016. The SimGrid Team. All rights reserved. */
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. */
8 #include "simgrid/msg.h"
9 #include "simgrid/simix.h" /* semaphores for the barrier */
10 #include <xbt/replay.h>
11 #include "simgrid/s4u.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
14 int communicator_size = 0;
16 static void action_Isend(const char *const *action);
19 int last_Irecv_sender_id;
21 xbt_dynar_t isends; /* of msg_comm_t */
22 /* Used to implement irecv+wait */
23 xbt_dynar_t irecvs; /* of msg_comm_t */
24 xbt_dynar_t tasks; /* of msg_task_t */
25 } s_process_globals_t, *process_globals_t;
28 static double parse_double(const char *string)
33 value = strtod(string, &endptr);
35 THROWF(unknown_error, 0, "%s is not a double", string);
39 #define ACT_DEBUG(...) \
40 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) { \
41 char *NAME = xbt_str_join_array(action, " "); \
42 XBT_DEBUG(__VA_ARGS__); \
46 static void log_action(const char *const *action, double date)
48 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose)) {
49 char *name = xbt_str_join_array(action, " ");
50 XBT_VERB("%s %f", name, date);
55 static void asynchronous_cleanup(void)
57 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
59 /* Destroy any isend which correspond to completed communications */
62 int pos_found = MSG_comm_testany(globals->isends);
63 if (pos_found == -1) /* none remaining */
65 xbt_dynar_remove_at(globals->isends, pos_found, &comm);
66 MSG_comm_destroy(comm);
71 static void action_send(const char *const *action)
74 const char *size_str = action[3];
75 double size = parse_double(size_str);
76 double clock = MSG_get_clock();
78 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
80 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
84 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
87 log_action(action, MSG_get_clock() - clock);
88 asynchronous_cleanup();
91 static void action_Isend(const char *const *action)
94 const char *size = action[3];
95 double clock = MSG_get_clock();
96 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
98 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
99 msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
100 xbt_dynar_push(globals->isends, &comm);
102 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
103 log_action(action, MSG_get_clock() - clock);
104 asynchronous_cleanup();
107 static void action_recv(const char *const *action)
109 char mailbox_name[250];
110 msg_task_t task = NULL;
111 double clock = MSG_get_clock();
113 snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
115 ACT_DEBUG("Receiving: %s", NAME);
116 msg_error_t res = MSG_task_receive(&task, mailbox_name);
117 log_action(action, MSG_get_clock() - clock);
120 MSG_task_destroy(task);
122 asynchronous_cleanup();
125 static void action_Irecv(const char *const *action)
128 double clock = MSG_get_clock();
129 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
131 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
133 snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
135 xbt_dynar_push(globals->tasks, &t);
136 msg_comm_t c = MSG_task_irecv((msg_task**)xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
137 xbt_dynar_push(globals->irecvs, &c);
139 log_action(action, MSG_get_clock() - clock);
140 asynchronous_cleanup();
143 static void action_wait(const char *const *action)
145 msg_task_t task = NULL;
147 double clock = MSG_get_clock();
148 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
150 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
151 xbt_str_join_array(action, " "));
153 ACT_DEBUG("Entering %s", NAME);
154 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
155 MSG_comm_wait(comm, -1);
156 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
157 MSG_comm_destroy(comm);
158 MSG_task_destroy(task);
160 log_action(action, MSG_get_clock() - clock);
163 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
164 static void action_barrier(const char *const *action)
166 // static smx_mutex_t mutex = NULL;
167 // static smx_cond_t cond = NULL;
168 static simgrid::s4u::Mutex *mutex = NULL;
169 static simgrid::s4u::ConditionVariable *cond = NULL;
170 static int processes_arrived_sofar = 0;
171 if (mutex == NULL) { // first arriving on the barrier
172 mutex = new simgrid::s4u::Mutex();
173 cond = new simgrid::s4u::ConditionVariable();
174 processes_arrived_sofar = 0;
176 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
179 std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
180 if (++processes_arrived_sofar == communicator_size) {
181 // We can notify without the lock:
189 ACT_DEBUG("Exiting barrier: %s", NAME);
191 processes_arrived_sofar--;
192 if (processes_arrived_sofar<=0) {
199 static void action_bcast(const char *const *action)
202 double comm_size = parse_double(action[2]);
203 msg_task_t task = NULL;
204 double clock = MSG_get_clock();
206 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
208 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
210 const char * process_name = MSG_process_get_name(MSG_process_self());
212 char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
214 if (!strcmp(process_name, "p0")) {
215 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
217 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
219 for (int i = 1; i < communicator_size; i++) {
220 snprintf(mailbox,79, "%s_p0_p%d", bcast_identifier, i);
221 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
223 MSG_comm_waitall(comms, communicator_size - 1, -1);
224 for (int i = 1; i < communicator_size; i++)
225 MSG_comm_destroy(comms[i - 1]);
228 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
230 snprintf(mailbox,79, "%s_p0_%s", bcast_identifier, process_name);
231 MSG_task_receive(&task, mailbox);
232 MSG_task_destroy(task);
233 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
236 log_action(action, MSG_get_clock() - clock);
237 xbt_free(bcast_identifier);
240 static void action_comm_size(const char *const *action)
242 const char *size = action[2];
243 double clock = MSG_get_clock();
245 communicator_size = parse_double(size);
246 log_action(action, MSG_get_clock() - clock);
249 static void action_compute(const char *const *action)
251 const char *amount = action[2];
252 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
253 double clock = MSG_get_clock();
255 ACT_DEBUG("Entering %s", NAME);
256 MSG_task_execute(task);
257 MSG_task_destroy(task);
258 log_action(action, MSG_get_clock() - clock);
261 static void action_init(const char *const *action)
263 XBT_DEBUG("Initialize the counters");
264 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
265 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
266 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
267 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
268 MSG_process_set_data(MSG_process_self(), globals);
271 static void action_finalize(const char *const *action)
273 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
275 asynchronous_cleanup();
276 xbt_dynar_free_container(&(globals->isends));
277 xbt_dynar_free_container(&(globals->irecvs));
278 xbt_dynar_free_container(&(globals->tasks));
283 int main(int argc, char *argv[])
285 msg_error_t res = MSG_OK;
287 /* Check the given arguments */
288 MSG_init(&argc, argv);
289 /* Explicit initialization of the action module is required now*/
293 "Usage: %s platform_file deployment_file [action_files]\n"
294 "\t# if all actions are in the same file\n"
295 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
296 "\t# if actions are in separate files, specified in deployment\n"
297 "\tExample: %s msg_platform.xml msg_deployment.xml ",
298 argv[0],argv[0],argv[0]);
300 MSG_create_environment(argv[1]);
301 MSG_launch_application(argv[2]);
303 /* Action registration */
304 xbt_replay_action_register("init", action_init);
305 xbt_replay_action_register("finalize", action_finalize);
306 xbt_replay_action_register("comm_size", action_comm_size);
307 xbt_replay_action_register("send", action_send);
308 xbt_replay_action_register("Isend", action_Isend);
309 xbt_replay_action_register("recv", action_recv);
310 xbt_replay_action_register("Irecv", action_Irecv);
311 xbt_replay_action_register("wait", action_wait);
312 xbt_replay_action_register("barrier", action_barrier);
313 xbt_replay_action_register("bcast", action_bcast);
314 xbt_replay_action_register("compute", action_compute);
316 /* Actually do the simulation using MSG_action_trace_run */
317 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
319 XBT_INFO("Simulation time %g", MSG_get_clock());
321 MSG_action_exit(); /* Explicit finalization of the action module */
323 return res != MSG_OK;