Logo AND Algorithmique Numérique Distribuée

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