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 data = smpi_process_remote_data(index);
72 simcall_process_set_data(proc, data);
75 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
76 (*argv)[(*argc) - 1] = NULL;
81 // set the process attached to the mailbox
82 simcall_rdv_set_receiver(data->mailbox_small, proc);
83 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
87 void smpi_process_destroy(void)
89 int index = smpi_process_index();
90 process_data[index]->index = -100;
91 XBT_DEBUG("<%d> Process left the game", index);
95 * @brief Prepares the current process for termination.
97 void smpi_process_finalize(void)
99 // wait for all pending asynchronous comms to finish
100 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
101 simcall_process_sleep(0.01);
106 * @brief Check if a process is finalized
108 int smpi_process_finalized()
110 return (smpi_process_index() == -100);
111 // If finalized, this value has been set to -100;
115 * @brief Check if a process is initialized
117 int smpi_process_initialized(void)
119 int index = smpi_process_index();
120 return ((index != -100) && (index != MPI_UNDEFINED)
121 && (process_data[index]->initialized));
125 * @brief Mark a process as initialized (=MPI_Init called)
127 void smpi_process_mark_as_initialized(void)
129 int index = smpi_process_index();
130 if ((index != -100) && (index != MPI_UNDEFINED))
131 process_data[index]->initialized = 1;
136 int smpi_process_argc(void)
138 smpi_process_data_t data = smpi_process_data();
139 return data->argc ? *(data->argc) - 1 : 0;
142 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
144 smpi_process_data_t data = smpi_process_data();
148 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
151 arg = (*data->argv)[*index];
152 for (i = 0; i < len && arg[i] != '\0'; i++) {
155 for (; i < len; i++) {
161 int smpi_global_size(void)
163 char *value = getenv("SMPI_GLOBAL_SIZE");
167 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
174 smpi_process_data_t smpi_process_data(void)
176 return SIMIX_process_self_get_data(SIMIX_process_self());
179 smpi_process_data_t smpi_process_remote_data(int index)
181 return process_data[index];
184 void smpi_process_set_user_data(void *data)
186 smpi_process_data_t process_data = smpi_process_data();
187 process_data->data = data;
190 void *smpi_process_get_user_data()
192 smpi_process_data_t process_data = smpi_process_data();
193 return process_data->data;
196 int smpi_process_count(void)
198 return process_count;
201 int smpi_process_index(void)
203 smpi_process_data_t data = smpi_process_data();
204 //return -1 if not initialized
205 return data ? data->index : MPI_UNDEFINED;
208 smx_rdv_t smpi_process_mailbox(void)
210 smpi_process_data_t data = smpi_process_data();
211 return data->mailbox;
214 smx_rdv_t smpi_process_mailbox_small(void)
216 smpi_process_data_t data = smpi_process_data();
217 return data->mailbox_small;
220 smx_rdv_t smpi_process_remote_mailbox(int index)
222 smpi_process_data_t data = smpi_process_remote_data(index);
223 return data->mailbox;
227 smx_rdv_t smpi_process_remote_mailbox_small(int index)
229 smpi_process_data_t data = smpi_process_remote_data(index);
230 return data->mailbox_small;
233 xbt_os_timer_t smpi_process_timer(void)
235 smpi_process_data_t data = smpi_process_data();
239 void smpi_process_simulated_start(void)
241 smpi_process_data_t data = smpi_process_data();
242 data->simulated = SIMIX_get_clock();
245 double smpi_process_simulated_elapsed(void)
247 smpi_process_data_t data = smpi_process_data();
248 return SIMIX_get_clock() - data->simulated;
251 MPI_Comm smpi_process_comm_self(void)
253 smpi_process_data_t data = smpi_process_data();
254 return data->comm_self;
257 void smpi_process_set_sampling(int s)
259 smpi_process_data_t data = smpi_process_data();
263 int smpi_process_get_sampling(void)
265 smpi_process_data_t data = smpi_process_data();
266 return data->sampling;
269 void print_request(const char *message, MPI_Request request)
272 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
273 message, request, request->buf, request->size, request->src,
274 request->dst, request->tag, request->flags);
277 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
278 void *buff, size_t buff_size)
280 XBT_DEBUG("Copy the data over");
281 memcpy(comm->comm.dst_buff, buff, buff_size);
282 if (comm->comm.detached) {
283 // if this is a detached send, the source buffer was duplicated by SMPI
284 // sender to make the original buffer available to the application ASAP
286 //It seems that the request is used after the call there this should
287 //be free somewhereelse but where???
288 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
289 //inside the user data and should be free
290 comm->comm.src_buff = NULL;
294 void smpi_global_init(void)
298 char name[MAILBOX_NAME_MAXLEN];
300 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
301 process_count = SIMIX_process_count();
302 process_data = xbt_new(smpi_process_data_t, process_count);
303 for (i = 0; i < process_count; i++) {
304 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
305 process_data[i]->index = i;
306 process_data[i]->argc = NULL;
307 process_data[i]->argv = NULL;
308 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
309 process_data[i]->mailbox_small =
310 simcall_rdv_create(get_mailbox_name_small(name, i));
311 process_data[i]->timer = xbt_os_timer_new();
313 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
314 group = smpi_group_new(1);
315 process_data[i]->comm_self = smpi_comm_new(group);
316 process_data[i]->initialized = 0;
317 process_data[i]->sampling = 0;
319 smpi_group_set_mapping(group, i, 0);
321 group = smpi_group_new(process_count);
322 MPI_COMM_WORLD = smpi_comm_new(group);
323 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
324 for (i = 0; i < process_count; i++) {
325 smpi_group_set_mapping(group, i, i);
328 //check correctness of MPI parameters
330 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
331 sg_cfg_get_int("smpi/send_is_detached_thres"));
334 void smpi_global_destroy(void)
336 int count = smpi_process_count();
339 smpi_bench_destroy();
340 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
341 xbt_free(MPI_COMM_WORLD);
342 MPI_COMM_WORLD = MPI_COMM_NULL;
343 for (i = 0; i < count; i++) {
344 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
345 smpi_comm_destroy(process_data[i]->comm_self);
346 xbt_os_timer_free(process_data[i]->timer);
347 simcall_rdv_destroy(process_data[i]->mailbox);
348 simcall_rdv_destroy(process_data[i]->mailbox_small);
349 xbt_free(process_data[i]);
351 xbt_free(process_data);
357 /* Fortran specific stuff */
358 /* With smpicc, the following weak symbols are used */
359 /* With smpiff, the following weak symbols are replaced by those in libf2c */
360 int __attribute__ ((weak)) xargc;
361 char ** __attribute__ ((weak)) xargv;
364 void __attribute__ ((weak)) user_main_()
366 xbt_die("Should not be in this smpi_simulated_main");
370 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
372 smpi_process_init(&argc, &argv);
374 //xbt_die("Should not be in this smpi_simulated_main");
378 int __attribute__ ((weak)) main(int argc, char **argv)
380 return smpi_main(smpi_simulated_main_, argc, argv);
383 int __attribute__ ((weak)) MAIN__()
385 return smpi_main(smpi_simulated_main_, xargc, xargv);
389 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
391 srand(SMPI_RAND_SEED);
393 if (getenv("SMPI_PRETEND_CC") != NULL) {
394 /* Hack to ensure that smpicc can pretend to be a simple
395 * compiler. Particularly handy to pass it to the configuration tools */
399 /* Connect log categories. See xbt/log.c */
400 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
401 function: xbt_log_appender_file.c depends on it
402 DO NOT connect this in XBT or so, or it will be
403 useless to xbt_log_appender_file.c */
405 XBT_LOG_CONNECT(instr_smpi);
407 XBT_LOG_CONNECT(smpi_base);
408 XBT_LOG_CONNECT(smpi_bench);
409 XBT_LOG_CONNECT(smpi_coll);
410 XBT_LOG_CONNECT(smpi_comm);
411 XBT_LOG_CONNECT(smpi_group);
412 XBT_LOG_CONNECT(smpi_kernel);
413 XBT_LOG_CONNECT(smpi_mpi);
414 XBT_LOG_CONNECT(smpi_mpi_dt);
415 XBT_LOG_CONNECT(smpi_pmpi);
416 XBT_LOG_CONNECT(smpi_replay);
417 XBT_LOG_CONNECT(smpi_colls);
420 TRACE_global_init(&argc, argv);
422 TRACE_add_start_function(TRACE_smpi_alloc);
423 TRACE_add_end_function(TRACE_smpi_release);
426 SIMIX_global_init(&argc, argv);
432 // parse the platform file: get the host list
433 SIMIX_create_environment(argv[1]);
435 SIMIX_function_register_default(realmain);
436 SIMIX_launch_application(argv[2]);
438 int gather_id = find_coll_description(mpi_coll_gather_description,
439 sg_cfg_get_string("smpi/gather"));
440 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
441 void *, int, MPI_Datatype, int, MPI_Comm))
442 mpi_coll_gather_description[gather_id].coll;
444 int allgather_id = find_coll_description(mpi_coll_allgather_description,
445 sg_cfg_get_string("smpi/allgather"));
446 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
447 void *, int, MPI_Datatype, MPI_Comm))
448 mpi_coll_allgather_description[allgather_id].coll;
450 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
451 sg_cfg_get_string("smpi/allgatherv"));
452 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
453 int *, MPI_Datatype, MPI_Comm))
454 mpi_coll_allgatherv_description[allgatherv_id].coll;
456 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
457 sg_cfg_get_string("smpi/allreduce"));
458 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
459 MPI_Datatype dtype, MPI_Op op,
461 mpi_coll_allreduce_description[allreduce_id].coll;
463 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
464 sg_cfg_get_string("smpi/alltoall"));
465 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
466 void *, int, MPI_Datatype, MPI_Comm))
467 mpi_coll_alltoall_description[alltoall_id].coll;
469 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
470 sg_cfg_get_string("smpi/alltoallv"));
471 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
472 void *, int *, int *, MPI_Datatype,
474 mpi_coll_alltoallv_description[alltoallv_id].coll;
476 int bcast_id = find_coll_description(mpi_coll_bcast_description,
477 sg_cfg_get_string("smpi/bcast"));
478 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
479 int root, MPI_Comm com))
480 mpi_coll_bcast_description[bcast_id].coll;
482 int reduce_id = find_coll_description(mpi_coll_reduce_description,
483 sg_cfg_get_string("smpi/reduce"));
484 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
485 MPI_Datatype datatype, MPI_Op op,
486 int root, MPI_Comm comm))
487 mpi_coll_reduce_description[reduce_id].coll;
489 int reduce_scatter_id =
490 find_coll_description(mpi_coll_reduce_scatter_description,
491 sg_cfg_get_string("smpi/reduce_scatter"));
492 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
493 MPI_Datatype dtype, MPI_Op op,
495 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
497 int scatter_id = find_coll_description(mpi_coll_scatter_description,
498 sg_cfg_get_string("smpi/scatter"));
499 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
500 MPI_Datatype sendtype, void *recvbuf,
501 int recvcount, MPI_Datatype recvtype,
502 int root, MPI_Comm comm))
503 mpi_coll_scatter_description[scatter_id].coll;
505 int barrier_id = find_coll_description(mpi_coll_barrier_description,
506 sg_cfg_get_string("smpi/barrier"));
507 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
508 mpi_coll_barrier_description[barrier_id].coll;
510 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
511 smpi_running_power = sg_cfg_get_double("smpi/running_power");
512 if (smpi_cpu_threshold < 0)
513 smpi_cpu_threshold = DBL_MAX;
517 /* Clean IO before the run */
522 MC_do_the_modelcheck_for_real();
526 if (sg_cfg_get_boolean("smpi/display_timing"))
527 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
529 smpi_global_destroy();