Logo AND Algorithmique Numérique Distribuée

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