Logo AND Algorithmique Numérique Distribuée

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