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. */
10 #include "smpi_mpi_dt_private.h"
13 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
16 "Logging specific to SMPI (kernel)");
18 typedef struct s_smpi_process_data {
20 xbt_fifo_t pending_sent;
21 xbt_fifo_t pending_recv;
25 } s_smpi_process_data_t;
27 static smpi_process_data_t *process_data = NULL;
28 static int process_count = 0;
30 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
32 smpi_process_data_t smpi_process_data(void)
34 return SIMIX_process_get_data(SIMIX_process_self());
37 smpi_process_data_t smpi_process_remote_data(int index)
39 return process_data[index];
42 int smpi_process_count(void)
47 int smpi_process_index(void)
49 smpi_process_data_t data = smpi_process_data();
54 xbt_os_timer_t smpi_process_timer(void)
56 smpi_process_data_t data = smpi_process_data();
61 void smpi_process_simulated_start(void)
63 smpi_process_data_t data = smpi_process_data();
65 data->simulated = SIMIX_get_clock();
68 double smpi_process_simulated_elapsed(void)
70 smpi_process_data_t data = smpi_process_data();
72 return SIMIX_get_clock() - data->simulated;
75 MPI_Comm smpi_process_comm_self(void)
77 smpi_process_data_t data = smpi_process_data();
79 return data->comm_self;
82 void print_request(const char *message, MPI_Request request)
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);
90 DEBUG5("%s (request %p with rdv %p and match %p) %s",
91 message, request, request->rdv, request->match, req);
95 void smpi_process_post_send(MPI_Comm comm, MPI_Request request)
97 int index = smpi_group_index(smpi_comm_group(comm), request->dst);
98 smpi_process_data_t data = smpi_process_remote_data(index);
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;
118 request->rdv = SIMIX_rdv_create(NULL);
119 xbt_fifo_push(data->pending_sent, request);
122 void smpi_process_post_recv(MPI_Request request)
124 smpi_process_data_t data = smpi_process_data();
125 xbt_fifo_item_t item;
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;
144 request->rdv = SIMIX_rdv_create(NULL);
145 xbt_fifo_push(data->pending_recv, request);
148 void smpi_global_init(void)
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);
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);
174 void smpi_global_destroy(void)
176 int count = smpi_process_count();
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]);
189 xbt_free(process_data);
193 int main(int argc, char **argv)
195 srand(SMPI_RAND_SEED);
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,
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,
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,
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,
222 TRACE_global_init(&argc, argv);
225 SIMIX_global_init(&argc, argv);
231 // parse the platform file: get the host list
232 SIMIX_create_environment(argv[1]);
234 SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
235 SIMIX_launch_application(argv[2]);
239 /* Clean IO before the run */
245 if (_surf_do_model_check)
249 while (SIMIX_solve(NULL, NULL) != -1.0);
251 if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
252 INFO1("simulation time %g", SIMIX_get_clock());
254 smpi_global_destroy();
256 SIMIX_message_sizes_output("toto.txt");
259 TRACE_smpi_release();