Logo AND Algorithmique Numérique Distribuée

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