Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6c9f39014836d8e41ed24279ebdeebd20c97d257
[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_comm_destroy(MPI_COMM_WORLD);
150   MPI_COMM_WORLD = MPI_COMM_NULL;
151   for(i = 0; i < count; i++) {
152     smpi_comm_destroy(process_data[i]->comm_self);
153     xbt_os_timer_free(process_data[i]->timer);
154     xbt_fifo_free(process_data[i]->pending_recv);
155     xbt_fifo_free(process_data[i]->pending_sent);
156     xbt_free(process_data[i]);
157   }
158   xbt_free(process_data);
159   process_data = NULL;
160 }
161
162 int main(int argc, char **argv)
163 {
164   srand(SMPI_RAND_SEED);
165
166   double default_reference_speed = 20000.0;
167   xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
168                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
169                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL, NULL);
170
171   int default_display_timing = 0;
172   xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
173                    "Boolean indicating whether we should display the timing after simulation.",
174                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
175
176   int default_display_smpe = 0;
177   xbt_cfg_register(&_surf_cfg_set, "smpi/log_events",
178                    "Boolean indicating whether we should display simulated time spent in MPI calls.",
179                    xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL, NULL);
180
181   double default_threshold = 1e-6;
182   xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
183                    "Minimal computation time (in seconds) not discarded.",
184                    xbt_cfgelm_double, &default_threshold, 1, 1, NULL, NULL);
185
186 #ifdef HAVE_TRACING
187   TRACE_global_init (&argc, argv);
188 #endif
189
190   SIMIX_global_init(&argc, argv);
191
192 #ifdef HAVE_TRACING
193   TRACE_smpi_start ();
194 #endif
195
196   // parse the platform file: get the host list
197   SIMIX_create_environment(argv[1]);
198
199   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
200   SIMIX_launch_application(argv[2]);
201
202   smpi_global_init();
203
204   /* Clean IO before the run */
205   fflush(stdout);
206   fflush(stderr);
207   SIMIX_init();
208
209 #ifdef HAVE_MC
210   if (_surf_do_model_check)
211     MC_modelcheck(1);
212   else
213 #endif
214     while (SIMIX_solve(NULL, NULL) != -1.0);
215
216   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
217     INFO1("simulation time %g", SIMIX_get_clock());
218
219   smpi_global_destroy();
220
221   SIMIX_message_sizes_output("toto.txt");
222
223 #ifdef HAVE_TRACING
224   TRACE_smpi_end ();
225 #endif
226
227   SIMIX_clean();
228   return 0;
229 }