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]);
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]->index = -100;
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 return (smpi_process_index() == -100);
114 // If finalized, this value has been set to -100;
118 * @brief Check if a process is initialized
120 int smpi_process_initialized(void)
122 int index = smpi_process_index();
123 return ((index != -100) && (index != MPI_UNDEFINED)
124 && (process_data[index]->initialized));
128 * @brief Mark a process as initialized (=MPI_Init called)
130 void smpi_process_mark_as_initialized(void)
132 int index = smpi_process_index();
133 if ((index != -100) && (index != MPI_UNDEFINED))
134 process_data[index]->initialized = 1;
139 int smpi_process_argc(void)
141 smpi_process_data_t data = smpi_process_data();
142 return data->argc ? *(data->argc) - 1 : 0;
145 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
147 smpi_process_data_t data = smpi_process_data();
151 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
154 arg = (*data->argv)[*index];
155 for (i = 0; i < len && arg[i] != '\0'; i++) {
158 for (; i < len; i++) {
164 int smpi_global_size(void)
166 char *value = getenv("SMPI_GLOBAL_SIZE");
170 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
177 smpi_process_data_t smpi_process_data(void)
179 return SIMIX_process_self_get_data(SIMIX_process_self());
182 smpi_process_data_t smpi_process_remote_data(int index)
184 return process_data[index];
187 void smpi_process_set_user_data(void *data)
189 smpi_process_data_t process_data = smpi_process_data();
190 process_data->data = data;
193 void *smpi_process_get_user_data()
195 smpi_process_data_t process_data = smpi_process_data();
196 return process_data->data;
199 int smpi_process_count(void)
201 return process_count;
204 int smpi_process_index(void)
206 smpi_process_data_t data = smpi_process_data();
207 //return -1 if not initialized
208 return data ? data->index : MPI_UNDEFINED;
211 smx_rdv_t smpi_process_mailbox(void)
213 smpi_process_data_t data = smpi_process_data();
214 return data->mailbox;
217 smx_rdv_t smpi_process_mailbox_small(void)
219 smpi_process_data_t data = smpi_process_data();
220 return data->mailbox_small;
223 smx_rdv_t smpi_process_remote_mailbox(int index)
225 smpi_process_data_t data = smpi_process_remote_data(index);
226 return data->mailbox;
230 smx_rdv_t smpi_process_remote_mailbox_small(int index)
232 smpi_process_data_t data = smpi_process_remote_data(index);
233 return data->mailbox_small;
236 xbt_os_timer_t smpi_process_timer(void)
238 smpi_process_data_t data = smpi_process_data();
242 void smpi_process_simulated_start(void)
244 smpi_process_data_t data = smpi_process_data();
245 data->simulated = SIMIX_get_clock();
248 double smpi_process_simulated_elapsed(void)
250 smpi_process_data_t data = smpi_process_data();
251 return SIMIX_get_clock() - data->simulated;
254 MPI_Comm smpi_process_comm_self(void)
256 smpi_process_data_t data = smpi_process_data();
257 return data->comm_self;
260 void smpi_process_set_sampling(int s)
262 smpi_process_data_t data = smpi_process_data();
266 int smpi_process_get_sampling(void)
268 smpi_process_data_t data = smpi_process_data();
269 return data->sampling;
272 void print_request(const char *message, MPI_Request request)
275 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
276 message, request, request->buf, request->size, request->src,
277 request->dst, request->tag, request->flags);
280 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
281 void *buff, size_t buff_size)
283 XBT_DEBUG("Copy the data over");
284 memcpy(comm->comm.dst_buff, buff, buff_size);
285 if (comm->comm.detached) {
286 // if this is a detached send, the source buffer was duplicated by SMPI
287 // sender to make the original buffer available to the application ASAP
289 //It seems that the request is used after the call there this should
290 //be free somewhereelse but where???
291 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
292 //inside the user data and should be free
293 comm->comm.src_buff = NULL;
297 void smpi_global_init(void)
301 char name[MAILBOX_NAME_MAXLEN];
303 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
304 process_count = SIMIX_process_count();
305 process_data = xbt_new(smpi_process_data_t, process_count);
306 for (i = 0; i < process_count; i++) {
307 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
308 process_data[i]->index = i;
309 process_data[i]->argc = NULL;
310 process_data[i]->argv = NULL;
311 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
312 process_data[i]->mailbox_small =
313 simcall_rdv_create(get_mailbox_name_small(name, i));
314 process_data[i]->timer = xbt_os_timer_new();
316 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
317 group = smpi_group_new(1);
318 process_data[i]->comm_self = smpi_comm_new(group);
319 process_data[i]->initialized = 0;
320 process_data[i]->sampling = 0;
322 smpi_group_set_mapping(group, i, 0);
324 group = smpi_group_new(process_count);
325 MPI_COMM_WORLD = smpi_comm_new(group);
326 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
327 for (i = 0; i < process_count; i++) {
328 smpi_group_set_mapping(group, i, i);
331 //check correctness of MPI parameters
333 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
334 sg_cfg_get_int("smpi/send_is_detached_thres"));
337 void smpi_global_destroy(void)
339 int count = smpi_process_count();
342 smpi_bench_destroy();
343 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
344 xbt_free(MPI_COMM_WORLD);
345 MPI_COMM_WORLD = MPI_COMM_NULL;
346 for (i = 0; i < count; i++) {
347 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
348 smpi_comm_destroy(process_data[i]->comm_self);
349 xbt_os_timer_free(process_data[i]->timer);
350 simcall_rdv_destroy(process_data[i]->mailbox);
351 simcall_rdv_destroy(process_data[i]->mailbox_small);
352 xbt_free(process_data[i]);
354 xbt_free(process_data);
360 /* Fortran specific stuff */
361 /* With smpicc, the following weak symbols are used */
362 /* With smpiff, the following weak symbols are replaced by those in libf2c */
363 int __attribute__ ((weak)) xargc;
364 char ** __attribute__ ((weak)) xargv;
367 void __attribute__ ((weak)) user_main_()
369 xbt_die("Should not be in this smpi_simulated_main");
373 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
375 smpi_process_init(&argc, &argv);
377 //xbt_die("Should not be in this smpi_simulated_main");
381 int __attribute__ ((weak)) main(int argc, char **argv)
383 return smpi_main(smpi_simulated_main_, argc, argv);
386 int __attribute__ ((weak)) MAIN__()
388 return smpi_main(smpi_simulated_main_, xargc, xargv);
392 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
394 srand(SMPI_RAND_SEED);
396 if (getenv("SMPI_PRETEND_CC") != NULL) {
397 /* Hack to ensure that smpicc can pretend to be a simple
398 * compiler. Particularly handy to pass it to the configuration tools */
402 /* Connect log categories. See xbt/log.c */
403 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
404 function: xbt_log_appender_file.c depends on it
405 DO NOT connect this in XBT or so, or it will be
406 useless to xbt_log_appender_file.c */
408 XBT_LOG_CONNECT(instr_smpi);
410 XBT_LOG_CONNECT(smpi_base);
411 XBT_LOG_CONNECT(smpi_bench);
412 XBT_LOG_CONNECT(smpi_coll);
413 XBT_LOG_CONNECT(smpi_colls);
414 XBT_LOG_CONNECT(smpi_comm);
415 XBT_LOG_CONNECT(smpi_dvfs);
416 XBT_LOG_CONNECT(smpi_group);
417 XBT_LOG_CONNECT(smpi_kernel);
418 XBT_LOG_CONNECT(smpi_mpi);
419 XBT_LOG_CONNECT(smpi_mpi_dt);
420 XBT_LOG_CONNECT(smpi_pmpi);
421 XBT_LOG_CONNECT(smpi_replay);
424 TRACE_global_init(&argc, argv);
426 TRACE_add_start_function(TRACE_smpi_alloc);
427 TRACE_add_end_function(TRACE_smpi_release);
430 SIMIX_global_init(&argc, argv);
436 // parse the platform file: get the host list
437 SIMIX_create_environment(argv[1]);
439 SIMIX_function_register_default(realmain);
440 SIMIX_launch_application(argv[2]);
442 int gather_id = find_coll_description(mpi_coll_gather_description,
443 sg_cfg_get_string("smpi/gather"));
444 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
445 void *, int, MPI_Datatype, int, MPI_Comm))
446 mpi_coll_gather_description[gather_id].coll;
448 int allgather_id = find_coll_description(mpi_coll_allgather_description,
449 sg_cfg_get_string("smpi/allgather"));
450 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
451 void *, int, MPI_Datatype, MPI_Comm))
452 mpi_coll_allgather_description[allgather_id].coll;
454 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
455 sg_cfg_get_string("smpi/allgatherv"));
456 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
457 int *, MPI_Datatype, MPI_Comm))
458 mpi_coll_allgatherv_description[allgatherv_id].coll;
460 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
461 sg_cfg_get_string("smpi/allreduce"));
462 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
463 MPI_Datatype dtype, MPI_Op op,
465 mpi_coll_allreduce_description[allreduce_id].coll;
467 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
468 sg_cfg_get_string("smpi/alltoall"));
469 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
470 void *, int, MPI_Datatype, MPI_Comm))
471 mpi_coll_alltoall_description[alltoall_id].coll;
473 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
474 sg_cfg_get_string("smpi/alltoallv"));
475 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
476 void *, int *, int *, MPI_Datatype,
478 mpi_coll_alltoallv_description[alltoallv_id].coll;
480 int bcast_id = find_coll_description(mpi_coll_bcast_description,
481 sg_cfg_get_string("smpi/bcast"));
482 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
483 int root, MPI_Comm com))
484 mpi_coll_bcast_description[bcast_id].coll;
486 int reduce_id = find_coll_description(mpi_coll_reduce_description,
487 sg_cfg_get_string("smpi/reduce"));
488 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
489 MPI_Datatype datatype, MPI_Op op,
490 int root, MPI_Comm comm))
491 mpi_coll_reduce_description[reduce_id].coll;
493 int reduce_scatter_id =
494 find_coll_description(mpi_coll_reduce_scatter_description,
495 sg_cfg_get_string("smpi/reduce_scatter"));
496 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
497 MPI_Datatype dtype, MPI_Op op,
499 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
501 int scatter_id = find_coll_description(mpi_coll_scatter_description,
502 sg_cfg_get_string("smpi/scatter"));
503 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
504 MPI_Datatype sendtype, void *recvbuf,
505 int recvcount, MPI_Datatype recvtype,
506 int root, MPI_Comm comm))
507 mpi_coll_scatter_description[scatter_id].coll;
509 int barrier_id = find_coll_description(mpi_coll_barrier_description,
510 sg_cfg_get_string("smpi/barrier"));
511 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
512 mpi_coll_barrier_description[barrier_id].coll;
514 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
515 smpi_running_power = sg_cfg_get_double("smpi/running_power");
516 if (smpi_cpu_threshold < 0)
517 smpi_cpu_threshold = DBL_MAX;
521 /* Clean IO before the run */
526 MC_do_the_modelcheck_for_real();
530 if (sg_cfg_get_boolean("smpi/display_timing"))
531 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
533 smpi_global_destroy();