1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "simix/simix.h" /* semaphores for the barrier */
11 #include "xbt.h" /* calloc, printf */
12 #include "simgrid_config.h" /* getline */
13 #include "instr/instr_private.h"
15 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory);
17 XBT_LOG_NEW_DEFAULT_CATEGORY(actions,
18 "Messages specific for this msg example");
19 int communicator_size = 0;
21 static void action_Isend(char*const* action);
24 int last_Irecv_sender_id;
27 int allReduce_counter;
28 xbt_dynar_t isends; /* of msg_comm_t */
29 /* Used to implement irecv+wait */
30 xbt_dynar_t irecvs; /* of msg_comm_t */
31 xbt_dynar_t tasks; /* of m_task_t */
32 } s_process_globals_t, *process_globals_t;
35 static double parse_double(const char *string)
40 value = strtod(string, &endptr);
42 THROW1(unknown_error, 0, "%s is not a double", string);
46 static int get_rank (const char *process_name)
48 return atoi(&(process_name[1]));
51 static void asynchronous_cleanup(void) {
52 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
54 /* Destroy any isend which correspond to completed communications */
57 while ((found = MSG_comm_testany(globals->isends)) != -1) {
58 xbt_dynar_remove_at(globals->isends,found,&comm);
59 MSG_comm_destroy(comm);
64 static void action_send(char*const* action)
68 const char *size_str = action[3];
69 double size=parse_double(size_str);
70 double clock = MSG_get_clock(); /* this "call" is free thanks to inlining */
72 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
74 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
75 name = xbt_str_join_array(action, " ");
78 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
79 int dst_traced = get_rank(action[2]);
80 TRACE_smpi_ptp_in(rank, rank, dst_traced, "send");
81 TRACE_smpi_send(rank, rank, dst_traced);
84 DEBUG2("Entering Send: %s (size: %lg)", name, size);
88 MSG_task_send(MSG_task_create(name, 0, size, NULL), to);
91 VERB2("%s %f", name, MSG_get_clock() - clock);
93 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
97 TRACE_smpi_ptp_out(rank, rank, dst_traced, "send");
100 asynchronous_cleanup();
103 static void action_Isend(char*const* action)
106 const char *size = action[3];
107 double clock = MSG_get_clock();
108 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
111 sprintf(to, "%s_%s", MSG_process_get_name(MSG_process_self()),action[2]);
113 MSG_task_isend( MSG_task_create(to,0,parse_double(size),NULL), to);
114 xbt_dynar_push(globals->isends,&comm);
116 DEBUG1("Isend on %s", MSG_process_get_name(MSG_process_self()));
117 VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
119 asynchronous_cleanup();
123 static void action_recv(char*const* action)
126 char mailbox_name[250];
127 m_task_t task = NULL;
128 double clock = MSG_get_clock();
130 sprintf(mailbox_name, "%s_%s", action[2],
131 MSG_process_get_name(MSG_process_self()));
133 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
134 name = xbt_str_join_array(action, " ");
137 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
138 int src_traced = get_rank(action[2]);
139 TRACE_smpi_ptp_in(rank, src_traced, rank, "recv");
142 DEBUG1("Receiving: %s", name);
143 MSG_task_receive(&task, mailbox_name);
144 // MSG_task_receive(&task, MSG_process_get_name(MSG_process_self()));
145 VERB2("%s %f", name, MSG_get_clock() - clock);
146 MSG_task_destroy(task);
148 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
151 TRACE_smpi_ptp_out(rank, src_traced, rank, "recv");
152 TRACE_smpi_recv(rank, src_traced, rank);
155 asynchronous_cleanup();
158 static void action_Irecv(char*const* action)
161 double clock = MSG_get_clock();
162 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
164 DEBUG1("Irecv on %s", MSG_process_get_name(MSG_process_self()));
166 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
167 int src_traced = get_rank(action[2]);
168 globals->last_Irecv_sender_id = src_traced;
169 MSG_process_set_data(MSG_process_self(), (void *) globals);
171 TRACE_smpi_ptp_in(rank, src_traced, rank, "Irecv");
174 sprintf(mailbox, "%s_%s", action[2],
175 MSG_process_get_name(MSG_process_self()));
177 xbt_dynar_push(globals->tasks,&t);
180 xbt_dynar_get_ptr(globals->tasks, xbt_dynar_length(globals->tasks)-1),
182 xbt_dynar_push(globals->irecvs,&c);
184 VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
187 TRACE_smpi_ptp_out(rank, src_traced, rank, "Irecv");
190 asynchronous_cleanup();
194 static void action_wait(char*const* action)
197 m_task_t task = NULL;
199 double clock = MSG_get_clock();
200 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
202 xbt_assert1(xbt_dynar_length(globals->irecvs),
203 "action wait not preceded by any irecv: %s", xbt_str_join_array(action," "));
205 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
206 name = xbt_str_join_array(action, " ");
208 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
209 int src_traced = counters->last_Irecv_sender_id;
210 int rank = get_rank(MSG_process_get_name(MSG_process_self()));
211 TRACE_smpi_ptp_in(rank, src_traced, rank, "wait");
214 DEBUG1("Entering %s", name);
215 comm = xbt_dynar_pop_as(globals->irecvs,msg_comm_t);
216 MSG_comm_wait(comm,-1);
217 task = xbt_dynar_pop_as(globals->tasks,m_task_t);
218 MSG_comm_destroy(comm);
219 MSG_task_destroy(task);
221 VERB2("%s %f", name, MSG_get_clock() - clock);
222 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
225 TRACE_smpi_ptp_out(rank, src_traced, rank, "wait");
226 TRACE_smpi_recv(rank, src_traced, rank);
231 /* FIXME: that's a poor man's implementation: we should take the message exchanges into account */
232 static void action_barrier(char*const* action)
235 static smx_mutex_t mutex = NULL;
236 static smx_cond_t cond = NULL;
237 static int processes_arrived_sofar=0;
239 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
240 name = xbt_str_join_array(action, " ");
242 if (mutex == NULL) { // first arriving on the barrier
243 mutex = SIMIX_req_mutex_init();
244 cond = SIMIX_req_cond_init();
245 processes_arrived_sofar=0;
247 DEBUG2("Entering barrier: %s (%d already there)", name,processes_arrived_sofar);
249 SIMIX_req_mutex_lock(mutex);
250 if (++processes_arrived_sofar == communicator_size) {
251 SIMIX_req_cond_broadcast(cond);
252 SIMIX_req_mutex_unlock(mutex);
254 SIMIX_req_cond_wait(cond,mutex);
255 SIMIX_req_mutex_unlock(mutex);
258 DEBUG1("Exiting barrier: %s", name);
260 processes_arrived_sofar--;
261 if (!processes_arrived_sofar) {
262 SIMIX_req_cond_destroy(cond);
263 SIMIX_req_mutex_destroy(mutex);
267 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
272 static void action_reduce(char*const* action)
275 char *reduce_identifier;
277 double comm_size = parse_double(action[2]);
278 double comp_size = parse_double(action[3]);
279 m_task_t comp_task = NULL;
280 const char *process_name;
281 double clock = MSG_get_clock();
283 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
285 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
286 "can't use collective operations");
288 process_name = MSG_process_get_name(MSG_process_self());
290 reduce_identifier = bprintf("reduce_%d", counters->reduce_counter++);
292 if (!strcmp(process_name, "p0")) {
293 DEBUG2("%s: %s is the Root", reduce_identifier, process_name);
295 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
296 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
297 for (i = 1; i < communicator_size; i++) {
298 sprintf(mailbox, "%s_p%d_p0", reduce_identifier, i);
299 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
301 MSG_comm_waitall(comms,communicator_size-1,-1);
302 for (i = 1; i < communicator_size; i++) {
303 MSG_comm_destroy(comms[i-1]);
304 MSG_task_destroy(tasks[i-1]);
308 comp_task = MSG_task_create("reduce_comp", comp_size, 0, NULL);
309 DEBUG1("%s: computing 'reduce_comp'", reduce_identifier);
310 MSG_task_execute(comp_task);
311 MSG_task_destroy(comp_task);
312 DEBUG1("%s: computed", reduce_identifier);
315 DEBUG2("%s: %s sends", reduce_identifier, process_name);
316 sprintf(mailbox, "%s_%s_p0", reduce_identifier, process_name);
317 DEBUG1("put on %s", mailbox);
318 MSG_task_send(MSG_task_create(reduce_identifier, 0, comm_size, NULL),
322 VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
323 free(reduce_identifier);
326 static void action_bcast(char*const* action)
329 char *bcast_identifier;
331 double comm_size = parse_double(action[2]);
332 m_task_t task = NULL;
333 const char *process_name;
334 double clock = MSG_get_clock();
336 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
338 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
339 "can't use collective operations");
341 process_name = MSG_process_get_name(MSG_process_self());
343 bcast_identifier = bprintf("bcast_%d", counters->bcast_counter++);
345 if (!strcmp(process_name, "p0")) {
346 DEBUG2("%s: %s is the Root", bcast_identifier, process_name);
348 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
350 for (i = 1; i < communicator_size; i++) {
351 sprintf(mailbox, "%s_p0_p%d", bcast_identifier, i);
353 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
356 MSG_comm_waitall(comms,communicator_size-1,-1);
357 for (i = 1; i < communicator_size; i++)
358 MSG_comm_destroy(comms[i-1]);
361 DEBUG2("%s: all messages sent by %s have been received",
362 bcast_identifier, process_name);
365 sprintf(mailbox, "%s_p0_%s", bcast_identifier, process_name);
366 MSG_task_receive(&task, mailbox);
367 MSG_task_destroy(task);
368 DEBUG2("%s: %s has received", bcast_identifier, process_name);
371 VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
372 free(bcast_identifier);
376 static void action_sleep(char*const* action)
379 const char *duration = action[2];
380 double clock = MSG_get_clock();
382 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
383 name = xbt_str_join_array(action, " ");
385 DEBUG1("Entering %s", name);
386 MSG_process_sleep(parse_double(duration));
387 VERB2("%s %f ", name, MSG_get_clock() - clock);
389 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
393 static void action_allReduce(char*const* action) {
395 char *allreduce_identifier;
397 double comm_size = parse_double(action[2]);
398 double comp_size = parse_double(action[3]);
399 m_task_t task = NULL, comp_task = NULL;
400 const char *process_name;
401 double clock = MSG_get_clock();
403 process_globals_t counters = (process_globals_t) MSG_process_get_data(MSG_process_self());
405 xbt_assert0(communicator_size, "Size of Communicator is not defined, "
406 "can't use collective operations");
408 process_name = MSG_process_get_name(MSG_process_self());
410 allreduce_identifier = bprintf("allReduce_%d", counters->allReduce_counter++);
412 if (!strcmp(process_name, "p0")) {
413 DEBUG2("%s: %s is the Root", allreduce_identifier, process_name);
415 msg_comm_t *comms = xbt_new0(msg_comm_t,communicator_size-1);
416 m_task_t *tasks = xbt_new0(m_task_t,communicator_size-1);
417 for (i = 1; i < communicator_size; i++) {
418 sprintf(mailbox, "%s_p%d_p0", allreduce_identifier, i);
419 comms[i-1] = MSG_task_irecv(&(tasks[i-1]),mailbox);
421 MSG_comm_waitall(comms,communicator_size-1,-1);
422 for (i = 1; i < communicator_size; i++) {
423 MSG_comm_destroy(comms[i-1]);
424 MSG_task_destroy(tasks[i-1]);
428 comp_task = MSG_task_create("allReduce_comp", comp_size, 0, NULL);
429 DEBUG1("%s: computing 'reduce_comp'", allreduce_identifier);
430 MSG_task_execute(comp_task);
431 MSG_task_destroy(comp_task);
432 DEBUG1("%s: computed", allreduce_identifier);
434 for (i = 1; i < communicator_size; i++) {
435 sprintf(mailbox, "%s_p0_p%d", allreduce_identifier, i);
437 MSG_task_isend(MSG_task_create(mailbox,0,comm_size,NULL),
440 MSG_comm_waitall(comms,communicator_size-1,-1);
441 for (i = 1; i < communicator_size; i++)
442 MSG_comm_destroy(comms[i-1]);
445 DEBUG2("%s: all messages sent by %s have been received",
446 allreduce_identifier, process_name);
449 DEBUG2("%s: %s sends", allreduce_identifier, process_name);
450 sprintf(mailbox, "%s_%s_p0", allreduce_identifier, process_name);
451 DEBUG1("put on %s", mailbox);
452 MSG_task_send(MSG_task_create(allreduce_identifier, 0, comm_size, NULL),
455 sprintf(mailbox, "%s_p0_%s", allreduce_identifier, process_name);
456 MSG_task_receive(&task, mailbox);
457 MSG_task_destroy(task);
458 DEBUG2("%s: %s has received", allreduce_identifier, process_name);
461 VERB2("%s %f", xbt_str_join_array(action, " "), MSG_get_clock() - clock);
462 free(allreduce_identifier);
465 static void action_comm_size(char*const* action)
468 const char *size = action[2];
469 double clock = MSG_get_clock();
471 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
472 name = xbt_str_join_array(action, " ");
473 communicator_size = parse_double(size);
474 VERB2("%s %f", name, MSG_get_clock() - clock);
475 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
479 static void action_compute(char*const* action)
482 const char *amout = action[2];
483 m_task_t task = MSG_task_create(name, parse_double(amout), 0, NULL);
484 double clock = MSG_get_clock();
486 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
487 name = xbt_str_join_array(action, " ");
488 DEBUG1("Entering %s", name);
489 MSG_task_execute(task);
490 MSG_task_destroy(task);
491 VERB2("%s %f", name, MSG_get_clock() - clock);
492 if (XBT_LOG_ISENABLED(actions, xbt_log_priority_verbose))
496 static void action_init(char*const* action)
499 TRACE_smpi_init(get_rank(MSG_process_get_name(MSG_process_self())));
501 DEBUG0("Initialize the counters");
502 process_globals_t globals = (process_globals_t) calloc(1, sizeof(s_process_globals_t));
503 globals->isends = xbt_dynar_new(sizeof(msg_comm_t),NULL);
504 globals->irecvs = xbt_dynar_new(sizeof(msg_comm_t),NULL);
505 globals->tasks = xbt_dynar_new(sizeof(m_task_t),NULL);
506 MSG_process_set_data(MSG_process_self(),globals);
510 static void action_finalize(char*const* action)
513 TRACE_smpi_finalize(get_rank(MSG_process_get_name(MSG_process_self())));
515 process_globals_t globals = (process_globals_t) MSG_process_get_data(MSG_process_self());
517 xbt_dynar_free_container(&(globals->isends));
518 xbt_dynar_free_container(&(globals->irecvs));
519 xbt_dynar_free_container(&(globals->tasks));
525 int main(int argc, char *argv[])
527 MSG_error_t res = MSG_OK;
529 smx_factory_initializer_to_use = SIMIX_ctx_raw_factory_init;
531 /* Check the given arguments */
532 MSG_global_init(&argc, argv);
534 printf("Usage: %s platform_file deployment_file [action_files]\n",
537 ("example: %s msg_platform.xml msg_deployment.xml actions # if all actions are in the same file\n",
540 ("example: %s msg_platform.xml msg_deployment.xml # if actions are in separate files, specified in deployment\n",
545 /* Simulation setting */
546 MSG_create_environment(argv[1]);
548 /* No need to register functions as in classical MSG programs: the actions get started anyway */
549 MSG_launch_application(argv[2]);
551 /* Action registration */
552 MSG_action_register("init", action_init);
553 MSG_action_register("finalize", action_finalize);
554 MSG_action_register("comm_size",action_comm_size);
555 MSG_action_register("send", action_send);
556 MSG_action_register("Isend", action_Isend);
557 MSG_action_register("recv", action_recv);
558 MSG_action_register("Irecv", action_Irecv);
559 MSG_action_register("wait", action_wait);
560 MSG_action_register("barrier", action_barrier);
561 MSG_action_register("bcast", action_bcast);
562 MSG_action_register("reduce", action_reduce);
563 MSG_action_register("allReduce",action_allReduce);
564 MSG_action_register("sleep", action_sleep);
565 MSG_action_register("compute", action_compute);
568 /* Actually do the simulation using MSG_action_trace_run */
569 res = MSG_action_trace_run(argv[3]); // it's ok to pass a NULL argument here
571 INFO1("Simulation time %g", MSG_get_clock());