Logo AND Algorithmique Numérique Distribuée

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