Logo AND Algorithmique Numérique Distribuée

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