Logo AND Algorithmique Numérique Distribuée

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