Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Better fix for r7026.
[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_init()
140 {
141   smpi_global = xbt_new(s_smpi_global_t, 1);
142 }
143
144 void smpi_global_init()
145 {
146   int i;
147
148   /* Connect our log channels: that must be done manually under windows */
149 #ifdef XBT_LOG_CONNECT
150   XBT_LOG_CONNECT(smpi_base, smpi);
151   XBT_LOG_CONNECT(smpi_bench, smpi);
152   XBT_LOG_CONNECT(smpi_kernel, smpi);
153   XBT_LOG_CONNECT(smpi_mpi, smpi);
154   XBT_LOG_CONNECT(smpi_receiver, smpi);
155   XBT_LOG_CONNECT(smpi_sender, smpi);
156   XBT_LOG_CONNECT(smpi_util, smpi);
157 #endif
158
159   // config vars
160   smpi_global->reference_speed =
161     xbt_cfg_get_double(_surf_cfg_set, "reference_speed");
162
163   // mallocators
164   smpi_global->request_mallocator =
165     xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE, smpi_request_new,
166                        smpi_request_free, smpi_request_reset);
167   smpi_global->message_mallocator =
168     xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE, smpi_message_new,
169                        smpi_message_free, smpi_message_reset);
170
171   smpi_global->process_count = SIMIX_process_count();
172   DEBUG1("There is %d processes", smpi_global->process_count);
173
174   // sender/receiver processes
175   smpi_global->main_processes =
176     xbt_new(smx_process_t, smpi_global->process_count);
177
178   // timers
179   smpi_global->timer = xbt_os_timer_new();
180   smpi_global->timer_cond = SIMIX_cond_init();
181
182   smpi_global->do_once_duration_nodes = NULL;
183   smpi_global->do_once_duration = NULL;
184   smpi_global->do_once_mutex = SIMIX_mutex_init();
185
186
187   smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
188
189   // global communicator
190   smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
191   smpi_mpi_global->mpi_comm_world->size = smpi_global->process_count;
192   smpi_mpi_global->mpi_comm_world->barrier_count = 0;
193   smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
194   smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
195   smpi_mpi_global->mpi_comm_world->rank_to_index_map =
196     xbt_new(int, smpi_global->process_count);
197   smpi_mpi_global->mpi_comm_world->index_to_rank_map =
198     xbt_new(int, smpi_global->process_count);
199   for (i = 0; i < smpi_global->process_count; i++) {
200     smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
201     smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
202   }
203
204   // mpi datatypes
205   smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1); /* we can think of it as a placeholder for value*/
206   smpi_mpi_global->mpi_byte->size = (size_t) 1;
207   smpi_mpi_global->mpi_byte->lb = (ptrdiff_t) 0; 
208   smpi_mpi_global->mpi_byte->ub = smpi_mpi_global->mpi_byte->lb + smpi_mpi_global->mpi_byte->size;
209   smpi_mpi_global->mpi_byte->flags = DT_FLAG_BASIC;
210
211   smpi_mpi_global->mpi_char = xbt_new(s_smpi_mpi_datatype_t, 1);
212   smpi_mpi_global->mpi_char->size = (size_t) 1;
213   smpi_mpi_global->mpi_char->lb = (ptrdiff_t) 0; //&(smpi_mpi_global->mpi_char);
214   smpi_mpi_global->mpi_char->ub = smpi_mpi_global->mpi_char->lb + smpi_mpi_global->mpi_char->size; 
215   smpi_mpi_global->mpi_char->flags = DT_FLAG_BASIC;
216
217   smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
218   smpi_mpi_global->mpi_int->size = sizeof(int);
219   smpi_mpi_global->mpi_int->lb = (ptrdiff_t) 0; // &(smpi_mpi_global->mpi_int);
220   smpi_mpi_global->mpi_int->ub = smpi_mpi_global->mpi_int->lb + smpi_mpi_global->mpi_int->size;
221   smpi_mpi_global->mpi_int->flags = DT_FLAG_BASIC;
222
223   smpi_mpi_global->mpi_float = xbt_new(s_smpi_mpi_datatype_t, 1);
224   smpi_mpi_global->mpi_float->size = sizeof(float);
225   smpi_mpi_global->mpi_float->lb = (ptrdiff_t) 0; // &(smpi_mpi_global->mpi_float);
226   smpi_mpi_global->mpi_float->ub = smpi_mpi_global->mpi_float->lb + smpi_mpi_global->mpi_float->size;
227   smpi_mpi_global->mpi_float->flags = DT_FLAG_BASIC;
228
229   smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
230   smpi_mpi_global->mpi_double->size = sizeof(double);
231   smpi_mpi_global->mpi_double->lb = (ptrdiff_t) 0; //&(smpi_mpi_global->mpi_float);
232   smpi_mpi_global->mpi_double->ub = smpi_mpi_global->mpi_double->lb + smpi_mpi_global->mpi_double->size;
233   smpi_mpi_global->mpi_double->flags = DT_FLAG_BASIC;
234
235   // mpi operations
236   smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
237   smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
238   smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
239   smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
240   smpi_mpi_global->mpi_prod = xbt_new(s_smpi_mpi_op_t, 1);
241   smpi_mpi_global->mpi_prod->func = smpi_mpi_prod_func;
242   smpi_mpi_global->mpi_min = xbt_new(s_smpi_mpi_op_t, 1);
243   smpi_mpi_global->mpi_min->func = smpi_mpi_min_func;
244   smpi_mpi_global->mpi_max = xbt_new(s_smpi_mpi_op_t, 1);
245   smpi_mpi_global->mpi_max->func = smpi_mpi_max_func;
246
247 }
248
249 void smpi_global_destroy()
250 {
251   smpi_do_once_duration_node_t curr, next;
252
253   // processes
254   xbt_free(smpi_global->main_processes);
255
256   // mallocators
257   xbt_mallocator_free(smpi_global->request_mallocator);
258   xbt_mallocator_free(smpi_global->message_mallocator);
259
260   xbt_os_timer_free(smpi_global->timer);
261   SIMIX_cond_destroy(smpi_global->timer_cond);
262
263   for (curr = smpi_global->do_once_duration_nodes; NULL != curr; curr = next) {
264     next = curr->next;
265     xbt_free(curr->file);
266     xbt_free(curr);
267   }
268
269   SIMIX_mutex_destroy(smpi_global->do_once_mutex);
270
271   xbt_free(smpi_global);
272   smpi_global = NULL;
273
274   /* free smpi_mpi_global */
275   SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
276   SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
277   xbt_free(smpi_mpi_global->mpi_comm_world->rank_to_index_map);
278   xbt_free(smpi_mpi_global->mpi_comm_world);
279
280   xbt_free(smpi_mpi_global->mpi_byte);
281   xbt_free(smpi_mpi_global->mpi_char);
282   xbt_free(smpi_mpi_global->mpi_int);
283   xbt_free(smpi_mpi_global->mpi_double);
284   xbt_free(smpi_mpi_global->mpi_float);
285
286   xbt_free(smpi_mpi_global->mpi_land);
287   xbt_free(smpi_mpi_global->mpi_sum);
288   xbt_free(smpi_mpi_global->mpi_prod);
289   xbt_free(smpi_mpi_global->mpi_max);
290   xbt_free(smpi_mpi_global->mpi_min);
291
292   xbt_free(smpi_mpi_global);
293
294 }
295
296 int smpi_process_index()
297 {
298   smpi_process_data_t pdata =
299     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
300   return pdata->index;
301 }
302
303 smx_mutex_t smpi_process_mutex()
304 {
305   smpi_process_data_t pdata =
306     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
307   return pdata->mutex;
308 }
309
310 smx_cond_t smpi_process_cond()
311 {
312   smpi_process_data_t pdata =
313     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
314   return pdata->cond;
315 }
316
317 static void smpi_cfg_cb_host_speed(const char *name, int pos)
318 {
319   smpi_global->reference_speed =
320     xbt_cfg_get_double_at(_surf_cfg_set, name, pos);
321 }
322
323 int smpi_run_simulation(int *argc, char **argv)
324 {
325   srand(SMPI_RAND_SEED);
326
327   double default_reference_speed = 20000.0;
328   xbt_cfg_register(&_surf_cfg_set, "reference_speed",
329                    "Power of the host running the simulation (in flop/s). Used to bench the operations.",
330                    xbt_cfgelm_double, &default_reference_speed, 1, 1,
331                    smpi_cfg_cb_host_speed, NULL);
332
333   int default_display_timing = 0;
334   xbt_cfg_register(&_surf_cfg_set, "display_timing",
335                    "Boolean indicating whether we should display the timing after simulation.",
336                    xbt_cfgelm_int, &default_display_timing, 1, 1, NULL, NULL);
337
338   // Allocate minimal things before parsing command line arguments
339   smpi_init();
340
341   SIMIX_global_init(argc, argv);
342
343
344   // parse the platform file: get the host list
345   SIMIX_create_environment(argv[1]);
346
347   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
348   SIMIX_launch_application(argv[2]);
349
350   // must initialize globals between creating environment and launching app....
351   smpi_global_init();
352
353   /* Clean IO before the run */
354   fflush(stdout);
355   fflush(stderr);
356   SIMIX_init();
357
358   while (SIMIX_solve(NULL, NULL) != -1.0);
359   
360   // FIXME: cleanup incomplete
361
362   if (xbt_cfg_get_int(_surf_cfg_set, "display_timing"))
363     INFO1("simulation time %g", SIMIX_get_clock());
364
365   smpi_global_destroy();
366   SIMIX_clean();
367
368   return 0;
369 }
370
371 int main(int argc, char** argv)
372 {
373   return smpi_run_simulation(&argc, argv);
374 }