Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9a8f5d2d1d0bde2cbd225b878c8ebbe7fdcc4216
[simgrid.git] / src / smpi / smpi_global.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8 #include "smpi_mpi_dt_private.h"
9 #include "mc/mc.h"
10 #include "src/mc/mc_record.h"
11 #include "xbt/replay.h"
12 #include "surf/surf.h"
13 #include "src/simix/smx_private.h"
14 #include "simgrid/sg_config.h"
15 #include "src/mc/mc_replay.h"
16 #include "src/msg/msg_private.h"
17
18 #include <float.h>              /* DBL_MAX */
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi, "Logging specific to SMPI (kernel)");
24
25 typedef struct s_smpi_process_data {
26   double simulated;
27   int *argc;
28   char ***argv;
29   smx_mailbox_t mailbox;
30   smx_mailbox_t mailbox_small;
31   xbt_mutex_t mailboxes_mutex;
32   xbt_os_timer_t timer;
33   MPI_Comm comm_self;
34   MPI_Comm comm_intra;
35   MPI_Comm* comm_world;
36   void *data;                   /* user data */
37   int index;
38   char state;
39   int sampling;                 /* inside an SMPI_SAMPLE_ block? */
40   char* instance_id;
41   int replaying;                /* is the process replaying a trace */
42   xbt_bar_t finalization_barrier;
43   int return_value;
44 } s_smpi_process_data_t;
45
46 static smpi_process_data_t *process_data = NULL;
47 int process_count = 0;
48 int smpi_universe_size = 0;
49 int* index_to_process_data = NULL;
50 extern double smpi_total_benched_time;
51 xbt_os_timer_t global_timer;
52 MPI_Comm MPI_COMM_WORLD = MPI_COMM_UNINITIALIZED;
53
54 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
55 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
56 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
57
58 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
59
60 static char *get_mailbox_name(char *str, int index)
61 {
62   snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2), index);
63   return str;
64 }
65
66 static char *get_mailbox_name_small(char *str, int index)
67 {
68   snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2), index);
69   return str;
70 }
71
72 void smpi_process_init(int *argc, char ***argv)
73 {
74   int index=-1;
75   smpi_process_data_t data;
76   smx_process_t proc;
77
78   if (argc && argv) {
79     proc = SIMIX_process_self();
80     //FIXME: dirty cleanup method to avoid using msg cleanup functions on these processes when using MSG+SMPI
81     SIMIX_process_set_cleanup_function(proc, SIMIX_process_cleanup);
82     char* instance_id = (*argv)[1];
83     int rank = xbt_str_parse_int((*argv)[2], "Invalid rank: %s");
84     index = smpi_process_index_of_smx_process(proc);
85
86     if(!index_to_process_data){
87       index_to_process_data=(int*)xbt_malloc(SIMIX_process_count()*sizeof(int));
88     }
89
90     if(smpi_privatize_global_variables){
91       /* Now using segment index of the process  */
92       index = proc->segment_index;
93       /* Done at the process's creation */
94       SMPI_switch_data_segment(index);
95     }
96
97     MPI_Comm* temp_comm_world;
98     xbt_bar_t temp_bar;
99     smpi_deployment_register_process(instance_id, rank, index, &temp_comm_world, &temp_bar);
100     data              = smpi_process_remote_data(index);
101     data->comm_world  = temp_comm_world;
102     if(temp_bar != NULL) data->finalization_barrier = temp_bar;
103     data->index       = index;
104     data->instance_id = instance_id;
105     data->replaying   = 0;
106     //xbt_free(simcall_process_get_data(proc));
107
108     simdata_process_t simdata = static_cast<simdata_process_t>(simcall_process_get_data(proc));
109     simdata->data             = data;
110
111     if (*argc > 3) {
112       free((*argv)[1]);
113       memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
114       (*argv)[(*argc) - 1] = NULL;
115       (*argv)[(*argc) - 2] = NULL;
116     }
117     (*argc)-=2;
118     data->argc = argc;
119     data->argv = argv;
120     // set the process attached to the mailbox
121     simcall_mbox_set_receiver(data->mailbox_small, proc);
122     XBT_DEBUG("<%d> New process in the game: %p", index, proc);
123   }
124   xbt_assert(smpi_process_data(),
125       "smpi_process_data() returned NULL. You probably gave a NULL parameter to MPI_Init. Although it's required by "
126       "MPI-2, this is currently not supported by SMPI.");
127 }
128
129 void smpi_process_destroy(void)
130 {
131   int index = smpi_process_index();
132   if(smpi_privatize_global_variables){
133     smpi_switch_data_segment(index);
134   }
135   process_data[index_to_process_data[index]]->state = SMPI_FINALIZED;
136   XBT_DEBUG("<%d> Process left the game", index);
137 }
138
139 /** @brief Prepares the current process for termination. */
140 void smpi_process_finalize(void)
141 {
142     // This leads to an explosion of the search graph which cannot be reduced:
143     if(MC_is_active() || MC_record_replay_is_active())
144       return;
145
146     int index = smpi_process_index();
147     // wait for all pending asynchronous comms to finish
148     xbt_barrier_wait(process_data[index_to_process_data[index]]->finalization_barrier);
149 }
150
151 /** @brief Check if a process is finalized */
152 int smpi_process_finalized()
153 {
154   int index = smpi_process_index();
155     if (index != MPI_UNDEFINED)
156       return (process_data[index_to_process_data[index]]->state == SMPI_FINALIZED);
157     else
158       return 0;
159 }
160
161 /** @brief Check if a process is initialized */
162 int smpi_process_initialized(void)
163 {
164   if (!index_to_process_data){
165     return false;
166   } else{
167     int index = smpi_process_index();
168     return ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state == SMPI_INITIALIZED));
169   }
170 }
171
172 /** @brief Mark a process as initialized (=MPI_Init called) */
173 void smpi_process_mark_as_initialized(void)
174 {
175   int index = smpi_process_index();
176   if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
177     process_data[index_to_process_data[index]]->state = SMPI_INITIALIZED;
178 }
179
180 void smpi_process_set_replaying(int value){
181   int index = smpi_process_index();
182   if ((index != MPI_UNDEFINED) && (process_data[index_to_process_data[index]]->state != SMPI_FINALIZED))
183     process_data[index_to_process_data[index]]->replaying = value;
184 }
185
186 int smpi_process_get_replaying(){
187   int index = smpi_process_index();
188   if (index != MPI_UNDEFINED)
189     return process_data[index_to_process_data[index]]->replaying;
190   else return _xbt_replay_is_active();
191 }
192
193 int smpi_global_size(void)
194 {
195   char *value = getenv("SMPI_GLOBAL_SIZE");
196   xbt_assert(value,"Please set env var SMPI_GLOBAL_SIZE to the expected number of processes.");
197
198   return xbt_str_parse_int(value, "SMPI_GLOBAL_SIZE contains a non-numerical value: %s");
199 }
200
201 smpi_process_data_t smpi_process_data(void)
202 {
203   simdata_process_t simdata = static_cast<simdata_process_t>(SIMIX_process_self_get_data());
204   return static_cast<smpi_process_data_t>(simdata->data);
205 }
206
207 smpi_process_data_t smpi_process_remote_data(int index)
208 {
209   return process_data[index_to_process_data[index]];
210 }
211
212 void smpi_process_set_user_data(void *data)
213 {
214   smpi_process_data_t process_data = smpi_process_data();
215   process_data->data = data;
216 }
217
218 void *smpi_process_get_user_data()
219 {
220   smpi_process_data_t process_data = smpi_process_data();
221   return process_data->data;
222 }
223
224 int smpi_process_count(void)
225 {
226   return process_count;
227 }
228
229 int smpi_process_index(void)
230 {
231   smpi_process_data_t data = smpi_process_data();
232   //return -1 if not initialized
233   return data ? data->index : MPI_UNDEFINED;
234 }
235
236 MPI_Comm smpi_process_comm_world(void)
237 {
238   smpi_process_data_t data = smpi_process_data();
239   //return MPI_COMM_NULL if not initialized
240   return data ? *data->comm_world : MPI_COMM_NULL;
241 }
242
243 smx_mailbox_t smpi_process_mailbox(void)
244 {
245   smpi_process_data_t data = smpi_process_data();
246   return data->mailbox;
247 }
248
249 smx_mailbox_t smpi_process_mailbox_small(void)
250 {
251   smpi_process_data_t data = smpi_process_data();
252   return data->mailbox_small;
253 }
254
255 xbt_mutex_t smpi_process_mailboxes_mutex(void)
256 {
257   smpi_process_data_t data = smpi_process_data();
258   return data->mailboxes_mutex;
259 }
260
261 smx_mailbox_t smpi_process_remote_mailbox(int index)
262 {
263   smpi_process_data_t data = smpi_process_remote_data(index);
264   return data->mailbox;
265 }
266
267 smx_mailbox_t smpi_process_remote_mailbox_small(int index)
268 {
269   smpi_process_data_t data = smpi_process_remote_data(index);
270   return data->mailbox_small;
271 }
272
273 xbt_mutex_t smpi_process_remote_mailboxes_mutex(int index)
274 {
275   smpi_process_data_t data = smpi_process_remote_data(index);
276   return data->mailboxes_mutex;
277 }
278
279 xbt_os_timer_t smpi_process_timer(void)
280 {
281   smpi_process_data_t data = smpi_process_data();
282   return data->timer;
283 }
284
285 void smpi_process_simulated_start(void)
286 {
287   smpi_process_data_t data = smpi_process_data();
288   data->simulated = SIMIX_get_clock();
289 }
290
291 double smpi_process_simulated_elapsed(void)
292 {
293   smpi_process_data_t data = smpi_process_data();
294   return SIMIX_get_clock() - data->simulated;
295 }
296
297 MPI_Comm smpi_process_comm_self(void)
298 {
299   smpi_process_data_t data = smpi_process_data();
300   if(data->comm_self==MPI_COMM_NULL){
301     MPI_Group group = smpi_group_new(1);
302     data->comm_self = smpi_comm_new(group, NULL);
303     smpi_group_set_mapping(group, smpi_process_index(), 0);
304   }
305
306   return data->comm_self;
307 }
308
309 MPI_Comm smpi_process_get_comm_intra(void)
310 {
311   smpi_process_data_t data = smpi_process_data();
312   return data->comm_intra;
313 }
314
315 void smpi_process_set_comm_intra(MPI_Comm comm)
316 {
317   smpi_process_data_t data = smpi_process_data();
318   data->comm_intra = comm;
319 }
320
321 void smpi_process_set_sampling(int s)
322 {
323   smpi_process_data_t data = smpi_process_data();
324   data->sampling = s;
325 }
326
327 int smpi_process_get_sampling(void)
328 {
329   smpi_process_data_t data = smpi_process_data();
330   return data->sampling;
331 }
332
333 void print_request(const char *message, MPI_Request request)
334 {
335   XBT_VERB("%s  request %p  [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
336        message, request, request->buf, request->size, request->src, request->dst, request->tag, request->flags);
337 }
338
339 void smpi_comm_copy_buffer_callback(smx_synchro_t comm, void *buff, size_t buff_size)
340 {
341   XBT_DEBUG("Copy the data over");
342   void* tmpbuff=buff;
343
344   if((smpi_privatize_global_variables) && ((char*)buff >= smpi_start_data_exe)
345       && ((char*)buff < smpi_start_data_exe + smpi_size_data_exe )
346     ){
347        XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
348        smpi_switch_data_segment(((smpi_process_data_t)(((simdata_process_t)SIMIX_process_get_data(comm->comm.src_proc))->data))->index);
349        tmpbuff = (void*)xbt_malloc(buff_size);
350        memcpy(tmpbuff, buff, buff_size);
351   }
352
353   if((smpi_privatize_global_variables) && ((char*)comm->comm.dst_buff >= smpi_start_data_exe)
354       && ((char*)comm->comm.dst_buff < smpi_start_data_exe + smpi_size_data_exe )){
355        XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
356        smpi_switch_data_segment(((smpi_process_data_t)(((simdata_process_t)SIMIX_process_get_data(comm->comm.dst_proc))->data))->index);
357   }
358
359   memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
360   if (comm->comm.detached) {
361     // if this is a detached send, the source buffer was duplicated by SMPI
362     // sender to make the original buffer available to the application ASAP
363     xbt_free(buff);
364     //It seems that the request is used after the call there this should be free somewhere else but where???
365     //xbt_free(comm->comm.src_data);// inside SMPI the request is kept inside the user data and should be free
366     comm->comm.src_buff = NULL;
367   }
368
369   if(tmpbuff!=buff)xbt_free(tmpbuff);
370 }
371
372 void smpi_comm_null_copy_buffer_callback(smx_synchro_t comm, void *buff, size_t buff_size)
373 {
374   return;
375 }
376
377 static void smpi_check_options(){
378   //check correctness of MPI parameters
379
380    xbt_assert(xbt_cfg_get_int("smpi/async-small-thresh") <= xbt_cfg_get_int("smpi/send-is-detached-thresh"));
381
382    if (xbt_cfg_is_default_value("smpi/running-power")) {
383      XBT_INFO("You did not set the power of the host running the simulation.  "
384               "The timings will certainly not be accurate.  "
385               "Use the option \"--cfg=smpi/running-power:<flops>\" to set its value."
386               "Check http://simgrid.org/simgrid/latest/doc/options.html#options_smpi_bench for more information.");
387    }
388 }
389
390 int smpi_enabled(void) {
391   return process_data != NULL;
392 }
393
394 void smpi_global_init(void)
395 {
396   int i;
397   MPI_Group group;
398   char name[MAILBOX_NAME_MAXLEN];
399   int smpirun=0;
400
401   if (!MC_is_active()) {
402     global_timer = xbt_os_timer_new();
403     xbt_os_walltimer_start(global_timer);
404   }
405   if (process_count == 0){
406     process_count = SIMIX_process_count();
407     smpirun=1;
408   }
409   smpi_universe_size = process_count;
410   process_data = xbt_new0(smpi_process_data_t, process_count);
411   for (i = 0; i < process_count; i++) {
412     process_data[i]                       = xbt_new(s_smpi_process_data_t, 1);
413     //process_data[i]->index              = i;
414     process_data[i]->argc                 = NULL;
415     process_data[i]->argv                 = NULL;
416     process_data[i]->mailbox              = simcall_mbox_create(get_mailbox_name(name, i));
417     process_data[i]->mailbox_small        = simcall_mbox_create(get_mailbox_name_small(name, i));
418     process_data[i]->mailboxes_mutex      = xbt_mutex_init();
419     process_data[i]->timer                = xbt_os_timer_new();
420     if (MC_is_active())
421       MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
422     process_data[i]->comm_self            = MPI_COMM_NULL;
423     process_data[i]->comm_intra           = MPI_COMM_NULL;
424     process_data[i]->comm_world           = NULL;
425     process_data[i]->state                = SMPI_UNINITIALIZED;
426     process_data[i]->sampling             = 0;
427     process_data[i]->finalization_barrier = NULL;
428     process_data[i]->return_value         = 0;
429   }
430   //if the process was launched through smpirun script we generate a global mpi_comm_world
431   //if not, we let MPI_COMM_NULL, and the comm world will be private to each mpi instance
432   if(smpirun){
433     group = smpi_group_new(process_count);
434     MPI_COMM_WORLD = smpi_comm_new(group, NULL);
435     MPI_Attr_put(MPI_COMM_WORLD, MPI_UNIVERSE_SIZE, (void *)(MPI_Aint)process_count);
436     xbt_bar_t bar=xbt_barrier_init(process_count);
437
438     for (i = 0; i < process_count; i++) {
439       smpi_group_set_mapping(group, i, i);
440       process_data[i]->finalization_barrier = bar;
441     }
442   }
443 }
444
445 void smpi_global_destroy(void)
446 {
447   int count = smpi_process_count();
448   int i;
449
450   smpi_bench_destroy();
451   if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
452       while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
453       xbt_barrier_destroy(process_data[0]->finalization_barrier);
454   }else{
455       smpi_deployment_cleanup_instances();
456   }
457   for (i = 0; i < count; i++) {
458     if(process_data[i]->comm_self!=MPI_COMM_NULL){
459       smpi_comm_destroy(process_data[i]->comm_self);
460     }
461     if(process_data[i]->comm_intra!=MPI_COMM_NULL){
462       smpi_comm_destroy(process_data[i]->comm_intra);
463     }
464     xbt_os_timer_free(process_data[i]->timer);
465     xbt_mutex_destroy(process_data[i]->mailboxes_mutex);
466     xbt_free(process_data[i]);
467   }
468   xbt_free(process_data);
469   process_data = NULL;
470
471   if (MPI_COMM_WORLD != MPI_COMM_UNINITIALIZED){
472     smpi_comm_cleanup_smp(MPI_COMM_WORLD);
473     smpi_comm_cleanup_attributes(MPI_COMM_WORLD);
474     xbt_free(MPI_COMM_WORLD);
475   }
476
477   MPI_COMM_WORLD = MPI_COMM_NULL;
478
479   xbt_free(index_to_process_data);
480   if(smpi_privatize_global_variables)
481     smpi_destroy_global_memory_segments();
482   smpi_free_static();
483 }
484
485 #ifndef WIN32
486
487 void __attribute__ ((weak)) user_main_()
488 {
489   xbt_die("Should not be in this smpi_simulated_main");
490   return;
491 }
492
493 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
494 {
495   smpi_process_init(&argc, &argv);
496   user_main_();
497   return 0;
498 }
499
500 inline static int smpi_main_wrapper(int argc, char **argv){
501   int ret = smpi_simulated_main_(argc,argv);
502   if(ret !=0){
503     XBT_WARN("SMPI process did not return 0. Return value : %d", ret);
504     smpi_process_data()->return_value=ret;
505   }
506   return 0;
507 }
508
509 int __attribute__ ((weak)) main(int argc, char **argv)
510 {
511   return smpi_main(smpi_main_wrapper, argc, argv);
512 }
513
514 #endif
515
516 extern "C" {
517 static void smpi_init_logs(){
518
519   /* Connect log categories.  See xbt/log.c */
520
521   XBT_LOG_CONNECT(smpi);  /* Keep this line as soon as possible in this function: xbt_log_appender_file.c depends on it
522                              DO NOT connect this in XBT or so, or it will be useless to xbt_log_appender_file.c */
523   XBT_LOG_CONNECT(instr_smpi);
524   XBT_LOG_CONNECT(smpi_base);
525   XBT_LOG_CONNECT(smpi_bench);
526   XBT_LOG_CONNECT(smpi_coll);
527   XBT_LOG_CONNECT(smpi_colls);
528   XBT_LOG_CONNECT(smpi_comm);
529   XBT_LOG_CONNECT(smpi_dvfs);
530   XBT_LOG_CONNECT(smpi_group);
531   XBT_LOG_CONNECT(smpi_kernel);
532   XBT_LOG_CONNECT(smpi_mpi);
533   XBT_LOG_CONNECT(smpi_mpi_dt);
534   XBT_LOG_CONNECT(smpi_pmpi);
535   XBT_LOG_CONNECT(smpi_replay);
536   XBT_LOG_CONNECT(smpi_rma);
537 }
538 }
539
540 static void smpi_init_options(){
541   int gather_id = find_coll_description(mpi_coll_gather_description, xbt_cfg_get_string("smpi/gather"),"gather");
542     mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype, void *, int, MPI_Datatype, int, MPI_Comm))
543         mpi_coll_gather_description[gather_id].coll;
544
545     int allgather_id = find_coll_description(mpi_coll_allgather_description,
546                                              xbt_cfg_get_string("smpi/allgather"),"allgather");
547     mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype, void *, int, MPI_Datatype, MPI_Comm))
548         mpi_coll_allgather_description[allgather_id].coll;
549
550     int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
551                                               xbt_cfg_get_string("smpi/allgatherv"),"allgatherv");
552     mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *, int *, MPI_Datatype, MPI_Comm))
553         mpi_coll_allgatherv_description[allgatherv_id].coll;
554
555     int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
556                                              xbt_cfg_get_string("smpi/allreduce"),"allreduce");
557     mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm))
558         mpi_coll_allreduce_description[allreduce_id].coll;
559
560     int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
561                                             xbt_cfg_get_string("smpi/alltoall"),"alltoall");
562     mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype, void *, int, MPI_Datatype, MPI_Comm))
563         mpi_coll_alltoall_description[alltoall_id].coll;
564
565     int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
566                                              xbt_cfg_get_string("smpi/alltoallv"),"alltoallv");
567     mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype, void *, int *, int *, MPI_Datatype, MPI_Comm))
568         mpi_coll_alltoallv_description[alltoallv_id].coll;
569
570     int bcast_id = find_coll_description(mpi_coll_bcast_description, xbt_cfg_get_string("smpi/bcast"),"bcast");
571     mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm com))
572         mpi_coll_bcast_description[bcast_id].coll;
573
574     int reduce_id = find_coll_description(mpi_coll_reduce_description, xbt_cfg_get_string("smpi/reduce"),"reduce");
575     mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
576                                     MPI_Comm comm)) mpi_coll_reduce_description[reduce_id].coll;
577
578     int reduce_scatter_id =
579         find_coll_description(mpi_coll_reduce_scatter_description,
580                               xbt_cfg_get_string("smpi/reduce-scatter"),"reduce_scatter");
581     mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,MPI_Datatype dtype, MPI_Op op,
582                                            MPI_Comm comm)) mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
583
584     int scatter_id = find_coll_description(mpi_coll_scatter_description, xbt_cfg_get_string("smpi/scatter"),"scatter");
585     mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
586                                     int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm))
587         mpi_coll_scatter_description[scatter_id].coll;
588
589     int barrier_id = find_coll_description(mpi_coll_barrier_description, xbt_cfg_get_string("smpi/barrier"),"barrier");
590     mpi_coll_barrier_fun = (int (*)(MPI_Comm comm)) mpi_coll_barrier_description[barrier_id].coll;
591
592     smpi_cpu_threshold = xbt_cfg_get_double("smpi/cpu-threshold");
593     smpi_running_power = xbt_cfg_get_double("smpi/running-power");
594     smpi_privatize_global_variables = xbt_cfg_get_boolean("smpi/privatize-global-variables");
595     if (smpi_cpu_threshold < 0)
596       smpi_cpu_threshold = DBL_MAX;
597 }
598
599 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
600 {
601   srand(SMPI_RAND_SEED);
602
603   if (getenv("SMPI_PRETEND_CC") != NULL) {
604     /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the
605      * configuration tools */
606     return 0;
607   }
608   smpi_init_logs();
609
610   TRACE_global_init(&argc, argv);
611   TRACE_add_start_function(TRACE_smpi_alloc);
612   TRACE_add_end_function(TRACE_smpi_release);
613
614   SIMIX_global_init(&argc, argv);
615   MSG_init(&argc,argv);
616
617   SMPI_switch_data_segment = smpi_switch_data_segment;
618
619   smpi_init_options();
620
621   // parse the platform file: get the host list
622   SIMIX_create_environment(argv[1]);
623   SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
624   SIMIX_function_register_default(realmain);
625   SIMIX_launch_application(argv[2]);
626
627   smpi_global_init();
628
629   smpi_check_options();
630
631   if(smpi_privatize_global_variables)
632     smpi_initialize_global_memory_segments();
633
634   /* Clean IO before the run */
635   fflush(stdout);
636   fflush(stderr);
637
638   if (MC_is_active()) {
639     MC_run();
640   } else {
641   
642     SIMIX_run();
643
644     xbt_os_walltimer_stop(global_timer);
645     if (xbt_cfg_get_boolean("smpi/display-timing")){
646       double global_time = xbt_os_timer_elapsed(global_timer);
647       XBT_INFO("Simulated time: %g seconds. \n\n"
648           "The simulation took %g seconds (after parsing and platform setup)\n"
649           "%g seconds were actual computation of the application",
650           SIMIX_get_clock(), global_time , smpi_total_benched_time);
651           
652       if (smpi_total_benched_time/global_time>=0.75)
653       XBT_INFO("More than 75%% of the time was spent inside the application code.\n"
654       "You may want to use sampling functions or trace replay to reduce this.");
655     }
656   }
657   int count = smpi_process_count();
658   int i, ret=0;
659   for (i = 0; i < count; i++) {
660     if(process_data[i]->return_value!=0){
661       ret=process_data[i]->return_value;//return first non 0 value
662       break;
663     }
664   }
665   smpi_global_destroy();
666
667   TRACE_end();
668
669   return ret;
670 }
671
672 // This function can be called from extern file, to initialize logs, options, and processes of smpi
673 // without the need of smpirun
674 void SMPI_init(){
675   smpi_init_logs();
676   smpi_init_options();
677   smpi_global_init();
678   smpi_check_options();
679   if (TRACE_is_enabled() && TRACE_is_configured())
680     TRACE_smpi_alloc();
681   if(smpi_privatize_global_variables)
682     smpi_initialize_global_memory_segments();
683 }
684
685 void SMPI_finalize(){
686   smpi_global_destroy();
687 }