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