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 "mc/mc_record.h"
11 #include "xbt/replay.h"
12 #include "surf/surf.h"
13 #include "simix/smx_private.h"
14 #include "simgrid/sg_config.h"
15 #include "mc/mc_replay.h"
17 #include <float.h> /* DBL_MAX */
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
23 "Logging specific to SMPI (kernel)");
25 typedef struct s_smpi_process_data {
30 smx_rdv_t mailbox_small;
31 xbt_mutex_t mailboxes_mutex;
36 void *data; /* user data */
39 int sampling; /* inside an SMPI_SAMPLE_ block? */
41 int replaying; /* is the process replaying a trace */
42 xbt_bar_t finalization_barrier;
43 } s_smpi_process_data_t;
45 static smpi_process_data_t *process_data = NULL;
46 int process_count = 0;
47 int smpi_universe_size = 0;
48 int* index_to_process_data = NULL;
49 extern double smpi_total_benched_time;
50 xbt_os_timer_t global_timer;
51 MPI_Comm MPI_COMM_WORLD = MPI_COMM_UNINITIALIZED;
53 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
54 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
55 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
57 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
59 static char *get_mailbox_name(char *str, int index)
61 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
66 static char *get_mailbox_name_small(char *str, int index)
68 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
73 void smpi_process_init(int *argc, char ***argv)
76 smpi_process_data_t data;
80 proc = SIMIX_process_self();
81 //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
82 proc->context->cleanup_func=SIMIX_process_cleanup;
83 char* instance_id = (*argv)[1];
84 int rank = atoi((*argv)[2]);
85 /* Now using segment index of the process */
86 index = proc->segment_index;
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 /* Done at the process's creation */
94 SMPI_switch_data_segment(index);
97 MPI_Comm* temp_comm_world;
99 smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world ,&temp_bar);
100 data = smpi_process_remote_data(index);
101 data->comm_world = temp_comm_world;
102 if(temp_bar != NULL) data->finalization_barrier = temp_bar;
104 data->instance_id = instance_id;
106 xbt_free(simcall_process_get_data(proc));
107 simcall_process_set_data(proc, data);
110 memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
111 (*argv)[(*argc) - 1] = NULL;
112 (*argv)[(*argc) - 2] = NULL;
117 // set the process attached to the mailbox
118 simcall_rdv_set_receiver(data->mailbox_small, proc);
119 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
122 if (smpi_process_data() == NULL)
123 xbt_die("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.");
126 void smpi_process_destroy(void)
128 int index = smpi_process_index();
129 if(smpi_privatize_global_variables){
130 smpi_switch_data_segment(index);
132 process_data[index_to_process_data[index]]->state = SMPI_FINALIZED;
133 XBT_DEBUG("<%d> Process left the game", index);
137 * @brief Prepares the current process for termination.
139 void smpi_process_finalize(void)
141 // This leads to an explosion of the search graph
142 // which cannot be reduced:
143 if(MC_is_active() || MC_record_replay_is_active())
146 int index = smpi_process_index();
147 // wait for all pending asynchronous comms to finish
148 xbt_barrier_wait(process_data[index_to_process_data[index]]->finalization_barrier);
152 * @brief Check if a process is finalized
154 int smpi_process_finalized()
156 int index = smpi_process_index();
157 if (index != MPI_UNDEFINED)
158 return (process_data[index_to_process_data[index]]->state == SMPI_FINALIZED);
164 * @brief Check if a process is initialized
166 int smpi_process_initialized(void)
168 int index = smpi_process_index();
169 return ( (index != MPI_UNDEFINED)
170 && (process_data[index_to_process_data[index]]->state == SMPI_INITIALIZED));
174 * @brief Mark a process as initialized (=MPI_Init called)
176 void smpi_process_mark_as_initialized(void)
178 int index = smpi_process_index();
179 if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
180 process_data[index_to_process_data[index]]->state = SMPI_INITIALIZED;
183 void smpi_process_set_replaying(int value){
184 int index = smpi_process_index();
185 if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
186 process_data[index_to_process_data[index]]->replaying = value;
189 int smpi_process_get_replaying(){
190 int index = smpi_process_index();
191 if (index != MPI_UNDEFINED)
192 return process_data[index_to_process_data[index]]->replaying;
193 else return _xbt_replay_is_active();
197 int smpi_global_size(void)
199 char *value = getenv("SMPI_GLOBAL_SIZE");
203 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
209 smpi_process_data_t smpi_process_data(void)
211 return SIMIX_process_self_get_data(SIMIX_process_self());
214 smpi_process_data_t smpi_process_remote_data(int index)
216 return process_data[index_to_process_data[index]];
219 void smpi_process_set_user_data(void *data)
221 smpi_process_data_t process_data = smpi_process_data();
222 process_data->data = data;
225 void *smpi_process_get_user_data()
227 smpi_process_data_t process_data = smpi_process_data();
228 return process_data->data;
231 int smpi_process_count(void)
233 return process_count;
236 int smpi_process_index(void)
238 smpi_process_data_t data = smpi_process_data();
239 //return -1 if not initialized
240 return data ? data->index : MPI_UNDEFINED;
243 MPI_Comm smpi_process_comm_world(void)
245 smpi_process_data_t data = smpi_process_data();
246 //return MPI_COMM_NULL if not initialized
247 return data ? *data->comm_world : MPI_COMM_NULL;
250 smx_rdv_t smpi_process_mailbox(void)
252 smpi_process_data_t data = smpi_process_data();
253 return data->mailbox;
256 smx_rdv_t smpi_process_mailbox_small(void)
258 smpi_process_data_t data = smpi_process_data();
259 return data->mailbox_small;
262 xbt_mutex_t smpi_process_mailboxes_mutex(void)
264 smpi_process_data_t data = smpi_process_data();
265 return data->mailboxes_mutex;
268 smx_rdv_t smpi_process_remote_mailbox(int index)
270 smpi_process_data_t data = smpi_process_remote_data(index);
271 return data->mailbox;
275 smx_rdv_t smpi_process_remote_mailbox_small(int index)
277 smpi_process_data_t data = smpi_process_remote_data(index);
278 return data->mailbox_small;
281 xbt_mutex_t smpi_process_remote_mailboxes_mutex(int index)
283 smpi_process_data_t data = smpi_process_remote_data(index);
284 return data->mailboxes_mutex;
287 xbt_os_timer_t smpi_process_timer(void)
289 smpi_process_data_t data = smpi_process_data();
293 void smpi_process_simulated_start(void)
295 smpi_process_data_t data = smpi_process_data();
296 data->simulated = SIMIX_get_clock();
299 double smpi_process_simulated_elapsed(void)
301 smpi_process_data_t data = smpi_process_data();
302 return SIMIX_get_clock() - data->simulated;
305 MPI_Comm smpi_process_comm_self(void)
307 smpi_process_data_t data = smpi_process_data();
308 if(data->comm_self==MPI_COMM_NULL){
309 MPI_Group group = smpi_group_new(1);
310 data->comm_self = smpi_comm_new(group, NULL);
311 smpi_group_set_mapping(group, smpi_process_index(), 0);
314 return data->comm_self;
317 MPI_Comm smpi_process_get_comm_intra(void)
319 smpi_process_data_t data = smpi_process_data();
320 return data->comm_intra;
323 void smpi_process_set_comm_intra(MPI_Comm comm)
325 smpi_process_data_t data = smpi_process_data();
326 data->comm_intra = comm;
329 void smpi_process_set_sampling(int s)
331 smpi_process_data_t data = smpi_process_data();
335 int smpi_process_get_sampling(void)
337 smpi_process_data_t data = smpi_process_data();
338 return data->sampling;
342 void print_request(const char *message, MPI_Request request)
345 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
346 message, request, request->buf, request->size, request->src,
347 request->dst, request->tag, request->flags);
350 void smpi_comm_copy_buffer_callback(smx_synchro_t comm,
351 void *buff, size_t buff_size)
353 XBT_DEBUG("Copy the data over");
356 if((smpi_privatize_global_variables)
357 && ((char*)buff >= smpi_start_data_exe)
358 && ((char*)buff < smpi_start_data_exe + smpi_size_data_exe )
360 XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
361 smpi_switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.src_proc))->index);
362 tmpbuff = (void*)xbt_malloc(buff_size);
363 memcpy(tmpbuff, buff, buff_size);
367 if((smpi_privatize_global_variables)
368 && ((char*)comm->comm.dst_buff >= smpi_start_data_exe)
369 && ((char*)comm->comm.dst_buff < smpi_start_data_exe + smpi_size_data_exe )
371 XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
372 smpi_switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.dst_proc))->index);
376 memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
377 if (comm->comm.detached) {
378 // if this is a detached send, the source buffer was duplicated by SMPI
379 // sender to make the original buffer available to the application ASAP
381 //It seems that the request is used after the call there this should
382 //be free somewhereelse but where???
383 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
384 //inside the user data and should be free
385 comm->comm.src_buff = NULL;
388 if(tmpbuff!=buff)xbt_free(tmpbuff);
393 void smpi_comm_null_copy_buffer_callback(smx_synchro_t comm,
394 void *buff, size_t buff_size)
399 static void smpi_check_options(){
400 //check correctness of MPI parameters
402 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
403 sg_cfg_get_int("smpi/send_is_detached_thres"));
405 if (sg_cfg_is_default_value("smpi/running_power")) {
406 XBT_INFO("You did not set the power of the host running the simulation. "
407 "The timings will certainly not be accurate. "
408 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
409 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information.");
413 int smpi_enabled(void) {
414 return process_data != NULL;
417 void smpi_global_init(void)
421 char name[MAILBOX_NAME_MAXLEN];
424 if (!MC_is_active()) {
425 global_timer = xbt_os_timer_new();
426 xbt_os_walltimer_start(global_timer);
428 if (process_count == 0){
429 process_count = SIMIX_process_count();
432 smpi_universe_size = process_count;
433 process_data = xbt_new0(smpi_process_data_t, process_count);
434 for (i = 0; i < process_count; i++) {
435 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
436 //process_data[i]->index = i;
437 process_data[i]->argc = NULL;
438 process_data[i]->argv = NULL;
439 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
440 process_data[i]->mailbox_small =
441 simcall_rdv_create(get_mailbox_name_small(name, i));
442 process_data[i]->mailboxes_mutex=xbt_mutex_init();
443 process_data[i]->timer = xbt_os_timer_new();
445 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
446 process_data[i]->comm_self = MPI_COMM_NULL;
447 process_data[i]->comm_intra = MPI_COMM_NULL;
448 process_data[i]->comm_world = NULL;
449 process_data[i]->state = SMPI_UNINITIALIZED;
450 process_data[i]->sampling = 0;
451 process_data[i]->finalization_barrier = NULL;
453 //if the process was launched through smpirun script
454 //we generate a global mpi_comm_world
455 //if not, we let MPI_COMM_NULL, and the comm world
456 //will be private to each mpi instance
458 group = smpi_group_new(process_count);
459 MPI_COMM_WORLD = smpi_comm_new(group, NULL);
460 MPI_Attr_put(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, (void *)(MPI_Aint)process_count);
461 xbt_bar_t bar=xbt_barrier_init(process_count);
463 for (i = 0; i < process_count; i++) {
464 smpi_group_set_mapping(group, i, i);
465 process_data[i]->finalization_barrier = bar;
470 void smpi_global_destroy(void)
472 int count = smpi_process_count();
475 smpi_bench_destroy();
476 if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
477 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
478 xbt_free(MPI_COMM_WORLD);
479 xbt_barrier_destroy(process_data[0]->finalization_barrier);
481 smpi_deployment_cleanup_instances();
483 MPI_COMM_WORLD = MPI_COMM_NULL;
484 for (i = 0; i < count; i++) {
485 if(process_data[i]->comm_self!=MPI_COMM_NULL){
486 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
487 smpi_comm_destroy(process_data[i]->comm_self);
489 if(process_data[i]->comm_intra!=MPI_COMM_NULL){
490 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_intra));
491 smpi_comm_destroy(process_data[i]->comm_intra);
493 xbt_os_timer_free(process_data[i]->timer);
494 simcall_rdv_destroy(process_data[i]->mailbox);
495 simcall_rdv_destroy(process_data[i]->mailbox_small);
496 xbt_mutex_destroy(process_data[i]->mailboxes_mutex);
497 xbt_free(process_data[i]);
499 xbt_free(process_data);
502 xbt_free(index_to_process_data);
503 if(smpi_privatize_global_variables)
504 smpi_destroy_global_memory_segments();
509 void __attribute__ ((weak)) user_main_()
511 xbt_die("Should not be in this smpi_simulated_main");
515 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
517 smpi_process_init(&argc, &argv);
522 int __attribute__ ((weak)) main(int argc, char **argv)
524 return smpi_main(smpi_simulated_main_, argc, argv);
529 static void smpi_init_logs(){
531 /* Connect log categories. See xbt/log.c */
532 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
533 function: xbt_log_appender_file.c depends on it
534 DO NOT connect this in XBT or so, or it will be
535 useless to xbt_log_appender_file.c */
536 XBT_LOG_CONNECT(instr_smpi);
537 XBT_LOG_CONNECT(smpi_base);
538 XBT_LOG_CONNECT(smpi_bench);
539 XBT_LOG_CONNECT(smpi_coll);
540 XBT_LOG_CONNECT(smpi_colls);
541 XBT_LOG_CONNECT(smpi_comm);
542 XBT_LOG_CONNECT(smpi_dvfs);
543 XBT_LOG_CONNECT(smpi_group);
544 XBT_LOG_CONNECT(smpi_kernel);
545 XBT_LOG_CONNECT(smpi_mpi);
546 XBT_LOG_CONNECT(smpi_mpi_dt);
547 XBT_LOG_CONNECT(smpi_pmpi);
548 XBT_LOG_CONNECT(smpi_replay);
549 XBT_LOG_CONNECT(smpi_rma);
554 static void smpi_init_options(){
555 int gather_id = find_coll_description(mpi_coll_gather_description,
556 sg_cfg_get_string("smpi/gather"),"gather");
557 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
558 void *, int, MPI_Datatype, int, MPI_Comm))
559 mpi_coll_gather_description[gather_id].coll;
561 int allgather_id = find_coll_description(mpi_coll_allgather_description,
562 sg_cfg_get_string("smpi/allgather"),"allgather");
563 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
564 void *, int, MPI_Datatype, MPI_Comm))
565 mpi_coll_allgather_description[allgather_id].coll;
567 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
568 sg_cfg_get_string("smpi/allgatherv"),"allgatherv");
569 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
570 int *, MPI_Datatype, MPI_Comm))
571 mpi_coll_allgatherv_description[allgatherv_id].coll;
573 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
574 sg_cfg_get_string("smpi/allreduce"),"allreduce");
575 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
576 MPI_Datatype dtype, MPI_Op op,
578 mpi_coll_allreduce_description[allreduce_id].coll;
580 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
581 sg_cfg_get_string("smpi/alltoall"),"alltoall");
582 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
583 void *, int, MPI_Datatype, MPI_Comm))
584 mpi_coll_alltoall_description[alltoall_id].coll;
586 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
587 sg_cfg_get_string("smpi/alltoallv"),"alltoallv");
588 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
589 void *, int *, int *, MPI_Datatype,
591 mpi_coll_alltoallv_description[alltoallv_id].coll;
593 int bcast_id = find_coll_description(mpi_coll_bcast_description,
594 sg_cfg_get_string("smpi/bcast"),"bcast");
595 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
596 int root, MPI_Comm com))
597 mpi_coll_bcast_description[bcast_id].coll;
599 int reduce_id = find_coll_description(mpi_coll_reduce_description,
600 sg_cfg_get_string("smpi/reduce"),"reduce");
601 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
602 MPI_Datatype datatype, MPI_Op op,
603 int root, MPI_Comm comm))
604 mpi_coll_reduce_description[reduce_id].coll;
606 int reduce_scatter_id =
607 find_coll_description(mpi_coll_reduce_scatter_description,
608 sg_cfg_get_string("smpi/reduce_scatter"),"reduce_scatter");
609 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
610 MPI_Datatype dtype, MPI_Op op,
612 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
614 int scatter_id = find_coll_description(mpi_coll_scatter_description,
615 sg_cfg_get_string("smpi/scatter"),"scatter");
616 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
617 MPI_Datatype sendtype, void *recvbuf,
618 int recvcount, MPI_Datatype recvtype,
619 int root, MPI_Comm comm))
620 mpi_coll_scatter_description[scatter_id].coll;
622 int barrier_id = find_coll_description(mpi_coll_barrier_description,
623 sg_cfg_get_string("smpi/barrier"),"barrier");
624 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
625 mpi_coll_barrier_description[barrier_id].coll;
627 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
628 smpi_running_power = sg_cfg_get_double("smpi/running_power");
629 smpi_privatize_global_variables = sg_cfg_get_boolean("smpi/privatize_global_variables");
630 if (smpi_cpu_threshold < 0)
631 smpi_cpu_threshold = DBL_MAX;
635 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
637 srand(SMPI_RAND_SEED);
639 if (getenv("SMPI_PRETEND_CC") != NULL) {
640 /* Hack to ensure that smpicc can pretend to be a simple
641 * compiler. Particularly handy to pass it to the configuration tools */
647 TRACE_global_init(&argc, argv);
649 TRACE_add_start_function(TRACE_smpi_alloc);
650 TRACE_add_end_function(TRACE_smpi_release);
652 SIMIX_global_init(&argc, argv);
653 MSG_init(&argc,argv);
655 SMPI_switch_data_segment = smpi_switch_data_segment;
659 // parse the platform file: get the host list
660 SIMIX_create_environment(argv[1]);
661 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
662 SIMIX_function_register_default(realmain);
663 SIMIX_launch_application(argv[2]);
667 smpi_check_options();
669 if(smpi_privatize_global_variables)
670 smpi_initialize_global_memory_segments();
672 /* Clean IO before the run */
676 if (MC_is_active()) {
682 xbt_os_walltimer_stop(global_timer);
683 if (sg_cfg_get_boolean("smpi/display_timing")){
684 double global_time = xbt_os_timer_elapsed(global_timer);
685 XBT_INFO("Simulated time: %g seconds. \n\n"
686 "The simulation took %g seconds (after parsing and platform setup)\n"
687 "%g seconds were actual computation of the application"
688 , SIMIX_get_clock(), global_time , smpi_total_benched_time);
690 if (smpi_total_benched_time/global_time>=0.75)
691 XBT_INFO("More than 75%% of the time was spent inside the application code.\n"
692 "You may want to use sampling functions or trace replay to reduce this.");
696 smpi_global_destroy();
703 // This function can be called from extern file, to initialize logs, options, and processes of smpi
704 // without the need of smpirun
709 smpi_check_options();
710 if (TRACE_is_enabled() && TRACE_is_configured())
712 if(smpi_privatize_global_variables)
713 smpi_initialize_global_memory_segments();
716 void SMPI_finalize(){
717 smpi_global_destroy();