Logo AND Algorithmique Numérique Distribuée

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