Logo AND Algorithmique Numérique Distribuée

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