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 "simgrid/s4u.hpp"
11 #include "xbt/replay.hpp"
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 /* 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;
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__); \
35 static void log_action(const char *const *action, double date)
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);
44 static void asynchronous_cleanup()
46 process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
48 /* Destroy any isend which correspond to completed communications */
51 int pos_found = MSG_comm_testany(globals->isends);
52 if (pos_found == -1) /* none remaining */
54 xbt_dynar_remove_at(globals->isends, pos_found, &comm);
55 MSG_comm_destroy(comm);
60 static void action_send(const char *const *action)
63 double size = xbt_str_parse_double(action[3], "%s is not a double");
64 double clock = MSG_get_clock();
66 snprintf(to,249, "%s_%s", MSG_process_get_name(MSG_process_self()), action[2]);
68 ACT_DEBUG("Entering Send: %s (size: %g)", NAME, size);
72 MSG_task_send(MSG_task_create(to, 0, size, NULL), to);
75 log_action(action, MSG_get_clock() - clock);
76 asynchronous_cleanup();
79 static void action_Isend(const char *const *action)
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()) );
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);
90 XBT_DEBUG("Isend on %s", MSG_process_get_name(MSG_process_self()));
91 log_action(action, MSG_get_clock() - clock);
92 asynchronous_cleanup();
95 static void action_recv(const char *const *action)
97 char mailbox_name[250];
98 msg_task_t task = NULL;
99 double clock = MSG_get_clock();
101 snprintf(mailbox_name,249, "%s_%s", action[2], MSG_process_get_name(MSG_process_self()));
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);
108 MSG_task_destroy(task);
110 asynchronous_cleanup();
113 static void action_Irecv(const char *const *action)
116 double clock = MSG_get_clock();
117 process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
119 XBT_DEBUG("Irecv on %s", MSG_process_get_name(MSG_process_self()));
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();
129 static void action_wait(const char *const *action)
132 double clock = MSG_get_clock();
133 process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
135 xbt_assert(xbt_dynar_length(globals->irecvs), "action wait not preceded by any irecv: %s",
136 xbt_str_join_array(action, " "));
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);
146 log_action(action, MSG_get_clock() - clock);
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)
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;
160 ACT_DEBUG("Entering barrier: %s (%d already there)", NAME, processes_arrived_sofar);
163 std::unique_lock<simgrid::s4u::Mutex> lock(*mutex);
164 if (++processes_arrived_sofar == communicator_size) {
165 // We can notify without the lock:
173 ACT_DEBUG("Exiting barrier: %s", NAME);
175 processes_arrived_sofar--;
176 if (processes_arrived_sofar<=0) {
182 static void action_bcast(const char *const *action)
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();
189 process_globals_t counters = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
191 xbt_assert(communicator_size, "Size of Communicator is not defined, can't use collective operations");
193 const char * process_name = MSG_process_get_name(MSG_process_self());
195 char *bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
197 if (!strcmp(process_name, "p0")) {
198 XBT_DEBUG("%s: %s is the Root", bcast_identifier, process_name);
200 msg_comm_t *comms = xbt_new0(msg_comm_t, communicator_size - 1);
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);
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]);
211 XBT_DEBUG("%s: all messages sent by %s have been received", bcast_identifier, process_name);
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);
219 log_action(action, MSG_get_clock() - clock);
220 xbt_free(bcast_identifier);
223 static void action_comm_size(const char *const *action)
225 double clock = MSG_get_clock();
227 communicator_size = xbt_str_parse_int(action[2], "%s is not an int");
228 log_action(action, MSG_get_clock() - clock);
231 static void action_compute(const char *const *action)
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();
237 ACT_DEBUG("Entering %s", NAME);
238 MSG_task_execute(task);
239 MSG_task_destroy(task);
240 log_action(action, MSG_get_clock() - clock);
243 static void action_init(const char *const *action)
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);
252 static void action_finalize(const char *const *action)
254 process_globals_t globals = static_cast<process_globals_t>( MSG_process_get_data(MSG_process_self()) );
256 asynchronous_cleanup();
257 xbt_dynar_free_container(&(globals->isends));
258 xbt_dynar_free_container(&(globals->irecvs));
263 int main(int argc, char *argv[])
265 msg_error_t res = MSG_OK;
267 /* Check the given arguments */
268 MSG_init(&argc, argv);
269 /* Explicit initialization of the action module is required now*/
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]);
280 MSG_create_environment(argv[1]);
281 MSG_launch_application(argv[2]);
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);
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
299 XBT_INFO("Simulation time %g", MSG_get_clock());
301 MSG_action_exit(); /* Explicit finalization of the action module */
303 return res != MSG_OK;