Logo AND Algorithmique Numérique Distribuée

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