1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "smpi_mpi_dt_private.h"
14 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
17 "Logging specific to SMPI (kernel)");
19 typedef struct s_smpi_process_data {
23 xbt_fifo_t pending_sent;
24 xbt_fifo_t pending_recv;
28 } s_smpi_process_data_t;
30 static smpi_process_data_t *process_data = NULL;
31 static int process_count = 0;
33 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
35 void smpi_process_init(int *argc, char ***argv)
38 smpi_process_data_t data;
42 proc = SIMIX_process_self();
43 index = atoi((*argv)[1]);
44 data = smpi_process_remote_data(index);
45 SIMIX_process_set_data(proc, data);
48 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
49 (*argv)[(*argc) - 1] = NULL;
54 DEBUG2("<%d> New process in the game: %p", index, proc);
58 void smpi_process_destroy(void)
60 int index = smpi_process_index();
62 DEBUG1("<%d> Process left the game", index);
65 int smpi_process_argc(void) {
66 smpi_process_data_t data = smpi_process_data();
68 return data->argc ? *(data->argc) - 1 : 0;
71 int smpi_process_getarg(integer* index, char* dst, ftnlen len) {
72 smpi_process_data_t data = smpi_process_data();
76 if(!data->argc || !data->argv
77 || *index < 1 || *index >= *(data->argc)) {
80 arg = (*data->argv)[*index];
81 for(i = 0; i < len && arg[i] != '\0'; i++) {
90 int smpi_global_rank(void) {
91 return smpi_process_index();
94 int smpi_global_size(void) {
95 char* value = getenv("SMPI_GLOBAL_SIZE");
103 smpi_process_data_t smpi_process_data(void)
105 return SIMIX_process_get_data(SIMIX_process_self());
108 smpi_process_data_t smpi_process_remote_data(int index)
110 return process_data[index];
113 int smpi_process_count(void)
115 return process_count;
118 int smpi_process_index(void)
120 smpi_process_data_t data = smpi_process_data();
125 xbt_os_timer_t smpi_process_timer(void)
127 smpi_process_data_t data = smpi_process_data();
132 void smpi_process_simulated_start(void)
134 smpi_process_data_t data = smpi_process_data();
136 data->simulated = SIMIX_get_clock();
139 double smpi_process_simulated_elapsed(void)
141 smpi_process_data_t data = smpi_process_data();
143 return SIMIX_get_clock() - data->simulated;
146 MPI_Comm smpi_process_comm_self(void)
148 smpi_process_data_t data = smpi_process_data();
150 return data->comm_self;
153 void print_request(const char *message, MPI_Request request)
157 ("[buf = %p, size = %zu, src = %d, dst = %d, tag= %d, complete = %d, flags = %u]",
158 request->buf, request->size, request->src, request->dst,
159 request->tag, request->complete, request->flags);
161 DEBUG5("%s (request %p with rdv %p and match %p) %s",
162 message, request, request->rdv, request->match, req);
166 void smpi_process_post_send(MPI_Comm comm, MPI_Request request)
168 int index = smpi_group_index(smpi_comm_group(comm), request->dst);
169 smpi_process_data_t data = smpi_process_remote_data(index);
170 xbt_fifo_item_t item;
173 print_request("Isend", request);
174 xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
175 if (req->comm == request->comm
176 && (req->src == MPI_ANY_SOURCE || req->src == request->src)
177 && (req->tag == MPI_ANY_TAG || req->tag == request->tag)) {
178 print_request("Match found", req);
179 xbt_fifo_remove_item(data->pending_recv, item);
180 /* Materialize the *_ANY_* fields from corresponding irecv request */
181 req->src = request->src;
182 req->tag = request->tag;
183 req->match = request;
184 request->rdv = req->rdv;
185 request->match = req;
189 request->rdv = SIMIX_rdv_create(NULL);
190 xbt_fifo_push(data->pending_sent, request);
193 void smpi_process_post_recv(MPI_Request request)
195 smpi_process_data_t data = smpi_process_data();
196 xbt_fifo_item_t item;
199 print_request("Irecv", request);
200 xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
201 if (req->comm == request->comm
202 && (request->src == MPI_ANY_SOURCE || req->src == request->src)
203 && (request->tag == MPI_ANY_TAG || req->tag == request->tag)) {
204 print_request("Match found", req);
205 xbt_fifo_remove_item(data->pending_sent, item);
206 /* Materialize the *_ANY_* fields from the irecv request */
207 req->match = request;
208 request->src = req->src;
209 request->tag = req->tag;
210 request->rdv = req->rdv;
211 request->match = req;
215 request->rdv = SIMIX_rdv_create(NULL);
216 xbt_fifo_push(data->pending_recv, request);
219 void smpi_global_init(void)
224 SIMIX_network_set_copy_data_callback
225 (&SIMIX_network_copy_buffer_callback);
226 process_count = SIMIX_process_count();
227 process_data = xbt_new(smpi_process_data_t, process_count);
228 for (i = 0; i < process_count; i++) {
229 process_data[i] = xbt_new(s_smpi_process_data_t, 1);
230 process_data[i]->index = i;
231 process_data[i]->argc = NULL;
232 process_data[i]->argv = NULL;
233 process_data[i]->pending_sent = xbt_fifo_new();
234 process_data[i]->pending_recv = xbt_fifo_new();
235 process_data[i]->timer = xbt_os_timer_new();
236 group = smpi_group_new(1);
237 process_data[i]->comm_self = smpi_comm_new(group);
238 smpi_group_set_mapping(group, i, 0);
240 group = smpi_group_new(process_count);
241 MPI_COMM_WORLD = smpi_comm_new(group);
242 for (i = 0; i < process_count; i++) {
243 smpi_group_set_mapping(group, i, i);
247 void smpi_global_destroy(void)
249 int count = smpi_process_count();
252 smpi_bench_destroy();
253 smpi_comm_destroy(MPI_COMM_WORLD);
254 MPI_COMM_WORLD = MPI_COMM_NULL;
255 for (i = 0; i < count; i++) {
256 smpi_comm_destroy(process_data[i]->comm_self);
257 xbt_os_timer_free(process_data[i]->timer);
258 xbt_fifo_free(process_data[i]->pending_recv);
259 xbt_fifo_free(process_data[i]->pending_sent);
260 xbt_free(process_data[i]);
262 xbt_free(process_data);
266 /* Fortran specific stuff */
267 /* With smpicc, the following weak symbols are used */
268 /* With smpiff, the following weak symbols are replaced by those in libf2c */
269 int __attribute__((weak)) xargc;
270 char** __attribute__((weak)) xargv;
272 int __attribute__((weak)) main(int argc, char** argv) {
280 srand(SMPI_RAND_SEED);
282 double default_reference_speed = 20000.0;
283 xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
284 "Power of the host running the simulation (in flop/s). Used to bench the operations.",
285 xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL,
288 int default_display_timing = 0;
289 xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
290 "Boolean indicating whether we should display the timing after simulation.",
291 xbt_cfgelm_int, &default_display_timing, 1, 1, NULL,
294 int default_display_smpe = 0;
295 xbt_cfg_register(&_surf_cfg_set, "smpi/log_events",
296 "Boolean indicating whether we should display simulated time spent in MPI calls.",
297 xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL,
300 double default_threshold = 1e-6;
301 xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
302 "Minimal computation time (in seconds) not discarded.",
303 xbt_cfgelm_double, &default_threshold, 1, 1, NULL,
307 TRACE_global_init(&xargc, xargv);
310 SIMIX_global_init(&xargc, xargv);
316 // parse the platform file: get the host list
317 SIMIX_create_environment(xargv[1]);
319 SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
320 SIMIX_launch_application(xargv[2]);
324 /* Clean IO before the run */
330 if (_surf_do_model_check)
334 while (SIMIX_solve(NULL, NULL) != -1.0);
336 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
337 INFO1("simulation time %g", SIMIX_get_clock());
339 smpi_global_destroy();
341 SIMIX_message_sizes_output("toto.txt");
344 TRACE_smpi_release();