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"
13 #include "surf/surf.h"
15 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
18 "Logging specific to SMPI (kernel)");
20 typedef struct s_smpi_process_data {
24 xbt_fifo_t pending_sent;
25 xbt_fifo_t pending_recv;
29 } s_smpi_process_data_t;
31 static smpi_process_data_t *process_data = NULL;
32 static int process_count = 0;
34 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
36 void smpi_process_init(int *argc, char ***argv)
39 smpi_process_data_t data;
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);
49 memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
50 (*argv)[(*argc) - 1] = NULL;
55 DEBUG2("<%d> New process in the game: %p", index, proc);
59 void smpi_process_destroy(void)
61 int index = smpi_process_index();
63 DEBUG1("<%d> Process left the game", index);
66 int smpi_process_argc(void) {
67 smpi_process_data_t data = smpi_process_data();
69 return data->argc ? *(data->argc) - 1 : 0;
72 int smpi_process_getarg(int* index, char* dst, size_t len) {
73 smpi_process_data_t data = smpi_process_data();
77 if(!data->argc || !data->argv
78 || *index < 1 || *index >= *(data->argc)) {
81 arg = (*data->argv)[*index];
82 for(i = 0; i < len && arg[i] != '\0'; i++) {
91 int smpi_global_rank(void) {
92 return smpi_process_index();
95 int smpi_global_size(void) {
96 char* value = getenv("SMPI_GLOBAL_SIZE");
104 smpi_process_data_t smpi_process_data(void)
106 return SIMIX_req_process_get_data(SIMIX_process_self());
109 smpi_process_data_t smpi_process_remote_data(int index)
111 return process_data[index];
114 int smpi_process_count(void)
116 return process_count;
119 int smpi_process_index(void)
121 smpi_process_data_t data = smpi_process_data();
126 xbt_os_timer_t smpi_process_timer(void)
128 smpi_process_data_t data = smpi_process_data();
133 void smpi_process_simulated_start(void)
135 smpi_process_data_t data = smpi_process_data();
137 data->simulated = SIMIX_get_clock();
140 double smpi_process_simulated_elapsed(void)
142 smpi_process_data_t data = smpi_process_data();
144 return SIMIX_get_clock() - data->simulated;
147 MPI_Comm smpi_process_comm_self(void)
149 smpi_process_data_t data = smpi_process_data();
151 return data->comm_self;
154 void print_request(const char *message, MPI_Request request)
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);
162 DEBUG5("%s (request %p with rdv %p and match %p) %s",
163 message, request, request->rdv, request->match, req);
167 void smpi_process_post_send(MPI_Comm comm, MPI_Request request)
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;
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;
190 request->rdv = SIMIX_req_rdv_create(NULL);
191 xbt_fifo_push(data->pending_sent, request);
194 void smpi_process_post_recv(MPI_Request request)
196 smpi_process_data_t data = smpi_process_data();
197 xbt_fifo_item_t item;
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;
216 request->rdv = SIMIX_req_rdv_create(NULL);
217 xbt_fifo_push(data->pending_recv, request);
220 void smpi_global_init(void)
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);
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);
248 void smpi_global_destroy(void)
250 int count = smpi_process_count();
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]);
263 xbt_free(process_data);
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;
273 int __attribute__((weak)) main(int argc, char** argv) {
281 srand(SMPI_RAND_SEED);
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,
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,
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,
302 TRACE_global_init(&xargc, xargv);
305 SIMIX_global_init(&xargc, xargv);
311 // parse the platform file: get the host list
312 SIMIX_create_environment(xargv[1]);
314 SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
315 SIMIX_launch_application(xargv[2]);
319 /* Clean IO before the run */
328 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
329 INFO1("simulation time %g", SIMIX_get_clock());
331 smpi_global_destroy();
334 TRACE_smpi_release();