Logo AND Algorithmique Numérique Distribuée

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