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 } s_smpi_process_data_t;
35 static smpi_process_data_t *process_data = NULL;
36 static int process_count = 0;
38 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
39 int MPI_UNIVERSE_SIZE;
41 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
42 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
43 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
45 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
47 static char *get_mailbox_name(char *str, int index)
49 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
54 static char *get_mailbox_name_small(char *str, int index)
56 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
61 void smpi_process_init(int *argc, char ***argv)
64 smpi_process_data_t data;
68 proc = SIMIX_process_self();
69 index = atoi((*argv)[1]);
70 data = smpi_process_remote_data(index);
71 simcall_process_set_data(proc, data);
74 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
75 (*argv)[(*argc) - 1] = NULL;
80 // set the process attached to the mailbox
81 simcall_rdv_set_receiver(data->mailbox_small, proc);
82 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
86 void smpi_process_destroy(void)
88 int index = smpi_process_index();
89 process_data[index]->index = -100;
90 XBT_DEBUG("<%d> Process left the game", index);
94 * @brief Prepares the current process for termination.
96 void smpi_process_finalize(void)
98 // wait for all pending asynchronous comms to finish
99 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
100 simcall_process_sleep(0.01);
105 * @brief Check if a process is finalized
107 int smpi_process_finalized()
109 return (smpi_process_index() == -100);
110 // If finalized, this value has been set to -100;
114 * @brief Check if a process is initialized
116 int smpi_process_initialized(void)
118 int index = smpi_process_index();
119 return ((index != -100) && (index != MPI_UNDEFINED)
120 && (process_data[index]->initialized));
124 * @brief Mark a process as initialized (=MPI_Init called)
126 void smpi_process_mark_as_initialized(void)
128 int index = smpi_process_index();
129 if ((index != -100) && (index != MPI_UNDEFINED))
130 process_data[index]->initialized = 1;
135 int smpi_process_argc(void)
137 smpi_process_data_t data = smpi_process_data();
138 return data->argc ? *(data->argc) - 1 : 0;
141 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
143 smpi_process_data_t data = smpi_process_data();
147 if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
150 arg = (*data->argv)[*index];
151 for (i = 0; i < len && arg[i] != '\0'; i++) {
154 for (; i < len; i++) {
160 int smpi_global_size(void)
162 char *value = getenv("SMPI_GLOBAL_SIZE");
166 "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
173 smpi_process_data_t smpi_process_data(void)
175 return SIMIX_process_self_get_data(SIMIX_process_self());
178 smpi_process_data_t smpi_process_remote_data(int index)
180 return process_data[index];
183 void smpi_process_set_user_data(void *data)
185 smpi_process_data_t process_data = smpi_process_data();
186 process_data->data = data;
189 void *smpi_process_get_user_data()
191 smpi_process_data_t process_data = smpi_process_data();
192 return process_data->data;
195 int smpi_process_count(void)
197 return process_count;
200 int smpi_process_index(void)
202 smpi_process_data_t data = smpi_process_data();
203 //return -1 if not initialized
204 return data ? data->index : MPI_UNDEFINED;
207 smx_rdv_t smpi_process_mailbox(void)
209 smpi_process_data_t data = smpi_process_data();
210 return data->mailbox;
213 smx_rdv_t smpi_process_mailbox_small(void)
215 smpi_process_data_t data = smpi_process_data();
216 return data->mailbox_small;
219 smx_rdv_t smpi_process_remote_mailbox(int index)
221 smpi_process_data_t data = smpi_process_remote_data(index);
222 return data->mailbox;
226 smx_rdv_t smpi_process_remote_mailbox_small(int index)
228 smpi_process_data_t data = smpi_process_remote_data(index);
229 return data->mailbox_small;
232 xbt_os_timer_t smpi_process_timer(void)
234 smpi_process_data_t data = smpi_process_data();
238 void smpi_process_simulated_start(void)
240 smpi_process_data_t data = smpi_process_data();
241 data->simulated = SIMIX_get_clock();
244 double smpi_process_simulated_elapsed(void)
246 smpi_process_data_t data = smpi_process_data();
247 return SIMIX_get_clock() - data->simulated;
250 MPI_Comm smpi_process_comm_self(void)
252 smpi_process_data_t data = smpi_process_data();
253 return data->comm_self;
256 void print_request(const char *message, MPI_Request request)
259 ("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
260 message, request, request->buf, request->size, request->src,
261 request->dst, request->tag, request->flags);
264 static void SMPI_comm_copy_buffer_callback(smx_action_t comm,
265 void *buff, size_t buff_size)
267 XBT_DEBUG("Copy the data over");
268 memcpy(comm->comm.dst_buff, buff, buff_size);
269 if (comm->comm.detached) {
270 // if this is a detached send, the source buffer was duplicated by SMPI
271 // sender to make the original buffer available to the application ASAP
273 //It seems that the request is used after the call there this should
274 //be free somewhereelse but where???
275 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
276 //inside the user data and should be free
277 comm->comm.src_buff = NULL;
281 void smpi_global_init(void)
285 char name[MAILBOX_NAME_MAXLEN];
287 SIMIX_comm_set_copy_data_callback(&SMPI_comm_copy_buffer_callback);
288 process_count = SIMIX_process_count();
289 process_data = xbt_new(smpi_process_data_t, process_count);
290 for (i = 0; i < process_count; i++) {
291 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
292 process_data[i]->index = i;
293 process_data[i]->argc = NULL;
294 process_data[i]->argv = NULL;
295 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
296 process_data[i]->mailbox_small =
297 simcall_rdv_create(get_mailbox_name_small(name, i));
298 process_data[i]->timer = xbt_os_timer_new();
300 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
301 group = smpi_group_new(1);
302 process_data[i]->comm_self = smpi_comm_new(group);
303 process_data[i]->initialized = 0;
305 smpi_group_set_mapping(group, i, 0);
307 group = smpi_group_new(process_count);
308 MPI_COMM_WORLD = smpi_comm_new(group);
309 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
310 for (i = 0; i < process_count; i++) {
311 smpi_group_set_mapping(group, i, i);
314 //check correctness of MPI parameters
316 xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
317 sg_cfg_get_int("smpi/send_is_detached_thres"));
320 void smpi_global_destroy(void)
322 int count = smpi_process_count();
325 smpi_bench_destroy();
326 while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
327 xbt_free(MPI_COMM_WORLD);
328 MPI_COMM_WORLD = MPI_COMM_NULL;
329 for (i = 0; i < count; i++) {
330 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
331 smpi_comm_destroy(process_data[i]->comm_self);
332 xbt_os_timer_free(process_data[i]->timer);
333 simcall_rdv_destroy(process_data[i]->mailbox);
334 simcall_rdv_destroy(process_data[i]->mailbox_small);
335 xbt_free(process_data[i]);
337 xbt_free(process_data);
343 /* Fortran specific stuff */
344 /* With smpicc, the following weak symbols are used */
345 /* With smpiff, the following weak symbols are replaced by those in libf2c */
346 int __attribute__ ((weak)) xargc;
347 char ** __attribute__ ((weak)) xargv;
350 void __attribute__ ((weak)) user_main_()
352 xbt_die("Should not be in this smpi_simulated_main");
356 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
358 smpi_process_init(&argc, &argv);
360 //xbt_die("Should not be in this smpi_simulated_main");
364 int __attribute__ ((weak)) main(int argc, char **argv)
366 return smpi_main(smpi_simulated_main_, argc, argv);
369 int __attribute__ ((weak)) MAIN__()
371 return smpi_main(smpi_simulated_main_, xargc, xargv);
375 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
377 srand(SMPI_RAND_SEED);
379 if (getenv("SMPI_PRETEND_CC") != NULL) {
380 /* Hack to ensure that smpicc can pretend to be a simple
381 * compiler. Particularly handy to pass it to the configuration tools */
385 /* Connect log categories. See xbt/log.c */
386 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
387 function: xbt_log_appender_file.c depends on it
388 DO NOT connect this in XBT or so, or it will be
389 useless to xbt_log_appender_file.c */
391 XBT_LOG_CONNECT(instr_smpi);
393 XBT_LOG_CONNECT(smpi_base);
394 XBT_LOG_CONNECT(smpi_bench);
395 XBT_LOG_CONNECT(smpi_coll);
396 XBT_LOG_CONNECT(smpi_comm);
397 XBT_LOG_CONNECT(smpi_group);
398 XBT_LOG_CONNECT(smpi_kernel);
399 XBT_LOG_CONNECT(smpi_mpi);
400 XBT_LOG_CONNECT(smpi_mpi_dt);
401 XBT_LOG_CONNECT(smpi_pmpi);
402 XBT_LOG_CONNECT(smpi_replay);
403 XBT_LOG_CONNECT(smpi_colls);
406 TRACE_global_init(&argc, argv);
408 TRACE_add_start_function(TRACE_smpi_alloc);
409 TRACE_add_end_function(TRACE_smpi_release);
412 SIMIX_global_init(&argc, argv);
418 // parse the platform file: get the host list
419 SIMIX_create_environment(argv[1]);
421 SIMIX_function_register_default(realmain);
422 SIMIX_launch_application(argv[2]);
424 int gather_id = find_coll_description(mpi_coll_gather_description,
425 sg_cfg_get_string("smpi/gather"));
426 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
427 void *, int, MPI_Datatype, int, MPI_Comm))
428 mpi_coll_gather_description[gather_id].coll;
430 int allgather_id = find_coll_description(mpi_coll_allgather_description,
431 sg_cfg_get_string("smpi/allgather"));
432 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
433 void *, int, MPI_Datatype, MPI_Comm))
434 mpi_coll_allgather_description[allgather_id].coll;
436 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
437 sg_cfg_get_string("smpi/allgatherv"));
438 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
439 int *, MPI_Datatype, MPI_Comm))
440 mpi_coll_allgatherv_description[allgatherv_id].coll;
442 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
443 sg_cfg_get_string("smpi/allreduce"));
444 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
445 MPI_Datatype dtype, MPI_Op op,
447 mpi_coll_allreduce_description[allreduce_id].coll;
449 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
450 sg_cfg_get_string("smpi/alltoall"));
451 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
452 void *, int, MPI_Datatype, MPI_Comm))
453 mpi_coll_alltoall_description[alltoall_id].coll;
455 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
456 sg_cfg_get_string("smpi/alltoallv"));
457 mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
458 void *, int *, int *, MPI_Datatype,
460 mpi_coll_alltoallv_description[alltoallv_id].coll;
462 int bcast_id = find_coll_description(mpi_coll_bcast_description,
463 sg_cfg_get_string("smpi/bcast"));
464 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
465 int root, MPI_Comm com))
466 mpi_coll_bcast_description[bcast_id].coll;
468 int reduce_id = find_coll_description(mpi_coll_reduce_description,
469 sg_cfg_get_string("smpi/reduce"));
470 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
471 MPI_Datatype datatype, MPI_Op op,
472 int root, MPI_Comm comm))
473 mpi_coll_reduce_description[reduce_id].coll;
475 int reduce_scatter_id =
476 find_coll_description(mpi_coll_reduce_scatter_description,
477 sg_cfg_get_string("smpi/reduce_scatter"));
478 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
479 MPI_Datatype dtype, MPI_Op op,
481 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
483 int scatter_id = find_coll_description(mpi_coll_scatter_description,
484 sg_cfg_get_string("smpi/scatter"));
485 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
486 MPI_Datatype sendtype, void *recvbuf,
487 int recvcount, MPI_Datatype recvtype,
488 int root, MPI_Comm comm))
489 mpi_coll_scatter_description[scatter_id].coll;
491 int barrier_id = find_coll_description(mpi_coll_barrier_description,
492 sg_cfg_get_string("smpi/barrier"));
493 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
494 mpi_coll_barrier_description[barrier_id].coll;
496 smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
497 smpi_running_power = sg_cfg_get_double("smpi/running_power");
498 if (smpi_cpu_threshold < 0)
499 smpi_cpu_threshold = DBL_MAX;
503 /* Clean IO before the run */
508 MC_do_the_modelcheck_for_real();
512 if (sg_cfg_get_boolean("smpi/display_timing"))
513 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
515 smpi_global_destroy();