Logo AND Algorithmique Numérique Distribuée

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