1 /* Copyright (c) 2007-2013. 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]);
71 smpi_current_rank = index;
72 data = smpi_process_remote_data(index);
73 simcall_process_set_data(proc, data);
76 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
77 (*argv)[(*argc) - 1] = NULL;
82 // set the process attached to the mailbox
83 simcall_rdv_set_receiver(data->mailbox_small, proc);
84 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
88 void smpi_process_destroy(void)
90 int index = smpi_process_index();
91 process_data[index]->index = -100;
92 XBT_DEBUG("<%d> Process left the game", index);
96 * @brief Prepares the current process for termination.
98 void smpi_process_finalize(void)
100 // wait for all pending asynchronous comms to finish
101 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
102 simcall_process_sleep(0.01);
107 * @brief Check if a process is finalized
109 int smpi_process_finalized()
111 return (smpi_process_index() == -100);
112 // If finalized, this value has been set to -100;
116 * @brief Check if a process is initialized
118 int smpi_process_initialized(void)
120 int index = smpi_process_index();
121 return ((index != -100) && (index != MPI_UNDEFINED)
122 && (process_data[index]->initialized));
126 * @brief Mark a process as initialized (=MPI_Init called)
128 void smpi_process_mark_as_initialized(void)
130 int index = smpi_process_index();
131 if ((index != -100) && (index != MPI_UNDEFINED))
132 process_data[index]->initialized = 1;
137 int smpi_process_argc(void)
139 smpi_process_data_t data = smpi_process_data();
140 return data->argc ? *(data->argc) - 1 : 0;
143 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
145 smpi_process_data_t data = smpi_process_data();
149 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
152 arg = (*data->argv)[*index];
153 for (i = 0; i < len && arg[i] != '\0'; i++) {
156 for (; i < len; i++) {
162 int smpi_global_size(void)
164 char *value = getenv("SMPI_GLOBAL_SIZE");
168 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
175 smpi_process_data_t smpi_process_data(void)
177 return SIMIX_process_self_get_data(SIMIX_process_self());
180 smpi_process_data_t smpi_process_remote_data(int index)
182 return process_data[index];
185 void smpi_process_set_user_data(void *data)
187 smpi_process_data_t process_data = smpi_process_data();
188 process_data->data = data;
191 void *smpi_process_get_user_data()
193 smpi_process_data_t process_data = smpi_process_data();
194 return process_data->data;
197 int smpi_process_count(void)
199 return process_count;
202 int smpi_process_index(void)
204 smpi_process_data_t data = smpi_process_data();
205 //return -1 if not initialized
206 return data ? data->index : MPI_UNDEFINED;
209 smx_rdv_t smpi_process_mailbox(void)
211 smpi_process_data_t data = smpi_process_data();
212 return data->mailbox;
215 smx_rdv_t smpi_process_mailbox_small(void)
217 smpi_process_data_t data = smpi_process_data();
218 return data->mailbox_small;
221 smx_rdv_t smpi_process_remote_mailbox(int index)
223 smpi_process_data_t data = smpi_process_remote_data(index);
224 return data->mailbox;
228 smx_rdv_t smpi_process_remote_mailbox_small(int index)
230 smpi_process_data_t data = smpi_process_remote_data(index);
231 return data->mailbox_small;
234 xbt_os_timer_t smpi_process_timer(void)
236 smpi_process_data_t data = smpi_process_data();
240 void smpi_process_simulated_start(void)
242 smpi_process_data_t data = smpi_process_data();
243 data->simulated = SIMIX_get_clock();
246 double smpi_process_simulated_elapsed(void)
248 smpi_process_data_t data = smpi_process_data();
249 return SIMIX_get_clock() - data->simulated;
252 MPI_Comm smpi_process_comm_self(void)
254 smpi_process_data_t data = smpi_process_data();
255 return data->comm_self;
258 void smpi_process_set_sampling(int s)
260 smpi_process_data_t data = smpi_process_data();
264 int smpi_process_get_sampling(void)
266 smpi_process_data_t data = smpi_process_data();
267 return data->sampling;
270 void print_request(const char *message, MPI_Request request)
273 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
274 message, request, request->buf, request->size, request->src,
275 request->dst, request->tag, request->flags);
278 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
279 void *buff, size_t buff_size)
281 XBT_DEBUG("Copy the data over");
282 memcpy(comm->comm.dst_buff, buff, buff_size);
283 if (comm->comm.detached) {
284 // if this is a detached send, the source buffer was duplicated by SMPI
285 // sender to make the original buffer available to the application ASAP
287 //It seems that the request is used after the call there this should
288 //be free somewhereelse but where???
289 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
290 //inside the user data and should be free
291 comm->comm.src_buff = NULL;
295 void smpi_global_init(void)
299 char name[MAILBOX_NAME_MAXLEN];
301 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
302 process_count = SIMIX_process_count();
303 process_data = xbt_new(smpi_process_data_t, process_count);
304 for (i = 0; i < process_count; i++) {
305 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
306 process_data[i]->index = i;
307 process_data[i]->argc = NULL;
308 process_data[i]->argv = NULL;
309 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
310 process_data[i]->mailbox_small =
311 simcall_rdv_create(get_mailbox_name_small(name, i));
312 process_data[i]->timer = xbt_os_timer_new();
314 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
315 group = smpi_group_new(1);
316 process_data[i]->comm_self = smpi_comm_new(group);
317 process_data[i]->initialized = 0;
318 process_data[i]->sampling = 0;
320 smpi_group_set_mapping(group, i, 0);
322 group = smpi_group_new(process_count);
323 MPI_COMM_WORLD = smpi_comm_new(group);
324 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
325 for (i = 0; i < process_count; i++) {
326 smpi_group_set_mapping(group, i, i);
329 //check correctness of MPI parameters
331 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
332 sg_cfg_get_int("smpi/send_is_detached_thres"));
335 void smpi_global_destroy(void)
337 int count = smpi_process_count();
340 smpi_bench_destroy();
341 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
342 xbt_free(MPI_COMM_WORLD);
343 MPI_COMM_WORLD = MPI_COMM_NULL;
344 for (i = 0; i < count; i++) {
345 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
346 smpi_comm_destroy(process_data[i]->comm_self);
347 xbt_os_timer_free(process_data[i]->timer);
348 simcall_rdv_destroy(process_data[i]->mailbox);
349 simcall_rdv_destroy(process_data[i]->mailbox_small);
350 xbt_free(process_data[i]);
352 xbt_free(process_data);
358 /* Fortran specific stuff */
359 /* With smpicc, the following weak symbols are used */
360 /* With smpiff, the following weak symbols are replaced by those in libf2c */
361 int __attribute__ ((weak)) xargc;
362 char ** __attribute__ ((weak)) xargv;
365 void __attribute__ ((weak)) user_main_()
367 xbt_die("Should not be in this smpi_simulated_main");
371 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
373 smpi_process_init(&argc, &argv);
375 //xbt_die("Should not be in this smpi_simulated_main");
379 int __attribute__ ((weak)) main(int argc, char **argv)
381 return smpi_main(smpi_simulated_main_, argc, argv);
384 int __attribute__ ((weak)) MAIN__()
386 return smpi_main(smpi_simulated_main_, xargc, xargv);
390 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
392 srand(SMPI_RAND_SEED);
394 if (getenv("SMPI_PRETEND_CC") != NULL) {
395 /* Hack to ensure that smpicc can pretend to be a simple
396 * compiler. Particularly handy to pass it to the configuration tools */
400 /* Connect log categories. See xbt/log.c */
401 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
402 function: xbt_log_appender_file.c depends on it
403 DO NOT connect this in XBT or so, or it will be
404 useless to xbt_log_appender_file.c */
406 XBT_LOG_CONNECT(instr_smpi);
408 XBT_LOG_CONNECT(smpi_base);
409 XBT_LOG_CONNECT(smpi_bench);
410 XBT_LOG_CONNECT(smpi_coll);
411 XBT_LOG_CONNECT(smpi_colls);
412 XBT_LOG_CONNECT(smpi_comm);
413 XBT_LOG_CONNECT(smpi_dvfs);
414 XBT_LOG_CONNECT(smpi_group);
415 XBT_LOG_CONNECT(smpi_kernel);
416 XBT_LOG_CONNECT(smpi_mpi);
417 XBT_LOG_CONNECT(smpi_mpi_dt);
418 XBT_LOG_CONNECT(smpi_pmpi);
419 XBT_LOG_CONNECT(smpi_replay);
422 TRACE_global_init(&argc, argv);
424 TRACE_add_start_function(TRACE_smpi_alloc);
425 TRACE_add_end_function(TRACE_smpi_release);
428 SIMIX_global_init(&argc, argv);
434 // parse the platform file: get the host list
435 SIMIX_create_environment(argv[1]);
437 SIMIX_function_register_default(realmain);
438 SIMIX_launch_application(argv[2]);
440 int gather_id = find_coll_description(mpi_coll_gather_description,
441 sg_cfg_get_string("smpi/gather"));
442 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
443 void *, int, MPI_Datatype, int, MPI_Comm))
444 mpi_coll_gather_description[gather_id].coll;
446 int allgather_id = find_coll_description(mpi_coll_allgather_description,
447 sg_cfg_get_string("smpi/allgather"));
448 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
449 void *, int, MPI_Datatype, MPI_Comm))
450 mpi_coll_allgather_description[allgather_id].coll;
452 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
453 sg_cfg_get_string("smpi/allgatherv"));
454 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
455 int *, MPI_Datatype, MPI_Comm))
456 mpi_coll_allgatherv_description[allgatherv_id].coll;
458 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
459 sg_cfg_get_string("smpi/allreduce"));
460 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
461 MPI_Datatype dtype, MPI_Op op,
463 mpi_coll_allreduce_description[allreduce_id].coll;
465 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
466 sg_cfg_get_string("smpi/alltoall"));
467 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
468 void *, int, MPI_Datatype, MPI_Comm))
469 mpi_coll_alltoall_description[alltoall_id].coll;
471 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
472 sg_cfg_get_string("smpi/alltoallv"));
473 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
474 void *, int *, int *, MPI_Datatype,
476 mpi_coll_alltoallv_description[alltoallv_id].coll;
478 int bcast_id = find_coll_description(mpi_coll_bcast_description,
479 sg_cfg_get_string("smpi/bcast"));
480 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
481 int root, MPI_Comm com))
482 mpi_coll_bcast_description[bcast_id].coll;
484 int reduce_id = find_coll_description(mpi_coll_reduce_description,
485 sg_cfg_get_string("smpi/reduce"));
486 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
487 MPI_Datatype datatype, MPI_Op op,
488 int root, MPI_Comm comm))
489 mpi_coll_reduce_description[reduce_id].coll;
491 int reduce_scatter_id =
492 find_coll_description(mpi_coll_reduce_scatter_description,
493 sg_cfg_get_string("smpi/reduce_scatter"));
494 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
495 MPI_Datatype dtype, MPI_Op op,
497 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
499 int scatter_id = find_coll_description(mpi_coll_scatter_description,
500 sg_cfg_get_string("smpi/scatter"));
501 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
502 MPI_Datatype sendtype, void *recvbuf,
503 int recvcount, MPI_Datatype recvtype,
504 int root, MPI_Comm comm))
505 mpi_coll_scatter_description[scatter_id].coll;
507 int barrier_id = find_coll_description(mpi_coll_barrier_description,
508 sg_cfg_get_string("smpi/barrier"));
509 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
510 mpi_coll_barrier_description[barrier_id].coll;
512 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
513 smpi_running_power = sg_cfg_get_double("smpi/running_power");
514 if (smpi_cpu_threshold < 0)
515 smpi_cpu_threshold = DBL_MAX;
519 /* Clean IO before the run */
524 MC_do_the_modelcheck_for_real();
528 if (sg_cfg_get_boolean("smpi/display_timing"))
529 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
531 smpi_global_destroy();