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>
10 XBT_LOG_NEW_DEFAULT_CATEGORY(actions, "Messages specific for this msg example");
11 int communicator_size = 0;
13 static void action_Isend(const char *const *action);
16 int last_Irecv_sender_id;
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;
25 static double parse_double(const char *string)
30 value = strtod(string, &endptr);
32 THROWF(unknown_error, 0, "%s is not a double", string);
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__); \
43 static void log_action(const char *const *action, double date)
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);
52 static void asynchronous_cleanup(void)
54 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
56 /* Destroy any isend which correspond to completed communications */
59 int pos_found = MSG_comm_testany(globals->isends);
60 if (pos_found == -1) /* none remaining */
62 xbt_dynar_remove_at(globals->isends, pos_found, &comm);
63 MSG_comm_destroy(comm);
68 static void action_send(const char *const *action)
71 const char *size_str = action[3];
72 double size = parse_double(size_str);
73 double clock = MSG_get_clock();
75 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
77 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
81 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
84 log_action(action, MSG_get_clock() - clock);
85 asynchronous_cleanup();
88 static void action_Isend(const char *const *action)
91 const char *size = action[3];
92 double clock = MSG_get_clock();
93 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
95 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
96 msg_comm_t comm = MSG_task_isend(MSG_task_create(to, 0, parse_double(size), NULL), to);
97 xbt_dynar_push(globals->isends, &comm);
99 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
100 log_action(action, MSG_get_clock() - clock);
101 asynchronous_cleanup();
104 static void action_recv(const char *const *action)
106 char mailbox_name[250];
107 msg_task_t task = NULL;
108 double clock = MSG_get_clock();
110 snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
112 ACT_DEBUG("Receiving: %s", NAME);
113 msg_error_t res = MSG_task_receive(&task, mailbox_name);
114 log_action(action, MSG_get_clock() - clock);
117 MSG_task_destroy(task);
119 asynchronous_cleanup();
122 static void action_Irecv(const char *const *action)
125 double clock = MSG_get_clock();
126 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
128 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
130 snprintf(mailbox,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
132 xbt_dynar_push(globals->tasks, &t);
133 msg_comm_t c = MSG_task_irecv(xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks) - 1), mailbox);
134 xbt_dynar_push(globals->irecvs, &c);
136 log_action(action, MSG_get_clock() - clock);
137 asynchronous_cleanup();
140 static void action_wait(const char *const *action)
142 msg_task_t task = NULL;
144 double clock = MSG_get_clock();
145 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
147 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
148 xbt_str_join_array(action, " "));
150 ACT_DEBUG("Entering %s", NAME);
151 comm = xbt_dynar_pop_as(globals->irecvs, msg_comm_t);
152 MSG_comm_wait(comm, -1);
153 task = xbt_dynar_pop_as(globals->tasks, msg_task_t);
154 MSG_comm_destroy(comm);
155 MSG_task_destroy(task);
157 log_action(action, MSG_get_clock() - clock);
160 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
161 static void action_barrier(const char *const *action)
163 static smx_mutex_t mutex = NULL;
164 static smx_cond_t cond = NULL;
165 static int processes_arrived_sofar = 0;
167 if (mutex == NULL) { // first arriving on the barrier
168 mutex = simcall_mutex_init();
169 cond = simcall_cond_init();
170 processes_arrived_sofar = 0;
172 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
174 simcall_mutex_lock(mutex);
175 if (++processes_arrived_sofar == communicator_size) {
176 simcall_cond_broadcast(cond);
177 simcall_mutex_unlock(mutex);
179 simcall_cond_wait(cond, mutex);
180 simcall_mutex_unlock(mutex);
183 ACT_DEBUG("Exiting barrier: %s", NAME);
185 processes_arrived_sofar--;
186 if (processes_arrived_sofar<=0) {
187 SIMIX_cond_unref(cond);
188 SIMIX_mutex_unref(mutex);
193 static void action_bcast(const char *const *action)
196 double comm_size = parse_double(action[2]);
197 msg_task_t task = NULL;
198 double clock = MSG_get_clock();
200 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
202 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
204 const char * process_name = MSG_process_get_name(MSG_process_self());
206 char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
208 if (!strcmp(process_name, "p0")) {
209 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
211 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
213 for (int i = 1; i < communicator_size; i++) {
214 snprintf(mailbox,79, "%s_p0_p%d", bcast_identifier, i);
215 comms[i - 1] = MSG_task_isend(MSG_task_create(mailbox, 0, comm_size, NULL), mailbox);
217 MSG_comm_waitall(comms, communicator_size - 1, -1);
218 for (int i = 1; i < communicator_size; i++)
219 MSG_comm_destroy(comms[i - 1]);
222 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
224 snprintf(mailbox,79, "%s_p0_%s", bcast_identifier, process_name);
225 MSG_task_receive(&task, mailbox);
226 MSG_task_destroy(task);
227 XBT_DEBUG("%s: %s has received", bcast_identifier, process_name);
230 log_action(action, MSG_get_clock() - clock);
231 xbt_free(bcast_identifier);
234 static void action_comm_size(const char *const *action)
236 const char *size = action[2];
237 double clock = MSG_get_clock();
239 communicator_size = parse_double(size);
240 log_action(action, MSG_get_clock() - clock);
243 static void action_compute(const char *const *action)
245 const char *amount = action[2];
246 msg_task_t task = MSG_task_create("task", parse_double(amount), 0, NULL);
247 double clock = MSG_get_clock();
249 ACT_DEBUG("Entering %s", NAME);
250 MSG_task_execute(task);
251 MSG_task_destroy(task);
252 log_action(action, MSG_get_clock() - clock);
255 static void action_init(const char *const *action)
257 XBT_DEBUG("Initialize the counters");
258 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
259 globals->isends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
260 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
261 globals->tasks = xbt_dynar_new(sizeof(msg_task_t), NULL);
262 MSG_process_set_data(MSG_process_self(), globals);
265 static void action_finalize(const char *const *action)
267 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
269 asynchronous_cleanup();
270 xbt_dynar_free_container(&(globals->isends));
271 xbt_dynar_free_container(&(globals->irecvs));
272 xbt_dynar_free_container(&(globals->tasks));
277 int main(int argc, char *argv[])
279 msg_error_t res = MSG_OK;
281 /* Check the given arguments */
282 MSG_init(&argc, argv);
283 /* Explicit initialization of the action module is required now*/
287 "Usage: %s platform_file deployment_file [action_files]\n"
288 "\t# if all actions are in the same file\n"
289 "\tExample: %s msg_platform.xml msg_deployment.xml actions\n"
290 "\t# if actions are in separate files, specified in deployment\n"
291 "\tExample: %s msg_platform.xml msg_deployment.xml ",
292 argv[0],argv[0],argv[0]);
294 MSG_create_environment(argv[1]);
295 MSG_launch_application(argv[2]);
297 /* Action registration */
298 xbt_replay_action_register("init", action_init);
299 xbt_replay_action_register("finalize", action_finalize);
300 xbt_replay_action_register("comm_size", action_comm_size);
301 xbt_replay_action_register("send", action_send);
302 xbt_replay_action_register("Isend", action_Isend);
303 xbt_replay_action_register("recv", action_recv);
304 xbt_replay_action_register("Irecv", action_Irecv);
305 xbt_replay_action_register("wait", action_wait);
306 xbt_replay_action_register("barrier", action_barrier);
307 xbt_replay_action_register("bcast", action_bcast);
308 xbt_replay_action_register("compute", action_compute);
310 /* Actually do the simulation using MSG_action_trace_run */
311 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
313 XBT_INFO("Simulation time %g", MSG_get_clock());
315 MSG_action_exit(); /* Explicit finalization of the action module */
317 return res != MSG_OK;