Logo AND Algorithmique Numérique Distribuée

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