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? */
37 xbt_bar_t finalization_barrier;
38 } s_smpi_process_data_t;
40 static smpi_process_data_t *process_data = NULL;
41 int process_count = 0;
42 int* index_to_process_data = NULL;
45 MPI_Comm MPI_COMM_WORLD = MPI_COMM_UNINITIALIZED;
46 int MPI_UNIVERSE_SIZE;
48 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
49 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
50 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
52 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
54 static char *get_mailbox_name(char *str, int index)
56 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
61 static char *get_mailbox_name_small(char *str, int index)
63 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
68 void smpi_process_init(int *argc, char ***argv)
71 smpi_process_data_t data;
75 proc = SIMIX_process_self();
76 //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
77 proc->context->cleanup_func=SIMIX_process_cleanup;
78 char* instance_id = (*argv)[1];
79 int rank = atoi((*argv)[2]);
80 index = SIMIX_process_get_PID(proc) -1;
83 smpi_current_rank = index;
85 if(!index_to_process_data){
86 index_to_process_data=(int*)xbt_malloc(SIMIX_process_count()*sizeof(int));
88 MPI_Comm* temp_comm_world;
90 smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world ,&temp_bar);
91 data = smpi_process_remote_data(index);
92 data->comm_world = temp_comm_world;
93 if(temp_bar != NULL) data->finalization_barrier = temp_bar;
95 data->instance_id = instance_id;
96 xbt_free(simcall_process_get_data(proc));
97 simcall_process_set_data(proc, data);
101 memmove(&(*argv)[1], &(*argv)[3], sizeof(char *) * (*argc - 3));
102 (*argv)[(*argc) - 1] = NULL;
103 (*argv)[(*argc) - 2] = NULL;
108 // set the process attached to the mailbox
109 simcall_rdv_set_receiver(data->mailbox_small, proc);
110 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
112 if(smpi_privatize_global_variables){
113 switch_data_segment(index);
117 if (smpi_process_data() == NULL)
118 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.");
121 void smpi_process_destroy(void)
123 int index = smpi_process_index();
124 if(smpi_privatize_global_variables){
125 switch_data_segment(index);
127 process_data[index_to_process_data[index]]->state = SMPI_FINALIZED;
128 XBT_DEBUG("<%d> Process left the game", index);
132 * @brief Prepares the current process for termination.
134 void smpi_process_finalize(void)
136 int index = smpi_process_index();
137 // wait for all pending asynchronous comms to finish
138 xbt_barrier_wait(process_data[index_to_process_data[index]]->finalization_barrier);
143 * @brief Check if a process is finalized
145 int smpi_process_finalized()
147 int index = smpi_process_index();
148 if (index != MPI_UNDEFINED)
149 return (process_data[index_to_process_data[index]]->state == SMPI_FINALIZED);
155 * @brief Check if a process is initialized
157 int smpi_process_initialized(void)
159 int index = smpi_process_index();
160 return ( (index != MPI_UNDEFINED)
161 && (process_data[index_to_process_data[index]]->state == SMPI_INITIALIZED));
165 * @brief Mark a process as initialized (=MPI_Init called)
167 void smpi_process_mark_as_initialized(void)
169 int index = smpi_process_index();
170 if ((index != MPI_UNDEFINED) && (!process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
171 process_data[index_to_process_data[index]]->state = SMPI_INITIALIZED;
176 int smpi_process_argc(void)
178 smpi_process_data_t data = smpi_process_data();
179 return data->argc ? *(data->argc) - 1 : 0;
182 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
184 smpi_process_data_t data = smpi_process_data();
188 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
191 arg = (*data->argv)[*index];
192 for (i = 0; i < len && arg[i] != '\0'; i++) {
195 for (; i < len; i++) {
202 int smpi_global_size(void)
204 char *value = getenv("SMPI_GLOBAL_SIZE");
208 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
214 smpi_process_data_t smpi_process_data(void)
216 return SIMIX_process_self_get_data(SIMIX_process_self());
219 smpi_process_data_t smpi_process_remote_data(int index)
221 return process_data[index_to_process_data[index]];
224 void smpi_process_set_user_data(void *data)
226 smpi_process_data_t process_data = smpi_process_data();
227 process_data->data = data;
230 void *smpi_process_get_user_data()
232 smpi_process_data_t process_data = smpi_process_data();
233 return process_data->data;
236 int smpi_process_count(void)
238 return process_count;
241 int smpi_process_index(void)
243 smpi_process_data_t data = smpi_process_data();
244 //return -1 if not initialized
245 return data ? data->index : MPI_UNDEFINED;
248 MPI_Comm smpi_process_comm_world(void)
250 smpi_process_data_t data = smpi_process_data();
251 //return MPI_COMM_NULL if not initialized
252 return data ? *data->comm_world : MPI_COMM_NULL;
255 smx_rdv_t smpi_process_mailbox(void)
257 smpi_process_data_t data = smpi_process_data();
258 return data->mailbox;
261 smx_rdv_t smpi_process_mailbox_small(void)
263 smpi_process_data_t data = smpi_process_data();
264 return data->mailbox_small;
267 smx_rdv_t smpi_process_remote_mailbox(int index)
269 smpi_process_data_t data = smpi_process_remote_data(index);
270 return data->mailbox;
274 smx_rdv_t smpi_process_remote_mailbox_small(int index)
276 smpi_process_data_t data = smpi_process_remote_data(index);
277 return data->mailbox_small;
280 xbt_os_timer_t smpi_process_timer(void)
282 smpi_process_data_t data = smpi_process_data();
286 void smpi_process_simulated_start(void)
288 smpi_process_data_t data = smpi_process_data();
289 data->simulated = SIMIX_get_clock();
292 double smpi_process_simulated_elapsed(void)
294 smpi_process_data_t data = smpi_process_data();
295 return SIMIX_get_clock() - data->simulated;
298 MPI_Comm smpi_process_comm_self(void)
300 smpi_process_data_t data = smpi_process_data();
301 if(data->comm_self==MPI_COMM_NULL){
302 MPI_Group group = smpi_group_new(1);
303 data->comm_self = smpi_comm_new(group, NULL);
304 smpi_group_set_mapping(group, smpi_process_index(), 0);
307 return data->comm_self;
310 void smpi_process_set_sampling(int s)
312 smpi_process_data_t data = smpi_process_data();
316 int smpi_process_get_sampling(void)
318 smpi_process_data_t data = smpi_process_data();
319 return data->sampling;
322 void print_request(const char *message, MPI_Request request)
325 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
326 message, request, request->buf, request->size, request->src,
327 request->dst, request->tag, request->flags);
330 void smpi_comm_copy_buffer_callback(smx_action_t comm,
331 void *buff, size_t buff_size)
333 XBT_DEBUG("Copy the data over");
334 if(_xbt_replay_is_active()) return;
337 if((smpi_privatize_global_variables)
338 && ((char*)buff >= start_data_exe)
339 && ((char*)buff < start_data_exe + size_data_exe )
341 XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
342 switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.src_proc))->index);
343 tmpbuff = (void*)xbt_malloc(buff_size);
344 memcpy(tmpbuff, buff, buff_size);
348 if((smpi_privatize_global_variables)
349 && ((char*)comm->comm.dst_buff >= start_data_exe)
350 && ((char*)comm->comm.dst_buff < start_data_exe + size_data_exe )
352 XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
353 switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.dst_proc))->index);
357 memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
358 if (comm->comm.detached) {
359 // if this is a detached send, the source buffer was duplicated by SMPI
360 // sender to make the original buffer available to the application ASAP
362 //It seems that the request is used after the call there this should
363 //be free somewhereelse but where???
364 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
365 //inside the user data and should be free
366 comm->comm.src_buff = NULL;
369 if(tmpbuff!=buff)xbt_free(tmpbuff);
373 static void smpi_check_options(){
374 //check correctness of MPI parameters
376 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
377 sg_cfg_get_int("smpi/send_is_detached_thres"));
379 if (sg_cfg_is_default_value("smpi/running_power")) {
380 XBT_INFO("You did not set the power of the host running the simulation. "
381 "The timings will certainly not be accurate. "
382 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
383 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information. ");
387 void smpi_global_init(void)
391 char name[MAILBOX_NAME_MAXLEN];
395 if (process_count == 0){
396 process_count = SIMIX_process_count();
399 process_data = xbt_new0(smpi_process_data_t, process_count);
400 for (i = 0; i < process_count; i++) {
401 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
402 //process_data[i]->index = i;
403 process_data[i]->argc = NULL;
404 process_data[i]->argv = NULL;
405 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
406 process_data[i]->mailbox_small =
407 simcall_rdv_create(get_mailbox_name_small(name, i));
408 process_data[i]->timer = xbt_os_timer_new();
410 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
411 process_data[i]->comm_self = MPI_COMM_NULL;
412 process_data[i]->comm_world = NULL;
413 process_data[i]->state = SMPI_UNINITIALIZED;
414 process_data[i]->sampling = 0;
415 process_data[i]->finalization_barrier = NULL;
417 //if the process was launched through smpirun script
418 //we generate a global mpi_comm_world
419 //if not, we let MPI_COMM_NULL, and the comm world
420 //will be private to each mpi instance
422 group = smpi_group_new(process_count);
423 MPI_COMM_WORLD = smpi_comm_new(group, NULL);
424 xbt_bar_t bar=xbt_barrier_init(process_count);
426 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
427 for (i = 0; i < process_count; i++) {
428 smpi_group_set_mapping(group, i, i);
429 process_data[i]->finalization_barrier = bar;
434 void smpi_global_destroy(void)
436 int count = smpi_process_count();
439 smpi_bench_destroy();
440 if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
441 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
442 xbt_free(MPI_COMM_WORLD);
443 xbt_barrier_destroy(process_data[0]->finalization_barrier);
445 smpi_deployment_cleanup_instances();
447 MPI_COMM_WORLD = MPI_COMM_NULL;
448 for (i = 0; i < count; i++) {
449 if(process_data[i]->comm_self!=MPI_COMM_NULL){
450 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
451 smpi_comm_destroy(process_data[i]->comm_self);
453 xbt_os_timer_free(process_data[i]->timer);
454 simcall_rdv_destroy(process_data[i]->mailbox);
455 simcall_rdv_destroy(process_data[i]->mailbox_small);
456 xbt_free(process_data[i]);
458 xbt_free(process_data);
461 xbt_free(index_to_process_data);
462 if(smpi_privatize_global_variables)
463 smpi_destroy_global_memory_segments();
467 /* Fortran specific stuff */
468 /* With smpicc, the following weak symbols are used */
469 /* With smpiff, the following weak symbols are replaced by those in libf2c */
470 int __attribute__ ((weak)) xargc;
471 char ** __attribute__ ((weak)) xargv;
474 void __attribute__ ((weak)) user_main_()
476 xbt_die("Should not be in this smpi_simulated_main");
480 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
482 smpi_process_init(&argc, &argv);
487 int __attribute__ ((weak)) main(int argc, char **argv)
489 return smpi_main(smpi_simulated_main_, argc, argv);
492 int __attribute__ ((weak)) MAIN__()
494 return smpi_main(smpi_simulated_main_, xargc, xargv);
498 static void smpi_init_logs(){
500 /* Connect log categories. See xbt/log.c */
501 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
502 function: xbt_log_appender_file.c depends on it
503 DO NOT connect this in XBT or so, or it will be
504 useless to xbt_log_appender_file.c */
506 XBT_LOG_CONNECT(instr_smpi);
508 XBT_LOG_CONNECT(smpi_base);
509 XBT_LOG_CONNECT(smpi_bench);
510 XBT_LOG_CONNECT(smpi_coll);
511 XBT_LOG_CONNECT(smpi_colls);
512 XBT_LOG_CONNECT(smpi_comm);
513 XBT_LOG_CONNECT(smpi_dvfs);
514 XBT_LOG_CONNECT(smpi_group);
515 XBT_LOG_CONNECT(smpi_kernel);
516 XBT_LOG_CONNECT(smpi_mpi);
517 XBT_LOG_CONNECT(smpi_mpi_dt);
518 XBT_LOG_CONNECT(smpi_pmpi);
519 XBT_LOG_CONNECT(smpi_replay);
524 static void smpi_init_options(){
525 int gather_id = find_coll_description(mpi_coll_gather_description,
526 sg_cfg_get_string("smpi/gather"));
527 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
528 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 sg_cfg_get_string("smpi/allgather"));
533 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
534 void *, int, MPI_Datatype, MPI_Comm))
535 mpi_coll_allgather_description[allgather_id].coll;
537 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
538 sg_cfg_get_string("smpi/allgatherv"));
539 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
540 int *, MPI_Datatype, MPI_Comm))
541 mpi_coll_allgatherv_description[allgatherv_id].coll;
543 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
544 sg_cfg_get_string("smpi/allreduce"));
545 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
546 MPI_Datatype dtype, MPI_Op op,
548 mpi_coll_allreduce_description[allreduce_id].coll;
550 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
551 sg_cfg_get_string("smpi/alltoall"));
552 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
553 void *, int, MPI_Datatype, MPI_Comm))
554 mpi_coll_alltoall_description[alltoall_id].coll;
556 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
557 sg_cfg_get_string("smpi/alltoallv"));
558 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
559 void *, int *, int *, MPI_Datatype,
561 mpi_coll_alltoallv_description[alltoallv_id].coll;
563 int bcast_id = find_coll_description(mpi_coll_bcast_description,
564 sg_cfg_get_string("smpi/bcast"));
565 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
566 int root, MPI_Comm com))
567 mpi_coll_bcast_description[bcast_id].coll;
569 int reduce_id = find_coll_description(mpi_coll_reduce_description,
570 sg_cfg_get_string("smpi/reduce"));
571 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
572 MPI_Datatype datatype, MPI_Op op,
573 int root, MPI_Comm comm))
574 mpi_coll_reduce_description[reduce_id].coll;
576 int reduce_scatter_id =
577 find_coll_description(mpi_coll_reduce_scatter_description,
578 sg_cfg_get_string("smpi/reduce_scatter"));
579 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
580 MPI_Datatype dtype, MPI_Op op,
582 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
584 int scatter_id = find_coll_description(mpi_coll_scatter_description,
585 sg_cfg_get_string("smpi/scatter"));
586 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
587 MPI_Datatype sendtype, void *recvbuf,
588 int recvcount, MPI_Datatype recvtype,
589 int root, MPI_Comm comm))
590 mpi_coll_scatter_description[scatter_id].coll;
592 int barrier_id = find_coll_description(mpi_coll_barrier_description,
593 sg_cfg_get_string("smpi/barrier"));
594 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
595 mpi_coll_barrier_description[barrier_id].coll;
597 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
598 smpi_running_power = sg_cfg_get_double("smpi/running_power");
599 smpi_privatize_global_variables = sg_cfg_get_boolean("smpi/privatize_global_variables");
600 if (smpi_cpu_threshold < 0)
601 smpi_cpu_threshold = DBL_MAX;
605 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
607 srand(SMPI_RAND_SEED);
609 if (getenv("SMPI_PRETEND_CC") != NULL) {
610 /* Hack to ensure that smpicc can pretend to be a simple
611 * compiler. Particularly handy to pass it to the configuration tools */
618 TRACE_global_init(&argc, argv);
620 TRACE_add_start_function(TRACE_smpi_alloc);
621 TRACE_add_end_function(TRACE_smpi_release);
624 SIMIX_global_init(&argc, argv);
626 // parse the platform file: get the host list
627 SIMIX_create_environment(argv[1]);
628 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
629 SIMIX_function_register_default(realmain);
630 SIMIX_launch_application(argv[2]);
636 smpi_check_options();
638 if(smpi_privatize_global_variables)
639 smpi_initialize_global_memory_segments();
641 /* Clean IO before the run */
646 MC_do_the_modelcheck_for_real();
650 if (sg_cfg_get_boolean("smpi/display_timing"))
651 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
653 smpi_global_destroy();
662 // This function can be called from extern file, to initialize logs, options, and processes of smpi
663 // without the need of smpirun
668 smpi_check_options();
670 if (TRACE_is_enabled() && TRACE_is_configured()) {
674 if(smpi_privatize_global_variables)
675 smpi_initialize_global_memory_segments();
678 void SMPI_finalize(){
679 smpi_global_destroy();