Logo AND Algorithmique Numérique Distribuée

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