1 /* Copyright (c) 2007-2014. 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 "xbt/replay.h"
11 #include "surf/surf.h"
12 #include "simix/smx_private.h"
13 #include "simgrid/sg_config.h"
15 #include <float.h> /* DBL_MAX */
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
21 "Logging specific to SMPI (kernel)");
23 typedef struct s_smpi_process_data {
28 smx_rdv_t mailbox_small;
32 void *data; /* user data */
35 int sampling; /* inside an SMPI_SAMPLE_ block? */
38 xbt_bar_t finalization_barrier;
39 } s_smpi_process_data_t;
41 static smpi_process_data_t *process_data = NULL;
42 int process_count = 0;
43 int* index_to_process_data = NULL;
46 MPI_Comm MPI_COMM_WORLD = MPI_COMM_UNINITIALIZED;
47 int MPI_UNIVERSE_SIZE;
49 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
50 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
51 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
53 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
55 static char *get_mailbox_name(char *str, int index)
57 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
62 static char *get_mailbox_name_small(char *str, int index)
64 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
69 void smpi_process_init(int *argc, char ***argv)
72 smpi_process_data_t data;
76 proc = SIMIX_process_self();
77 //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
78 proc->context->cleanup_func=SIMIX_process_cleanup;
79 char* instance_id = (*argv)[1];
80 int rank = atoi((*argv)[2]);
81 index = SIMIX_process_get_PID(proc) -1;
83 if(!index_to_process_data){
84 index_to_process_data=(int*)xbt_malloc(SIMIX_process_count()*sizeof(int));
86 MPI_Comm* temp_comm_world;
88 smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world ,&temp_bar);
89 data = smpi_process_remote_data(index);
90 data->comm_world = temp_comm_world;
91 if(temp_bar != NULL) data->finalization_barrier = temp_bar;
93 data->instance_id = instance_id;
94 xbt_free(simcall_process_get_data(proc));
95 simcall_process_set_data(proc, data);
99 memmove(&(*argv)[1], &(*argv)[3], sizeof(char *) * (*argc - 3));
100 (*argv)[(*argc) - 1] = NULL;
101 (*argv)[(*argc) - 2] = NULL;
107 // set the process attached to the mailbox
108 simcall_rdv_set_receiver(data->mailbox_small, proc);
109 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
111 if(smpi_privatize_global_variables){
112 switch_data_segment(index);
116 if (smpi_process_data() == NULL)
117 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.");
120 void smpi_process_destroy(void)
122 int index = smpi_process_index();
123 if(smpi_privatize_global_variables){
124 switch_data_segment(index);
126 process_data[index_to_process_data[index]]->state = SMPI_FINALIZED;
127 XBT_DEBUG("<%d> Process left the game", index);
131 * @brief Prepares the current process for termination.
133 void smpi_process_finalize(void)
135 int index = smpi_process_index();
136 // wait for all pending asynchronous comms to finish
137 xbt_barrier_wait(process_data[index_to_process_data[index]]->finalization_barrier);
142 * @brief Check if a process is finalized
144 int smpi_process_finalized()
146 int index = smpi_process_index();
147 if (index != MPI_UNDEFINED)
148 return (process_data[index_to_process_data[index]]->state == SMPI_FINALIZED);
154 * @brief Check if a process is initialized
156 int smpi_process_initialized(void)
158 int index = smpi_process_index();
159 return ( (index != MPI_UNDEFINED)
160 && (process_data[index_to_process_data[index]]->state == SMPI_INITIALIZED));
164 * @brief Mark a process as initialized (=MPI_Init called)
166 void smpi_process_mark_as_initialized(void)
168 int index = smpi_process_index();
169 if ((index != MPI_UNDEFINED) && (!process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
170 process_data[index_to_process_data[index]]->state = SMPI_INITIALIZED;
174 int smpi_global_size(void)
176 char *value = getenv("SMPI_GLOBAL_SIZE");
180 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
186 smpi_process_data_t smpi_process_data(void)
188 return SIMIX_process_self_get_data(SIMIX_process_self());
191 smpi_process_data_t smpi_process_remote_data(int index)
193 return process_data[index_to_process_data[index]];
196 void smpi_process_set_user_data(void *data)
198 smpi_process_data_t process_data = smpi_process_data();
199 process_data->data = data;
202 void *smpi_process_get_user_data()
204 smpi_process_data_t process_data = smpi_process_data();
205 return process_data->data;
209 int smpi_process_get_win_id()
211 smpi_process_data_t process_data = smpi_process_data();
212 return process_data->nb_wins;
215 void smpi_process_set_win_id(int id)
217 smpi_process_data_t process_data = smpi_process_data();
218 process_data->nb_wins = id;
222 int smpi_process_count(void)
224 return process_count;
227 int smpi_process_index(void)
229 smpi_process_data_t data = smpi_process_data();
230 //return -1 if not initialized
231 return data ? data->index : MPI_UNDEFINED;
234 MPI_Comm smpi_process_comm_world(void)
236 smpi_process_data_t data = smpi_process_data();
237 //return MPI_COMM_NULL if not initialized
238 return data ? *data->comm_world : MPI_COMM_NULL;
241 smx_rdv_t smpi_process_mailbox(void)
243 smpi_process_data_t data = smpi_process_data();
244 return data->mailbox;
247 smx_rdv_t smpi_process_mailbox_small(void)
249 smpi_process_data_t data = smpi_process_data();
250 return data->mailbox_small;
253 smx_rdv_t smpi_process_remote_mailbox(int index)
255 smpi_process_data_t data = smpi_process_remote_data(index);
256 return data->mailbox;
260 smx_rdv_t smpi_process_remote_mailbox_small(int index)
262 smpi_process_data_t data = smpi_process_remote_data(index);
263 return data->mailbox_small;
266 xbt_os_timer_t smpi_process_timer(void)
268 smpi_process_data_t data = smpi_process_data();
272 void smpi_process_simulated_start(void)
274 smpi_process_data_t data = smpi_process_data();
275 data->simulated = SIMIX_get_clock();
278 double smpi_process_simulated_elapsed(void)
280 smpi_process_data_t data = smpi_process_data();
281 return SIMIX_get_clock() - data->simulated;
284 MPI_Comm smpi_process_comm_self(void)
286 smpi_process_data_t data = smpi_process_data();
287 if(data->comm_self==MPI_COMM_NULL){
288 MPI_Group group = smpi_group_new(1);
289 data->comm_self = smpi_comm_new(group, NULL);
290 smpi_group_set_mapping(group, smpi_process_index(), 0);
293 return data->comm_self;
296 void smpi_process_set_sampling(int s)
298 smpi_process_data_t data = smpi_process_data();
302 int smpi_process_get_sampling(void)
304 smpi_process_data_t data = smpi_process_data();
305 return data->sampling;
308 void print_request(const char *message, MPI_Request request)
311 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
312 message, request, request->buf, request->size, request->src,
313 request->dst, request->tag, request->flags);
316 void smpi_comm_copy_buffer_callback(smx_action_t comm,
317 void *buff, size_t buff_size)
319 XBT_DEBUG("Copy the data over");
320 if(_xbt_replay_is_active()) return;
323 if((smpi_privatize_global_variables)
324 && ((char*)buff >= start_data_exe)
325 && ((char*)buff < start_data_exe + size_data_exe )
327 XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
328 switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.src_proc))->index);
329 tmpbuff = (void*)xbt_malloc(buff_size);
330 memcpy(tmpbuff, buff, buff_size);
334 if((smpi_privatize_global_variables)
335 && ((char*)comm->comm.dst_buff >= start_data_exe)
336 && ((char*)comm->comm.dst_buff < start_data_exe + size_data_exe )
338 XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
339 switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.dst_proc))->index);
343 memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
344 if (comm->comm.detached) {
345 // if this is a detached send, the source buffer was duplicated by SMPI
346 // sender to make the original buffer available to the application ASAP
348 //It seems that the request is used after the call there this should
349 //be free somewhereelse but where???
350 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
351 //inside the user data and should be free
352 comm->comm.src_buff = NULL;
355 if(tmpbuff!=buff)xbt_free(tmpbuff);
359 static void smpi_check_options(){
360 //check correctness of MPI parameters
362 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
363 sg_cfg_get_int("smpi/send_is_detached_thres"));
365 if (sg_cfg_is_default_value("smpi/running_power")) {
366 XBT_INFO("You did not set the power of the host running the simulation. "
367 "The timings will certainly not be accurate. "
368 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
369 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information. ");
373 int smpi_enabled(void) {
374 return process_data != NULL;
377 void smpi_global_init(void)
381 char name[MAILBOX_NAME_MAXLEN];
385 if (process_count == 0){
386 process_count = SIMIX_process_count();
389 process_data = xbt_new0(smpi_process_data_t, process_count);
390 for (i = 0; i < process_count; i++) {
391 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
392 //process_data[i]->index = i;
393 process_data[i]->argc = NULL;
394 process_data[i]->argv = NULL;
395 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
396 process_data[i]->mailbox_small =
397 simcall_rdv_create(get_mailbox_name_small(name, i));
398 process_data[i]->timer = xbt_os_timer_new();
400 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
401 process_data[i]->comm_self = MPI_COMM_NULL;
402 process_data[i]->comm_world = NULL;
403 process_data[i]->state = SMPI_UNINITIALIZED;
404 process_data[i]->sampling = 0;
405 process_data[i]->finalization_barrier = NULL;
407 //if the process was launched through smpirun script
408 //we generate a global mpi_comm_world
409 //if not, we let MPI_COMM_NULL, and the comm world
410 //will be private to each mpi instance
412 group = smpi_group_new(process_count);
413 MPI_COMM_WORLD = smpi_comm_new(group, NULL);
414 xbt_bar_t bar=xbt_barrier_init(process_count);
416 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
417 for (i = 0; i < process_count; i++) {
418 smpi_group_set_mapping(group, i, i);
419 process_data[i]->finalization_barrier = bar;
424 void smpi_global_destroy(void)
426 int count = smpi_process_count();
429 smpi_bench_destroy();
430 if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
431 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
432 xbt_free(MPI_COMM_WORLD);
433 xbt_barrier_destroy(process_data[0]->finalization_barrier);
435 smpi_deployment_cleanup_instances();
437 MPI_COMM_WORLD = MPI_COMM_NULL;
438 for (i = 0; i < count; i++) {
439 if(process_data[i]->comm_self!=MPI_COMM_NULL){
440 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
441 smpi_comm_destroy(process_data[i]->comm_self);
443 xbt_os_timer_free(process_data[i]->timer);
444 simcall_rdv_destroy(process_data[i]->mailbox);
445 simcall_rdv_destroy(process_data[i]->mailbox_small);
446 xbt_free(process_data[i]);
448 xbt_free(process_data);
451 xbt_free(index_to_process_data);
452 if(smpi_privatize_global_variables)
453 smpi_destroy_global_memory_segments();
458 void __attribute__ ((weak)) user_main_()
460 xbt_die("Should not be in this smpi_simulated_main");
464 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
466 smpi_process_init(&argc, &argv);
471 int __attribute__ ((weak)) main(int argc, char **argv)
473 return smpi_main(smpi_simulated_main_, argc, argv);
478 static void smpi_init_logs(){
480 /* Connect log categories. See xbt/log.c */
481 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
482 function: xbt_log_appender_file.c depends on it
483 DO NOT connect this in XBT or so, or it will be
484 useless to xbt_log_appender_file.c */
486 XBT_LOG_CONNECT(instr_smpi);
488 XBT_LOG_CONNECT(smpi_base);
489 XBT_LOG_CONNECT(smpi_bench);
490 XBT_LOG_CONNECT(smpi_coll);
491 XBT_LOG_CONNECT(smpi_colls);
492 XBT_LOG_CONNECT(smpi_comm);
493 XBT_LOG_CONNECT(smpi_dvfs);
494 XBT_LOG_CONNECT(smpi_group);
495 XBT_LOG_CONNECT(smpi_kernel);
496 XBT_LOG_CONNECT(smpi_mpi);
497 XBT_LOG_CONNECT(smpi_mpi_dt);
498 XBT_LOG_CONNECT(smpi_pmpi);
499 XBT_LOG_CONNECT(smpi_replay);
500 XBT_LOG_CONNECT(smpi_rma);
505 static void smpi_init_options(){
506 int gather_id = find_coll_description(mpi_coll_gather_description,
507 sg_cfg_get_string("smpi/gather"));
508 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
509 void *, int, MPI_Datatype, int, MPI_Comm))
510 mpi_coll_gather_description[gather_id].coll;
512 int allgather_id = find_coll_description(mpi_coll_allgather_description,
513 sg_cfg_get_string("smpi/allgather"));
514 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
515 void *, int, MPI_Datatype, MPI_Comm))
516 mpi_coll_allgather_description[allgather_id].coll;
518 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
519 sg_cfg_get_string("smpi/allgatherv"));
520 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
521 int *, MPI_Datatype, MPI_Comm))
522 mpi_coll_allgatherv_description[allgatherv_id].coll;
524 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
525 sg_cfg_get_string("smpi/allreduce"));
526 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
527 MPI_Datatype dtype, MPI_Op op,
529 mpi_coll_allreduce_description[allreduce_id].coll;
531 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
532 sg_cfg_get_string("smpi/alltoall"));
533 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
534 void *, int, MPI_Datatype, MPI_Comm))
535 mpi_coll_alltoall_description[alltoall_id].coll;
537 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
538 sg_cfg_get_string("smpi/alltoallv"));
539 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
540 void *, int *, int *, MPI_Datatype,
542 mpi_coll_alltoallv_description[alltoallv_id].coll;
544 int bcast_id = find_coll_description(mpi_coll_bcast_description,
545 sg_cfg_get_string("smpi/bcast"));
546 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
547 int root, MPI_Comm com))
548 mpi_coll_bcast_description[bcast_id].coll;
550 int reduce_id = find_coll_description(mpi_coll_reduce_description,
551 sg_cfg_get_string("smpi/reduce"));
552 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
553 MPI_Datatype datatype, MPI_Op op,
554 int root, MPI_Comm comm))
555 mpi_coll_reduce_description[reduce_id].coll;
557 int reduce_scatter_id =
558 find_coll_description(mpi_coll_reduce_scatter_description,
559 sg_cfg_get_string("smpi/reduce_scatter"));
560 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
561 MPI_Datatype dtype, MPI_Op op,
563 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
565 int scatter_id = find_coll_description(mpi_coll_scatter_description,
566 sg_cfg_get_string("smpi/scatter"));
567 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
568 MPI_Datatype sendtype, void *recvbuf,
569 int recvcount, MPI_Datatype recvtype,
570 int root, MPI_Comm comm))
571 mpi_coll_scatter_description[scatter_id].coll;
573 int barrier_id = find_coll_description(mpi_coll_barrier_description,
574 sg_cfg_get_string("smpi/barrier"));
575 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
576 mpi_coll_barrier_description[barrier_id].coll;
578 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
579 smpi_running_power = sg_cfg_get_double("smpi/running_power");
580 smpi_privatize_global_variables = sg_cfg_get_boolean("smpi/privatize_global_variables");
581 if (smpi_cpu_threshold < 0)
582 smpi_cpu_threshold = DBL_MAX;
586 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
588 srand(SMPI_RAND_SEED);
590 if (getenv("SMPI_PRETEND_CC") != NULL) {
591 /* Hack to ensure that smpicc can pretend to be a simple
592 * compiler. Particularly handy to pass it to the configuration tools */
599 TRACE_global_init(&argc, argv);
601 TRACE_add_start_function(TRACE_smpi_alloc);
602 TRACE_add_end_function(TRACE_smpi_release);
605 SIMIX_global_init(&argc, argv);
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]);
617 smpi_check_options();
619 if(smpi_privatize_global_variables)
620 smpi_initialize_global_memory_segments();
622 /* Clean IO before the run */
627 MC_do_the_modelcheck_for_real();
631 if (sg_cfg_get_boolean("smpi/display_timing"))
632 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
634 smpi_global_destroy();
643 // This function can be called from extern file, to initialize logs, options, and processes of smpi
644 // without the need of smpirun
649 smpi_check_options();
651 if (TRACE_is_enabled() && TRACE_is_configured()) {
655 if(smpi_privatize_global_variables)
656 smpi_initialize_global_memory_segments();
659 void SMPI_finalize(){
660 smpi_global_destroy();