Logo AND Algorithmique Numérique Distribuée

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