Logo AND Algorithmique Numérique Distribuée

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