Logo AND Algorithmique Numérique Distribuée

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