Logo AND Algorithmique Numérique Distribuée

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