1 /* Copyright (c) 2007-2015. 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. */
8 #include "smpi_mpi_dt_private.h"
10 #include "src/mc/mc_record.h"
11 #include "xbt/replay.h"
12 #include "surf/surf.h"
13 #include "src/simix/smx_private.h"
14 #include "simgrid/sg_config.h"
15 #include "src/mc/mc_replay.h"
16 #include "src/msg/msg_private.h"
18 #include <float.h> /* DBL_MAX */
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
24 "Logging specific to SMPI (kernel)");
26 typedef struct s_smpi_process_data {
31 smx_rdv_t mailbox_small;
32 xbt_mutex_t mailboxes_mutex;
37 void *data; /* user data */
40 int sampling; /* inside an SMPI_SAMPLE_ block? */
42 int replaying; /* is the process replaying a trace */
43 xbt_bar_t finalization_barrier;
44 } s_smpi_process_data_t;
46 static smpi_process_data_t *process_data = NULL;
47 int process_count = 0;
48 int smpi_universe_size = 0;
49 int* index_to_process_data = NULL;
50 extern double smpi_total_benched_time;
51 xbt_os_timer_t global_timer;
52 MPI_Comm MPI_COMM_WORLD = MPI_COMM_UNINITIALIZED;
54 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
55 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
56 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
58 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
60 static char *get_mailbox_name(char *str, int index)
62 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
67 static char *get_mailbox_name_small(char *str, int index)
69 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
74 void smpi_process_init(int *argc, char ***argv)
77 smpi_process_data_t data;
81 proc = SIMIX_process_self();
82 //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
83 SIMIX_process_set_cleanup_function(proc, SIMIX_process_cleanup);
84 char* instance_id = (*argv)[1];
85 int rank = atoi((*argv)[2]);
86 index = smpi_process_index_of_smx_process(proc);
88 if(!index_to_process_data){
89 index_to_process_data=(int*)xbt_malloc(SIMIX_process_count()*sizeof(int));
92 if(smpi_privatize_global_variables){
93 /* Now using segment index of the process */
94 index = proc->segment_index;
95 /* Done at the process's creation */
96 SMPI_switch_data_segment(index);
99 MPI_Comm* temp_comm_world;
101 smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world ,&temp_bar);
102 data = smpi_process_remote_data(index);
103 data->comm_world = temp_comm_world;
104 if(temp_bar != NULL) data->finalization_barrier = temp_bar;
106 data->instance_id = instance_id;
108 //xbt_free(simcall_process_get_data(proc));
110 simdata_process_t simdata = static_cast<simdata_process_t>(simcall_process_get_data(proc));
111 simdata->data = data;
115 memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
116 (*argv)[(*argc) - 1] = NULL;
117 (*argv)[(*argc) - 2] = NULL;
122 // set the process attached to the mailbox
123 simcall_rdv_set_receiver(data->mailbox_small, proc);
124 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
127 xbt_assert(smpi_process_data(),
128 "smpi_process_data() returned NULL. You probably gave a NULL parameter to MPI_Init. Although it's required by MPI-2, this is currently not supported by SMPI.");
131 void smpi_process_destroy(void)
133 int index = smpi_process_index();
134 if(smpi_privatize_global_variables){
135 smpi_switch_data_segment(index);
137 process_data[index_to_process_data[index]]->state = SMPI_FINALIZED;
138 XBT_DEBUG("<%d> Process left the game", index);
142 * @brief Prepares the current process for termination.
144 void smpi_process_finalize(void)
146 // This leads to an explosion of the search graph
147 // which cannot be reduced:
148 if(MC_is_active() || MC_record_replay_is_active())
151 int index = smpi_process_index();
152 // wait for all pending asynchronous comms to finish
153 xbt_barrier_wait(process_data[index_to_process_data[index]]->finalization_barrier);
157 * @brief Check if a process is finalized
159 int smpi_process_finalized()
161 int index = smpi_process_index();
162 if (index != MPI_UNDEFINED)
163 return (process_data[index_to_process_data[index]]->state == SMPI_FINALIZED);
169 * @brief Check if a process is initialized
171 int smpi_process_initialized(void)
173 if (!index_to_process_data){
177 int index = smpi_process_index();
178 return ( (index != MPI_UNDEFINED)
179 && (process_data[index_to_process_data[index]]->state == SMPI_INITIALIZED));
184 * @brief Mark a process as initialized (=MPI_Init called)
186 void smpi_process_mark_as_initialized(void)
188 int index = smpi_process_index();
189 if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
190 process_data[index_to_process_data[index]]->state = SMPI_INITIALIZED;
193 void smpi_process_set_replaying(int value){
194 int index = smpi_process_index();
195 if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
196 process_data[index_to_process_data[index]]->replaying = value;
199 int smpi_process_get_replaying(){
200 int index = smpi_process_index();
201 if (index != MPI_UNDEFINED)
202 return process_data[index_to_process_data[index]]->replaying;
203 else return _xbt_replay_is_active();
207 int smpi_global_size(void)
209 char *value = getenv("SMPI_GLOBAL_SIZE");
213 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
219 smpi_process_data_t smpi_process_data(void)
221 simdata_process_t simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data(SIMIX_process_self()));
222 return static_cast<smpi_process_data_t>(simdata->data);
225 smpi_process_data_t smpi_process_remote_data(int index)
227 return process_data[index_to_process_data[index]];
230 void smpi_process_set_user_data(void *data)
232 smpi_process_data_t process_data = smpi_process_data();
233 process_data->data = data;
236 void *smpi_process_get_user_data()
238 smpi_process_data_t process_data = smpi_process_data();
239 return process_data->data;
242 int smpi_process_count(void)
244 return process_count;
247 int smpi_process_index(void)
249 smpi_process_data_t data = smpi_process_data();
250 //return -1 if not initialized
251 return data ? data->index : MPI_UNDEFINED;
254 MPI_Comm smpi_process_comm_world(void)
256 smpi_process_data_t data = smpi_process_data();
257 //return MPI_COMM_NULL if not initialized
258 return data ? *data->comm_world : MPI_COMM_NULL;
261 smx_rdv_t smpi_process_mailbox(void)
263 smpi_process_data_t data = smpi_process_data();
264 return data->mailbox;
267 smx_rdv_t smpi_process_mailbox_small(void)
269 smpi_process_data_t data = smpi_process_data();
270 return data->mailbox_small;
273 xbt_mutex_t smpi_process_mailboxes_mutex(void)
275 smpi_process_data_t data = smpi_process_data();
276 return data->mailboxes_mutex;
279 smx_rdv_t smpi_process_remote_mailbox(int index)
281 smpi_process_data_t data = smpi_process_remote_data(index);
282 return data->mailbox;
286 smx_rdv_t smpi_process_remote_mailbox_small(int index)
288 smpi_process_data_t data = smpi_process_remote_data(index);
289 return data->mailbox_small;
292 xbt_mutex_t smpi_process_remote_mailboxes_mutex(int index)
294 smpi_process_data_t data = smpi_process_remote_data(index);
295 return data->mailboxes_mutex;
298 xbt_os_timer_t smpi_process_timer(void)
300 smpi_process_data_t data = smpi_process_data();
304 void smpi_process_simulated_start(void)
306 smpi_process_data_t data = smpi_process_data();
307 data->simulated = SIMIX_get_clock();
310 double smpi_process_simulated_elapsed(void)
312 smpi_process_data_t data = smpi_process_data();
313 return SIMIX_get_clock() - data->simulated;
316 MPI_Comm smpi_process_comm_self(void)
318 smpi_process_data_t data = smpi_process_data();
319 if(data->comm_self==MPI_COMM_NULL){
320 MPI_Group group = smpi_group_new(1);
321 data->comm_self = smpi_comm_new(group, NULL);
322 smpi_group_set_mapping(group, smpi_process_index(), 0);
325 return data->comm_self;
328 MPI_Comm smpi_process_get_comm_intra(void)
330 smpi_process_data_t data = smpi_process_data();
331 return data->comm_intra;
334 void smpi_process_set_comm_intra(MPI_Comm comm)
336 smpi_process_data_t data = smpi_process_data();
337 data->comm_intra = comm;
340 void smpi_process_set_sampling(int s)
342 smpi_process_data_t data = smpi_process_data();
346 int smpi_process_get_sampling(void)
348 smpi_process_data_t data = smpi_process_data();
349 return data->sampling;
353 void print_request(const char *message, MPI_Request request)
356 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
357 message, request, request->buf, request->size, request->src,
358 request->dst, request->tag, request->flags);
361 void smpi_comm_copy_buffer_callback(smx_synchro_t comm,
362 void *buff, size_t buff_size)
364 XBT_DEBUG("Copy the data over");
367 if((smpi_privatize_global_variables)
368 && ((char*)buff >= smpi_start_data_exe)
369 && ((char*)buff < smpi_start_data_exe + smpi_size_data_exe )
371 XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
372 smpi_switch_data_segment(((smpi_process_data_t)(((simdata_process_t)SIMIX_process_get_data(comm->comm.src_proc))->data))->index);
373 tmpbuff = (void*)xbt_malloc(buff_size);
374 memcpy(tmpbuff, buff, buff_size);
378 if((smpi_privatize_global_variables)
379 && ((char*)comm->comm.dst_buff >= smpi_start_data_exe)
380 && ((char*)comm->comm.dst_buff < smpi_start_data_exe + smpi_size_data_exe )
382 XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
383 smpi_switch_data_segment(((smpi_process_data_t)(((simdata_process_t)SIMIX_process_get_data(comm->comm.dst_proc))->data))->index);
387 memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
388 if (comm->comm.detached) {
389 // if this is a detached send, the source buffer was duplicated by SMPI
390 // sender to make the original buffer available to the application ASAP
392 //It seems that the request is used after the call there this should
393 //be free somewhereelse but where???
394 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
395 //inside the user data and should be free
396 comm->comm.src_buff = NULL;
399 if(tmpbuff!=buff)xbt_free(tmpbuff);
404 void smpi_comm_null_copy_buffer_callback(smx_synchro_t comm,
405 void *buff, size_t buff_size)
410 static void smpi_check_options(){
411 //check correctness of MPI parameters
413 xbt_assert(sg_cfg_get_int("smpi/async_small_thresh") <=
414 sg_cfg_get_int("smpi/send_is_detached_thresh"));
416 if (sg_cfg_is_default_value("smpi/running_power")) {
417 XBT_INFO("You did not set the power of the host running the simulation. "
418 "The timings will certainly not be accurate. "
419 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
420 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information.");
424 int smpi_enabled(void) {
425 return process_data != NULL;
428 void smpi_global_init(void)
432 char name[MAILBOX_NAME_MAXLEN];
435 if (!MC_is_active()) {
436 global_timer = xbt_os_timer_new();
437 xbt_os_walltimer_start(global_timer);
439 if (process_count == 0){
440 process_count = SIMIX_process_count();
443 smpi_universe_size = process_count;
444 process_data = xbt_new0(smpi_process_data_t, process_count);
445 for (i = 0; i < process_count; i++) {
446 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
447 //process_data[i]->index = i;
448 process_data[i]->argc = NULL;
449 process_data[i]->argv = NULL;
450 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
451 process_data[i]->mailbox_small =
452 simcall_rdv_create(get_mailbox_name_small(name, i));
453 process_data[i]->mailboxes_mutex = xbt_mutex_init();
454 process_data[i]->timer = xbt_os_timer_new();
456 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
457 process_data[i]->comm_self = MPI_COMM_NULL;
458 process_data[i]->comm_intra = MPI_COMM_NULL;
459 process_data[i]->comm_world = NULL;
460 process_data[i]->state = SMPI_UNINITIALIZED;
461 process_data[i]->sampling = 0;
462 process_data[i]->finalization_barrier = NULL;
464 //if the process was launched through smpirun script
465 //we generate a global mpi_comm_world
466 //if not, we let MPI_COMM_NULL, and the comm world
467 //will be private to each mpi instance
469 group = smpi_group_new(process_count);
470 MPI_COMM_WORLD = smpi_comm_new(group, NULL);
471 MPI_Attr_put(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, (void *)(MPI_Aint)process_count);
472 xbt_bar_t bar=xbt_barrier_init(process_count);
474 for (i = 0; i < process_count; i++) {
475 smpi_group_set_mapping(group, i, i);
476 process_data[i]->finalization_barrier = bar;
481 void smpi_global_destroy(void)
483 int count = smpi_process_count();
486 smpi_bench_destroy();
487 if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
488 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
489 xbt_free(MPI_COMM_WORLD);
490 xbt_barrier_destroy(process_data[0]->finalization_barrier);
492 smpi_deployment_cleanup_instances();
494 MPI_COMM_WORLD = MPI_COMM_NULL;
495 for (i = 0; i < count; i++) {
496 if(process_data[i]->comm_self!=MPI_COMM_NULL){
497 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
498 smpi_comm_destroy(process_data[i]->comm_self);
500 if(process_data[i]->comm_intra!=MPI_COMM_NULL){
501 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_intra));
502 smpi_comm_destroy(process_data[i]->comm_intra);
504 xbt_os_timer_free(process_data[i]->timer);
505 simcall_rdv_destroy(process_data[i]->mailbox);
506 simcall_rdv_destroy(process_data[i]->mailbox_small);
507 xbt_mutex_destroy(process_data[i]->mailboxes_mutex);
508 xbt_free(process_data[i]);
510 xbt_free(process_data);
513 xbt_free(index_to_process_data);
514 if(smpi_privatize_global_variables)
515 smpi_destroy_global_memory_segments();
520 void __attribute__ ((weak)) user_main_()
522 xbt_die("Should not be in this smpi_simulated_main");
526 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
528 smpi_process_init(&argc, &argv);
533 int __attribute__ ((weak)) main(int argc, char **argv)
535 return smpi_main(smpi_simulated_main_, argc, argv);
541 static void smpi_init_logs(){
543 /* Connect log categories. See xbt/log.c */
545 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
546 function: xbt_log_appender_file.c depends on it
547 DO NOT connect this in XBT or so, or it will be
548 useless to xbt_log_appender_file.c */
550 XBT_LOG_CONNECT(instr_smpi);
551 XBT_LOG_CONNECT(smpi_base);
552 XBT_LOG_CONNECT(smpi_bench);
553 XBT_LOG_CONNECT(smpi_coll);
554 XBT_LOG_CONNECT(smpi_colls);
555 XBT_LOG_CONNECT(smpi_comm);
556 XBT_LOG_CONNECT(smpi_dvfs);
557 XBT_LOG_CONNECT(smpi_group);
558 XBT_LOG_CONNECT(smpi_kernel);
559 XBT_LOG_CONNECT(smpi_mpi);
560 XBT_LOG_CONNECT(smpi_mpi_dt);
561 XBT_LOG_CONNECT(smpi_pmpi);
562 XBT_LOG_CONNECT(smpi_replay);
563 XBT_LOG_CONNECT(smpi_rma);
567 static void smpi_init_options(){
568 int gather_id = find_coll_description(mpi_coll_gather_description,
569 sg_cfg_get_string("smpi/gather"),"gather");
570 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
571 void *, int, MPI_Datatype, int, MPI_Comm))
572 mpi_coll_gather_description[gather_id].coll;
574 int allgather_id = find_coll_description(mpi_coll_allgather_description,
575 sg_cfg_get_string("smpi/allgather"),"allgather");
576 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
577 void *, int, MPI_Datatype, MPI_Comm))
578 mpi_coll_allgather_description[allgather_id].coll;
580 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
581 sg_cfg_get_string("smpi/allgatherv"),"allgatherv");
582 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
583 int *, MPI_Datatype, MPI_Comm))
584 mpi_coll_allgatherv_description[allgatherv_id].coll;
586 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
587 sg_cfg_get_string("smpi/allreduce"),"allreduce");
588 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
589 MPI_Datatype dtype, MPI_Op op,
591 mpi_coll_allreduce_description[allreduce_id].coll;
593 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
594 sg_cfg_get_string("smpi/alltoall"),"alltoall");
595 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
596 void *, int, MPI_Datatype, MPI_Comm))
597 mpi_coll_alltoall_description[alltoall_id].coll;
599 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
600 sg_cfg_get_string("smpi/alltoallv"),"alltoallv");
601 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
602 void *, int *, int *, MPI_Datatype,
604 mpi_coll_alltoallv_description[alltoallv_id].coll;
606 int bcast_id = find_coll_description(mpi_coll_bcast_description,
607 sg_cfg_get_string("smpi/bcast"),"bcast");
608 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
609 int root, MPI_Comm com))
610 mpi_coll_bcast_description[bcast_id].coll;
612 int reduce_id = find_coll_description(mpi_coll_reduce_description,
613 sg_cfg_get_string("smpi/reduce"),"reduce");
614 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
615 MPI_Datatype datatype, MPI_Op op,
616 int root, MPI_Comm comm))
617 mpi_coll_reduce_description[reduce_id].coll;
619 int reduce_scatter_id =
620 find_coll_description(mpi_coll_reduce_scatter_description,
621 sg_cfg_get_string("smpi/reduce_scatter"),"reduce_scatter");
622 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
623 MPI_Datatype dtype, MPI_Op op,
625 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
627 int scatter_id = find_coll_description(mpi_coll_scatter_description,
628 sg_cfg_get_string("smpi/scatter"),"scatter");
629 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
630 MPI_Datatype sendtype, void *recvbuf,
631 int recvcount, MPI_Datatype recvtype,
632 int root, MPI_Comm comm))
633 mpi_coll_scatter_description[scatter_id].coll;
635 int barrier_id = find_coll_description(mpi_coll_barrier_description,
636 sg_cfg_get_string("smpi/barrier"),"barrier");
637 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
638 mpi_coll_barrier_description[barrier_id].coll;
640 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
641 smpi_running_power = sg_cfg_get_double("smpi/running_power");
642 smpi_privatize_global_variables = sg_cfg_get_boolean("smpi/privatize_global_variables");
643 if (smpi_cpu_threshold < 0)
644 smpi_cpu_threshold = DBL_MAX;
648 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
650 srand(SMPI_RAND_SEED);
652 if (getenv("SMPI_PRETEND_CC") != NULL) {
653 /* Hack to ensure that smpicc can pretend to be a simple
654 * compiler. Particularly handy to pass it to the configuration tools */
660 TRACE_global_init(&argc, argv);
662 TRACE_add_start_function(TRACE_smpi_alloc);
663 TRACE_add_end_function(TRACE_smpi_release);
665 SIMIX_global_init(&argc, argv);
666 MSG_init(&argc,argv);
668 SMPI_switch_data_segment = smpi_switch_data_segment;
672 // parse the platform file: get the host list
673 SIMIX_create_environment(argv[1]);
674 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
675 SIMIX_function_register_default(realmain);
676 SIMIX_launch_application(argv[2]);
680 smpi_check_options();
682 if(smpi_privatize_global_variables)
683 smpi_initialize_global_memory_segments();
685 /* Clean IO before the run */
689 if (MC_is_active()) {
695 xbt_os_walltimer_stop(global_timer);
696 if (sg_cfg_get_boolean("smpi/display_timing")){
697 double global_time = xbt_os_timer_elapsed(global_timer);
698 XBT_INFO("Simulated time: %g seconds. \n\n"
699 "The simulation took %g seconds (after parsing and platform setup)\n"
700 "%g seconds were actual computation of the application"
701 , SIMIX_get_clock(), global_time , smpi_total_benched_time);
703 if (smpi_total_benched_time/global_time>=0.75)
704 XBT_INFO("More than 75%% of the time was spent inside the application code.\n"
705 "You may want to use sampling functions or trace replay to reduce this.");
709 smpi_global_destroy();
716 // This function can be called from extern file, to initialize logs, options, and processes of smpi
717 // without the need of smpirun
722 smpi_check_options();
723 if (TRACE_is_enabled() && TRACE_is_configured())
725 if(smpi_privatize_global_variables)
726 smpi_initialize_global_memory_segments();
729 void SMPI_finalize(){
730 smpi_global_destroy();