Logo AND Algorithmique Numérique Distribuée

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