Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
basic layer for simdag bindings ( modifyin' the return value of simulate from SD_tas...
[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
12 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
15                                 "Logging specific to SMPI (kernel)");
16
17 typedef struct s_smpi_process_data {
18   int index;
19   xbt_fifo_t pending_sent;
20   xbt_fifo_t pending_recv;
21   xbt_os_timer_t timer;
22   MPI_Comm comm_self;
23 } s_smpi_process_data_t;
24
25 static smpi_process_data_t* process_data = NULL;
26 static int process_count = 0;
27
28 MPI_Comm MPI_COMM_WORLD = MPI_COMM_NULL;
29
30 smpi_process_data_t smpi_process_data(void) {
31   return SIMIX_process_get_data(SIMIX_process_self());
32 }
33
34 smpi_process_data_t smpi_process_remote_data(int index) {
35   return process_data[index];
36 }
37
38 int smpi_process_count(void) {
39   return process_count;
40 }
41
42 int smpi_process_index(void) {
43   smpi_process_data_t data = smpi_process_data();
44
45   return data->index;
46 }
47
48 xbt_os_timer_t smpi_process_timer(void) {
49   smpi_process_data_t data = smpi_process_data();
50
51   return data->timer;
52 }
53
54 MPI_Comm smpi_process_comm_self(void) {
55   smpi_process_data_t data = smpi_process_data();
56
57   return data->comm_self;
58 }
59
60 void print_request(const char* message, MPI_Request request) {
61   char* req = bprintf("[buf = %p, size = %zu, src = %d, dst = %d, tag= %d, complete = %d, flags = %u]",
62                       request->buf, request->size, request->src, request->dst, request->tag, request->complete, request->flags);
63
64   DEBUG5("%s  (request %p with rdv %p and match %p) %s",
65          message, request, request->rdv, request->match, req);
66   free(req);
67 }
68
69 void smpi_process_post_send(MPI_Comm comm, MPI_Request request) {
70   int index = smpi_group_index(smpi_comm_group(comm), request->dst);
71   smpi_process_data_t data = smpi_process_remote_data(index);
72   xbt_fifo_item_t item;
73   MPI_Request req;
74
75   print_request("Isend", request);
76   xbt_fifo_foreach(data->pending_recv, item, req, MPI_Request) {
77     if(req->comm == request->comm
78        && (req->src == MPI_ANY_SOURCE || req->src == request->src)
79        && (req->tag == MPI_ANY_TAG || req->tag == request->tag)){
80       print_request("Match found", req);
81       xbt_fifo_remove_item(data->pending_recv, item);
82       /* Materialize the *_ANY_* fields from corresponding irecv request */
83       req->src = request->src;
84       req->tag = request->tag;
85       req->match = request;
86       request->rdv = req->rdv;
87       request->match = req;
88       return;
89     }
90   }
91   request->rdv = SIMIX_rdv_create(NULL);
92   xbt_fifo_push(data->pending_sent, request);
93 }
94
95 void smpi_process_post_recv(MPI_Request request) {
96   smpi_process_data_t data = smpi_process_data();
97   xbt_fifo_item_t item;
98   MPI_Request req;
99
100   print_request("Irecv", request);
101   xbt_fifo_foreach(data->pending_sent, item, req, MPI_Request) {
102     if(req->comm == request->comm
103        && (request->src == MPI_ANY_SOURCE || req->src == request->src)
104        && (request->tag == MPI_ANY_TAG || req->tag == request->tag)){
105       print_request("Match found", req);
106       xbt_fifo_remove_item(data->pending_sent, item);
107       /* Materialize the *_ANY_* fields from the irecv request */
108       req->match = request;
109       request->src = req->src;
110       request->tag = req->tag;
111       request->rdv = req->rdv;
112       request->match = req;
113       return;
114     }
115   }
116   request->rdv = SIMIX_rdv_create(NULL);
117   xbt_fifo_push(data->pending_recv, request);
118 }
119
120 void smpi_global_init(void) {
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     group = smpi_group_new(1);
134     process_data[i]->comm_self = smpi_comm_new(group);
135     smpi_group_set_mapping(group, i, 0);
136   }
137   group = smpi_group_new(process_count);
138   MPI_COMM_WORLD = smpi_comm_new(group);
139   for(i = 0; i < process_count; i++) {
140     smpi_group_set_mapping(group, i, i);
141   }
142 }
143
144 void smpi_global_destroy(void) {
145   int count = smpi_process_count();
146   int i;
147
148   smpi_comm_destroy(MPI_COMM_WORLD);
149   MPI_COMM_WORLD = MPI_COMM_NULL;
150   for(i = 0; i < count; i++) {
151     smpi_comm_destroy(process_data[i]->comm_self);
152     xbt_os_timer_free(process_data[i]->timer);
153     xbt_fifo_free(process_data[i]->pending_recv);
154     xbt_fifo_free(process_data[i]->pending_sent);
155     xbt_free(process_data[i]);
156   }
157   xbt_free(process_data);
158   process_data = NULL;
159 }
160
161 int main(int argc, char **argv)
162 {
163   srand(SMPI_RAND_SEED);
164
165   double default_reference_speed = 20000.0;
166   xbt_cfg_register(&_surf_cfg_set, "smpi/running_power",
167                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
168                    xbt_cfgelm_double, &default_reference_speed, 1, 1, NULL, NULL);
169
170   int default_display_timing = 0;
171   xbt_cfg_register(&_surf_cfg_set, "smpi/display_timing",
172                    "Boolean indicating whether we should display the timing after simulation.",
173                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
174
175   int default_display_smpe = 0;
176   xbt_cfg_register(&_surf_cfg_set, "smpi/log_events",
177                    "Boolean indicating whether we should display simulated time spent in MPI calls.",
178                    xbt_cfgelm_int, &default_display_smpe, 1, 1, NULL, NULL);
179
180   double default_threshold = 1e-6;
181   xbt_cfg_register(&_surf_cfg_set, "smpi/cpu_threshold",
182                    "Minimal computation time (in seconds) not discarded.",
183                    xbt_cfgelm_double, &default_threshold, 1, 1, NULL, NULL);
184
185   SIMIX_global_init(&argc, argv);
186
187   // parse the platform file: get the host list
188   SIMIX_create_environment(argv[1]);
189
190   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
191   SIMIX_launch_application(argv[2]);
192
193   smpi_global_init();
194
195   /* Clean IO before the run */
196   fflush(stdout);
197   fflush(stderr);
198   SIMIX_init();
199
200   while (SIMIX_solve(NULL, NULL) != -1.0);
201
202   if (xbt_cfg_get_int(_surf_cfg_set, "smpi/display_timing"))
203     INFO1("simulation time %g", SIMIX_get_clock());
204
205   smpi_global_destroy();
206
207   SIMIX_message_sizes_output("toto.txt");
208
209   SIMIX_clean();
210   return 0;
211 }