Logo AND Algorithmique Numérique Distribuée

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