Logo AND Algorithmique Numérique Distribuée

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