Logo AND Algorithmique Numérique Distribuée

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