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. */
6 #include "simgrid/msg.h"
7 #include "simgrid/simix.h" /* semaphores for the barrier */
8 #include <xbt/replay.h>
9 #include "simgrid/s4u.h"
11 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
12 int communicator_size = 0;
14 static void action_Isend(const char *const *action);
17 int last_Irecv_sender_id;
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;
26 static double parse_double(const char *string)
31 value = strtod(string, &endptr);
33 THROWF(unknown_error, 0, "%s is not a double", string);
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__); \
44 static void log_action(const char *const *action, double date)
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);
53 static void asynchronous_cleanup(void)
55 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
57 /* Destroy any isend which correspond to completed communications */
60 int pos_found = MSG_comm_testany(globals->isends);
61 if (pos_found == -1) /* none remaining */
63 xbt_dynar_remove_at(globals->isends, pos_found, &comm);
64 MSG_comm_destroy(comm);
69 static void action_send(const char *const *action)
72 const char *size_str = action[3];
73 double size = parse_double(size_str);
74 double clock = MSG_get_clock();
76 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
78 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
82 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
85 log_action(action, MSG_get_clock() - clock);
86 asynchronous_cleanup();
89 static void action_Isend(const char *const *action)
92 const char *size = action[3];
93 double clock = MSG_get_clock();
94 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
96 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
97 msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
98 xbt_dynar_push(globals->isends, &comm);
100 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
101 log_action(action, MSG_get_clock() - clock);
102 asynchronous_cleanup();
105 static void action_recv(const char *const *action)
107 char mailbox_name[250];
108 msg_task_t task = NULL;
109 double clock = MSG_get_clock();
111 snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
113 ACT_DEBUG("Receiving: %s", NAME);
114 msg_error_t res = MSG_task_receive(&task, mailbox_name);
115 log_action(action, MSG_get_clock() - clock);
118 MSG_task_destroy(task);
120 asynchronous_cleanup();
123 static void action_Irecv(const char *const *action)
126 double clock = MSG_get_clock();
127 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
129 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
131 snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
133 xbt_dynar_push(globals->tasks, &t);
134 msg_comm_t c = MSG_task_irecv((msg_task**)xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
135 xbt_dynar_push(globals->irecvs, &c);
137 log_action(action, MSG_get_clock() - clock);
138 asynchronous_cleanup();
141 static void action_wait(const char *const *action)
143 msg_task_t task = NULL;
145 double clock = MSG_get_clock();
146 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
148 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
149 xbt_str_join_array(action, " "));
151 ACT_DEBUG("Entering %s", NAME);
152 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
153 MSG_comm_wait(comm, -1);
154 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
155 MSG_comm_destroy(comm);
156 MSG_task_destroy(task);
158 log_action(action, MSG_get_clock() - clock);
161 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
162 static void action_barrier(const char *const *action)
164 // static smx_mutex_t mutex = NULL;
165 // static smx_cond_t cond = NULL;
166 static simgrid::s4u::Mutex *mutex = NULL;
167 static simgrid::s4u::ConditionVariable *cond = NULL;
168 static int processes_arrived_sofar = 0;
169 if (mutex == NULL) { // first arriving on the barrier
170 mutex = new simgrid::s4u::Mutex();
171 cond = new simgrid::s4u::ConditionVariable();
172 processes_arrived_sofar = 0;
174 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
176 if (++processes_arrived_sofar == communicator_size) {
184 ACT_DEBUG("Exiting barrier: %s", NAME);
186 processes_arrived_sofar--;
187 if (processes_arrived_sofar<=0) {
194 static void action_bcast(const char *const *action)
197 double comm_size = parse_double(action[2]);
198 msg_task_t task = NULL;
199 double clock = MSG_get_clock();
201 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
203 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
205 const char * process_name = MSG_process_get_name(MSG_process_self());
207 char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
209 if (!strcmp(process_name, "p0")) {
210 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
212 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
214 for (int i = 1; i < communicator_size; i++) {
215 snprintf(mailbox,79, "%s_p0_p%d", bcast_identifier, i);
216 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
218 MSG_comm_waitall(comms, communicator_size - 1, -1);
219 for (int i = 1; i < communicator_size; i++)
220 MSG_comm_destroy(comms[i - 1]);
223 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
225 snprintf(mailbox,79, "%s_p0_%s", bcast_identifier, process_name);
226 MSG_task_receive(&task, mailbox);
227 MSG_task_destroy(task);
228 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
231 log_action(action, MSG_get_clock() - clock);
232 xbt_free(bcast_identifier);
235 static void action_comm_size(const char *const *action)
237 const char *size = action[2];
238 double clock = MSG_get_clock();
240 communicator_size = parse_double(size);
241 log_action(action, MSG_get_clock() - clock);
244 static void action_compute(const char *const *action)
246 const char *amount = action[2];
247 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
248 double clock = MSG_get_clock();
250 ACT_DEBUG("Entering %s", NAME);
251 MSG_task_execute(task);
252 MSG_task_destroy(task);
253 log_action(action, MSG_get_clock() - clock);
256 static void action_init(const char *const *action)
258 XBT_DEBUG("Initialize the counters");
259 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
260 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
261 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
262 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
263 MSG_process_set_data(MSG_process_self(), globals);
266 static void action_finalize(const char *const *action)
268 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
270 asynchronous_cleanup();
271 xbt_dynar_free_container(&(globals->isends));
272 xbt_dynar_free_container(&(globals->irecvs));
273 xbt_dynar_free_container(&(globals->tasks));
278 int main(int argc, char *argv[])
280 msg_error_t res = MSG_OK;
282 /* Check the given arguments */
283 MSG_init(&argc, argv);
284 /* Explicit initialization of the action module is required now*/
288 "Usage: %s platform_file deployment_file [action_files]\n"
289 "\t# if all actions are in the same file\n"
290 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
291 "\t# if actions are in separate files, specified in deployment\n"
292 "\tExample: %s msg_platform.xml msg_deployment.xml ",
293 argv[0],argv[0],argv[0]);
295 MSG_create_environment(argv[1]);
296 MSG_launch_application(argv[2]);
298 /* Action registration */
299 xbt_replay_action_register("init", action_init);
300 xbt_replay_action_register("finalize", action_finalize);
301 xbt_replay_action_register("comm_size", action_comm_size);
302 xbt_replay_action_register("send", action_send);
303 xbt_replay_action_register("Isend", action_Isend);
304 xbt_replay_action_register("recv", action_recv);
305 xbt_replay_action_register("Irecv", action_Irecv);
306 xbt_replay_action_register("wait", action_wait);
307 xbt_replay_action_register("barrier", action_barrier);
308 xbt_replay_action_register("bcast", action_bcast);
309 xbt_replay_action_register("compute", action_compute);
311 /* Actually do the simulation using MSG_action_trace_run */
312 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
314 XBT_INFO("Simulation time %g", MSG_get_clock());
316 MSG_action_exit(); /* Explicit finalization of the action module */
318 return res != MSG_OK;