Logo AND Algorithmique Numérique Distribuée

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