Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_global.c
1 /* Copyright (c) 2007-2014. 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 "xbt/replay.h"
11 #include "surf/surf.h"
12 #include "simix/smx_private.h"
13 #include "simgrid/sg_config.h"
14
15 #include <float.h>              /* DBL_MAX */
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
21                                 "Logging specific to SMPI (kernel)");
22
23 typedef struct s_smpi_process_data {
24   double simulated;
25   int *argc;
26   char ***argv;
27   smx_rdv_t mailbox;
28   smx_rdv_t mailbox_small;
29   xbt_os_timer_t timer;
30   MPI_Comm comm_self;
31   void *data;                   /* user data */
32   int index;
33   char state;
34   int sampling;                 /* inside an SMPI_SAMPLE_ block? */
35 } s_smpi_process_data_t;
36
37 static smpi_process_data_t *process_data = NULL;
38 static int process_count = 0;
39
40 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
41 int MPI_UNIVERSE_SIZE;
42
43 MPI_Errhandler *MPI_ERRORS_RETURN = NULL;
44 MPI_Errhandler *MPI_ERRORS_ARE_FATAL = NULL;
45 MPI_Errhandler *MPI_ERRHANDLER_NULL = NULL;
46
47 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
48
49 static char *get_mailbox_name(char *str, int index)
50 {
51   snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int) (sizeof(int) * 2),
52            index);
53   return str;
54 }
55
56 static char *get_mailbox_name_small(char *str, int index)
57 {
58   snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int) (sizeof(int) * 2),
59            index);
60   return str;
61 }
62
63 void smpi_process_init(int *argc, char ***argv)
64 {
65   int index;
66   smpi_process_data_t data;
67   smx_process_t proc;
68
69   if (argc && argv) {
70     proc = SIMIX_process_self();
71     index = atoi((*argv)[1]);
72 #ifdef SMPI_F2C
73     smpi_current_rank = index;
74 #endif
75     data = smpi_process_remote_data(index);
76     simcall_process_set_data(proc, data);
77     if (*argc > 2) {
78       free((*argv)[1]);
79       memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
80       (*argv)[(*argc) - 1] = NULL;
81     }
82     (*argc)--;
83     data->argc = argc;
84     data->argv = argv;
85     // set the process attached to the mailbox
86     simcall_rdv_set_receiver(data->mailbox_small, proc);
87     XBT_DEBUG("<%d> New process in the game: %p", index, proc);
88   }
89   if (smpi_process_data() == NULL)
90     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.");
91 }
92
93 void smpi_process_destroy(void)
94 {
95   int index = smpi_process_index();
96   process_data[index]->state = SMPI_FINALIZED;
97   XBT_DEBUG("<%d> Process left the game", index);
98 }
99
100 /**
101  * @brief Prepares the current process for termination.
102  */
103 void smpi_process_finalize(void)
104 {
105 #if 0
106   // wait for all pending asynchronous comms to finish
107   while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
108     simcall_process_sleep(0.01);
109   }
110 #else
111   int i;
112   int size = smpi_comm_size(MPI_COMM_WORLD);
113   int rank = smpi_comm_rank(MPI_COMM_WORLD);
114   /* All non-root send & receive zero-length message. */
115   if (rank > 0) {
116     smpi_mpi_ssend (NULL, 0, MPI_BYTE, 0,
117                     COLL_TAG_BARRIER,
118                     MPI_COMM_WORLD);
119     smpi_mpi_recv (NULL, 0, MPI_BYTE, 0,
120                     COLL_TAG_BARRIER,
121                     MPI_COMM_WORLD, MPI_STATUS_IGNORE);
122   }
123   /* The root collects and broadcasts the messages. */
124   else {
125     MPI_Request* requests;
126     requests = (MPI_Request*)malloc( size * sizeof(MPI_Request) );
127     for (i = 1; i < size; ++i) {
128       requests[i] = smpi_mpi_irecv(NULL, 0, MPI_BYTE, MPI_ANY_SOURCE,
129                                    COLL_TAG_BARRIER, MPI_COMM_WORLD
130                                    );
131     }
132     smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
133     for (i = 1; i < size; ++i) {
134       requests[i] = smpi_mpi_issend(NULL, 0, MPI_BYTE, i,
135                                    COLL_TAG_BARRIER,
136                                    MPI_COMM_WORLD
137                                    );
138     }
139     smpi_mpi_waitall( size-1, requests+1, MPI_STATUSES_IGNORE );
140     free( requests );
141   }
142 #endif
143 }
144
145 /**
146  * @brief Check if a process is finalized
147  */
148 int smpi_process_finalized()
149 {
150   int index = smpi_process_index();
151     if (index != MPI_UNDEFINED)
152       return (process_data[index]->state == SMPI_FINALIZED);
153     else
154       return 0;
155 }
156
157 /**
158  * @brief Check if a process is initialized
159  */
160 int smpi_process_initialized(void)
161 {
162   int index = smpi_process_index();
163   return ( (index != MPI_UNDEFINED)
164           && (process_data[index]->state == SMPI_INITIALIZED));
165 }
166
167 /**
168  * @brief Mark a process as initialized (=MPI_Init called)
169  */
170 void smpi_process_mark_as_initialized(void)
171 {
172   int index = smpi_process_index();
173   if ((index != MPI_UNDEFINED) && (!process_data[index]->state != SMPI_FINALIZED))
174     process_data[index]->state = SMPI_INITIALIZED;
175 }
176
177
178 #ifdef SMPI_F2C
179 int smpi_process_argc(void)
180 {
181   smpi_process_data_t data = smpi_process_data();
182   return data->argc ? *(data->argc) - 1 : 0;
183 }
184
185 int smpi_process_getarg(integer * index, char *dst, ftnlen len)
186 {
187   smpi_process_data_t data = smpi_process_data();
188   char *arg;
189   ftnlen i;
190
191   if (!data->argc || !data->argv || *index < 1 || *index >= *(data->argc)) {
192     return -1;
193   }
194   arg = (*data->argv)[*index];
195   for (i = 0; i < len && arg[i] != '\0'; i++) {
196     dst[i] = arg[i];
197   }
198   for (; i < len; i++) {
199     dst[i] = ' ';
200   }
201   return 0;
202 }
203 #endif
204
205 int smpi_global_size(void)
206 {
207   char *value = getenv("SMPI_GLOBAL_SIZE");
208
209   if (!value) {
210     fprintf(stderr,
211             "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
212     xbt_abort();
213   }
214   return atoi(value);
215 }
216
217 smpi_process_data_t smpi_process_data(void)
218 {
219   return SIMIX_process_self_get_data(SIMIX_process_self());
220 }
221
222 smpi_process_data_t smpi_process_remote_data(int index)
223 {
224   return process_data[index];
225 }
226
227 void smpi_process_set_user_data(void *data)
228 {
229   smpi_process_data_t process_data = smpi_process_data();
230   process_data->data = data;
231 }
232
233 void *smpi_process_get_user_data()
234 {
235   smpi_process_data_t process_data = smpi_process_data();
236   return process_data->data;
237 }
238
239 int smpi_process_count(void)
240 {
241   return process_count;
242 }
243
244 int smpi_process_index(void)
245 {
246   smpi_process_data_t data = smpi_process_data();
247   //return -1 if not initialized
248   return data ? data->index : MPI_UNDEFINED;
249 }
250
251 smx_rdv_t smpi_process_mailbox(void)
252 {
253   smpi_process_data_t data = smpi_process_data();
254   return data->mailbox;
255 }
256
257 smx_rdv_t smpi_process_mailbox_small(void)
258 {
259   smpi_process_data_t data = smpi_process_data();
260   return data->mailbox_small;
261 }
262
263 smx_rdv_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
270 smx_rdv_t smpi_process_remote_mailbox_small(int index)
271 {
272   smpi_process_data_t data = smpi_process_remote_data(index);
273   return data->mailbox_small;
274 }
275
276 xbt_os_timer_t smpi_process_timer(void)
277 {
278   smpi_process_data_t data = smpi_process_data();
279   return data->timer;
280 }
281
282 void smpi_process_simulated_start(void)
283 {
284   smpi_process_data_t data = smpi_process_data();
285   data->simulated = SIMIX_get_clock();
286 }
287
288 double smpi_process_simulated_elapsed(void)
289 {
290   smpi_process_data_t data = smpi_process_data();
291   return SIMIX_get_clock() - data->simulated;
292 }
293
294 MPI_Comm smpi_process_comm_self(void)
295 {
296   smpi_process_data_t data = smpi_process_data();
297   if(data->comm_self==MPI_COMM_NULL){
298     MPI_Group group = smpi_group_new(1);
299     data->comm_self = smpi_comm_new(group);
300     smpi_group_set_mapping(group, smpi_process_index(), 0);
301   }
302
303   return data->comm_self;
304 }
305
306 void smpi_process_set_sampling(int s)
307 {
308   smpi_process_data_t data = smpi_process_data();
309   data->sampling = s;
310 }
311
312 int smpi_process_get_sampling(void)
313 {
314   smpi_process_data_t data = smpi_process_data();
315   return data->sampling;
316 }
317
318 void print_request(const char *message, MPI_Request request)
319 {
320   XBT_DEBUG
321       ("%s  request %p  [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
322        message, request, request->buf, request->size, request->src,
323        request->dst, request->tag, request->flags);
324 }
325
326 static void smpi_comm_copy_buffer_callback(smx_action_t comm,
327                                            void *buff, size_t buff_size)
328 {
329   XBT_DEBUG("Copy the data over");
330   if(_xbt_replay_is_active()) return;
331   void* tmpbuff=buff;
332
333   if((smpi_privatize_global_variables)
334       && ((char*)buff >= start_data_exe)
335       && ((char*)buff < start_data_exe + size_data_exe )
336     ){
337        XBT_WARN("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
338        switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.src_proc))->index);
339        tmpbuff = (void*)xbt_malloc(buff_size);
340        memcpy(tmpbuff, buff, buff_size);
341   }
342
343
344   if((smpi_privatize_global_variables)
345       && ((char*)comm->comm.dst_buff >= start_data_exe)
346       && ((char*)comm->comm.dst_buff < start_data_exe + size_data_exe )
347     ){
348        XBT_WARN("Privatization : We are copying to a zone inside global memory - Switch data segment");
349        switch_data_segment(((smpi_process_data_t)SIMIX_process_get_data(comm->comm.dst_proc))->index);
350   }
351
352
353   memcpy(comm->comm.dst_buff, tmpbuff, buff_size);
354   if (comm->comm.detached) {
355     // if this is a detached send, the source buffer was duplicated by SMPI
356     // sender to make the original buffer available to the application ASAP
357     xbt_free(buff);
358     //It seems that the request is used after the call there this should
359     //be free somewhereelse  but where???
360     //xbt_free(comm->comm.src_data);// inside SMPI the request is keep
361     //inside the user data and should be free 
362     comm->comm.src_buff = NULL;
363   }
364
365   if(tmpbuff!=buff)xbt_free(tmpbuff);
366
367 }
368
369 void smpi_global_init(void)
370 {
371   int i;
372   MPI_Group group;
373   char name[MAILBOX_NAME_MAXLEN];
374
375   SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_buffer_callback);
376   process_count = SIMIX_process_count();
377   process_data = xbt_new(smpi_process_data_t, process_count);
378   for (i = 0; i < process_count; i++) {
379     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
380     process_data[i]->index = i;
381     process_data[i]->argc = NULL;
382     process_data[i]->argv = NULL;
383     process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
384     process_data[i]->mailbox_small =
385         simcall_rdv_create(get_mailbox_name_small(name, i));
386     process_data[i]->timer = xbt_os_timer_new();
387     if (MC_is_active())
388       MC_ignore_heap(process_data[i]->timer, xbt_os_timer_size());
389     process_data[i]->comm_self = MPI_COMM_NULL;
390     process_data[i]->state = SMPI_UNINITIALIZED;
391     process_data[i]->sampling = 0;
392   }
393   group = smpi_group_new(process_count);
394   MPI_COMM_WORLD = smpi_comm_new(group);
395   MPI_UNIVERSE_SIZE = smpi_comm_size(MPI_COMM_WORLD);
396   for (i = 0; i < process_count; i++) {
397     smpi_group_set_mapping(group, i, i);
398   }
399
400   //check correctness of MPI parameters
401
402   xbt_assert(sg_cfg_get_int("smpi/async_small_thres") <=
403              sg_cfg_get_int("smpi/send_is_detached_thres"));
404
405   if (sg_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   if(smpi_privatize_global_variables)
412     smpi_initialize_global_memory_segments();
413 }
414
415 void smpi_global_destroy(void)
416 {
417   int count = smpi_process_count();
418   int i;
419
420   smpi_bench_destroy();
421   while (smpi_group_unuse(smpi_comm_group(MPI_COMM_WORLD)) > 0);
422   xbt_free(MPI_COMM_WORLD);
423   MPI_COMM_WORLD = MPI_COMM_NULL;
424   for (i = 0; i < count; i++) {
425     if(process_data[i]->comm_self!=MPI_COMM_NULL){
426       smpi_group_unuse(smpi_comm_group(process_data[i]->comm_self));
427       smpi_comm_destroy(process_data[i]->comm_self);
428     }
429     xbt_os_timer_free(process_data[i]->timer);
430     simcall_rdv_destroy(process_data[i]->mailbox);
431     simcall_rdv_destroy(process_data[i]->mailbox_small);
432     xbt_free(process_data[i]);
433   }
434   xbt_free(process_data);
435   process_data = NULL;
436   if(smpi_privatize_global_variables)
437     smpi_destroy_global_memory_segments();
438   smpi_free_static();
439 }
440
441 /* Fortran specific stuff */
442 /* With smpicc, the following weak symbols are used */
443 /* With smpiff, the following weak symbols are replaced by those in libf2c */
444 int __attribute__ ((weak)) xargc;
445 char ** __attribute__ ((weak)) xargv;
446
447 #ifndef WIN32
448 void __attribute__ ((weak)) user_main_()
449 {
450   xbt_die("Should not be in this smpi_simulated_main");
451   return;
452 }
453
454 int __attribute__ ((weak)) smpi_simulated_main_(int argc, char **argv)
455 {
456   smpi_process_init(&argc, &argv);
457   user_main_();
458   return 0;
459 }
460
461 int __attribute__ ((weak)) main(int argc, char **argv)
462 {
463   return smpi_main(smpi_simulated_main_, argc, argv);
464 }
465
466 int __attribute__ ((weak)) MAIN__()
467 {
468   return smpi_main(smpi_simulated_main_, xargc, xargv);
469 };
470 #endif
471
472 int smpi_main(int (*realmain) (int argc, char *argv[]), int argc, char *argv[])
473 {
474   srand(SMPI_RAND_SEED);
475
476   if (getenv("SMPI_PRETEND_CC") != NULL) {
477     /* Hack to ensure that smpicc can pretend to be a simple
478      * compiler. Particularly handy to pass it to the configuration tools */
479     return 0;
480   }
481
482   /* Connect log categories.  See xbt/log.c */
483   XBT_LOG_CONNECT(smpi);        /* Keep this line as soon as possible in this
484                                    function: xbt_log_appender_file.c depends on it
485                                    DO NOT connect this in XBT or so, or it will be
486                                    useless to xbt_log_appender_file.c */
487 #ifdef HAVE_TRACING
488   XBT_LOG_CONNECT(instr_smpi);
489 #endif
490   XBT_LOG_CONNECT(smpi_base);
491   XBT_LOG_CONNECT(smpi_bench);
492   XBT_LOG_CONNECT(smpi_coll);
493   XBT_LOG_CONNECT(smpi_colls);
494   XBT_LOG_CONNECT(smpi_comm);
495   XBT_LOG_CONNECT(smpi_dvfs);
496   XBT_LOG_CONNECT(smpi_group);
497   XBT_LOG_CONNECT(smpi_kernel);
498   XBT_LOG_CONNECT(smpi_mpi);
499   XBT_LOG_CONNECT(smpi_mpi_dt);
500   XBT_LOG_CONNECT(smpi_pmpi);
501   XBT_LOG_CONNECT(smpi_replay);
502
503 #ifdef HAVE_TRACING
504   TRACE_global_init(&argc, argv);
505
506   TRACE_add_start_function(TRACE_smpi_alloc);
507   TRACE_add_end_function(TRACE_smpi_release);
508 #endif
509
510   SIMIX_global_init(&argc, argv);
511
512 #ifdef HAVE_TRACING
513   TRACE_start();
514 #endif
515
516   // parse the platform file: get the host list
517   SIMIX_create_environment(argv[1]);
518
519   SIMIX_function_register_default(realmain);
520   SIMIX_launch_application(argv[2]);
521
522   int gather_id = find_coll_description(mpi_coll_gather_description,
523                                         sg_cfg_get_string("smpi/gather"));
524   mpi_coll_gather_fun = (int (*)(void *, int, MPI_Datatype,
525                                  void *, int, MPI_Datatype, int, MPI_Comm))
526       mpi_coll_gather_description[gather_id].coll;
527
528   int allgather_id = find_coll_description(mpi_coll_allgather_description,
529                                            sg_cfg_get_string("smpi/allgather"));
530   mpi_coll_allgather_fun = (int (*)(void *, int, MPI_Datatype,
531                                     void *, int, MPI_Datatype, MPI_Comm))
532       mpi_coll_allgather_description[allgather_id].coll;
533
534   int allgatherv_id = find_coll_description(mpi_coll_allgatherv_description,
535                                             sg_cfg_get_string("smpi/allgatherv"));
536   mpi_coll_allgatherv_fun = (int (*)(void *, int, MPI_Datatype, void *, int *,
537                                      int *, MPI_Datatype, MPI_Comm))
538       mpi_coll_allgatherv_description[allgatherv_id].coll;
539
540   int allreduce_id = find_coll_description(mpi_coll_allreduce_description,
541                                            sg_cfg_get_string("smpi/allreduce"));
542   mpi_coll_allreduce_fun = (int (*)(void *sbuf, void *rbuf, int rcount,
543                                     MPI_Datatype dtype, MPI_Op op,
544                                     MPI_Comm comm))
545       mpi_coll_allreduce_description[allreduce_id].coll;
546
547   int alltoall_id = find_coll_description(mpi_coll_alltoall_description,
548                                           sg_cfg_get_string("smpi/alltoall"));
549   mpi_coll_alltoall_fun = (int (*)(void *, int, MPI_Datatype,
550                                    void *, int, MPI_Datatype, MPI_Comm))
551       mpi_coll_alltoall_description[alltoall_id].coll;
552
553   int alltoallv_id = find_coll_description(mpi_coll_alltoallv_description,
554                                            sg_cfg_get_string("smpi/alltoallv"));
555   mpi_coll_alltoallv_fun = (int (*)(void *, int *, int *, MPI_Datatype,
556                                     void *, int *, int *, MPI_Datatype,
557                                     MPI_Comm))
558       mpi_coll_alltoallv_description[alltoallv_id].coll;
559
560   int bcast_id = find_coll_description(mpi_coll_bcast_description,
561                                        sg_cfg_get_string("smpi/bcast"));
562   mpi_coll_bcast_fun = (int (*)(void *buf, int count, MPI_Datatype datatype,
563                                 int root, MPI_Comm com))
564       mpi_coll_bcast_description[bcast_id].coll;
565
566   int reduce_id = find_coll_description(mpi_coll_reduce_description,
567                                         sg_cfg_get_string("smpi/reduce"));
568   mpi_coll_reduce_fun = (int (*)(void *buf, void *rbuf, int count,
569                                  MPI_Datatype datatype, MPI_Op op,
570                                  int root, MPI_Comm comm))
571       mpi_coll_reduce_description[reduce_id].coll;
572
573   int reduce_scatter_id =
574       find_coll_description(mpi_coll_reduce_scatter_description,
575                             sg_cfg_get_string("smpi/reduce_scatter"));
576   mpi_coll_reduce_scatter_fun = (int (*)(void *sbuf, void *rbuf, int *rcounts,
577                                          MPI_Datatype dtype, MPI_Op op,
578                                          MPI_Comm comm))
579       mpi_coll_reduce_scatter_description[reduce_scatter_id].coll;
580
581   int scatter_id = find_coll_description(mpi_coll_scatter_description,
582                                          sg_cfg_get_string("smpi/scatter"));
583   mpi_coll_scatter_fun = (int (*)(void *sendbuf, int sendcount,
584                                   MPI_Datatype sendtype, void *recvbuf,
585                                   int recvcount, MPI_Datatype recvtype,
586                                   int root, MPI_Comm comm))
587       mpi_coll_scatter_description[scatter_id].coll;
588
589   int barrier_id = find_coll_description(mpi_coll_barrier_description,
590                                          sg_cfg_get_string("smpi/barrier"));
591   mpi_coll_barrier_fun = (int (*)(MPI_Comm comm))
592       mpi_coll_barrier_description[barrier_id].coll;
593
594   smpi_cpu_threshold = sg_cfg_get_double("smpi/cpu_threshold");
595   smpi_running_power = sg_cfg_get_double("smpi/running_power");
596   smpi_privatize_global_variables = sg_cfg_get_boolean("smpi/privatize_global_variables");
597   if (smpi_cpu_threshold < 0)
598     smpi_cpu_threshold = DBL_MAX;
599
600   smpi_global_init();
601
602   /* Clean IO before the run */
603   fflush(stdout);
604   fflush(stderr);
605
606   if (MC_is_active())
607     MC_do_the_modelcheck_for_real();
608   else
609     SIMIX_run();
610
611   if (sg_cfg_get_boolean("smpi/display_timing"))
612     XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
613
614   smpi_global_destroy();
615
616 #ifdef HAVE_TRACING
617   TRACE_end();
618 #endif
619
620   return 0;
621 }