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;
31 void *data; /* user data */
34 int sampling; /* inside an SMPI_SAMPLE_ block? */
35 } s_smpi_process_data_t;
37 static smpi_process_data_t *process_data = NULL;
38 static int process_count = 0;
40 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
41 int MPI_UNIVERSE_SIZE;
43 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
44 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
45 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
47 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
49 static char *get_mailbox_name(char *str, int index)
51 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
56 static char *get_mailbox_name_small(char *str, int index)
58 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
63 void smpi_process_init(int *argc, char ***argv)
66 smpi_process_data_t data;
70 proc = SIMIX_process_self();
71 index = atoi((*argv)[1]);
73 smpi_current_rank = index;
75 data = smpi_process_remote_data(index);
76 simcall_process_set_data(proc, data);
79 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
80 (*argv)[(*argc) - 1] = NULL;
85 // set the process attached to the mailbox
86 simcall_rdv_set_receiver(data->mailbox_small, proc);
87 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
91 void smpi_process_destroy(void)
93 int index = smpi_process_index();
94 process_data[index]->state = SMPI_FINALIZED;
95 XBT_DEBUG("<%d> Process left the game", index);
99 * @brief Prepares the current process for termination.
101 void smpi_process_finalize(void)
104 int size = smpi_comm_size(MPI_COMM_WORLD);
105 int rank = smpi_comm_rank(MPI_COMM_WORLD);
106 /* All non-root send & receive zero-length message. */
108 smpi_mpi_ssend (NULL, 0, MPI_BYTE, 0,
111 smpi_mpi_recv (NULL, 0, MPI_BYTE, 0,
113 MPI_COMM_WORLD, MPI_STATUS_IGNORE);
115 /* The root collects and broadcasts the messages. */
117 MPI_Request* requests;
118 requests = (MPI_Request*)malloc( size * sizeof(MPI_Request) );
119 for (i = 1; i < size; ++i) {
120 requests[i] = smpi_mpi_irecv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE,
121 COLL_TAG_BARRIER, MPI_COMM_WORLD
124 smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
125 for (i = 1; i < size; ++i) {
126 requests[i] = smpi_mpi_issend(NULL, 0, MPI_BYTE, i,
131 smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
138 * @brief Check if a process is finalized
140 int smpi_process_finalized()
142 int index = smpi_process_index();
143 if (index != MPI_UNDEFINED)
144 return (process_data[index]->state == SMPI_FINALIZED);
150 * @brief Check if a process is initialized
152 int smpi_process_initialized(void)
154 int index = smpi_process_index();
155 return ( (index != MPI_UNDEFINED)
156 && (process_data[index]->state == SMPI_INITIALIZED));
160 * @brief Mark a process as initialized (=MPI_Init called)
162 void smpi_process_mark_as_initialized(void)
164 int index = smpi_process_index();
165 if ((index != MPI_UNDEFINED) && (!process_data[index]->state != SMPI_FINALIZED))
166 process_data[index]->state = SMPI_INITIALIZED;
171 int smpi_process_argc(void)
173 smpi_process_data_t data = smpi_process_data();
174 return data->argc ? *(data->argc) - 1 : 0;
177 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
179 smpi_process_data_t data = smpi_process_data();
183 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
186 arg = (*data->argv)[*index];
187 for (i = 0; i < len && arg[i] != '\0'; i++) {
190 for (; i < len; i++) {
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];
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 smx_rdv_t smpi_process_mailbox(void)
245 smpi_process_data_t data = smpi_process_data();
246 return data->mailbox;
249 smx_rdv_t smpi_process_mailbox_small(void)
251 smpi_process_data_t data = smpi_process_data();
252 return data->mailbox_small;
255 smx_rdv_t smpi_process_remote_mailbox(int index)
257 smpi_process_data_t data = smpi_process_remote_data(index);
258 return data->mailbox;
262 smx_rdv_t smpi_process_remote_mailbox_small(int index)
264 smpi_process_data_t data = smpi_process_remote_data(index);
265 return data->mailbox_small;
268 xbt_os_timer_t smpi_process_timer(void)
270 smpi_process_data_t data = smpi_process_data();
274 void smpi_process_simulated_start(void)
276 smpi_process_data_t data = smpi_process_data();
277 data->simulated = SIMIX_get_clock();
280 double smpi_process_simulated_elapsed(void)
282 smpi_process_data_t data = smpi_process_data();
283 return SIMIX_get_clock() - data->simulated;
286 MPI_Comm smpi_process_comm_self(void)
288 smpi_process_data_t data = smpi_process_data();
289 if(data->comm_self==MPI_COMM_NULL){
290 MPI_Group group = smpi_group_new(1);
291 data->comm_self = smpi_comm_new(group);
292 smpi_group_set_mapping(group, smpi_process_index(), 0);
295 return data->comm_self;
298 void smpi_process_set_sampling(int s)
300 smpi_process_data_t data = smpi_process_data();
304 int smpi_process_get_sampling(void)
306 smpi_process_data_t data = smpi_process_data();
307 return data->sampling;
310 void print_request(const char *message, MPI_Request request)
313 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
314 message, request, request->buf, request->size, request->src,
315 request->dst, request->tag, request->flags);
318 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
319 void *buff, size_t buff_size)
321 XBT_DEBUG("Copy the data over");
322 if(_xbt_replay_is_active()) return;
323 memcpy(comm->comm.dst_buff, buff, buff_size);
324 if (comm->comm.detached) {
325 // if this is a detached send, the source buffer was duplicated by SMPI
326 // sender to make the original buffer available to the application ASAP
328 //It seems that the request is used after the call there this should
329 //be free somewhereelse but where???
330 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
331 //inside the user data and should be free
332 comm->comm.src_buff = NULL;
336 void smpi_global_init(void)
340 char name[MAILBOX_NAME_MAXLEN];
342 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
343 process_count = SIMIX_process_count();
344 process_data = xbt_new(smpi_process_data_t, process_count);
345 for (i = 0; i < process_count; i++) {
346 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
347 process_data[i]->index = i;
348 process_data[i]->argc = NULL;
349 process_data[i]->argv = NULL;
350 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
351 process_data[i]->mailbox_small =
352 simcall_rdv_create(get_mailbox_name_small(name, i));
353 process_data[i]->timer = xbt_os_timer_new();
355 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
356 process_data[i]->comm_self = MPI_COMM_NULL;
357 process_data[i]->state = SMPI_UNINITIALIZED;
358 process_data[i]->sampling = 0;
360 group = smpi_group_new(process_count);
361 MPI_COMM_WORLD = smpi_comm_new(group);
362 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
363 for (i = 0; i < process_count; i++) {
364 smpi_group_set_mapping(group, i, i);
367 //check correctness of MPI parameters
369 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
370 sg_cfg_get_int("smpi/send_is_detached_thres"));
372 if (sg_cfg_is_default_value("smpi/running_power")) {
373 XBT_INFO("You did not set the power of the host running the simulation. "
374 "The timings will certainly not be accurate. "
375 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
376 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information. ");
380 void smpi_global_destroy(void)
382 int count = smpi_process_count();
385 smpi_bench_destroy();
386 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
387 xbt_free(MPI_COMM_WORLD);
388 MPI_COMM_WORLD = MPI_COMM_NULL;
389 for (i = 0; i < count; i++) {
390 if(process_data[i]->comm_self!=MPI_COMM_NULL){
391 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
392 smpi_comm_destroy(process_data[i]->comm_self);
394 xbt_os_timer_free(process_data[i]->timer);
395 simcall_rdv_destroy(process_data[i]->mailbox);
396 simcall_rdv_destroy(process_data[i]->mailbox_small);
397 xbt_free(process_data[i]);
399 xbt_free(process_data);
405 /* Fortran specific stuff */
406 /* With smpicc, the following weak symbols are used */
407 /* With smpiff, the following weak symbols are replaced by those in libf2c */
408 int __attribute__ ((weak)) xargc;
409 char ** __attribute__ ((weak)) xargv;
412 void __attribute__ ((weak)) user_main_()
414 xbt_die("Should not be in this smpi_simulated_main");
418 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
420 smpi_process_init(&argc, &argv);
422 //xbt_die("Should not be in this smpi_simulated_main");
426 int __attribute__ ((weak)) main(int argc, char **argv)
428 return smpi_main(smpi_simulated_main_, argc, argv);
431 int __attribute__ ((weak)) MAIN__()
433 return smpi_main(smpi_simulated_main_, xargc, xargv);
437 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
439 srand(SMPI_RAND_SEED);
441 if (getenv("SMPI_PRETEND_CC") != NULL) {
442 /* Hack to ensure that smpicc can pretend to be a simple
443 * compiler. Particularly handy to pass it to the configuration tools */
447 /* Connect log categories. See xbt/log.c */
448 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
449 function: xbt_log_appender_file.c depends on it
450 DO NOT connect this in XBT or so, or it will be
451 useless to xbt_log_appender_file.c */
453 XBT_LOG_CONNECT(instr_smpi);
455 XBT_LOG_CONNECT(smpi_base);
456 XBT_LOG_CONNECT(smpi_bench);
457 XBT_LOG_CONNECT(smpi_coll);
458 XBT_LOG_CONNECT(smpi_colls);
459 XBT_LOG_CONNECT(smpi_comm);
460 XBT_LOG_CONNECT(smpi_dvfs);
461 XBT_LOG_CONNECT(smpi_group);
462 XBT_LOG_CONNECT(smpi_kernel);
463 XBT_LOG_CONNECT(smpi_mpi);
464 XBT_LOG_CONNECT(smpi_mpi_dt);
465 XBT_LOG_CONNECT(smpi_pmpi);
466 XBT_LOG_CONNECT(smpi_replay);
469 TRACE_global_init(&argc, argv);
471 TRACE_add_start_function(TRACE_smpi_alloc);
472 TRACE_add_end_function(TRACE_smpi_release);
475 SIMIX_global_init(&argc, argv);
481 // parse the platform file: get the host list
482 SIMIX_create_environment(argv[1]);
484 SIMIX_function_register_default(realmain);
485 SIMIX_launch_application(argv[2]);
487 int gather_id = find_coll_description(mpi_coll_gather_description,
488 sg_cfg_get_string("smpi/gather"));
489 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
490 void *, int, MPI_Datatype, int, MPI_Comm))
491 mpi_coll_gather_description[gather_id].coll;
493 int allgather_id = find_coll_description(mpi_coll_allgather_description,
494 sg_cfg_get_string("smpi/allgather"));
495 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
496 void *, int, MPI_Datatype, MPI_Comm))
497 mpi_coll_allgather_description[allgather_id].coll;
499 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
500 sg_cfg_get_string("smpi/allgatherv"));
501 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
502 int *, MPI_Datatype, MPI_Comm))
503 mpi_coll_allgatherv_description[allgatherv_id].coll;
505 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
506 sg_cfg_get_string("smpi/allreduce"));
507 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
508 MPI_Datatype dtype, MPI_Op op,
510 mpi_coll_allreduce_description[allreduce_id].coll;
512 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
513 sg_cfg_get_string("smpi/alltoall"));
514 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
515 void *, int, MPI_Datatype, MPI_Comm))
516 mpi_coll_alltoall_description[alltoall_id].coll;
518 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
519 sg_cfg_get_string("smpi/alltoallv"));
520 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
521 void *, int *, int *, MPI_Datatype,
523 mpi_coll_alltoallv_description[alltoallv_id].coll;
525 int bcast_id = find_coll_description(mpi_coll_bcast_description,
526 sg_cfg_get_string("smpi/bcast"));
527 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
528 int root, MPI_Comm com))
529 mpi_coll_bcast_description[bcast_id].coll;
531 int reduce_id = find_coll_description(mpi_coll_reduce_description,
532 sg_cfg_get_string("smpi/reduce"));
533 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
534 MPI_Datatype datatype, MPI_Op op,
535 int root, MPI_Comm comm))
536 mpi_coll_reduce_description[reduce_id].coll;
538 int reduce_scatter_id =
539 find_coll_description(mpi_coll_reduce_scatter_description,
540 sg_cfg_get_string("smpi/reduce_scatter"));
541 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
542 MPI_Datatype dtype, MPI_Op op,
544 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
546 int scatter_id = find_coll_description(mpi_coll_scatter_description,
547 sg_cfg_get_string("smpi/scatter"));
548 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
549 MPI_Datatype sendtype, void *recvbuf,
550 int recvcount, MPI_Datatype recvtype,
551 int root, MPI_Comm comm))
552 mpi_coll_scatter_description[scatter_id].coll;
554 int barrier_id = find_coll_description(mpi_coll_barrier_description,
555 sg_cfg_get_string("smpi/barrier"));
556 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
557 mpi_coll_barrier_description[barrier_id].coll;
559 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
560 smpi_running_power = sg_cfg_get_double("smpi/running_power");
561 if (smpi_cpu_threshold < 0)
562 smpi_cpu_threshold = DBL_MAX;
566 /* Clean IO before the run */
571 MC_do_the_modelcheck_for_real();
575 if (sg_cfg_get_boolean("smpi/display_timing"))
576 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
578 smpi_global_destroy();