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
104 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
105 simcall_process_sleep(0.01);
110 * @brief Check if a process is finalized
112 int smpi_process_finalized()
114 int index = smpi_process_index();
115 if (index != MPI_UNDEFINED)
116 return (process_data[index]->state == SMPI_FINALIZED);
122 * @brief Check if a process is initialized
124 int smpi_process_initialized(void)
126 int index = smpi_process_index();
127 return ( (index != MPI_UNDEFINED)
128 && (process_data[index]->state == SMPI_INITIALIZED));
132 * @brief Mark a process as initialized (=MPI_Init called)
134 void smpi_process_mark_as_initialized(void)
136 int index = smpi_process_index();
137 if ((index != MPI_UNDEFINED) && (!process_data[index]->state != SMPI_FINALIZED))
138 process_data[index]->state = SMPI_INITIALIZED;
143 int smpi_process_argc(void)
145 smpi_process_data_t data = smpi_process_data();
146 return data->argc ? *(data->argc) - 1 : 0;
149 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
151 smpi_process_data_t data = smpi_process_data();
155 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
158 arg = (*data->argv)[*index];
159 for (i = 0; i < len && arg[i] != '\0'; i++) {
162 for (; i < len; i++) {
169 int smpi_global_size(void)
171 char *value = getenv("SMPI_GLOBAL_SIZE");
175 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
181 smpi_process_data_t smpi_process_data(void)
183 return SIMIX_process_self_get_data(SIMIX_process_self());
186 smpi_process_data_t smpi_process_remote_data(int index)
188 return process_data[index];
191 void smpi_process_set_user_data(void *data)
193 smpi_process_data_t process_data = smpi_process_data();
194 process_data->data = data;
197 void *smpi_process_get_user_data()
199 smpi_process_data_t process_data = smpi_process_data();
200 return process_data->data;
203 int smpi_process_count(void)
205 return process_count;
208 int smpi_process_index(void)
210 smpi_process_data_t data = smpi_process_data();
211 //return -1 if not initialized
212 return data ? data->index : MPI_UNDEFINED;
215 smx_rdv_t smpi_process_mailbox(void)
217 smpi_process_data_t data = smpi_process_data();
218 return data->mailbox;
221 smx_rdv_t smpi_process_mailbox_small(void)
223 smpi_process_data_t data = smpi_process_data();
224 return data->mailbox_small;
227 smx_rdv_t smpi_process_remote_mailbox(int index)
229 smpi_process_data_t data = smpi_process_remote_data(index);
230 return data->mailbox;
234 smx_rdv_t smpi_process_remote_mailbox_small(int index)
236 smpi_process_data_t data = smpi_process_remote_data(index);
237 return data->mailbox_small;
240 xbt_os_timer_t smpi_process_timer(void)
242 smpi_process_data_t data = smpi_process_data();
246 void smpi_process_simulated_start(void)
248 smpi_process_data_t data = smpi_process_data();
249 data->simulated = SIMIX_get_clock();
252 double smpi_process_simulated_elapsed(void)
254 smpi_process_data_t data = smpi_process_data();
255 return SIMIX_get_clock() - data->simulated;
258 MPI_Comm smpi_process_comm_self(void)
260 smpi_process_data_t data = smpi_process_data();
261 if(data->comm_self==MPI_COMM_NULL){
262 MPI_Group group = smpi_group_new(1);
263 data->comm_self = smpi_comm_new(group);
264 smpi_group_set_mapping(group, smpi_process_index(), 0);
267 return data->comm_self;
270 void smpi_process_set_sampling(int s)
272 smpi_process_data_t data = smpi_process_data();
276 int smpi_process_get_sampling(void)
278 smpi_process_data_t data = smpi_process_data();
279 return data->sampling;
282 void print_request(const char *message, MPI_Request request)
285 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
286 message, request, request->buf, request->size, request->src,
287 request->dst, request->tag, request->flags);
290 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
291 void *buff, size_t buff_size)
293 XBT_DEBUG("Copy the data over");
294 memcpy(comm->comm.dst_buff, buff, buff_size);
295 if (comm->comm.detached) {
296 // if this is a detached send, the source buffer was duplicated by SMPI
297 // sender to make the original buffer available to the application ASAP
299 //It seems that the request is used after the call there this should
300 //be free somewhereelse but where???
301 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
302 //inside the user data and should be free
303 comm->comm.src_buff = NULL;
307 void smpi_global_init(void)
311 char name[MAILBOX_NAME_MAXLEN];
313 SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
314 process_count = SIMIX_process_count();
315 process_data = xbt_new(smpi_process_data_t, process_count);
316 for (i = 0; i < process_count; i++) {
317 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
318 process_data[i]->index = i;
319 process_data[i]->argc = NULL;
320 process_data[i]->argv = NULL;
321 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
322 process_data[i]->mailbox_small =
323 simcall_rdv_create(get_mailbox_name_small(name, i));
324 process_data[i]->timer = xbt_os_timer_new();
326 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
327 process_data[i]->comm_self = MPI_COMM_NULL;
328 process_data[i]->state = SMPI_UNINITIALIZED;
329 process_data[i]->sampling = 0;
331 group = smpi_group_new(process_count);
332 MPI_COMM_WORLD = smpi_comm_new(group);
333 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
334 for (i = 0; i < process_count; i++) {
335 smpi_group_set_mapping(group, i, i);
338 //check correctness of MPI parameters
340 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
341 sg_cfg_get_int("smpi/send_is_detached_thres"));
343 if (sg_cfg_is_default_value("smpi/running_power")) {
344 XBT_INFO("You did not set the power of the host running the simulation. "
345 "The timings will certainly not be accurate. "
346 "Use the option \"--cfg=smpi/running_power:<flops>\" to set its value."
347 "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information. ");
351 void smpi_global_destroy(void)
353 int count = smpi_process_count();
356 smpi_bench_destroy();
357 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
358 xbt_free(MPI_COMM_WORLD);
359 MPI_COMM_WORLD = MPI_COMM_NULL;
360 for (i = 0; i < count; i++) {
361 if(process_data[i]->comm_self!=MPI_COMM_NULL){
362 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
363 smpi_comm_destroy(process_data[i]->comm_self);
365 xbt_os_timer_free(process_data[i]->timer);
366 simcall_rdv_destroy(process_data[i]->mailbox);
367 simcall_rdv_destroy(process_data[i]->mailbox_small);
368 xbt_free(process_data[i]);
370 xbt_free(process_data);
376 /* Fortran specific stuff */
377 /* With smpicc, the following weak symbols are used */
378 /* With smpiff, the following weak symbols are replaced by those in libf2c */
379 int __attribute__ ((weak)) xargc;
380 char ** __attribute__ ((weak)) xargv;
383 void __attribute__ ((weak)) user_main_()
385 xbt_die("Should not be in this smpi_simulated_main");
389 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
391 smpi_process_init(&argc, &argv);
393 //xbt_die("Should not be in this smpi_simulated_main");
397 int __attribute__ ((weak)) main(int argc, char **argv)
399 return smpi_main(smpi_simulated_main_, argc, argv);
402 int __attribute__ ((weak)) MAIN__()
404 return smpi_main(smpi_simulated_main_, xargc, xargv);
408 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
410 srand(SMPI_RAND_SEED);
412 if (getenv("SMPI_PRETEND_CC") != NULL) {
413 /* Hack to ensure that smpicc can pretend to be a simple
414 * compiler. Particularly handy to pass it to the configuration tools */
418 /* Connect log categories. See xbt/log.c */
419 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
420 function: xbt_log_appender_file.c depends on it
421 DO NOT connect this in XBT or so, or it will be
422 useless to xbt_log_appender_file.c */
424 XBT_LOG_CONNECT(instr_smpi);
426 XBT_LOG_CONNECT(smpi_base);
427 XBT_LOG_CONNECT(smpi_bench);
428 XBT_LOG_CONNECT(smpi_coll);
429 XBT_LOG_CONNECT(smpi_colls);
430 XBT_LOG_CONNECT(smpi_comm);
431 XBT_LOG_CONNECT(smpi_dvfs);
432 XBT_LOG_CONNECT(smpi_group);
433 XBT_LOG_CONNECT(smpi_kernel);
434 XBT_LOG_CONNECT(smpi_mpi);
435 XBT_LOG_CONNECT(smpi_mpi_dt);
436 XBT_LOG_CONNECT(smpi_pmpi);
437 XBT_LOG_CONNECT(smpi_replay);
440 TRACE_global_init(&argc, argv);
442 TRACE_add_start_function(TRACE_smpi_alloc);
443 TRACE_add_end_function(TRACE_smpi_release);
446 SIMIX_global_init(&argc, argv);
452 // parse the platform file: get the host list
453 SIMIX_create_environment(argv[1]);
455 SIMIX_function_register_default(realmain);
456 SIMIX_launch_application(argv[2]);
458 int gather_id = find_coll_description(mpi_coll_gather_description,
459 sg_cfg_get_string("smpi/gather"));
460 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
461 void *, int, MPI_Datatype, int, MPI_Comm))
462 mpi_coll_gather_description[gather_id].coll;
464 int allgather_id = find_coll_description(mpi_coll_allgather_description,
465 sg_cfg_get_string("smpi/allgather"));
466 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
467 void *, int, MPI_Datatype, MPI_Comm))
468 mpi_coll_allgather_description[allgather_id].coll;
470 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
471 sg_cfg_get_string("smpi/allgatherv"));
472 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
473 int *, MPI_Datatype, MPI_Comm))
474 mpi_coll_allgatherv_description[allgatherv_id].coll;
476 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
477 sg_cfg_get_string("smpi/allreduce"));
478 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
479 MPI_Datatype dtype, MPI_Op op,
481 mpi_coll_allreduce_description[allreduce_id].coll;
483 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
484 sg_cfg_get_string("smpi/alltoall"));
485 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
486 void *, int, MPI_Datatype, MPI_Comm))
487 mpi_coll_alltoall_description[alltoall_id].coll;
489 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
490 sg_cfg_get_string("smpi/alltoallv"));
491 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
492 void *, int *, int *, MPI_Datatype,
494 mpi_coll_alltoallv_description[alltoallv_id].coll;
496 int bcast_id = find_coll_description(mpi_coll_bcast_description,
497 sg_cfg_get_string("smpi/bcast"));
498 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
499 int root, MPI_Comm com))
500 mpi_coll_bcast_description[bcast_id].coll;
502 int reduce_id = find_coll_description(mpi_coll_reduce_description,
503 sg_cfg_get_string("smpi/reduce"));
504 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
505 MPI_Datatype datatype, MPI_Op op,
506 int root, MPI_Comm comm))
507 mpi_coll_reduce_description[reduce_id].coll;
509 int reduce_scatter_id =
510 find_coll_description(mpi_coll_reduce_scatter_description,
511 sg_cfg_get_string("smpi/reduce_scatter"));
512 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
513 MPI_Datatype dtype, MPI_Op op,
515 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
517 int scatter_id = find_coll_description(mpi_coll_scatter_description,
518 sg_cfg_get_string("smpi/scatter"));
519 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
520 MPI_Datatype sendtype, void *recvbuf,
521 int recvcount, MPI_Datatype recvtype,
522 int root, MPI_Comm comm))
523 mpi_coll_scatter_description[scatter_id].coll;
525 int barrier_id = find_coll_description(mpi_coll_barrier_description,
526 sg_cfg_get_string("smpi/barrier"));
527 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
528 mpi_coll_barrier_description[barrier_id].coll;
530 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
531 smpi_running_power = sg_cfg_get_double("smpi/running_power");
532 if (smpi_cpu_threshold < 0)
533 smpi_cpu_threshold = DBL_MAX;
537 /* Clean IO before the run */
542 MC_do_the_modelcheck_for_real();
546 if (sg_cfg_get_boolean("smpi/display_timing"))
547 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
549 smpi_global_destroy();