Logo AND Algorithmique Numérique Distribuée

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