Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2193fdc3bbb035511c9ce6491da181e1ea3a26f2
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdint.h>
2
3 #include "private.h"
4 #include "smpi_mpi_dt_private.h"
5
6 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
7
8 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
9                                 "Logging specific to SMPI (kernel)");
10
11 typedef struct s_smpi_process_data {
12   int index;
13   xbt_fifo_t pending_sent;
14   xbt_fifo_t pending_recv;
15   xbt_os_timer_t timer;
16   double simulated;
17 } s_smpi_process_data_t;
18
19 static smpi_process_data_t* process_data = NULL;
20 static int process_count = 0;
21
22 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
23
24 smpi_process_data_t smpi_process_data(void) {
25   return SIMIX_process_get_data(SIMIX_process_self());
26 }
27
28 smpi_process_data_t smpi_process_remote_data(int index) {
29   return process_data[index];
30 }
31
32 int smpi_process_count(void) {
33   return process_count;
34 }
35
36 int smpi_process_index(void) {
37   smpi_process_data_t data = smpi_process_data();
38
39   return data->index;
40 }
41
42 xbt_os_timer_t smpi_process_timer(void) {
43   smpi_process_data_t data = smpi_process_data();
44
45   return data->timer;
46 }
47
48 void smpi_process_simulated_reset(void) {
49   smpi_process_data_t data = smpi_process_data();
50
51   data->simulated = SIMIX_get_clock();
52 }
53
54 double smpi_process_simulated_elapsed(void) {
55   smpi_process_data_t data = smpi_process_data();
56
57   return SIMIX_get_clock() - data->simulated;
58 }
59
60 void smpi_process_post_send(MPI_Comm comm, MPI_Request request) {
61   int index = smpi_group_index(smpi_comm_group(comm), request->dst);
62   smpi_process_data_t data = smpi_process_remote_data(index);
63   xbt_fifo_item_t item;
64   MPI_Request req;
65
66   DEBUG4("isend for request %p [src = %d, dst = %d, tag = %d]",
67          request, request->src, request->dst, request->tag);
68   xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
69     if(req->comm == request->comm
70        && (req->src == MPI_ANY_SOURCE || req->src == request->src)
71        && (req->tag == MPI_ANY_TAG || req->tag == request->tag)){
72       DEBUG4("find matching request %p [src = %d, dst = %d, tag = %d]",
73              req, req->src, req->dst, req->tag);
74       xbt_fifo_remove_item(data->pending_recv, item);
75       /* Materialize the *_ANY_* fields from corresponding irecv request */
76       req->src = request->src;
77       req->tag = request->tag;
78       req->data = request->data;
79       request->rdv = req->rdv;
80       return;
81     } else {
82       DEBUG4("not matching request %p [src = %d, dst = %d, tag = %d]",
83              req, req->src, req->dst, req->tag);
84     }
85   }
86   request->rdv = SIMIX_rdv_create(NULL);
87   xbt_fifo_push(data->pending_sent, request);
88 }
89
90 void smpi_process_post_recv(MPI_Request request) {
91   smpi_process_data_t data = smpi_process_data();
92   xbt_fifo_item_t item;
93   MPI_Request req;
94
95   DEBUG4("irecv for request %p [src = %d, dst = %d, tag = %d]",
96          request, request->src, request->dst, request->tag);
97   xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
98     if(req->comm == request->comm
99        && (request->src == MPI_ANY_SOURCE || req->src == request->src)
100        && (request->tag == MPI_ANY_TAG || req->tag == request->tag)){
101       DEBUG4("find matching request %p [src = %d, dst = %d, tag = %d]",
102              req, req->src, req->dst, req->tag);
103       xbt_fifo_remove_item(data->pending_sent, item);
104       /* Materialize the *_ANY_* fields from the irecv request */
105       request->src = req->src;
106       request->tag = req->tag;
107       request->data = req->data;
108       request->rdv = req->rdv;
109       return;
110     } else {
111       DEBUG4("not matching request %p [src = %d, dst = %d, tag = %d]",
112              req, req->src, req->dst, req->tag);
113     }
114   }
115   request->rdv = SIMIX_rdv_create(NULL);
116   xbt_fifo_push(data->pending_recv, request);
117 }
118
119 void smpi_global_init(void) {
120   double clock = SIMIX_get_clock();
121   int i;
122   MPI_Group group;
123
124   SIMIX_network_set_copy_data_callback(&SIMIX_network_copy_buffer_callback);
125   process_count = SIMIX_process_count();
126   process_data = xbt_new(smpi_process_data_t, process_count);
127   for(i = 0; i < process_count; i++) {
128     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
129     process_data[i]->index = i;
130     process_data[i]->pending_sent = xbt_fifo_new();
131     process_data[i]->pending_recv = xbt_fifo_new();
132     process_data[i]->timer = xbt_os_timer_new();
133     process_data[i]->simulated = clock;
134   }
135   group = smpi_group_new(process_count);
136   MPI_COMM_WORLD = smpi_comm_new(group);
137   for(i = 0; i < process_count; i++) {
138     smpi_group_set_mapping(group, i, i);
139   }
140 }
141
142 void smpi_global_destroy(void) {
143   int count = smpi_process_count();
144   int i;
145
146   smpi_comm_destroy(MPI_COMM_WORLD);
147   MPI_COMM_WORLD = MPI_COMM_NULL;
148   for(i = 0; i < count; i++) {
149     xbt_os_timer_free(process_data[i]->timer);
150     xbt_fifo_free(process_data[i]->pending_recv);
151     xbt_fifo_free(process_data[i]->pending_sent);
152     xbt_free(process_data[i]);
153   }
154   xbt_free(process_data);
155   process_data = NULL;
156 }
157
158 int main(int argc, char **argv)
159 {
160   srand(SMPI_RAND_SEED);
161
162   double default_reference_speed = 20000.0;
163   xbt_cfg_register(&_surf_cfg_set, "reference_speed",
164                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
165                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL, NULL);
166
167   int default_display_timing = 0;
168   xbt_cfg_register(&_surf_cfg_set, "display_timing",
169                    "Boolean indicating whether we should display the timing after simulation.",
170                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
171
172   int default_display_smpe = 0;
173   xbt_cfg_register(&_surf_cfg_set, "SMPE",
174                    "Boolean indicating whether we should display simulated time spent in MPI calls.",
175                    xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL, NULL);
176
177   SIMIX_global_init(&argc, argv);
178
179   // parse the platform file: get the host list
180   SIMIX_create_environment(argv[1]);
181
182   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
183   SIMIX_launch_application(argv[2]);
184
185   smpi_global_init();
186
187   /* Clean IO before the run */
188   fflush(stdout);
189   fflush(stderr);
190   SIMIX_init();
191
192   while (SIMIX_solve(NULL, NULL) != -1.0);
193   
194   if (xbt_cfg_get_int(_surf_cfg_set, "display_timing"))
195     INFO1("simulation time %g", SIMIX_get_clock());
196
197   smpi_global_destroy();
198
199   SIMIX_clean();
200   return 0;
201 }