Logo AND Algorithmique Numérique Distribuée

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