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)
102 // wait for all pending asynchronous comms to finish
103 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
104 simcall_process_sleep(0.01);
109 * @brief Check if a process is finalized
111 int smpi_process_finalized()
113 int index = smpi_process_index();
114 if (index != MPI_UNDEFINED)
115 return (process_data[index]->state == SMPI_FINALIZED);
121 * @brief Check if a process is initialized
123 int smpi_process_initialized(void)
125 int index = smpi_process_index();
126 return ( (index != MPI_UNDEFINED)
127 && (process_data[index]->state == SMPI_INITIALIZED));
131 * @brief Mark a process as initialized (=MPI_Init called)
133 void smpi_process_mark_as_initialized(void)
135 int index = smpi_process_index();
136 if ((index != MPI_UNDEFINED) && (!process_data[index]->state != SMPI_FINALIZED))
137 process_data[index]->state = SMPI_INITIALIZED;
142 int smpi_process_argc(void)
144 smpi_process_data_t data = smpi_process_data();
145 return data->argc ? *(data->argc) - 1 : 0;
148 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
150 smpi_process_data_t data = smpi_process_data();
154 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
157 arg = (*data->argv)[*index];
158 for (i = 0; i < len && arg[i] != '\0'; i++) {
161 for (; i < len; i++) {
168 int smpi_global_size(void)
170 char *value = getenv("SMPI_GLOBAL_SIZE");
174 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
180 smpi_process_data_t smpi_process_data(void)
182 return SIMIX_process_self_get_data(SIMIX_process_self());
185 smpi_process_data_t smpi_process_remote_data(int index)
187 return process_data[index];
190 void smpi_process_set_user_data(void *data)
192 smpi_process_data_t process_data = smpi_process_data();
193 process_data->data = data;
196 void *smpi_process_get_user_data()
198 smpi_process_data_t process_data = smpi_process_data();
199 return process_data->data;
202 int smpi_process_count(void)
204 return process_count;
207 int smpi_process_index(void)
209 smpi_process_data_t data = smpi_process_data();
210 //return -1 if not initialized
211 return data ? data->index : MPI_UNDEFINED;
214 smx_rdv_t smpi_process_mailbox(void)
216 smpi_process_data_t data = smpi_process_data();
217 return data->mailbox;
220 smx_rdv_t smpi_process_mailbox_small(void)
222 smpi_process_data_t data = smpi_process_data();
223 return data->mailbox_small;
226 smx_rdv_t smpi_process_remote_mailbox(int index)
228 smpi_process_data_t data = smpi_process_remote_data(index);
229 return data->mailbox;
233 smx_rdv_t smpi_process_remote_mailbox_small(int index)
235 smpi_process_data_t data = smpi_process_remote_data(index);
236 return data->mailbox_small;
239 xbt_os_timer_t smpi_process_timer(void)
241 smpi_process_data_t data = smpi_process_data();
245 void smpi_process_simulated_start(void)
247 smpi_process_data_t data = smpi_process_data();
248 data->simulated = SIMIX_get_clock();
251 double smpi_process_simulated_elapsed(void)
253 smpi_process_data_t data = smpi_process_data();
254 return SIMIX_get_clock() - data->simulated;
257 MPI_Comm smpi_process_comm_self(void)
259 smpi_process_data_t data = smpi_process_data();
260 if(data->comm_self==MPI_COMM_NULL){
261 MPI_Group group = smpi_group_new(1);
262 data->comm_self = smpi_comm_new(group);
263 smpi_group_set_mapping(group, smpi_process_index(), 0);
266 return data->comm_self;
269 void smpi_process_set_sampling(int s)
271 smpi_process_data_t data = smpi_process_data();
275 int smpi_process_get_sampling(void)
277 smpi_process_data_t data = smpi_process_data();
278 return data->sampling;
281 void print_request(const char *message, MPI_Request request)
284 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
285 message, request, request->buf, request->size, request->src,
286 request->dst, request->tag, request->flags);
289 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
290 void *buff, size_t buff_size)
292 XBT_DEBUG("Copy the data over");
293 memcpy(comm->comm.dst_buff, buff, buff_size);
294 if (comm->comm.detached) {
295 // if this is a detached send, the source buffer was duplicated by SMPI
296 // sender to make the original buffer available to the application ASAP
298 //It seems that the request is used after the call there this should
299 //be free somewhereelse but where???
300 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
301 //inside the user data and should be free
302 comm->comm.src_buff = NULL;
306 void smpi_global_init(void)
310 char name[MAILBOX_NAME_MAXLEN];
312 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
313 process_count = SIMIX_process_count();
314 process_data = xbt_new(smpi_process_data_t, process_count);
315 for (i = 0; i < process_count; i++) {
316 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
317 process_data[i]->index = i;
318 process_data[i]->argc = NULL;
319 process_data[i]->argv = NULL;
320 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
321 process_data[i]->mailbox_small =
322 simcall_rdv_create(get_mailbox_name_small(name, i));
323 process_data[i]->timer = xbt_os_timer_new();
325 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
326 process_data[i]->comm_self = MPI_COMM_NULL;
327 process_data[i]->state = SMPI_UNINITIALIZED;
328 process_data[i]->sampling = 0;
330 group = smpi_group_new(process_count);
331 MPI_COMM_WORLD = smpi_comm_new(group);
332 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
333 for (i = 0; i < process_count; i++) {
334 smpi_group_set_mapping(group, i, i);
337 //check correctness of MPI parameters
339 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
340 sg_cfg_get_int("smpi/send_is_detached_thres"));
342 if (sg_cfg_is_default_value("smpi/running_power")) {
343 XBT_INFO("You did not set the power of the host running the simulation. "
344 "The timings will certainly not be accurate. "
345 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
346 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information. ");
350 void smpi_global_destroy(void)
352 int count = smpi_process_count();
355 smpi_bench_destroy();
356 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
357 xbt_free(MPI_COMM_WORLD);
358 MPI_COMM_WORLD = MPI_COMM_NULL;
359 for (i = 0; i < count; i++) {
360 if(process_data[i]->comm_self!=MPI_COMM_NULL){
361 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
362 smpi_comm_destroy(process_data[i]->comm_self);
364 xbt_os_timer_free(process_data[i]->timer);
365 simcall_rdv_destroy(process_data[i]->mailbox);
366 simcall_rdv_destroy(process_data[i]->mailbox_small);
367 xbt_free(process_data[i]);
369 xbt_free(process_data);
375 /* Fortran specific stuff */
376 /* With smpicc, the following weak symbols are used */
377 /* With smpiff, the following weak symbols are replaced by those in libf2c */
378 int __attribute__ ((weak)) xargc;
379 char ** __attribute__ ((weak)) xargv;
382 void __attribute__ ((weak)) user_main_()
384 xbt_die("Should not be in this smpi_simulated_main");
388 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
390 smpi_process_init(&argc, &argv);
392 //xbt_die("Should not be in this smpi_simulated_main");
396 int __attribute__ ((weak)) main(int argc, char **argv)
398 return smpi_main(smpi_simulated_main_, argc, argv);
401 int __attribute__ ((weak)) MAIN__()
403 return smpi_main(smpi_simulated_main_, xargc, xargv);
407 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
409 srand(SMPI_RAND_SEED);
411 if (getenv("SMPI_PRETEND_CC") != NULL) {
412 /* Hack to ensure that smpicc can pretend to be a simple
413 * compiler. Particularly handy to pass it to the configuration tools */
417 /* Connect log categories. See xbt/log.c */
418 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
419 function: xbt_log_appender_file.c depends on it
420 DO NOT connect this in XBT or so, or it will be
421 useless to xbt_log_appender_file.c */
423 XBT_LOG_CONNECT(instr_smpi);
425 XBT_LOG_CONNECT(smpi_base);
426 XBT_LOG_CONNECT(smpi_bench);
427 XBT_LOG_CONNECT(smpi_coll);
428 XBT_LOG_CONNECT(smpi_colls);
429 XBT_LOG_CONNECT(smpi_comm);
430 XBT_LOG_CONNECT(smpi_dvfs);
431 XBT_LOG_CONNECT(smpi_group);
432 XBT_LOG_CONNECT(smpi_kernel);
433 XBT_LOG_CONNECT(smpi_mpi);
434 XBT_LOG_CONNECT(smpi_mpi_dt);
435 XBT_LOG_CONNECT(smpi_pmpi);
436 XBT_LOG_CONNECT(smpi_replay);
439 TRACE_global_init(&argc, argv);
441 TRACE_add_start_function(TRACE_smpi_alloc);
442 TRACE_add_end_function(TRACE_smpi_release);
445 SIMIX_global_init(&argc, argv);
451 // parse the platform file: get the host list
452 SIMIX_create_environment(argv[1]);
454 SIMIX_function_register_default(realmain);
455 SIMIX_launch_application(argv[2]);
457 int gather_id = find_coll_description(mpi_coll_gather_description,
458 sg_cfg_get_string("smpi/gather"));
459 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
460 void *, int, MPI_Datatype, int, MPI_Comm))
461 mpi_coll_gather_description[gather_id].coll;
463 int allgather_id = find_coll_description(mpi_coll_allgather_description,
464 sg_cfg_get_string("smpi/allgather"));
465 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
466 void *, int, MPI_Datatype, MPI_Comm))
467 mpi_coll_allgather_description[allgather_id].coll;
469 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
470 sg_cfg_get_string("smpi/allgatherv"));
471 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
472 int *, MPI_Datatype, MPI_Comm))
473 mpi_coll_allgatherv_description[allgatherv_id].coll;
475 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
476 sg_cfg_get_string("smpi/allreduce"));
477 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
478 MPI_Datatype dtype, MPI_Op op,
480 mpi_coll_allreduce_description[allreduce_id].coll;
482 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
483 sg_cfg_get_string("smpi/alltoall"));
484 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
485 void *, int, MPI_Datatype, MPI_Comm))
486 mpi_coll_alltoall_description[alltoall_id].coll;
488 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
489 sg_cfg_get_string("smpi/alltoallv"));
490 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
491 void *, int *, int *, MPI_Datatype,
493 mpi_coll_alltoallv_description[alltoallv_id].coll;
495 int bcast_id = find_coll_description(mpi_coll_bcast_description,
496 sg_cfg_get_string("smpi/bcast"));
497 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
498 int root, MPI_Comm com))
499 mpi_coll_bcast_description[bcast_id].coll;
501 int reduce_id = find_coll_description(mpi_coll_reduce_description,
502 sg_cfg_get_string("smpi/reduce"));
503 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
504 MPI_Datatype datatype, MPI_Op op,
505 int root, MPI_Comm comm))
506 mpi_coll_reduce_description[reduce_id].coll;
508 int reduce_scatter_id =
509 find_coll_description(mpi_coll_reduce_scatter_description,
510 sg_cfg_get_string("smpi/reduce_scatter"));
511 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
512 MPI_Datatype dtype, MPI_Op op,
514 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
516 int scatter_id = find_coll_description(mpi_coll_scatter_description,
517 sg_cfg_get_string("smpi/scatter"));
518 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
519 MPI_Datatype sendtype, void *recvbuf,
520 int recvcount, MPI_Datatype recvtype,
521 int root, MPI_Comm comm))
522 mpi_coll_scatter_description[scatter_id].coll;
524 int barrier_id = find_coll_description(mpi_coll_barrier_description,
525 sg_cfg_get_string("smpi/barrier"));
526 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
527 mpi_coll_barrier_description[barrier_id].coll;
529 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
530 smpi_running_power = sg_cfg_get_double("smpi/running_power");
531 if (smpi_cpu_threshold < 0)
532 smpi_cpu_threshold = DBL_MAX;
536 /* Clean IO before the run */
541 MC_do_the_modelcheck_for_real();
545 if (sg_cfg_get_boolean("smpi/display_timing"))
546 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
548 smpi_global_destroy();