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 "surf/surf.h"
11 #include "simix/smx_private.h"
12 #include "simgrid/sg_config.h"
14 #include <float.h> /* DBL_MAX */
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
20 "Logging specific to SMPI (kernel)");
22 typedef struct s_smpi_process_data {
27 smx_rdv_t mailbox_small;
30 void *data; /* user data */
33 int sampling; /* inside an SMPI_SAMPLE_ block? */
34 } s_smpi_process_data_t;
36 static smpi_process_data_t *process_data = NULL;
37 static int process_count = 0;
39 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
40 int MPI_UNIVERSE_SIZE;
42 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
43 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
44 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
46 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
48 static char *get_mailbox_name(char *str, int index)
50 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
55 static char *get_mailbox_name_small(char *str, int index)
57 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
62 void smpi_process_init(int *argc, char ***argv)
65 smpi_process_data_t data;
69 proc = SIMIX_process_self();
70 index = atoi((*argv)[1]);
72 smpi_current_rank = index;
74 data = smpi_process_remote_data(index);
75 simcall_process_set_data(proc, data);
78 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
79 (*argv)[(*argc) - 1] = NULL;
84 // set the process attached to the mailbox
85 simcall_rdv_set_receiver(data->mailbox_small, proc);
86 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
90 void smpi_process_destroy(void)
92 int index = smpi_process_index();
93 process_data[index]->state = SMPI_FINALIZED;
94 XBT_DEBUG("<%d> Process left the game", index);
98 * @brief Prepares the current process for termination.
100 void smpi_process_finalize(void)
103 int size = smpi_comm_size(MPI_COMM_WORLD);
104 int rank = smpi_comm_rank(MPI_COMM_WORLD);
105 /* All non-root send & receive zero-length message. */
107 smpi_mpi_ssend (NULL, 0, MPI_BYTE, 0,
110 smpi_mpi_recv (NULL, 0, MPI_BYTE, 0,
112 MPI_COMM_WORLD, MPI_STATUS_IGNORE);
114 /* The root collects and broadcasts the messages. */
116 MPI_Request* requests;
117 requests = (MPI_Request*)malloc( size * sizeof(MPI_Request) );
118 for (i = 1; i < size; ++i) {
119 requests[i] = smpi_mpi_irecv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE,
120 COLL_TAG_BARRIER, MPI_COMM_WORLD
123 smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
124 for (i = 1; i < size; ++i) {
125 requests[i] = smpi_mpi_issend(NULL, 0, MPI_BYTE, i,
130 smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
137 * @brief Check if a process is finalized
139 int smpi_process_finalized()
141 int index = smpi_process_index();
142 if (index != MPI_UNDEFINED)
143 return (process_data[index]->state == SMPI_FINALIZED);
149 * @brief Check if a process is initialized
151 int smpi_process_initialized(void)
153 int index = smpi_process_index();
154 return ( (index != MPI_UNDEFINED)
155 && (process_data[index]->state == SMPI_INITIALIZED));
159 * @brief Mark a process as initialized (=MPI_Init called)
161 void smpi_process_mark_as_initialized(void)
163 int index = smpi_process_index();
164 if ((index != MPI_UNDEFINED) && (!process_data[index]->state != SMPI_FINALIZED))
165 process_data[index]->state = SMPI_INITIALIZED;
170 int smpi_process_argc(void)
172 smpi_process_data_t data = smpi_process_data();
173 return data->argc ? *(data->argc) - 1 : 0;
176 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
178 smpi_process_data_t data = smpi_process_data();
182 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
185 arg = (*data->argv)[*index];
186 for (i = 0; i < len && arg[i] != '\0'; i++) {
189 for (; i < len; i++) {
196 int smpi_global_size(void)
198 char *value = getenv("SMPI_GLOBAL_SIZE");
202 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
208 smpi_process_data_t smpi_process_data(void)
210 return SIMIX_process_self_get_data(SIMIX_process_self());
213 smpi_process_data_t smpi_process_remote_data(int index)
215 return process_data[index];
218 void smpi_process_set_user_data(void *data)
220 smpi_process_data_t process_data = smpi_process_data();
221 process_data->data = data;
224 void *smpi_process_get_user_data()
226 smpi_process_data_t process_data = smpi_process_data();
227 return process_data->data;
230 int smpi_process_count(void)
232 return process_count;
235 int smpi_process_index(void)
237 smpi_process_data_t data = smpi_process_data();
238 //return -1 if not initialized
239 return data ? data->index : MPI_UNDEFINED;
242 smx_rdv_t smpi_process_mailbox(void)
244 smpi_process_data_t data = smpi_process_data();
245 return data->mailbox;
248 smx_rdv_t smpi_process_mailbox_small(void)
250 smpi_process_data_t data = smpi_process_data();
251 return data->mailbox_small;
254 smx_rdv_t smpi_process_remote_mailbox(int index)
256 smpi_process_data_t data = smpi_process_remote_data(index);
257 return data->mailbox;
261 smx_rdv_t smpi_process_remote_mailbox_small(int index)
263 smpi_process_data_t data = smpi_process_remote_data(index);
264 return data->mailbox_small;
267 xbt_os_timer_t smpi_process_timer(void)
269 smpi_process_data_t data = smpi_process_data();
273 void smpi_process_simulated_start(void)
275 smpi_process_data_t data = smpi_process_data();
276 data->simulated = SIMIX_get_clock();
279 double smpi_process_simulated_elapsed(void)
281 smpi_process_data_t data = smpi_process_data();
282 return SIMIX_get_clock() - data->simulated;
285 MPI_Comm smpi_process_comm_self(void)
287 smpi_process_data_t data = smpi_process_data();
288 if(data->comm_self==MPI_COMM_NULL){
289 MPI_Group group = smpi_group_new(1);
290 data->comm_self = smpi_comm_new(group);
291 smpi_group_set_mapping(group, smpi_process_index(), 0);
294 return data->comm_self;
297 void smpi_process_set_sampling(int s)
299 smpi_process_data_t data = smpi_process_data();
303 int smpi_process_get_sampling(void)
305 smpi_process_data_t data = smpi_process_data();
306 return data->sampling;
309 void print_request(const char *message, MPI_Request request)
312 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
313 message, request, request->buf, request->size, request->src,
314 request->dst, request->tag, request->flags);
317 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
318 void *buff, size_t buff_size)
320 XBT_DEBUG("Copy the data over");
321 memcpy(comm->comm.dst_buff, buff, buff_size);
322 if (comm->comm.detached) {
323 // if this is a detached send, the source buffer was duplicated by SMPI
324 // sender to make the original buffer available to the application ASAP
326 //It seems that the request is used after the call there this should
327 //be free somewhereelse but where???
328 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
329 //inside the user data and should be free
330 comm->comm.src_buff = NULL;
334 void smpi_global_init(void)
338 char name[MAILBOX_NAME_MAXLEN];
340 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
341 process_count = SIMIX_process_count();
342 process_data = xbt_new(smpi_process_data_t, process_count);
343 for (i = 0; i < process_count; i++) {
344 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
345 process_data[i]->index = i;
346 process_data[i]->argc = NULL;
347 process_data[i]->argv = NULL;
348 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
349 process_data[i]->mailbox_small =
350 simcall_rdv_create(get_mailbox_name_small(name, i));
351 process_data[i]->timer = xbt_os_timer_new();
353 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
354 process_data[i]->comm_self = MPI_COMM_NULL;
355 process_data[i]->state = SMPI_UNINITIALIZED;
356 process_data[i]->sampling = 0;
358 group = smpi_group_new(process_count);
359 MPI_COMM_WORLD = smpi_comm_new(group);
360 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
361 for (i = 0; i < process_count; i++) {
362 smpi_group_set_mapping(group, i, i);
365 //check correctness of MPI parameters
367 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
368 sg_cfg_get_int("smpi/send_is_detached_thres"));
370 if (sg_cfg_is_default_value("smpi/running_power")) {
371 XBT_INFO("You did not set the power of the host running the simulation. "
372 "The timings will certainly not be accurate. "
373 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
374 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information. ");
378 void smpi_global_destroy(void)
380 int count = smpi_process_count();
383 smpi_bench_destroy();
384 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
385 xbt_free(MPI_COMM_WORLD);
386 MPI_COMM_WORLD = MPI_COMM_NULL;
387 for (i = 0; i < count; i++) {
388 if(process_data[i]->comm_self!=MPI_COMM_NULL){
389 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
390 smpi_comm_destroy(process_data[i]->comm_self);
392 xbt_os_timer_free(process_data[i]->timer);
393 simcall_rdv_destroy(process_data[i]->mailbox);
394 simcall_rdv_destroy(process_data[i]->mailbox_small);
395 xbt_free(process_data[i]);
397 xbt_free(process_data);
403 /* Fortran specific stuff */
404 /* With smpicc, the following weak symbols are used */
405 /* With smpiff, the following weak symbols are replaced by those in libf2c */
406 int __attribute__ ((weak)) xargc;
407 char ** __attribute__ ((weak)) xargv;
410 void __attribute__ ((weak)) user_main_()
412 xbt_die("Should not be in this smpi_simulated_main");
416 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
418 smpi_process_init(&argc, &argv);
420 //xbt_die("Should not be in this smpi_simulated_main");
424 int __attribute__ ((weak)) main(int argc, char **argv)
426 return smpi_main(smpi_simulated_main_, argc, argv);
429 int __attribute__ ((weak)) MAIN__()
431 return smpi_main(smpi_simulated_main_, xargc, xargv);
435 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
437 srand(SMPI_RAND_SEED);
439 if (getenv("SMPI_PRETEND_CC") != NULL) {
440 /* Hack to ensure that smpicc can pretend to be a simple
441 * compiler. Particularly handy to pass it to the configuration tools */
445 /* Connect log categories. See xbt/log.c */
446 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
447 function: xbt_log_appender_file.c depends on it
448 DO NOT connect this in XBT or so, or it will be
449 useless to xbt_log_appender_file.c */
451 XBT_LOG_CONNECT(instr_smpi);
453 XBT_LOG_CONNECT(smpi_base);
454 XBT_LOG_CONNECT(smpi_bench);
455 XBT_LOG_CONNECT(smpi_coll);
456 XBT_LOG_CONNECT(smpi_colls);
457 XBT_LOG_CONNECT(smpi_comm);
458 XBT_LOG_CONNECT(smpi_dvfs);
459 XBT_LOG_CONNECT(smpi_group);
460 XBT_LOG_CONNECT(smpi_kernel);
461 XBT_LOG_CONNECT(smpi_mpi);
462 XBT_LOG_CONNECT(smpi_mpi_dt);
463 XBT_LOG_CONNECT(smpi_pmpi);
464 XBT_LOG_CONNECT(smpi_replay);
467 TRACE_global_init(&argc, argv);
469 TRACE_add_start_function(TRACE_smpi_alloc);
470 TRACE_add_end_function(TRACE_smpi_release);
473 SIMIX_global_init(&argc, argv);
479 // parse the platform file: get the host list
480 SIMIX_create_environment(argv[1]);
482 SIMIX_function_register_default(realmain);
483 SIMIX_launch_application(argv[2]);
485 int gather_id = find_coll_description(mpi_coll_gather_description,
486 sg_cfg_get_string("smpi/gather"));
487 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
488 void *, int, MPI_Datatype, int, MPI_Comm))
489 mpi_coll_gather_description[gather_id].coll;
491 int allgather_id = find_coll_description(mpi_coll_allgather_description,
492 sg_cfg_get_string("smpi/allgather"));
493 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
494 void *, int, MPI_Datatype, MPI_Comm))
495 mpi_coll_allgather_description[allgather_id].coll;
497 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
498 sg_cfg_get_string("smpi/allgatherv"));
499 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
500 int *, MPI_Datatype, MPI_Comm))
501 mpi_coll_allgatherv_description[allgatherv_id].coll;
503 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
504 sg_cfg_get_string("smpi/allreduce"));
505 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
506 MPI_Datatype dtype, MPI_Op op,
508 mpi_coll_allreduce_description[allreduce_id].coll;
510 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
511 sg_cfg_get_string("smpi/alltoall"));
512 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
513 void *, int, MPI_Datatype, MPI_Comm))
514 mpi_coll_alltoall_description[alltoall_id].coll;
516 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
517 sg_cfg_get_string("smpi/alltoallv"));
518 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
519 void *, int *, int *, MPI_Datatype,
521 mpi_coll_alltoallv_description[alltoallv_id].coll;
523 int bcast_id = find_coll_description(mpi_coll_bcast_description,
524 sg_cfg_get_string("smpi/bcast"));
525 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
526 int root, MPI_Comm com))
527 mpi_coll_bcast_description[bcast_id].coll;
529 int reduce_id = find_coll_description(mpi_coll_reduce_description,
530 sg_cfg_get_string("smpi/reduce"));
531 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
532 MPI_Datatype datatype, MPI_Op op,
533 int root, MPI_Comm comm))
534 mpi_coll_reduce_description[reduce_id].coll;
536 int reduce_scatter_id =
537 find_coll_description(mpi_coll_reduce_scatter_description,
538 sg_cfg_get_string("smpi/reduce_scatter"));
539 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
540 MPI_Datatype dtype, MPI_Op op,
542 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
544 int scatter_id = find_coll_description(mpi_coll_scatter_description,
545 sg_cfg_get_string("smpi/scatter"));
546 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
547 MPI_Datatype sendtype, void *recvbuf,
548 int recvcount, MPI_Datatype recvtype,
549 int root, MPI_Comm comm))
550 mpi_coll_scatter_description[scatter_id].coll;
552 int barrier_id = find_coll_description(mpi_coll_barrier_description,
553 sg_cfg_get_string("smpi/barrier"));
554 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
555 mpi_coll_barrier_description[barrier_id].coll;
557 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
558 smpi_running_power = sg_cfg_get_double("smpi/running_power");
559 if (smpi_cpu_threshold < 0)
560 smpi_cpu_threshold = DBL_MAX;
564 /* Clean IO before the run */
569 MC_do_the_modelcheck_for_real();
573 if (sg_cfg_get_boolean("smpi/display_timing"))
574 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
576 smpi_global_destroy();