Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Comm_split is back to life.
[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
9 #include "private.h"
10 #include "smpi_mpi_dt_private.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
16                                 "Logging specific to SMPI (kernel)");
17
18 typedef struct s_smpi_process_data {
19   int index;
20   xbt_fifo_t pending_sent;
21   xbt_fifo_t pending_recv;
22   xbt_os_timer_t timer;
23   double simulated;
24   MPI_Comm comm_self;
25 } s_smpi_process_data_t;
26
27 static smpi_process_data_t *process_data = NULL;
28 static int process_count = 0;
29
30 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
31
32 smpi_process_data_t smpi_process_data(void)
33 {
34   return SIMIX_process_get_data(SIMIX_process_self());
35 }
36
37 smpi_process_data_t smpi_process_remote_data(int index)
38 {
39   return process_data[index];
40 }
41
42 int smpi_process_count(void)
43 {
44   return process_count;
45 }
46
47 int smpi_process_index(void)
48 {
49   smpi_process_data_t data = smpi_process_data();
50
51   return data->index;
52 }
53
54 xbt_os_timer_t smpi_process_timer(void)
55 {
56   smpi_process_data_t data = smpi_process_data();
57
58   return data->timer;
59 }
60
61 void smpi_process_simulated_start(void)
62 {
63   smpi_process_data_t data = smpi_process_data();
64
65   data->simulated = SIMIX_get_clock();
66 }
67
68 double smpi_process_simulated_elapsed(void)
69 {
70   smpi_process_data_t data = smpi_process_data();
71
72   return SIMIX_get_clock() - data->simulated;
73 }
74
75 MPI_Comm smpi_process_comm_self(void)
76 {
77   smpi_process_data_t data = smpi_process_data();
78
79   return data->comm_self;
80 }
81
82 void print_request(const char *message, MPI_Request request)
83 {
84   char *req =
85       bprintf
86       ("[buf = %p, size = %zu, src = %d, dst = %d, tag= %d, complete = %d, flags = %u]",
87        request->buf, request->size, request->src, request->dst,
88        request->tag, request->complete, request->flags);
89
90   DEBUG5("%s  (request %p with rdv %p and match %p) %s",
91          message, request, request->rdv, request->match, req);
92   free(req);
93 }
94
95 void smpi_process_post_send(MPI_Comm comm, MPI_Request request)
96 {
97   int index = smpi_group_index(smpi_comm_group(comm), request->dst);
98   smpi_process_data_t data = smpi_process_remote_data(index);
99   xbt_fifo_item_t item;
100   MPI_Request req;
101
102   print_request("Isend", request);
103   xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
104     if (req->comm == request->comm
105         && (req->src == MPI_ANY_SOURCE || req->src == request->src)
106         && (req->tag == MPI_ANY_TAG || req->tag == request->tag)) {
107       print_request("Match found", req);
108       xbt_fifo_remove_item(data->pending_recv, item);
109       /* Materialize the *_ANY_* fields from corresponding irecv request */
110       req->src = request->src;
111       req->tag = request->tag;
112       req->match = request;
113       request->rdv = req->rdv;
114       request->match = req;
115       return;
116     }
117   }
118   request->rdv = SIMIX_rdv_create(NULL);
119   xbt_fifo_push(data->pending_sent, request);
120 }
121
122 void smpi_process_post_recv(MPI_Request request)
123 {
124   smpi_process_data_t data = smpi_process_data();
125   xbt_fifo_item_t item;
126   MPI_Request req;
127
128   print_request("Irecv", request);
129   xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
130     if (req->comm == request->comm
131         && (request->src == MPI_ANY_SOURCE || req->src == request->src)
132         && (request->tag == MPI_ANY_TAG || req->tag == request->tag)) {
133       print_request("Match found", req);
134       xbt_fifo_remove_item(data->pending_sent, item);
135       /* Materialize the *_ANY_* fields from the irecv request */
136       req->match = request;
137       request->src = req->src;
138       request->tag = req->tag;
139       request->rdv = req->rdv;
140       request->match = req;
141       return;
142     }
143   }
144   request->rdv = SIMIX_rdv_create(NULL);
145   xbt_fifo_push(data->pending_recv, request);
146 }
147
148 void smpi_global_init(void)
149 {
150   int i;
151   MPI_Group group;
152
153   SIMIX_network_set_copy_data_callback
154       (&SIMIX_network_copy_buffer_callback);
155   process_count = SIMIX_process_count();
156   process_data = xbt_new(smpi_process_data_t, process_count);
157   for (i = 0; i < process_count; i++) {
158     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
159     process_data[i]->index = i;
160     process_data[i]->pending_sent = xbt_fifo_new();
161     process_data[i]->pending_recv = xbt_fifo_new();
162     process_data[i]->timer = xbt_os_timer_new();
163     group = smpi_group_new(1);
164     process_data[i]->comm_self = smpi_comm_new(group);
165     smpi_group_set_mapping(group, i, 0);
166   }
167   group = smpi_group_new(process_count);
168   MPI_COMM_WORLD = smpi_comm_new(group);
169   for (i = 0; i < process_count; i++) {
170     smpi_group_set_mapping(group, i, i);
171   }
172 }
173
174 void smpi_global_destroy(void)
175 {
176   int count = smpi_process_count();
177   int i;
178
179   smpi_bench_destroy();
180   smpi_comm_destroy(MPI_COMM_WORLD);
181   MPI_COMM_WORLD = MPI_COMM_NULL;
182   for (i = 0; i < count; i++) {
183     smpi_comm_destroy(process_data[i]->comm_self);
184     xbt_os_timer_free(process_data[i]->timer);
185     xbt_fifo_free(process_data[i]->pending_recv);
186     xbt_fifo_free(process_data[i]->pending_sent);
187     xbt_free(process_data[i]);
188   }
189   xbt_free(process_data);
190   process_data = NULL;
191 }
192
193 int main(int argc, char **argv)
194 {
195   srand(SMPI_RAND_SEED);
196
197   double default_reference_speed = 20000.0;
198   xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
199                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
200                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL,
201                    NULL);
202
203   int default_display_timing = 0;
204   xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
205                    "Boolean indicating whether we should display the timing after simulation.",
206                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL,
207                    NULL);
208
209   int default_display_smpe = 0;
210   xbt_cfg_register(&_surf_cfg_set, "smpi/log_events",
211                    "Boolean indicating whether we should display simulated time spent in MPI calls.",
212                    xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL,
213                    NULL);
214
215   double default_threshold = 1e-6;
216   xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
217                    "Minimal computation time (in seconds) not discarded.",
218                    xbt_cfgelm_double, &default_threshold, 1, 1, NULL,
219                    NULL);
220
221 #ifdef HAVE_TRACING
222   TRACE_global_init(&argc, argv);
223 #endif
224
225   SIMIX_global_init(&argc, argv);
226
227 #ifdef HAVE_TRACING
228   TRACE_smpi_start();
229 #endif
230
231   // parse the platform file: get the host list
232   SIMIX_create_environment(argv[1]);
233
234   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
235   SIMIX_launch_application(argv[2]);
236
237   smpi_global_init();
238
239   /* Clean IO before the run */
240   fflush(stdout);
241   fflush(stderr);
242   SIMIX_init();
243
244 #ifdef HAVE_MC
245   if (_surf_do_model_check)
246     MC_modelcheck(1);
247   else
248 #endif
249     while (SIMIX_solve(NULL, NULL) != -1.0);
250
251   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
252     INFO1("simulation time %g", SIMIX_get_clock());
253
254   smpi_global_destroy();
255
256   SIMIX_message_sizes_output("toto.txt");
257
258 #ifdef HAVE_TRACING
259   TRACE_smpi_release();
260 #endif
261
262   SIMIX_clean();
263   return 0;
264 }