Logo AND Algorithmique Numérique Distribuée

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