Logo AND Algorithmique Numérique Distribuée

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