1 /* Copyright (c) 2007, 2008, 2009, 2010. 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. */
12 #include "smpi_mpi_dt_private.h"
14 #include "surf/surf.h"
15 #include "simix/smx_private.h"
16 #include "simgrid/sg_config.h"
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;
31 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) {
48 snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
52 static char* get_mailbox_name_small(char* str, int index) {
53 snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int)(sizeof(int) * 2), index);
57 void smpi_process_init(int *argc, char ***argv)
60 smpi_process_data_t data;
64 proc = SIMIX_process_self();
65 index = atoi((*argv)[1]);
66 data = smpi_process_remote_data(index);
67 simcall_process_set_data(proc, data);
70 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
71 (*argv)[(*argc) - 1] = NULL;
76 simcall_rdv_set_receiver(data->mailbox_small, proc);// set the process attached to the mailbox
77 XBT_DEBUG("<%d> New process in the game: %p", index, proc);
81 void smpi_process_destroy(void)
83 int index = smpi_process_index();
84 process_data[index]->index=-100;
85 XBT_DEBUG("<%d> Process left the game", index);
89 * @brief Prepares the current process for termination.
91 void smpi_process_finalize(void)
93 // wait for all pending asynchronous comms to finish
94 while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
95 simcall_process_sleep(0.01);
100 * @brief Check if a process is finalized
102 int smpi_process_finalized()
104 return (smpi_process_index()==-100);
105 // If finalized, this value has been set to -100;
109 * @brief Check if a process is initialized
111 int smpi_process_initialized(void)
113 int index = smpi_process_index();
114 return((index != -100) && (index!=MPI_UNDEFINED) && (process_data[index]->initialized));
118 * @brief Mark a process as initialized (=MPI_Init called)
120 void smpi_process_mark_as_initialized(void)
122 int index = smpi_process_index();
123 if((index != -100)&& (index!=MPI_UNDEFINED))process_data[index]->initialized=1;
128 int smpi_process_argc(void) {
129 smpi_process_data_t data = smpi_process_data();
131 return data->argc ? *(data->argc) - 1 : 0;
134 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
135 smpi_process_data_t data = smpi_process_data();
139 if(!data->argc || !data->argv
140 || *index < 1 || *index >= *(data->argc)) {
143 arg = (*data->argv)[*index];
144 for(i = 0; i < len && arg[i] != '\0'; i++) {
147 for(; i < len; i++) {
153 int smpi_global_size(void) {
154 char* value = getenv("SMPI_GLOBAL_SIZE");
157 fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
164 smpi_process_data_t smpi_process_data(void)
166 return SIMIX_process_self_get_data(SIMIX_process_self());
169 smpi_process_data_t smpi_process_remote_data(int index)
171 return process_data[index];
174 void smpi_process_set_user_data(void *data)
176 smpi_process_data_t process_data = smpi_process_data();
177 process_data->data = data;
180 void* smpi_process_get_user_data(){
181 smpi_process_data_t process_data = smpi_process_data();
182 return process_data->data;
185 int smpi_process_count(void)
187 return process_count;
190 int smpi_process_index(void)
192 smpi_process_data_t data = smpi_process_data();
193 //return -1 if not initialized
194 return data? data->index : MPI_UNDEFINED;
197 smx_rdv_t smpi_process_mailbox(void) {
198 smpi_process_data_t data = smpi_process_data();
200 return data->mailbox;
203 smx_rdv_t smpi_process_mailbox_small(void) {
204 smpi_process_data_t data = smpi_process_data();
206 return data->mailbox_small;
209 smx_rdv_t smpi_process_remote_mailbox(int index) {
210 smpi_process_data_t data = smpi_process_remote_data(index);
212 return data->mailbox;
216 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
217 smpi_process_data_t data = smpi_process_remote_data(index);
219 return data->mailbox_small;
222 xbt_os_timer_t smpi_process_timer(void)
224 smpi_process_data_t data = smpi_process_data();
229 void smpi_process_simulated_start(void) {
230 smpi_process_data_t data = smpi_process_data();
232 data->simulated = SIMIX_get_clock();
235 double smpi_process_simulated_elapsed(void) {
236 smpi_process_data_t data = smpi_process_data();
238 return SIMIX_get_clock() - data->simulated;
241 MPI_Comm smpi_process_comm_self(void) {
242 smpi_process_data_t data = smpi_process_data();
244 return data->comm_self;
247 void print_request(const char *message, MPI_Request request) {
248 XBT_DEBUG("%s request %p [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
249 message, request, request->buf, request->size,
250 request->src, request->dst, request->tag, request->flags);
253 static void SMPI_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size)
255 XBT_DEBUG("Copy the data over");
256 memcpy(comm->comm.dst_buff, buff, buff_size);
257 if (comm->comm.detached) { // if this is a detached send, the source buffer was duplicated by SMPI sender to make the original buffer available to the application ASAP
259 //It seems that the request is used after the call there this should
260 //be free somewhereelse but where???
261 //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
262 //inside the user data and should be free
263 comm->comm.src_buff = NULL;
267 void smpi_global_init(void)
271 char name[MAILBOX_NAME_MAXLEN];
273 SIMIX_comm_set_copy_data_callback(&SMPI_comm_copy_buffer_callback);
274 process_count = SIMIX_process_count();
275 process_data = xbt_new(smpi_process_data_t, process_count);
276 for (i = 0; i < process_count; i++) {
277 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
278 process_data[i]->index = i;
279 process_data[i]->argc = NULL;
280 process_data[i]->argv = NULL;
281 process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
282 process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
283 process_data[i]->timer = xbt_os_timer_new();
285 MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
286 group = smpi_group_new(1);
287 process_data[i]->comm_self = smpi_comm_new(group);
288 process_data[i]->initialized =0;
290 smpi_group_set_mapping(group, i, 0);
292 group = smpi_group_new(process_count);
293 MPI_COMM_WORLD = smpi_comm_new(group);
294 MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
295 for (i = 0; i < process_count; i++) {
296 smpi_group_set_mapping(group, i, i);
300 void smpi_global_destroy(void)
302 int count = smpi_process_count();
305 smpi_bench_destroy();
306 smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD));
307 smpi_comm_destroy(MPI_COMM_WORLD);
308 MPI_COMM_WORLD = MPI_COMM_NULL;
309 for (i = 0; i < count; i++) {
310 smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
311 smpi_comm_destroy(process_data[i]->comm_self);
312 xbt_os_timer_free(process_data[i]->timer);
313 simcall_rdv_destroy(process_data[i]->mailbox);
314 simcall_rdv_destroy(process_data[i]->mailbox_small);
315 xbt_free(process_data[i]);
317 xbt_free(process_data);
323 /* Fortran specific stuff */
324 /* With smpicc, the following weak symbols are used */
325 /* With smpiff, the following weak symbols are replaced by those in libf2c */
326 int __attribute__((weak)) xargc;
327 char** __attribute__((weak)) xargv;
330 void __attribute__((weak)) user_main_(){
331 xbt_die("Should not be in this smpi_simulated_main");
334 int __attribute__((weak)) smpi_simulated_main_(int argc, char** argv) {
335 smpi_process_init(&argc, &argv);
337 //xbt_die("Should not be in this smpi_simulated_main");
341 int __attribute__((weak)) main(int argc, char** argv) {
342 return smpi_main(smpi_simulated_main_,argc,argv);
345 int __attribute__((weak)) MAIN__(){
346 return smpi_main(smpi_simulated_main_,xargc, xargv);
350 int smpi_main(int (*realmain) (int argc, char *argv[]),int argc, char *argv[])
352 srand(SMPI_RAND_SEED);
354 if(getenv("SMPI_PRETEND_CC") != NULL) {
355 /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
359 /* Connect log categories. See xbt/log.c */
360 XBT_LOG_CONNECT(smpi); /* Keep this line as soon as possible in this
361 function: xbt_log_appender_file.c depends on it
362 DO NOT connect this in XBT or so, or it will be
363 useless to xbt_log_appender_file.c */
365 XBT_LOG_CONNECT(instr_smpi);
367 XBT_LOG_CONNECT(smpi_base);
368 XBT_LOG_CONNECT(smpi_bench);
369 XBT_LOG_CONNECT(smpi_coll);
370 XBT_LOG_CONNECT(smpi_comm);
371 XBT_LOG_CONNECT(smpi_group);
372 XBT_LOG_CONNECT(smpi_kernel);
373 XBT_LOG_CONNECT(smpi_mpi);
374 XBT_LOG_CONNECT(smpi_mpi_dt);
375 XBT_LOG_CONNECT(smpi_pmpi);
376 XBT_LOG_CONNECT(smpi_replay);
377 XBT_LOG_CONNECT(smpi_colls);
380 TRACE_global_init(&argc, argv);
382 TRACE_add_start_function(TRACE_smpi_alloc);
383 TRACE_add_end_function(TRACE_smpi_release);
386 SIMIX_global_init(&argc, argv);
392 // parse the platform file: get the host list
393 SIMIX_create_environment(argv[1]);
395 SIMIX_function_register_default(realmain);
396 SIMIX_launch_application(argv[2]);
398 int gather_id = find_coll_description(mpi_coll_gather_description,
399 sg_cfg_get_string("smpi/gather"));
400 mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
401 void*, int, MPI_Datatype, int, MPI_Comm))
402 mpi_coll_gather_description[gather_id].coll;
404 int allgather_id = find_coll_description(mpi_coll_allgather_description,
405 sg_cfg_get_string("smpi/allgather"));
406 mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
407 void*, int, MPI_Datatype, MPI_Comm))
408 mpi_coll_allgather_description[allgather_id].coll;
410 int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
411 sg_cfg_get_string("smpi/allgatherv"));
412 mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype,
413 void*, int*, int*, MPI_Datatype, MPI_Comm))
414 mpi_coll_allgatherv_description[allgatherv_id].coll;
416 int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
417 sg_cfg_get_string("smpi/allreduce"));
418 mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount, \
419 MPI_Datatype dtype, MPI_Op op, MPI_Comm comm))
420 mpi_coll_allreduce_description[allreduce_id].coll;
422 int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
423 sg_cfg_get_string("smpi/alltoall"));
424 mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
425 void*, int, MPI_Datatype, MPI_Comm))
426 mpi_coll_alltoall_description[alltoall_id].coll;
428 int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
429 sg_cfg_get_string("smpi/alltoallv"));
430 mpi_coll_alltoallv_fun = (int (*)(void *, int*, int*, MPI_Datatype,
431 void*, int*, int*, MPI_Datatype, MPI_Comm))
432 mpi_coll_alltoallv_description[alltoallv_id].coll;
434 int bcast_id = find_coll_description(mpi_coll_bcast_description,
435 sg_cfg_get_string("smpi/bcast"));
436 mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype, \
437 int root, MPI_Comm com))
438 mpi_coll_bcast_description[bcast_id].coll;
440 int reduce_id = find_coll_description(mpi_coll_reduce_description,
441 sg_cfg_get_string("smpi/reduce"));
442 mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count, MPI_Datatype datatype, \
443 MPI_Op op, int root, MPI_Comm comm))
444 mpi_coll_reduce_description[reduce_id].coll;
446 int reduce_scatter_id = find_coll_description(mpi_coll_reduce_scatter_description,
447 sg_cfg_get_string("smpi/reduce_scatter"));
448 mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,\
449 MPI_Datatype dtype,MPI_Op op,MPI_Comm comm))
450 mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
452 int scatter_id = find_coll_description(mpi_coll_scatter_description,
453 sg_cfg_get_string("smpi/scatter"));
454 mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount, MPI_Datatype sendtype,\
455 void *recvbuf, int recvcount, MPI_Datatype recvtype,\
456 int root, MPI_Comm comm))
457 mpi_coll_scatter_description[scatter_id].coll;
459 int barrier_id = find_coll_description(mpi_coll_barrier_description,
460 sg_cfg_get_string("smpi/barrier"));
461 mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
462 mpi_coll_barrier_description[barrier_id].coll;
466 /* Clean IO before the run */
471 MC_do_the_modelcheck_for_real();
475 if (sg_cfg_get_boolean("smpi/display_timing"))
476 XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
478 smpi_global_destroy();