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, "Logging specific to SMPI (kernel)");
25 typedef struct s_smpi_process_data {
29 smx_mailbox_t mailbox;
30 smx_mailbox_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), index);
65 static char *get_mailbox_name_small(char *str, int index)
67 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2), index);
71 void smpi_process_init(int *argc, char ***argv)
74 smpi_process_data_t data;
78 proc = SIMIX_process_self();
79 //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
80 SIMIX_process_set_cleanup_function(proc, SIMIX_process_cleanup);
81 char* instance_id = (*argv)[1];
82 int rank = xbt_str_parse_int((*argv)[2], "Invalid rank: %s");
83 index = smpi_process_index_of_smx_process(proc);
85 if(!index_to_process_data){
86 index_to_process_data=(int*)xbt_malloc(SIMIX_process_count()*sizeof(int));
89 if(smpi_privatize_global_variables){
90 /* Now using segment index of the process */
91 index = proc->segment_index;
92 /* Done at the process's creation */
93 SMPI_switch_data_segment(index);
96 MPI_Comm* temp_comm_world;
98 smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world, &temp_bar);
99 data = smpi_process_remote_data(index);
100 data->comm_world = temp_comm_world;
101 if(temp_bar != NULL) data->finalization_barrier = temp_bar;
103 data->instance_id = instance_id;
105 //xbt_free(simcall_process_get_data(proc));
107 simdata_process_t simdata = static_cast<simdata_process_t>(simcall_process_get_data(proc));
108 simdata->data = data;
112 memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
113 (*argv)[(*argc) - 1] = NULL;
114 (*argv)[(*argc) - 2] = NULL;
119 // set the process attached to the mailbox
120 simcall_mbox_set_receiver(data->mailbox_small, proc);
121 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
123 xbt_assert(smpi_process_data(),
124 "smpi_process_data() returned NULL. You probably gave a NULL parameter to MPI_Init. Although it's required by "
125 "MPI-2, this is currently not supported by SMPI.");
128 void smpi_process_destroy(void)
130 int index = smpi_process_index();
131 if(smpi_privatize_global_variables){
132 smpi_switch_data_segment(index);
134 process_data[index_to_process_data[index]]->state = SMPI_FINALIZED;
135 XBT_DEBUG("<%d> Process left the game", index);
138 /** @brief Prepares the current process for termination. */
139 void smpi_process_finalize(void)
141 // This leads to an explosion of the search graph which cannot be reduced:
142 if(MC_is_active() || MC_record_replay_is_active())
145 int index = smpi_process_index();
146 // wait for all pending asynchronous comms to finish
147 xbt_barrier_wait(process_data[index_to_process_data[index]]->finalization_barrier);
150 /** @brief Check if a process is finalized */
151 int smpi_process_finalized()
153 int index = smpi_process_index();
154 if (index != MPI_UNDEFINED)
155 return (process_data[index_to_process_data[index]]->state == SMPI_FINALIZED);
160 /** @brief Check if a process is initialized */
161 int smpi_process_initialized(void)
163 if (!index_to_process_data){
166 int index = smpi_process_index();
167 return ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state == SMPI_INITIALIZED));
171 /** @brief Mark a process as initialized (=MPI_Init called) */
172 void smpi_process_mark_as_initialized(void)
174 int index = smpi_process_index();
175 if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
176 process_data[index_to_process_data[index]]->state = SMPI_INITIALIZED;
179 void smpi_process_set_replaying(int value){
180 int index = smpi_process_index();
181 if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
182 process_data[index_to_process_data[index]]->replaying = value;
185 int smpi_process_get_replaying(){
186 int index = smpi_process_index();
187 if (index != MPI_UNDEFINED)
188 return process_data[index_to_process_data[index]]->replaying;
189 else return _xbt_replay_is_active();
192 int smpi_global_size(void)
194 char *value = getenv("SMPI_GLOBAL_SIZE");
195 xbt_assert(value,"Please set env var SMPI_GLOBAL_SIZE to the expected number of processes.");
197 return xbt_str_parse_int(value, "SMPI_GLOBAL_SIZE contains a non-numerical value: %s");
200 smpi_process_data_t smpi_process_data(void)
202 simdata_process_t simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
203 return static_cast<smpi_process_data_t>(simdata->data);
206 smpi_process_data_t smpi_process_remote_data(int index)
208 return process_data[index_to_process_data[index]];
211 void smpi_process_set_user_data(void *data)
213 smpi_process_data_t process_data = smpi_process_data();
214 process_data->data = data;
217 void *smpi_process_get_user_data()
219 smpi_process_data_t process_data = smpi_process_data();
220 return process_data->data;
223 int smpi_process_count(void)
225 return process_count;
228 int smpi_process_index(void)
230 smpi_process_data_t data = smpi_process_data();
231 //return -1 if not initialized
232 return data ? data->index : MPI_UNDEFINED;
235 MPI_Comm smpi_process_comm_world(void)
237 smpi_process_data_t data = smpi_process_data();
238 //return MPI_COMM_NULL if not initialized
239 return data ? *data->comm_world : MPI_COMM_NULL;
242 smx_mailbox_t smpi_process_mailbox(void)
244 smpi_process_data_t data = smpi_process_data();
245 return data->mailbox;
248 smx_mailbox_t smpi_process_mailbox_small(void)
250 smpi_process_data_t data = smpi_process_data();
251 return data->mailbox_small;
254 xbt_mutex_t smpi_process_mailboxes_mutex(void)
256 smpi_process_data_t data = smpi_process_data();
257 return data->mailboxes_mutex;
260 smx_mailbox_t smpi_process_remote_mailbox(int index)
262 smpi_process_data_t data = smpi_process_remote_data(index);
263 return data->mailbox;
266 smx_mailbox_t smpi_process_remote_mailbox_small(int index)
268 smpi_process_data_t data = smpi_process_remote_data(index);
269 return data->mailbox_small;
272 xbt_mutex_t smpi_process_remote_mailboxes_mutex(int index)
274 smpi_process_data_t data = smpi_process_remote_data(index);
275 return data->mailboxes_mutex;
278 xbt_os_timer_t smpi_process_timer(void)
280 smpi_process_data_t data = smpi_process_data();
284 void smpi_process_simulated_start(void)
286 smpi_process_data_t data = smpi_process_data();
287 data->simulated = SIMIX_get_clock();
290 double smpi_process_simulated_elapsed(void)
292 smpi_process_data_t data = smpi_process_data();
293 return SIMIX_get_clock() - data->simulated;
296 MPI_Comm smpi_process_comm_self(void)
298 smpi_process_data_t data = smpi_process_data();
299 if(data->comm_self==MPI_COMM_NULL){
300 MPI_Group group = smpi_group_new(1);
301 data->comm_self = smpi_comm_new(group, NULL);
302 smpi_group_set_mapping(group, smpi_process_index(), 0);
305 return data->comm_self;
308 MPI_Comm smpi_process_get_comm_intra(void)
310 smpi_process_data_t data = smpi_process_data();
311 return data->comm_intra;
314 void smpi_process_set_comm_intra(MPI_Comm comm)
316 smpi_process_data_t data = smpi_process_data();
317 data->comm_intra = comm;
320 void smpi_process_set_sampling(int s)
322 smpi_process_data_t data = smpi_process_data();
326 int smpi_process_get_sampling(void)
328 smpi_process_data_t data = smpi_process_data();
329 return data->sampling;
332 void print_request(const char *message, MPI_Request request)
334 XBT_VERB("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
335 message, request, request->buf, request->size, request->src, request->dst, request->tag, request->flags);
338 void smpi_comm_copy_buffer_callback(smx_synchro_t comm, void *buff, size_t buff_size)
340 XBT_DEBUG("Copy the data over");
343 if((smpi_privatize_global_variables) && ((char*)buff >= smpi_start_data_exe)
344 && ((char*)buff < smpi_start_data_exe + smpi_size_data_exe )
346 XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
347 smpi_switch_data_segment(((smpi_process_data_t)(((simdata_process_t)SIMIX_process_get_data(comm->comm.src_proc))->data))->index);
348 tmpbuff = (void*)xbt_malloc(buff_size);
349 memcpy(tmpbuff, buff, buff_size);
352 if((smpi_privatize_global_variables) && ((char*)comm->comm.dst_buff >= smpi_start_data_exe)
353 && ((char*)comm->comm.dst_buff < smpi_start_data_exe + smpi_size_data_exe )){
354 XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
355 smpi_switch_data_segment(((smpi_process_data_t)(((simdata_process_t)SIMIX_process_get_data(comm->comm.dst_proc))->data))->index);
358 memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
359 if (comm->comm.detached) {
360 // if this is a detached send, the source buffer was duplicated by SMPI
361 // sender to make the original buffer available to the application ASAP
363 //It seems that the request is used after the call there this should be free somewhere else but where???
364 //xbt_free(comm->comm.src_data);// inside SMPI the request is kept inside the user data and should be free
365 comm->comm.src_buff = NULL;
368 if(tmpbuff!=buff)xbt_free(tmpbuff);
371 void smpi_comm_null_copy_buffer_callback(smx_synchro_t comm, void *buff, size_t buff_size)
376 static void smpi_check_options(){
377 //check correctness of MPI parameters
379 xbt_assert(xbt_cfg_get_int("smpi/async_small_thresh") <= xbt_cfg_get_int("smpi/send_is_detached_thresh"));
381 if (xbt_cfg_is_default_value("smpi/running_power")) {
382 XBT_INFO("You did not set the power of the host running the simulation. "
383 "The timings will certainly not be accurate. "
384 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
385 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information.");
389 int smpi_enabled(void) {
390 return process_data != NULL;
393 void smpi_global_init(void)
397 char name[MAILBOX_NAME_MAXLEN];
400 if (!MC_is_active()) {
401 global_timer = xbt_os_timer_new();
402 xbt_os_walltimer_start(global_timer);
404 if (process_count == 0){
405 process_count = SIMIX_process_count();
408 smpi_universe_size = process_count;
409 process_data = xbt_new0(smpi_process_data_t, process_count);
410 for (i = 0; i < process_count; i++) {
411 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
412 //process_data[i]->index = i;
413 process_data[i]->argc = NULL;
414 process_data[i]->argv = NULL;
415 process_data[i]->mailbox = simcall_mbox_create(get_mailbox_name(name, i));
416 process_data[i]->mailbox_small = simcall_mbox_create(get_mailbox_name_small(name, i));
417 process_data[i]->mailboxes_mutex = xbt_mutex_init();
418 process_data[i]->timer = xbt_os_timer_new();
420 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
421 process_data[i]->comm_self = MPI_COMM_NULL;
422 process_data[i]->comm_intra = MPI_COMM_NULL;
423 process_data[i]->comm_world = NULL;
424 process_data[i]->state = SMPI_UNINITIALIZED;
425 process_data[i]->sampling = 0;
426 process_data[i]->finalization_barrier = NULL;
428 //if the process was launched through smpirun script we generate a global mpi_comm_world
429 //if not, we let MPI_COMM_NULL, and the comm world will be private to each mpi instance
431 group = smpi_group_new(process_count);
432 MPI_COMM_WORLD = smpi_comm_new(group, NULL);
433 MPI_Attr_put(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, (void *)(MPI_Aint)process_count);
434 xbt_bar_t bar=xbt_barrier_init(process_count);
436 for (i = 0; i < process_count; i++) {
437 smpi_group_set_mapping(group, i, i);
438 process_data[i]->finalization_barrier = bar;
443 void smpi_global_destroy(void)
445 int count = smpi_process_count();
448 smpi_bench_destroy();
449 if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
450 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
451 xbt_barrier_destroy(process_data[0]->finalization_barrier);
453 smpi_deployment_cleanup_instances();
455 for (i = 0; i < count; i++) {
456 if(process_data[i]->comm_self!=MPI_COMM_NULL){
457 smpi_comm_destroy(process_data[i]->comm_self);
459 if(process_data[i]->comm_intra!=MPI_COMM_NULL){
460 smpi_comm_destroy(process_data[i]->comm_intra);
462 xbt_os_timer_free(process_data[i]->timer);
463 simcall_mbox_destroy(process_data[i]->mailbox);
464 simcall_mbox_destroy(process_data[i]->mailbox_small);
465 xbt_mutex_destroy(process_data[i]->mailboxes_mutex);
466 xbt_free(process_data[i]);
468 xbt_free(process_data);
471 if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED)
472 xbt_free(MPI_COMM_WORLD);
473 MPI_COMM_WORLD = MPI_COMM_NULL;
475 xbt_free(index_to_process_data);
476 if(smpi_privatize_global_variables)
477 smpi_destroy_global_memory_segments();
482 void __attribute__ ((weak)) user_main_()
484 xbt_die("Should not be in this smpi_simulated_main");
488 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
490 smpi_process_init(&argc, &argv);
495 int __attribute__ ((weak)) main(int argc, char **argv)
497 return smpi_main(smpi_simulated_main_, argc, argv);
503 static void smpi_init_logs(){
505 /* Connect log categories. See xbt/log.c */
507 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this function: xbt_log_appender_file.c depends on it
508 DO NOT connect this in XBT or so, or it will be useless to xbt_log_appender_file.c */
509 XBT_LOG_CONNECT(instr_smpi);
510 XBT_LOG_CONNECT(smpi_base);
511 XBT_LOG_CONNECT(smpi_bench);
512 XBT_LOG_CONNECT(smpi_coll);
513 XBT_LOG_CONNECT(smpi_colls);
514 XBT_LOG_CONNECT(smpi_comm);
515 XBT_LOG_CONNECT(smpi_dvfs);
516 XBT_LOG_CONNECT(smpi_group);
517 XBT_LOG_CONNECT(smpi_kernel);
518 XBT_LOG_CONNECT(smpi_mpi);
519 XBT_LOG_CONNECT(smpi_mpi_dt);
520 XBT_LOG_CONNECT(smpi_pmpi);
521 XBT_LOG_CONNECT(smpi_replay);
522 XBT_LOG_CONNECT(smpi_rma);
526 static void smpi_init_options(){
527 int gather_id = find_coll_description(mpi_coll_gather_description, xbt_cfg_get_string("smpi/gather"),"gather");
528 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype, void *, int, MPI_Datatype, int, MPI_Comm))
529 mpi_coll_gather_description[gather_id].coll;
531 int allgather_id = find_coll_description(mpi_coll_allgather_description,
532 xbt_cfg_get_string("smpi/allgather"),"allgather");
533 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype, void *, int, MPI_Datatype, MPI_Comm))
534 mpi_coll_allgather_description[allgather_id].coll;
536 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
537 xbt_cfg_get_string("smpi/allgatherv"),"allgatherv");
538 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *, int *, MPI_Datatype, MPI_Comm))
539 mpi_coll_allgatherv_description[allgatherv_id].coll;
541 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
542 xbt_cfg_get_string("smpi/allreduce"),"allreduce");
543 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm))
544 mpi_coll_allreduce_description[allreduce_id].coll;
546 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
547 xbt_cfg_get_string("smpi/alltoall"),"alltoall");
548 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype, void *, int, MPI_Datatype, MPI_Comm))
549 mpi_coll_alltoall_description[alltoall_id].coll;
551 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
552 xbt_cfg_get_string("smpi/alltoallv"),"alltoallv");
553 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype, void *, int *, int *, MPI_Datatype, MPI_Comm))
554 mpi_coll_alltoallv_description[alltoallv_id].coll;
556 int bcast_id = find_coll_description(mpi_coll_bcast_description, xbt_cfg_get_string("smpi/bcast"),"bcast");
557 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com))
558 mpi_coll_bcast_description[bcast_id].coll;
560 int reduce_id = find_coll_description(mpi_coll_reduce_description, xbt_cfg_get_string("smpi/reduce"),"reduce");
561 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
562 MPI_Comm comm)) mpi_coll_reduce_description[reduce_id].coll;
564 int reduce_scatter_id =
565 find_coll_description(mpi_coll_reduce_scatter_description,
566 xbt_cfg_get_string("smpi/reduce_scatter"),"reduce_scatter");
567 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,MPI_Datatype dtype, MPI_Op op,
568 MPI_Comm comm)) mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
570 int scatter_id = find_coll_description(mpi_coll_scatter_description, xbt_cfg_get_string("smpi/scatter"),"scatter");
571 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
572 int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm))
573 mpi_coll_scatter_description[scatter_id].coll;
575 int barrier_id = find_coll_description(mpi_coll_barrier_description, xbt_cfg_get_string("smpi/barrier"),"barrier");
576 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm)) mpi_coll_barrier_description[barrier_id].coll;
578 smpi_cpu_threshold = xbt_cfg_get_double("smpi/cpu_threshold");
579 smpi_running_power = xbt_cfg_get_double("smpi/running_power");
580 smpi_privatize_global_variables = xbt_cfg_get_boolean("smpi/privatize_global_variables");
581 if (smpi_cpu_threshold < 0)
582 smpi_cpu_threshold = DBL_MAX;
585 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
587 srand(SMPI_RAND_SEED);
589 if (getenv("SMPI_PRETEND_CC") != NULL) {
590 /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the
591 * configuration tools */
596 TRACE_global_init(&argc, argv);
597 TRACE_add_start_function(TRACE_smpi_alloc);
598 TRACE_add_end_function(TRACE_smpi_release);
600 SIMIX_global_init(&argc, argv);
601 MSG_init(&argc,argv);
603 SMPI_switch_data_segment = smpi_switch_data_segment;
607 // parse the platform file: get the host list
608 SIMIX_create_environment(argv[1]);
609 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
610 SIMIX_function_register_default(realmain);
611 SIMIX_launch_application(argv[2]);
615 smpi_check_options();
617 if(smpi_privatize_global_variables)
618 smpi_initialize_global_memory_segments();
620 /* Clean IO before the run */
624 if (MC_is_active()) {
630 xbt_os_walltimer_stop(global_timer);
631 if (xbt_cfg_get_boolean("smpi/display_timing")){
632 double global_time = xbt_os_timer_elapsed(global_timer);
633 XBT_INFO("Simulated time: %g seconds. \n\n"
634 "The simulation took %g seconds (after parsing and platform setup)\n"
635 "%g seconds were actual computation of the application",
636 SIMIX_get_clock(), global_time , smpi_total_benched_time);
638 if (smpi_total_benched_time/global_time>=0.75)
639 XBT_INFO("More than 75%% of the time was spent inside the application code.\n"
640 "You may want to use sampling functions or trace replay to reduce this.");
643 smpi_global_destroy();
650 // This function can be called from extern file, to initialize logs, options, and processes of smpi
651 // without the need of smpirun
656 smpi_check_options();
657 if (TRACE_is_enabled() && TRACE_is_configured())
659 if(smpi_privatize_global_variables)
660 smpi_initialize_global_memory_segments();
663 void SMPI_finalize(){
664 smpi_global_destroy();