Logo AND Algorithmique Numérique Distribuée

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