Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make simix headers public so that we can write external bindings
[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   xbt_fifo_t pending_sent;
25   xbt_fifo_t pending_recv;
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 void smpi_process_init(int *argc, char ***argv)
37 {
38   int index;
39   smpi_process_data_t data;
40   smx_process_t proc;
41
42   if(argc && argv) {
43     proc = SIMIX_process_self();
44     index = atoi((*argv)[1]);
45     data = smpi_process_remote_data(index);
46     SIMIX_req_process_set_data(proc, data);
47     if (*argc > 2) {
48       free((*argv)[1]);
49       memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
50       (*argv)[(*argc) - 1] = NULL;
51     }
52     (*argc)--;
53     data->argc = argc;
54     data->argv = argv;
55     DEBUG2("<%d> New process in the game: %p", index, proc);
56   }
57 }
58
59 void smpi_process_destroy(void)
60 {
61   int index = smpi_process_index();
62
63   DEBUG1("<%d> Process left the game", index);
64 }
65
66 int smpi_process_argc(void) {
67   smpi_process_data_t data = smpi_process_data();
68
69   return data->argc ? *(data->argc) - 1 : 0;
70 }
71
72 int smpi_process_getarg(int* index, char* dst, size_t len) {
73   smpi_process_data_t data = smpi_process_data();
74   char* arg;
75   size_t i;
76
77   if(!data->argc || !data->argv
78      || *index < 1 || *index >= *(data->argc)) {
79     return -1;
80   }
81   arg = (*data->argv)[*index];
82   for(i = 0; i < len && arg[i] != '\0'; i++) {
83     dst[i] = arg[i];
84   }
85   for(; i < len; i++) {
86     dst[i] = ' ';
87   }
88   return 0;
89 }
90
91 int smpi_global_rank(void) {
92    return smpi_process_index();
93 }
94
95 int smpi_global_size(void) {
96    char* value = getenv("SMPI_GLOBAL_SIZE");
97
98    if(!value) {
99       abort();
100    }
101    return atoi(value);
102 }
103
104 smpi_process_data_t smpi_process_data(void)
105 {
106   return SIMIX_req_process_get_data(SIMIX_process_self());
107 }
108
109 smpi_process_data_t smpi_process_remote_data(int index)
110 {
111   return process_data[index];
112 }
113
114 int smpi_process_count(void)
115 {
116   return process_count;
117 }
118
119 int smpi_process_index(void)
120 {
121   smpi_process_data_t data = smpi_process_data();
122
123   return data->index;
124 }
125
126 xbt_os_timer_t smpi_process_timer(void)
127 {
128   smpi_process_data_t data = smpi_process_data();
129
130   return data->timer;
131 }
132
133 void smpi_process_simulated_start(void)
134 {
135   smpi_process_data_t data = smpi_process_data();
136
137   data->simulated = SIMIX_get_clock();
138 }
139
140 double smpi_process_simulated_elapsed(void)
141 {
142   smpi_process_data_t data = smpi_process_data();
143
144   return SIMIX_get_clock() - data->simulated;
145 }
146
147 MPI_Comm smpi_process_comm_self(void)
148 {
149   smpi_process_data_t data = smpi_process_data();
150
151   return data->comm_self;
152 }
153
154 void print_request(const char *message, MPI_Request request)
155 {
156   char *req =
157       bprintf
158       ("[buf = %p, size = %zu, src = %d, dst = %d, tag= %d, complete = %d, flags = %u]",
159        request->buf, request->size, request->src, request->dst,
160        request->tag, request->complete, request->flags);
161
162   DEBUG5("%s  (request %p with rdv %p and match %p) %s",
163          message, request, request->rdv, request->match, req);
164   free(req);
165 }
166
167 void smpi_process_post_send(MPI_Comm comm, MPI_Request request)
168 {
169   int index = smpi_group_index(smpi_comm_group(comm), request->dst);
170   smpi_process_data_t data = smpi_process_remote_data(index);
171   xbt_fifo_item_t item;
172   MPI_Request req;
173
174   print_request("Isend", request);
175   xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
176     if (req->comm == request->comm
177         && (req->src == MPI_ANY_SOURCE || req->src == request->src)
178         && (req->tag == MPI_ANY_TAG || req->tag == request->tag)) {
179       print_request("Match found", req);
180       xbt_fifo_remove_item(data->pending_recv, item);
181       /* Materialize the *_ANY_* fields from corresponding irecv request */
182       req->src = request->src;
183       req->tag = request->tag;
184       req->match = request;
185       request->rdv = req->rdv;
186       request->match = req;
187       return;
188     }
189   }
190   request->rdv = SIMIX_req_rdv_create(NULL);
191   xbt_fifo_push(data->pending_sent, request);
192 }
193
194 void smpi_process_post_recv(MPI_Request request)
195 {
196   smpi_process_data_t data = smpi_process_data();
197   xbt_fifo_item_t item;
198   MPI_Request req;
199
200   print_request("Irecv", request);
201   xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
202     if (req->comm == request->comm
203         && (request->src == MPI_ANY_SOURCE || req->src == request->src)
204         && (request->tag == MPI_ANY_TAG || req->tag == request->tag)) {
205       print_request("Match found", req);
206       xbt_fifo_remove_item(data->pending_sent, item);
207       /* Materialize the *_ANY_* fields from the irecv request */
208       req->match = request;
209       request->src = req->src;
210       request->tag = req->tag;
211       request->rdv = req->rdv;
212       request->match = req;
213       return;
214     }
215   }
216   request->rdv = SIMIX_req_rdv_create(NULL);
217   xbt_fifo_push(data->pending_recv, request);
218 }
219
220 void smpi_global_init(void)
221 {
222   int i;
223   MPI_Group group;
224
225   SIMIX_comm_set_copy_data_callback
226       (&SIMIX_comm_copy_buffer_callback);
227   process_count = SIMIX_process_count();
228   process_data = xbt_new(smpi_process_data_t, process_count);
229   for (i = 0; i < process_count; i++) {
230     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
231     process_data[i]->index = i;
232     process_data[i]->argc = NULL;
233     process_data[i]->argv = NULL;
234     process_data[i]->pending_sent = xbt_fifo_new();
235     process_data[i]->pending_recv = xbt_fifo_new();
236     process_data[i]->timer = xbt_os_timer_new();
237     group = smpi_group_new(1);
238     process_data[i]->comm_self = smpi_comm_new(group);
239     smpi_group_set_mapping(group, i, 0);
240   }
241   group = smpi_group_new(process_count);
242   MPI_COMM_WORLD = smpi_comm_new(group);
243   for (i = 0; i < process_count; i++) {
244     smpi_group_set_mapping(group, i, i);
245   }
246 }
247
248 void smpi_global_destroy(void)
249 {
250   int count = smpi_process_count();
251   int i;
252
253   smpi_bench_destroy();
254   smpi_comm_destroy(MPI_COMM_WORLD);
255   MPI_COMM_WORLD = MPI_COMM_NULL;
256   for (i = 0; i < count; i++) {
257     smpi_comm_destroy(process_data[i]->comm_self);
258     xbt_os_timer_free(process_data[i]->timer);
259     xbt_fifo_free(process_data[i]->pending_recv);
260     xbt_fifo_free(process_data[i]->pending_sent);
261     xbt_free(process_data[i]);
262   }
263   xbt_free(process_data);
264   process_data = NULL;
265 }
266
267 /* Fortran specific stuff */
268 /* With smpicc, the following weak symbols are used */
269 /* With smpiff, the following weak symbols are replaced by those in libf2c */
270 int __attribute__((weak)) xargc;
271 char** __attribute__((weak)) xargv;
272
273 int __attribute__((weak)) main(int argc, char** argv) {
274    xargc = argc;
275    xargv = argv;
276    return MAIN__();
277 }
278
279 int MAIN__(void)
280 {
281   srand(SMPI_RAND_SEED);
282
283   double default_reference_speed = 20000.0;
284   xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
285                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
286                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL,
287                    NULL);
288
289   int default_display_timing = 0;
290   xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
291                    "Boolean indicating whether we should display the timing after simulation.",
292                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL,
293                    NULL);
294
295   double default_threshold = 1e-6;
296   xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
297                    "Minimal computation time (in seconds) not discarded.",
298                    xbt_cfgelm_double, &default_threshold, 1, 1, NULL,
299                    NULL);
300
301 #ifdef HAVE_TRACING
302   TRACE_global_init(&xargc, xargv);
303 #endif
304
305   SIMIX_global_init(&xargc, xargv);
306
307 #ifdef HAVE_TRACING
308   TRACE_smpi_start();
309 #endif
310
311   // parse the platform file: get the host list
312   SIMIX_create_environment(xargv[1]);
313
314   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
315   SIMIX_launch_application(xargv[2]);
316
317   smpi_global_init();
318
319   /* Clean IO before the run */
320   fflush(stdout);
321   fflush(stderr);
322
323   if (MC_IS_ENABLED)
324     MC_modelcheck();
325   else
326     SIMIX_run();
327
328   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
329     INFO1("simulation time %g", SIMIX_get_clock());
330
331   smpi_global_destroy();
332
333 #ifdef HAVE_TRACING
334   TRACE_smpi_release();
335 #endif
336
337   SIMIX_clean();
338   return 0;
339 }