Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2c86f2ba9c7e471406410be61a3c9fbb0be321a8
[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   // config vars
157   smpi_global->reference_speed =
158     xbt_cfg_get_double(_surf_cfg_set, "reference_speed");
159
160   // mallocators
161   smpi_global->request_mallocator =
162     xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE, smpi_request_new,
163                        smpi_request_free, smpi_request_reset);
164   smpi_global->message_mallocator =
165     xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE, smpi_message_new,
166                        smpi_message_free, smpi_message_reset);
167
168   smpi_global->process_count = SIMIX_process_count();
169   DEBUG1("There is %d processes", smpi_global->process_count);
170
171   // sender/receiver processes
172   smpi_global->main_processes =
173     xbt_new(smx_process_t, smpi_global->process_count);
174
175   // timers
176   smpi_global->timer = xbt_os_timer_new();
177   smpi_global->timer_cond = SIMIX_cond_init();
178
179   smpi_global->do_once_duration_nodes = NULL;
180   smpi_global->do_once_duration = NULL;
181   smpi_global->do_once_mutex = SIMIX_mutex_init();
182
183
184   smpi_mpi_global = xbt_new(s_smpi_mpi_global_t, 1);
185
186   // global communicator
187   smpi_mpi_global->mpi_comm_world = xbt_new(s_smpi_mpi_communicator_t, 1);
188   smpi_mpi_global->mpi_comm_world->size = smpi_global->process_count;
189   smpi_mpi_global->mpi_comm_world->barrier_count = 0;
190   smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
191   smpi_mpi_global->mpi_comm_world->barrier_cond = SIMIX_cond_init();
192   smpi_mpi_global->mpi_comm_world->rank_to_index_map =
193     xbt_new(int, smpi_global->process_count);
194   smpi_mpi_global->mpi_comm_world->index_to_rank_map =
195     xbt_new(int, smpi_global->process_count);
196   for (i = 0; i < smpi_global->process_count; i++) {
197     smpi_mpi_global->mpi_comm_world->rank_to_index_map[i] = i;
198     smpi_mpi_global->mpi_comm_world->index_to_rank_map[i] = i;
199   }
200
201   // mpi datatypes
202   smpi_mpi_global->mpi_byte = xbt_new(s_smpi_mpi_datatype_t, 1); /* we can think of it as a placeholder for value*/
203   smpi_mpi_global->mpi_byte->size = (size_t) 1;
204   smpi_mpi_global->mpi_byte->lb = (ptrdiff_t) 0; 
205   smpi_mpi_global->mpi_byte->ub = smpi_mpi_global->mpi_byte->lb + smpi_mpi_global->mpi_byte->size;
206   smpi_mpi_global->mpi_byte->flags = DT_FLAG_BASIC;
207
208   smpi_mpi_global->mpi_char = xbt_new(s_smpi_mpi_datatype_t, 1);
209   smpi_mpi_global->mpi_char->size = (size_t) 1;
210   smpi_mpi_global->mpi_char->lb = (ptrdiff_t) 0; //&(smpi_mpi_global->mpi_char);
211   smpi_mpi_global->mpi_char->ub = smpi_mpi_global->mpi_char->lb + smpi_mpi_global->mpi_char->size; 
212   smpi_mpi_global->mpi_char->flags = DT_FLAG_BASIC;
213
214   smpi_mpi_global->mpi_int = xbt_new(s_smpi_mpi_datatype_t, 1);
215   smpi_mpi_global->mpi_int->size = sizeof(int);
216   smpi_mpi_global->mpi_int->lb = (ptrdiff_t) 0; // &(smpi_mpi_global->mpi_int);
217   smpi_mpi_global->mpi_int->ub = smpi_mpi_global->mpi_int->lb + smpi_mpi_global->mpi_int->size;
218   smpi_mpi_global->mpi_int->flags = DT_FLAG_BASIC;
219
220   smpi_mpi_global->mpi_float = xbt_new(s_smpi_mpi_datatype_t, 1);
221   smpi_mpi_global->mpi_float->size = sizeof(float);
222   smpi_mpi_global->mpi_float->lb = (ptrdiff_t) 0; // &(smpi_mpi_global->mpi_float);
223   smpi_mpi_global->mpi_float->ub = smpi_mpi_global->mpi_float->lb + smpi_mpi_global->mpi_float->size;
224   smpi_mpi_global->mpi_float->flags = DT_FLAG_BASIC;
225
226   smpi_mpi_global->mpi_double = xbt_new(s_smpi_mpi_datatype_t, 1);
227   smpi_mpi_global->mpi_double->size = sizeof(double);
228   smpi_mpi_global->mpi_double->lb = (ptrdiff_t) 0; //&(smpi_mpi_global->mpi_float);
229   smpi_mpi_global->mpi_double->ub = smpi_mpi_global->mpi_double->lb + smpi_mpi_global->mpi_double->size;
230   smpi_mpi_global->mpi_double->flags = DT_FLAG_BASIC;
231
232   // mpi operations
233   smpi_mpi_global->mpi_land = xbt_new(s_smpi_mpi_op_t, 1);
234   smpi_mpi_global->mpi_land->func = smpi_mpi_land_func;
235   smpi_mpi_global->mpi_sum = xbt_new(s_smpi_mpi_op_t, 1);
236   smpi_mpi_global->mpi_sum->func = smpi_mpi_sum_func;
237   smpi_mpi_global->mpi_prod = xbt_new(s_smpi_mpi_op_t, 1);
238   smpi_mpi_global->mpi_prod->func = smpi_mpi_prod_func;
239   smpi_mpi_global->mpi_min = xbt_new(s_smpi_mpi_op_t, 1);
240   smpi_mpi_global->mpi_min->func = smpi_mpi_min_func;
241   smpi_mpi_global->mpi_max = xbt_new(s_smpi_mpi_op_t, 1);
242   smpi_mpi_global->mpi_max->func = smpi_mpi_max_func;
243
244 }
245
246 void smpi_global_destroy()
247 {
248   smpi_do_once_duration_node_t curr, next;
249
250   // processes
251   xbt_free(smpi_global->main_processes);
252
253   // mallocators
254   xbt_mallocator_free(smpi_global->request_mallocator);
255   xbt_mallocator_free(smpi_global->message_mallocator);
256
257   xbt_os_timer_free(smpi_global->timer);
258   SIMIX_cond_destroy(smpi_global->timer_cond);
259
260   for (curr = smpi_global->do_once_duration_nodes; NULL != curr; curr = next) {
261     next = curr->next;
262     xbt_free(curr->file);
263     xbt_free(curr);
264   }
265
266   SIMIX_mutex_destroy(smpi_global->do_once_mutex);
267
268   xbt_free(smpi_global);
269   smpi_global = NULL;
270
271   /* free smpi_mpi_global */
272   SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
273   SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
274   xbt_free(smpi_mpi_global->mpi_comm_world->rank_to_index_map);
275   xbt_free(smpi_mpi_global->mpi_comm_world);
276
277   xbt_free(smpi_mpi_global->mpi_byte);
278   xbt_free(smpi_mpi_global->mpi_char);
279   xbt_free(smpi_mpi_global->mpi_int);
280   xbt_free(smpi_mpi_global->mpi_double);
281   xbt_free(smpi_mpi_global->mpi_float);
282
283   xbt_free(smpi_mpi_global->mpi_land);
284   xbt_free(smpi_mpi_global->mpi_sum);
285   xbt_free(smpi_mpi_global->mpi_prod);
286   xbt_free(smpi_mpi_global->mpi_max);
287   xbt_free(smpi_mpi_global->mpi_min);
288
289   xbt_free(smpi_mpi_global);
290
291 }
292
293 int smpi_process_index()
294 {
295   smpi_process_data_t pdata =
296     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
297   return pdata->index;
298 }
299
300 smx_mutex_t smpi_process_mutex()
301 {
302   smpi_process_data_t pdata =
303     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
304   return pdata->mutex;
305 }
306
307 smx_cond_t smpi_process_cond()
308 {
309   smpi_process_data_t pdata =
310     (smpi_process_data_t) SIMIX_process_get_data(SIMIX_process_self());
311   return pdata->cond;
312 }
313
314 static void smpi_cfg_cb_host_speed(const char *name, int pos)
315 {
316   if(smpi_global)
317   {
318     smpi_global->reference_speed =
319       xbt_cfg_get_double_at(_surf_cfg_set, name, pos);
320   }
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   SIMIX_global_init(argc, argv);
339
340
341   // parse the platform file: get the host list
342   SIMIX_create_environment(argv[1]);
343
344   SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
345   SIMIX_launch_application(argv[2]);
346
347   // must initialize globals between creating environment and launching app....
348   smpi_global_init();
349
350   /* Clean IO before the run */
351   fflush(stdout);
352   fflush(stderr);
353   SIMIX_init();
354
355   while (SIMIX_solve(NULL, NULL) != -1.0);
356   
357   // FIXME: cleanup incomplete
358
359   if (xbt_cfg_get_int(_surf_cfg_set, "display_timing"))
360     INFO1("simulation time %g", SIMIX_get_clock());
361
362   smpi_global_destroy();
363   SIMIX_clean();
364
365   return 0;
366 }