Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: kill dead code
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.h>
2
3 #include "private.h"
4
5 XBT_LOG_NEW_CATEGORY(smpi, "All SMPI categories");
6
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_kernel, smpi,
8                                 "Logging specific to SMPI (kernel)");
9
10 smpi_global_t smpi_global = NULL;
11
12 void *smpi_request_new(void);
13
14 void *smpi_request_new()
15 {
16   smpi_mpi_request_t request = xbt_new(s_smpi_mpi_request_t, 1);
17
18   request->buf = NULL;
19   request->completed = 0;
20   request->mutex = SIMIX_mutex_init();
21   request->cond = SIMIX_cond_init();
22   request->data = NULL;
23   request->forward = 0;
24
25   return request;
26 }
27
28 void smpi_request_free(void *pointer);
29
30 void smpi_request_free(void *pointer)
31 {
32
33   smpi_mpi_request_t request = pointer;
34
35   SIMIX_cond_destroy(request->cond);
36   SIMIX_mutex_destroy(request->mutex);
37   xbt_free(request);
38
39   return;
40 }
41
42 void smpi_request_reset(void *pointer);
43
44 void smpi_request_reset(void *pointer)
45 {
46   smpi_mpi_request_t request = pointer;
47
48   request->buf = NULL;
49   request->completed = 0;
50   request->data = NULL;
51   request->forward = 0;
52
53   return;
54 }
55
56
57 void *smpi_message_new(void);
58
59 void *smpi_message_new()
60 {
61   smpi_received_message_t message = xbt_new(s_smpi_received_message_t, 1);
62   message->buf = NULL;
63   return message;
64 }
65
66 void smpi_message_free(void *pointer);
67
68 void smpi_message_free(void *pointer)
69 {
70   xbt_free(pointer);
71   return;
72 }
73
74 void smpi_message_reset(void *pointer);
75
76 void smpi_message_reset(void *pointer)
77 {
78   smpi_received_message_t message = pointer;
79   message->buf = NULL;
80   return;
81 }
82
83 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t datatype,
84                         int src, int dst, int tag,
85                         smpi_mpi_communicator_t comm,
86                         smpi_mpi_request_t * requestptr)
87 {
88   int retval = MPI_SUCCESS;
89
90   smpi_mpi_request_t request = NULL;
91
92   // parameter checking prob belongs in smpi_mpi, but this is less repeat code
93   if (NULL == buf) {
94     retval = MPI_ERR_INTERN;
95   } else if (0 > count) {
96     retval = MPI_ERR_COUNT;
97   } else if (NULL == datatype) {
98     retval = MPI_ERR_TYPE;
99   } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
100     retval = MPI_ERR_RANK;
101   } else if (0 > dst || comm->size <= dst) {
102     retval = MPI_ERR_RANK;
103   } else if (MPI_ANY_TAG != tag && 0 > tag) {
104     retval = MPI_ERR_TAG;
105   } else if (NULL == comm) {
106     retval = MPI_ERR_COMM;
107   } else if (NULL == requestptr) {
108     retval = MPI_ERR_ARG;
109   } else {
110     request = xbt_mallocator_get(smpi_global->request_mallocator);
111     request->comm = comm;
112     request->src = src;
113     request->dst = dst;
114     request->tag = tag;
115     request->buf = buf;
116     request->datatype = datatype;
117     request->count = count;
118
119     *requestptr = request;
120   }
121   return retval;
122 }
123 /* FIXME: understand what they do and put the prototypes in a header file (live in smpi_base.c) */
124 void smpi_mpi_land_func(void *a, void *b, int *length,
125                         MPI_Datatype * datatype);
126 void smpi_mpi_sum_func(void *a, void *b, int *length,
127                        MPI_Datatype * datatype);
128
129 void smpi_global_init()
130 {
131   int i;
132
133   int size = SIMIX_host_get_number();
134
135   /* Connect our log channels: that must be done manually under windows */
136 #ifdef XBT_LOG_CONNECT
137   XBT_LOG_CONNECT(smpi_base, smpi);
138   XBT_LOG_CONNECT(smpi_bench, smpi);
139   XBT_LOG_CONNECT(smpi_kernel, smpi);
140   XBT_LOG_CONNECT(smpi_mpi, smpi);
141   XBT_LOG_CONNECT(smpi_receiver, smpi);
142   XBT_LOG_CONNECT(smpi_sender, smpi);
143   XBT_LOG_CONNECT(smpi_util, smpi);
144 #endif
145
146   smpi_global = xbt_new(s_smpi_global_t, 1);
147   // config variable
148   smpi_global->reference_speed = SMPI_DEFAULT_SPEED;
149
150   // host info blank until sim starts
151   // FIXME: is this okay?
152   smpi_global->hosts = NULL;
153   smpi_global->host_count = 0;
154
155   // mallocators
156   smpi_global->request_mallocator =
157     xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE, smpi_request_new,
158                        smpi_request_free, smpi_request_reset);
159   smpi_global->message_mallocator =
160     xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE, smpi_message_new,
161                        smpi_message_free, smpi_message_reset);
162
163   // sender/receiver processes
164   smpi_global->main_processes = xbt_new(smx_process_t, size);
165
166   // timers
167   smpi_global->timer = xbt_os_timer_new();
168   smpi_global->timer_mutex = SIMIX_mutex_init();
169   smpi_global->timer_cond = SIMIX_cond_init();
170
171   smpi_global->do_once_duration_nodes = NULL;
172   smpi_global->do_once_duration = NULL;
173   smpi_global->do_once_mutex = SIMIX_mutex_init();
174
175   smpi_global->hosts = SIMIX_host_get_table();
176   smpi_global->host_count = SIMIX_host_get_number();
177
178   smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
179
180   // global communicator
181   smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
182   smpi_mpi_global->mpi_comm_world->size = smpi_global->host_count;
183   smpi_mpi_global->mpi_comm_world->barrier_count = 0;
184   smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
185   smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
186   smpi_mpi_global->mpi_comm_world->rank_to_index_map =
187     xbt_new(int, smpi_global->host_count);
188   smpi_mpi_global->mpi_comm_world->index_to_rank_map =
189     xbt_new(int, smpi_global->host_count);
190   for (i = 0; i < smpi_global->host_count; i++) {
191     smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
192     smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
193   }
194
195   // mpi datatypes
196   smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1);
197   smpi_mpi_global->mpi_byte->size = (size_t) 1;
198   smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
199   smpi_mpi_global->mpi_int->size = sizeof(int);
200   smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
201   smpi_mpi_global->mpi_double->size = sizeof(double);
202
203   // mpi operations
204   smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
205   smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
206   smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
207   smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
208
209 }
210
211 void smpi_global_destroy()
212 {
213   smpi_do_once_duration_node_t curr, next;
214
215   // processes
216   xbt_free(smpi_global->main_processes);
217
218   // mallocators
219   xbt_mallocator_free(smpi_global->request_mallocator);
220   xbt_mallocator_free(smpi_global->message_mallocator);
221
222   xbt_os_timer_free(smpi_global->timer);
223   SIMIX_mutex_destroy(smpi_global->timer_mutex);
224   SIMIX_cond_destroy(smpi_global->timer_cond);
225
226   for (curr = smpi_global->do_once_duration_nodes; NULL != curr; curr = next) {
227     next = curr->next;
228     xbt_free(curr->file);
229     xbt_free(curr);
230   }
231
232   SIMIX_mutex_destroy(smpi_global->do_once_mutex);
233
234   xbt_free(smpi_global);
235   smpi_global = NULL;
236
237   /* free smpi_mpi_global */
238   SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
239   SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
240   xbt_free(smpi_mpi_global->mpi_comm_world);
241
242   xbt_free(smpi_mpi_global->mpi_byte);
243   xbt_free(smpi_mpi_global->mpi_int);
244   xbt_free(smpi_mpi_global->mpi_double);
245
246   xbt_free(smpi_mpi_global->mpi_land);
247   xbt_free(smpi_mpi_global->mpi_sum);
248
249   xbt_free(smpi_mpi_global);
250
251 }
252
253 int smpi_host_index()
254 {
255   smx_host_t host = SIMIX_host_self();
256   smpi_host_data_t hdata = (smpi_host_data_t) SIMIX_host_get_data(host);
257   return hdata->index;
258 }
259
260 smx_mutex_t smpi_host_mutex()
261 {
262   smx_host_t host = SIMIX_host_self();
263   smpi_host_data_t hdata = (smpi_host_data_t) SIMIX_host_get_data(host);
264   return hdata->mutex;
265 }
266
267 smx_cond_t smpi_host_cond()
268 {
269   smx_host_t host = SIMIX_host_self();
270   smpi_host_data_t hdata = (smpi_host_data_t) SIMIX_host_get_data(host);
271   return hdata->cond;
272 }
273
274 int smpi_run_simulation(int *argc, char **argv)
275 {
276   smx_cond_t cond = NULL;
277   smx_action_t action = NULL;
278
279   xbt_fifo_t actions_failed = xbt_fifo_new();
280   xbt_fifo_t actions_done = xbt_fifo_new();
281
282   srand(SMPI_RAND_SEED);
283
284   SIMIX_global_init(argc, argv);
285
286   // parse the platform file: get the host list
287   SIMIX_create_environment(argv[1]);
288
289   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
290   SIMIX_launch_application(argv[2]);
291
292   // must initialize globals between creating environment and launching app....
293   smpi_global_init();
294
295   /* Prepare to display some more info when dying on Ctrl-C pressing */
296   // FIXME: doesn't work
297   //signal(SIGINT, inthandler);
298
299   /* Clean IO before the run */
300   fflush(stdout);
301   fflush(stderr);
302   SIMIX_init();
303
304   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
305     while ((action = xbt_fifo_pop(actions_failed))) {
306       DEBUG1("** %s failed **", action->name);
307       while ((cond = xbt_fifo_pop(action->cond_list))) {
308         SIMIX_cond_broadcast(cond);
309       }
310     }
311     while ((action = xbt_fifo_pop(actions_done))) {
312       DEBUG1("** %s done **", action->name);
313       while ((cond = xbt_fifo_pop(action->cond_list))) {
314         SIMIX_cond_broadcast(cond);
315       }
316     }
317   }
318
319   // FIXME: cleanup incomplete
320   xbt_fifo_free(actions_failed);
321   xbt_fifo_free(actions_done);
322
323   INFO1("simulation time %g", SIMIX_get_clock());
324
325   smpi_global_destroy();
326
327   SIMIX_clean();
328
329   return 0;
330 }