Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Direct context switching: clean the semantics of parmap
[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 <stdlib.h>
9
10 #include "private.h"
11 #include "smpi_mpi_dt_private.h"
12 #include "mc/mc.h"
13 #include "surf/surf.h"
14
15 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
18                                 "Logging specific to SMPI (kernel)");
19
20 typedef struct s_smpi_process_data {
21   int index;
22   int* argc;
23   char*** argv;
24   smx_rdv_t mailbox;
25   xbt_os_timer_t timer;
26   double simulated;
27   MPI_Comm comm_self;
28 } s_smpi_process_data_t;
29
30 static smpi_process_data_t *process_data = NULL;
31 static int process_count = 0;
32
33 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
34
35 #define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
36
37 static char* get_mailbox_name(char* str, int index) {
38   snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", (int)(sizeof(int) * 2), index);
39   return str;
40 }
41
42 void smpi_process_init(int *argc, char ***argv)
43 {
44   int index;
45   smpi_process_data_t data;
46   smx_process_t proc;
47
48   if(argc && argv) {
49     proc = SIMIX_process_self();
50     index = atoi((*argv)[1]);
51     data = smpi_process_remote_data(index);
52     SIMIX_req_process_set_data(proc, data);
53     if (*argc > 2) {
54       free((*argv)[1]);
55       memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
56       (*argv)[(*argc) - 1] = NULL;
57     }
58     (*argc)--;
59     data->argc = argc;
60     data->argv = argv;
61     XBT_DEBUG("<%d> New process in the game: %p", index, proc);
62   }
63 }
64
65 void smpi_process_destroy(void)
66 {
67   int index = smpi_process_index();
68
69   XBT_DEBUG("<%d> Process left the game", index);
70 }
71
72 int smpi_process_argc(void) {
73   smpi_process_data_t data = smpi_process_data();
74
75   return data->argc ? *(data->argc) - 1 : 0;
76 }
77
78 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
79   smpi_process_data_t data = smpi_process_data();
80   char* arg;
81   ftnlen i;
82
83   if(!data->argc || !data->argv
84      || *index < 1 || *index >= *(data->argc)) {
85     return -1;
86   }
87   arg = (*data->argv)[*index];
88   for(i = 0; i < len && arg[i] != '\0'; i++) {
89     dst[i] = arg[i];
90   }
91   for(; i < len; i++) {
92     dst[i] = ' ';
93   }
94   return 0;
95 }
96
97 int smpi_global_size(void) {
98    char* value = getenv("SMPI_GLOBAL_SIZE");
99
100    if(!value) {
101       abort();
102    }
103    return atoi(value);
104 }
105
106 smpi_process_data_t smpi_process_data(void)
107 {
108   return SIMIX_process_self_get_data();
109 }
110
111 smpi_process_data_t smpi_process_remote_data(int index)
112 {
113   return process_data[index];
114 }
115
116 int smpi_process_count(void)
117 {
118   return process_count;
119 }
120
121 int smpi_process_index(void)
122 {
123   smpi_process_data_t data = smpi_process_data();
124
125   return data->index;
126 }
127
128 smx_rdv_t smpi_process_mailbox(void) {
129   smpi_process_data_t data = smpi_process_data();
130
131   return data->mailbox;
132 }
133
134 smx_rdv_t smpi_process_remote_mailbox(int index) {
135   smpi_process_data_t data = smpi_process_remote_data(index);
136
137   return data->mailbox;
138 }
139
140 xbt_os_timer_t smpi_process_timer(void)
141 {
142   smpi_process_data_t data = smpi_process_data();
143
144   return data->timer;
145 }
146
147 void smpi_process_simulated_start(void)
148 {
149   smpi_process_data_t data = smpi_process_data();
150
151   data->simulated = SIMIX_get_clock();
152 }
153
154 double smpi_process_simulated_elapsed(void)
155 {
156   smpi_process_data_t data = smpi_process_data();
157
158   return SIMIX_get_clock() - data->simulated;
159 }
160
161 MPI_Comm smpi_process_comm_self(void)
162 {
163   smpi_process_data_t data = smpi_process_data();
164
165   return data->comm_self;
166 }
167
168 void print_request(const char *message, MPI_Request request)
169 {
170   XBT_DEBUG("%s  request %p  [buf = %p, size = %zu, src = %d, dst = %d, tag = %d, flags = %x]",
171          message, request, request->buf, request->size,
172          request->src, request->dst, request->tag, request->flags);
173 }
174
175 void smpi_global_init(void)
176 {
177   int i;
178   MPI_Group group;
179   char name[MAILBOX_NAME_MAXLEN];
180
181   SIMIX_comm_set_copy_data_callback
182       (&SIMIX_comm_copy_buffer_callback);
183   process_count = SIMIX_process_count();
184   process_data = xbt_new(smpi_process_data_t, process_count);
185   for (i = 0; i < process_count; i++) {
186     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
187     process_data[i]->index = i;
188     process_data[i]->argc = NULL;
189     process_data[i]->argv = NULL;
190     process_data[i]->mailbox = SIMIX_req_rdv_create(get_mailbox_name(name, i));
191     process_data[i]->timer = xbt_os_timer_new();
192     group = smpi_group_new(1);
193     process_data[i]->comm_self = smpi_comm_new(group);
194     smpi_group_set_mapping(group, i, 0);
195   }
196   group = smpi_group_new(process_count);
197   MPI_COMM_WORLD = smpi_comm_new(group);
198   for (i = 0; i < process_count; i++) {
199     smpi_group_set_mapping(group, i, i);
200   }
201 }
202
203 void smpi_global_destroy(void)
204 {
205   int count = smpi_process_count();
206   int i;
207
208   smpi_bench_destroy();
209   smpi_comm_destroy(MPI_COMM_WORLD);
210   MPI_COMM_WORLD = MPI_COMM_NULL;
211   for (i = 0; i < count; i++) {
212     smpi_comm_destroy(process_data[i]->comm_self);
213     xbt_os_timer_free(process_data[i]->timer);
214     SIMIX_req_rdv_destroy(process_data[i]->mailbox);
215     xbt_free(process_data[i]);
216   }
217   xbt_free(process_data);
218   process_data = NULL;
219 }
220
221 /* Fortran specific stuff */
222 /* With smpicc, the following weak symbols are used */
223 /* With smpiff, the following weak symbols are replaced by those in libf2c */
224 int __attribute__((weak)) xargc;
225 char** __attribute__((weak)) xargv;
226
227 int __attribute__((weak)) main(int argc, char** argv) {
228    xargc = argc;
229    xargv = argv;
230    return MAIN__();
231 }
232
233 int MAIN__(void)
234 {
235   srand(SMPI_RAND_SEED);
236
237   double default_reference_speed = 20000.0;
238   xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
239                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
240                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL,
241                    NULL);
242
243   int default_display_timing = 0;
244   xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
245                    "Boolean indicating whether we should display the timing after simulation.",
246                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL,
247                    NULL);
248
249   double default_threshold = 1e-6;
250   xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
251                    "Minimal computation time (in seconds) not discarded.",
252                    xbt_cfgelm_double, &default_threshold, 1, 1, NULL,
253                    NULL);
254
255   if(getenv("SMPI_PRETEND_CC") != NULL) {
256     return 0;
257   }
258
259 #ifdef HAVE_TRACING
260   TRACE_global_init(&xargc, xargv);
261 #endif
262
263   SIMIX_global_init(&xargc, xargv);
264
265 #ifdef HAVE_TRACING
266   TRACE_smpi_start();
267 #endif
268
269   // parse the platform file: get the host list
270   SIMIX_create_environment(xargv[1]);
271
272   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
273   SIMIX_launch_application(xargv[2]);
274
275   smpi_global_init();
276
277   /* Clean IO before the run */
278   fflush(stdout);
279   fflush(stderr);
280
281   if (MC_IS_ENABLED)
282     MC_modelcheck();
283   else
284     SIMIX_run();
285
286   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
287     XBT_INFO("simulation time %g", SIMIX_get_clock());
288
289   smpi_global_destroy();
290
291 #ifdef HAVE_TRACING
292   TRACE_smpi_release();
293 #endif
294
295   SIMIX_clean();
296   return 0;
297 }