Logo AND Algorithmique Numérique Distribuée

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