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