Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
62f27d8d6e8fed484d08f3ebe728d22fa5d98d57
[simgrid.git] / src / smpi / smpi_global.c
1 #include <stdio.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 smpi_global_t smpi_global = NULL;
12
13 void *smpi_request_new(void);
14
15 void *smpi_request_new()
16 {
17   smpi_mpi_request_t request = xbt_new(s_smpi_mpi_request_t, 1);
18
19   request->buf = NULL;
20   request->completed = 0;
21   request->consumed = 0;
22   request->mutex = SIMIX_mutex_init();
23   request->cond = SIMIX_cond_init();
24   request->data = NULL;
25   request->forward = 0;
26
27   return request;
28 }
29
30 void smpi_request_free(void *pointer);
31
32 void smpi_request_free(void *pointer)
33 {
34
35   smpi_mpi_request_t request = pointer;
36
37   SIMIX_cond_destroy(request->cond);
38   SIMIX_mutex_destroy(request->mutex);
39   xbt_free(request);
40
41   return;
42 }
43
44 void smpi_request_reset(void *pointer);
45
46 void smpi_request_reset(void *pointer)
47 {
48   smpi_mpi_request_t request = pointer;
49
50   request->buf = NULL;
51   request->completed = 0;
52   request->consumed = 0;
53   request->data = NULL;
54   request->forward = 0;
55
56   return;
57 }
58
59
60 void *smpi_message_new(void);
61
62 void *smpi_message_new()
63 {
64   smpi_received_message_t message = xbt_new(s_smpi_received_message_t, 1);
65   message->buf = NULL;
66   return message;
67 }
68
69 void smpi_message_free(void *pointer);
70
71 void smpi_message_free(void *pointer)
72 {
73   xbt_free(pointer);
74   return;
75 }
76
77 void smpi_message_reset(void *pointer);
78
79 void smpi_message_reset(void *pointer)
80 {
81   smpi_received_message_t message = pointer;
82   message->buf = NULL;
83   return;
84 }
85
86 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t datatype,
87                         int src, int dst, int tag,
88                         smpi_mpi_communicator_t comm,
89                         smpi_mpi_request_t * requestptr)
90 {
91   int retval = MPI_SUCCESS;
92
93   smpi_mpi_request_t request = NULL;
94
95   // parameter checking prob belongs in smpi_mpi, but this is less repeat code
96   if (NULL == buf) {
97     retval = MPI_ERR_INTERN;
98   } else if (0 > count) {
99     retval = MPI_ERR_COUNT;
100   } else if (NULL == datatype) {
101     retval = MPI_ERR_TYPE;
102   } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
103     retval = MPI_ERR_RANK;
104   } else if (0 > dst || comm->size <= dst) {
105     retval = MPI_ERR_RANK;
106   } else if (MPI_ANY_TAG != tag && 0 > tag) {
107     retval = MPI_ERR_TAG;
108   } else if (NULL == comm) {
109     retval = MPI_ERR_COMM;
110   } else if (NULL == requestptr) {
111     retval = MPI_ERR_ARG;
112   } else {
113     request = xbt_mallocator_get(smpi_global->request_mallocator);
114     request->comm = comm;
115     request->src = src;
116     request->dst = dst;
117     request->tag = tag;
118     request->buf = buf;
119     request->datatype = datatype;
120     request->count = count;
121
122     *requestptr = request;
123   }
124   return retval;
125 }
126
127 /* FIXME: understand what they do and put the prototypes in a header file (live in smpi_base.c) */
128 void smpi_mpi_land_func(void *a, void *b, int *length,
129                         MPI_Datatype * datatype);
130 void smpi_mpi_sum_func(void *a, void *b, int *length,
131                        MPI_Datatype * datatype);
132 void smpi_mpi_prod_func(void *a, void *b, int *length,
133                        MPI_Datatype * datatype);
134 void smpi_mpi_min_func(void *a, void *b, int *length,
135                        MPI_Datatype * datatype);
136 void smpi_mpi_max_func(void *a, void *b, int *length,
137                        MPI_Datatype * datatype);
138
139 void smpi_global_init()
140 {
141   int i;
142
143   /* Connect our log channels: that must be done manually under windows */
144 #ifdef XBT_LOG_CONNECT
145   XBT_LOG_CONNECT(smpi_base, smpi);
146   XBT_LOG_CONNECT(smpi_bench, smpi);
147   XBT_LOG_CONNECT(smpi_kernel, smpi);
148   XBT_LOG_CONNECT(smpi_mpi, smpi);
149   XBT_LOG_CONNECT(smpi_receiver, smpi);
150   XBT_LOG_CONNECT(smpi_sender, smpi);
151   XBT_LOG_CONNECT(smpi_util, smpi);
152 #endif
153
154   smpi_global = xbt_new(s_smpi_global_t, 1);
155
156   // mallocators
157   smpi_global->request_mallocator =
158     xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE, smpi_request_new,
159                        smpi_request_free, smpi_request_reset);
160   smpi_global->message_mallocator =
161     xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE, smpi_message_new,
162                        smpi_message_free, smpi_message_reset);
163
164   smpi_global->process_count = SIMIX_process_count();
165   DEBUG1("There is %d processes", smpi_global->process_count);
166
167   // sender/receiver processes
168   smpi_global->main_processes =
169     xbt_new(smx_process_t, smpi_global->process_count);
170
171   // timers
172   smpi_global->timer = xbt_os_timer_new();
173   smpi_global->timer_cond = SIMIX_cond_init();
174
175   smpi_global->do_once_duration_nodes = NULL;
176   smpi_global->do_once_duration = NULL;
177   smpi_global->do_once_mutex = SIMIX_mutex_init();
178
179
180   smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
181
182   // global communicator
183   smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
184   smpi_mpi_global->mpi_comm_world->size = smpi_global->process_count;
185   smpi_mpi_global->mpi_comm_world->barrier_count = 0;
186   smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
187   smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
188   smpi_mpi_global->mpi_comm_world->rank_to_index_map =
189     xbt_new(int, smpi_global->process_count);
190   smpi_mpi_global->mpi_comm_world->index_to_rank_map =
191     xbt_new(int, smpi_global->process_count);
192   for (i = 0; i < smpi_global->process_count; i++) {
193     smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
194     smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
195   }
196
197   // mpi datatypes
198   smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1); /* we can think of it as a placeholder for value*/
199   smpi_mpi_global->mpi_byte->size = (size_t) 1;
200   smpi_mpi_global->mpi_byte->lb = (ptrdiff_t) 0; 
201   smpi_mpi_global->mpi_byte->ub = smpi_mpi_global->mpi_byte->lb + smpi_mpi_global->mpi_byte->size;
202   smpi_mpi_global->mpi_byte->flags = DT_FLAG_BASIC;
203
204   smpi_mpi_global->mpi_char = xbt_new(s_smpi_mpi_datatype_t, 1);
205   smpi_mpi_global->mpi_char->size = (size_t) 1;
206   smpi_mpi_global->mpi_char->lb = (ptrdiff_t) 0; //&(smpi_mpi_global->mpi_char);
207   smpi_mpi_global->mpi_char->ub = smpi_mpi_global->mpi_char->lb + smpi_mpi_global->mpi_char->size; 
208   smpi_mpi_global->mpi_char->flags = DT_FLAG_BASIC;
209
210   smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
211   smpi_mpi_global->mpi_int->size = sizeof(int);
212   smpi_mpi_global->mpi_int->lb = (ptrdiff_t) 0; // &(smpi_mpi_global->mpi_int);
213   smpi_mpi_global->mpi_int->ub = smpi_mpi_global->mpi_int->lb + smpi_mpi_global->mpi_int->size;
214   smpi_mpi_global->mpi_int->flags = DT_FLAG_BASIC;
215
216   smpi_mpi_global->mpi_float = xbt_new(s_smpi_mpi_datatype_t, 1);
217   smpi_mpi_global->mpi_float->size = sizeof(float);
218   smpi_mpi_global->mpi_float->lb = (ptrdiff_t) 0; // &(smpi_mpi_global->mpi_float);
219   smpi_mpi_global->mpi_float->ub = smpi_mpi_global->mpi_float->lb + smpi_mpi_global->mpi_float->size;
220   smpi_mpi_global->mpi_float->flags = DT_FLAG_BASIC;
221
222   smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
223   smpi_mpi_global->mpi_double->size = sizeof(double);
224   smpi_mpi_global->mpi_double->lb = (ptrdiff_t) 0; //&(smpi_mpi_global->mpi_float);
225   smpi_mpi_global->mpi_double->ub = smpi_mpi_global->mpi_double->lb + smpi_mpi_global->mpi_double->size;
226   smpi_mpi_global->mpi_double->flags = DT_FLAG_BASIC;
227
228   // mpi operations
229   smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
230   smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
231   smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
232   smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
233   smpi_mpi_global->mpi_prod = xbt_new(s_smpi_mpi_op_t, 1);
234   smpi_mpi_global->mpi_prod->func = smpi_mpi_prod_func;
235   smpi_mpi_global->mpi_min = xbt_new(s_smpi_mpi_op_t, 1);
236   smpi_mpi_global->mpi_min->func = smpi_mpi_min_func;
237   smpi_mpi_global->mpi_max = xbt_new(s_smpi_mpi_op_t, 1);
238   smpi_mpi_global->mpi_max->func = smpi_mpi_max_func;
239
240 }
241
242 void smpi_global_destroy()
243 {
244   smpi_do_once_duration_node_t curr, next;
245
246   // processes
247   xbt_free(smpi_global->main_processes);
248
249   // mallocators
250   xbt_mallocator_free(smpi_global->request_mallocator);
251   xbt_mallocator_free(smpi_global->message_mallocator);
252
253   xbt_os_timer_free(smpi_global->timer);
254   SIMIX_cond_destroy(smpi_global->timer_cond);
255
256   for (curr = smpi_global->do_once_duration_nodes; NULL != curr; curr = next) {
257     next = curr->next;
258     xbt_free(curr->file);
259     xbt_free(curr);
260   }
261
262   SIMIX_mutex_destroy(smpi_global->do_once_mutex);
263
264   xbt_free(smpi_global);
265   smpi_global = NULL;
266
267   /* free smpi_mpi_global */
268   SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
269   SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
270   xbt_free(smpi_mpi_global->mpi_comm_world->rank_to_index_map);
271   xbt_free(smpi_mpi_global->mpi_comm_world);
272
273   xbt_free(smpi_mpi_global->mpi_byte);
274   xbt_free(smpi_mpi_global->mpi_char);
275   xbt_free(smpi_mpi_global->mpi_int);
276   xbt_free(smpi_mpi_global->mpi_double);
277   xbt_free(smpi_mpi_global->mpi_float);
278
279   xbt_free(smpi_mpi_global->mpi_land);
280   xbt_free(smpi_mpi_global->mpi_sum);
281   xbt_free(smpi_mpi_global->mpi_prod);
282   xbt_free(smpi_mpi_global->mpi_max);
283   xbt_free(smpi_mpi_global->mpi_min);
284
285   xbt_free(smpi_mpi_global);
286
287 }
288
289 int smpi_process_index()
290 {
291   smpi_process_data_t pdata =
292     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
293   return pdata->index;
294 }
295
296 smx_mutex_t smpi_process_mutex()
297 {
298   smpi_process_data_t pdata =
299     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
300   return pdata->mutex;
301 }
302
303 smx_cond_t smpi_process_cond()
304 {
305   smpi_process_data_t pdata =
306     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
307   return pdata->cond;
308 }
309
310 static void smpi_cfg_cb_host_speed(const char *name, int pos)
311 {
312   smpi_global->reference_speed =
313     xbt_cfg_get_double_at(_surf_cfg_set, name, pos);
314 }
315
316 int smpi_run_simulation(int *argc, char **argv)
317 {
318   smx_action_t action = NULL;
319
320   xbt_fifo_t actions_failed = xbt_fifo_new();
321   xbt_fifo_t actions_done = xbt_fifo_new();
322
323   srand(SMPI_RAND_SEED);
324
325   double default_reference_speed = 20000.0;
326   xbt_cfg_register(&_surf_cfg_set, "reference_speed",
327                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
328                    xbt_cfgelm_double, &default_reference_speed, 1, 1,
329                    smpi_cfg_cb_host_speed, NULL);
330
331   int default_display_timing = 0;
332   xbt_cfg_register(&_surf_cfg_set, "display_timing",
333                    "Boolean indicating whether we should display the timing after simulation.",
334                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
335
336   SIMIX_global_init(argc, argv);
337
338
339   // parse the platform file: get the host list
340   SIMIX_create_environment(argv[1]);
341
342   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
343   SIMIX_launch_application(argv[2]);
344
345   // must initialize globals between creating environment and launching app....
346   smpi_global_init();
347
348   /* Clean IO before the run */
349   fflush(stdout);
350   fflush(stderr);
351   SIMIX_init();
352
353   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
354     while ((action = xbt_fifo_pop(actions_failed))) {
355       DEBUG1("** %s failed **", SIMIX_action_get_name(action));
356       SIMIX_action_signal_all(action);
357     }
358     while ((action = xbt_fifo_pop(actions_done))) {
359       DEBUG1("** %s done **", SIMIX_action_get_name(action));
360       SIMIX_action_signal_all(action);
361     }
362   }
363
364   // FIXME: cleanup incomplete
365   xbt_fifo_free(actions_failed);
366   xbt_fifo_free(actions_done);
367
368
369   if (xbt_cfg_get_int(_surf_cfg_set, "display_timing"))
370     INFO1("simulation time %g", SIMIX_get_clock());
371
372   smpi_global_destroy();
373   SIMIX_clean();
374
375   return 0;
376 }