Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add getter/setter functions in simix to access more cleanly the process associated...
[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
17
18 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
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   int index;
25   int* argc;
26   char*** argv;
27   smx_rdv_t mailbox;
28   smx_rdv_t mailbox_small;
29   xbt_os_timer_t timer;
30   double simulated;
31   MPI_Comm comm_self;
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 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
40
41 static char* get_mailbox_name(char* str, int index) {
42   snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
43   return str;
44 }
45
46 static char* get_mailbox_name_small(char* str, int index) {
47   snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", (int)(sizeof(int) * 2), index);
48   return str;
49 }
50
51 void smpi_process_init(int *argc, char ***argv)
52 {
53   int index;
54   smpi_process_data_t data;
55   smx_process_t proc;
56
57   if(argc && argv) {
58     proc = SIMIX_process_self();
59     index = atoi((*argv)[1]);
60     data = smpi_process_remote_data(index);
61     simcall_process_set_data(proc, data);
62     if (*argc > 2) {
63       free((*argv)[1]);
64       memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
65       (*argv)[(*argc) - 1] = NULL;
66     }
67     (*argc)--;
68     data->argc = argc;
69     data->argv = argv;
70     SIMIX_rdv_set_receiver(data->mailbox_small, proc);// set the process attached to the mailbox
71     XBT_DEBUG("<%d> New process in the game: %p", index, proc);
72   }
73 }
74
75 void smpi_process_destroy(void)
76 {
77   int index = smpi_process_index();
78
79   XBT_DEBUG("<%d> Process left the game", index);
80 }
81
82 /**
83  * @brief Prepares the current process for termination.
84  */
85 void smpi_process_finalize(void)
86 {
87   // wait for all pending asynchronous comms to finish
88   while (SIMIX_process_has_pending_comms(SIMIX_process_self())) {
89     simcall_process_sleep(0.01);
90   }
91 }
92
93 int smpi_process_argc(void) {
94   smpi_process_data_t data = smpi_process_data();
95
96   return data->argc ? *(data->argc) - 1 : 0;
97 }
98
99 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
100   smpi_process_data_t data = smpi_process_data();
101   char* arg;
102   ftnlen i;
103
104   if(!data->argc || !data->argv
105      || *index < 1 || *index >= *(data->argc)) {
106     return -1;
107   }
108   arg = (*data->argv)[*index];
109   for(i = 0; i < len && arg[i] != '\0'; i++) {
110     dst[i] = arg[i];
111   }
112   for(; i < len; i++) {
113     dst[i] = ' ';
114   }
115   return 0;
116 }
117
118 int smpi_global_size(void) {
119    char* value = getenv("SMPI_GLOBAL_SIZE");
120
121    if(!value) {
122       fprintf(stderr, "Please set env var SMPI_GLOBAL_SIZE to expected number of processes.\n");
123       abort();
124    }
125    return atoi(value);
126 }
127
128 smpi_process_data_t smpi_process_data(void)
129 {
130   return SIMIX_process_self_get_data(SIMIX_process_self());
131 }
132
133 smpi_process_data_t smpi_process_remote_data(int index)
134 {
135   return process_data[index];
136 }
137
138 int smpi_process_count(void)
139 {
140   return process_count;
141 }
142
143 int smpi_process_index(void)
144 {
145   smpi_process_data_t data = smpi_process_data();
146
147   return data->index;
148 }
149
150 smx_rdv_t smpi_process_mailbox(void) {
151   smpi_process_data_t data = smpi_process_data();
152
153   return data->mailbox;
154 }
155
156 smx_rdv_t smpi_process_mailbox_small(void) {
157   smpi_process_data_t data = smpi_process_data();
158
159   return data->mailbox_small;
160 }
161
162 smx_rdv_t smpi_process_remote_mailbox(int index) {
163   smpi_process_data_t data = smpi_process_remote_data(index);
164
165   return data->mailbox;
166 }
167
168
169 smx_rdv_t smpi_process_remote_mailbox_small(int index) {
170   smpi_process_data_t data = smpi_process_remote_data(index);
171
172   return data->mailbox_small;
173 }
174
175 xbt_os_timer_t smpi_process_timer(void)
176 {
177   smpi_process_data_t data = smpi_process_data();
178
179   return data->timer;
180 }
181
182 void smpi_process_simulated_start(void) {
183   smpi_process_data_t data = smpi_process_data();
184
185   data->simulated = SIMIX_get_clock();
186 }
187
188 double smpi_process_simulated_elapsed(void) {
189   smpi_process_data_t data = smpi_process_data();
190
191   return SIMIX_get_clock() - data->simulated;
192 }
193
194 MPI_Comm smpi_process_comm_self(void) {
195   smpi_process_data_t data = smpi_process_data();
196
197   return data->comm_self;
198 }
199
200 void print_request(const char *message, MPI_Request request) {
201   XBT_DEBUG("%s  request %p  [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
202          message, request, request->buf, request->size,
203          request->src, request->dst, request->tag, request->flags);
204 }
205
206 void smpi_global_init(void)
207 {
208   int i;
209   MPI_Group group;
210   char name[MAILBOX_NAME_MAXLEN];
211
212   SIMIX_comm_set_copy_data_callback(&smpi_comm_copy_data_callback);
213   process_count = SIMIX_process_count();
214   process_data = xbt_new(smpi_process_data_t, process_count);
215   for (i = 0; i < process_count; i++) {
216     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
217     process_data[i]->index = i;
218     process_data[i]->argc = NULL;
219     process_data[i]->argv = NULL;
220     process_data[i]->mailbox = simcall_rdv_create(get_mailbox_name(name, i));
221     process_data[i]->mailbox_small = simcall_rdv_create(get_mailbox_name_small(name, i));
222     process_data[i]->timer = xbt_os_timer_new();
223     group = smpi_group_new(1);
224     process_data[i]->comm_self = smpi_comm_new(group);
225     smpi_group_set_mapping(group, i, 0);
226   }
227   group = smpi_group_new(process_count);
228   MPI_COMM_WORLD = smpi_comm_new(group);
229   for (i = 0; i < process_count; i++) {
230     smpi_group_set_mapping(group, i, i);
231   }
232 }
233
234 void smpi_global_destroy(void)
235 {
236   int count = smpi_process_count();
237   int i;
238
239   smpi_bench_destroy();
240   smpi_comm_destroy(MPI_COMM_WORLD);
241   MPI_COMM_WORLD = MPI_COMM_NULL;
242   for (i = 0; i < count; i++) {
243     smpi_comm_destroy(process_data[i]->comm_self);
244     xbt_os_timer_free(process_data[i]->timer);
245     simcall_rdv_destroy(process_data[i]->mailbox);
246     simcall_rdv_destroy(process_data[i]->mailbox_small);
247     xbt_free(process_data[i]);
248   }
249   xbt_free(process_data);
250   process_data = NULL;
251
252   smpi_free_static();
253 }
254
255 /* Fortran specific stuff */
256 /* With smpicc, the following weak symbols are used */
257 /* With smpiff, the following weak symbols are replaced by those in libf2c */
258 int __attribute__((weak)) xargc;
259 char** __attribute__((weak)) xargv;
260
261 int __attribute__((weak)) main(int argc, char** argv) {
262    xargc = argc;
263    xargv = argv;
264    return MAIN__();
265 }
266
267 int MAIN__(void)
268 {
269   srand(SMPI_RAND_SEED);
270
271   if(getenv("SMPI_PRETEND_CC") != NULL) {
272   /* Hack to ensure that smpicc can pretend to be a simple compiler. Particularly handy to pass it to the configuration tools */
273     return 0;
274   }
275
276   /* Connect log categories.  See xbt/log.c */
277   XBT_LOG_CONNECT(smpi);
278   XBT_LOG_CONNECT(smpi_base);
279   XBT_LOG_CONNECT(smpi_bench);
280   XBT_LOG_CONNECT(smpi_coll);
281   XBT_LOG_CONNECT(smpi_comm);
282   XBT_LOG_CONNECT(smpi_group);
283   XBT_LOG_CONNECT(smpi_kernel);
284   XBT_LOG_CONNECT(smpi_mpi);
285   XBT_LOG_CONNECT(smpi_mpi_dt);
286   XBT_LOG_CONNECT(smpi_pmpi);
287
288 #ifdef HAVE_TRACING
289   TRACE_global_init(&xargc, xargv);
290 #endif
291
292   SIMIX_global_init(&xargc, xargv);
293
294 #ifdef HAVE_TRACING
295   TRACE_start();
296 #endif
297
298   // parse the platform file: get the host list
299   SIMIX_create_environment(xargv[1]);
300
301   SIMIX_function_register_default(smpi_simulated_main);
302   SIMIX_launch_application(xargv[2]);
303
304   smpi_global_init();
305
306   /* Clean IO before the run */
307   fflush(stdout);
308   fflush(stderr);
309
310   if (MC_IS_ENABLED)
311     MC_modelcheck();
312   else
313     SIMIX_run();
314
315   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
316     XBT_INFO("Simulation time: %g seconds.", SIMIX_get_clock());
317
318   smpi_global_destroy();
319
320 #ifdef HAVE_TRACING
321   TRACE_end();
322 #endif
323
324   SIMIX_clean();
325   return 0;
326 }