Logo AND Algorithmique Numérique Distribuée

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