Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cleanup toward persistent communications.
[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   MPI_Comm comm_self;
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 MPI_Comm smpi_process_comm_self(void) {
49   smpi_process_data_t data = smpi_process_data();
50
51   return data->comm_self;
52 }
53
54 void smpi_process_post_send(MPI_Comm comm, MPI_Request request) {
55   int index = smpi_group_index(smpi_comm_group(comm), request->dst);
56   smpi_process_data_t data = smpi_process_remote_data(index);
57   xbt_fifo_item_t item;
58   MPI_Request req;
59
60   DEBUG5("isend for request %p [src = %d, dst = %d, tag = %d, size = %zu]",
61          request, request->src, request->dst, request->tag, request->size);
62   xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
63     if(req->comm == request->comm
64        && (req->src == MPI_ANY_SOURCE || req->src == request->src)
65        && (req->tag == MPI_ANY_TAG || req->tag == request->tag)){
66       DEBUG4("find matching request %p [src = %d, dst = %d, tag = %d]",
67              req, req->src, req->dst, req->tag);
68       xbt_fifo_remove_item(data->pending_recv, item);
69       /* Materialize the *_ANY_* fields from corresponding irecv request */
70       req->src = request->src;
71       req->tag = request->tag;
72       req->match = request;
73       request->rdv = req->rdv;
74       request->match = req;
75       return;
76     } else {
77       DEBUG4("not matching request %p [src = %d, dst = %d, tag = %d]",
78              req, req->src, req->dst, req->tag);
79     }
80   }
81   request->rdv = SIMIX_rdv_create(NULL);
82   xbt_fifo_push(data->pending_sent, request);
83 }
84
85 void smpi_process_post_recv(MPI_Request request) {
86   smpi_process_data_t data = smpi_process_data();
87   xbt_fifo_item_t item;
88   MPI_Request req;
89
90   DEBUG5("irecv for request %p [src = %d, dst = %d, tag = %d, size = %zu]",
91          request, request->src, request->dst, request->tag, request->size);
92   xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
93     if(req->comm == request->comm
94        && (request->src == MPI_ANY_SOURCE || req->src == request->src)
95        && (request->tag == MPI_ANY_TAG || req->tag == request->tag)){
96       DEBUG4("find matching request %p [src = %d, dst = %d, tag = %d]",
97              req, req->src, req->dst, req->tag);
98       xbt_fifo_remove_item(data->pending_sent, item);
99       /* Materialize the *_ANY_* fields from the irecv request */
100       req->match = request;
101       request->src = req->src;
102       request->tag = req->tag;
103       request->rdv = req->rdv;
104       request->match = req;
105       return;
106     } else {
107       DEBUG4("not matching request %p [src = %d, dst = %d, tag = %d]",
108              req, req->src, req->dst, req->tag);
109     }
110   }
111   request->rdv = SIMIX_rdv_create(NULL);
112   xbt_fifo_push(data->pending_recv, request);
113 }
114
115 void smpi_global_init(void) {
116   int i;
117   MPI_Group group;
118
119   SIMIX_network_set_copy_data_callback(&SIMIX_network_copy_buffer_callback);
120   process_count = SIMIX_process_count();
121   process_data = xbt_new(smpi_process_data_t, process_count);
122   for(i = 0; i < process_count; i++) {
123     process_data[i] = xbt_new(s_smpi_process_data_t, 1);
124     process_data[i]->index = i;
125     process_data[i]->pending_sent = xbt_fifo_new();
126     process_data[i]->pending_recv = xbt_fifo_new();
127     process_data[i]->timer = xbt_os_timer_new();
128     group = smpi_group_new(1);
129     process_data[i]->comm_self = smpi_comm_new(group);
130     smpi_group_set_mapping(group, i, 0);
131   }
132   group = smpi_group_new(process_count);
133   MPI_COMM_WORLD = smpi_comm_new(group);
134   for(i = 0; i < process_count; i++) {
135     smpi_group_set_mapping(group, i, i);
136   }
137 }
138
139 void smpi_global_destroy(void) {
140   int count = smpi_process_count();
141   int i;
142
143   smpi_comm_destroy(MPI_COMM_WORLD);
144   MPI_COMM_WORLD = MPI_COMM_NULL;
145   for(i = 0; i < count; i++) {
146     smpi_comm_destroy(process_data[i]->comm_self);
147     xbt_os_timer_free(process_data[i]->timer);
148     xbt_fifo_free(process_data[i]->pending_recv);
149     xbt_fifo_free(process_data[i]->pending_sent);
150     xbt_free(process_data[i]);
151   }
152   xbt_free(process_data);
153   process_data = NULL;
154 }
155
156 int main(int argc, char **argv)
157 {
158   srand(SMPI_RAND_SEED);
159
160   double default_reference_speed = 20000.0;
161   xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
162                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
163                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL, NULL);
164
165   int default_display_timing = 0;
166   xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
167                    "Boolean indicating whether we should display the timing after simulation.",
168                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
169
170   int default_display_smpe = 0;
171   xbt_cfg_register(&_surf_cfg_set, "smpi/log_events",
172                    "Boolean indicating whether we should display simulated time spent in MPI calls.",
173                    xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL, NULL);
174
175   double default_threshold = 1e-6;
176   xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
177                    "Minimal computation time (in seconds) not discarded.",
178                    xbt_cfgelm_double, &default_threshold, 1, 1, NULL, NULL);
179
180   SIMIX_global_init(&argc, argv);
181
182   // parse the platform file: get the host list
183   SIMIX_create_environment(argv[1]);
184
185   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
186   SIMIX_launch_application(argv[2]);
187
188   smpi_global_init();
189
190   /* Clean IO before the run */
191   fflush(stdout);
192   fflush(stderr);
193   SIMIX_init();
194
195   while (SIMIX_solve(NULL, NULL) != -1.0);
196
197   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
198     INFO1("simulation time %g", SIMIX_get_clock());
199
200   smpi_global_destroy();
201
202   SIMIX_message_sizes_output("toto.txt");
203
204   SIMIX_clean();
205   return 0;
206 }