Logo AND Algorithmique Numérique Distribuée

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