Logo AND Algorithmique Numérique Distribuée

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