Logo AND Algorithmique Numérique Distribuée

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