Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b9b8d809c277d9bef1b164945a5ee5ee2d783bb
[simgrid.git] / src / smpi / src / smpi_base.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <sys/time.h>
4
5 #include "private.h"
6
7 SMPI_Global_t     smpi_global     = NULL;
8
9 SMPI_MPI_Global_t smpi_mpi_global = NULL;
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(smpi, "SMPI");
12
13 int inline smpi_mpi_comm_size(smpi_mpi_communicator_t *comm)
14 {
15         return comm->size;
16 }
17
18 // FIXME: smarter algorithm?
19 int smpi_mpi_comm_rank(smpi_mpi_communicator_t *comm, smx_host_t host)
20 {
21         int i;
22
23         for(i = comm->size - 1; i > 0 && host != comm->hosts[i]; i--);
24
25         return i;
26 }
27
28 int inline smpi_mpi_comm_rank_self(smpi_mpi_communicator_t *comm)
29 {
30         return smpi_mpi_comm_rank(comm, SIMIX_host_self());
31 }
32
33 int inline smpi_mpi_comm_world_rank_self()
34 {
35         return smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, SIMIX_host_self());
36 }
37
38 int smpi_sender(int argc, char **argv)
39 {
40         smx_mutex_t mutex;
41         smx_cond_t cond;
42         smx_process_t self;
43         smx_host_t shost;
44         int rank;
45
46         xbt_fifo_t request_queue;
47         smx_mutex_t request_queue_mutex;
48         int size;
49
50         int running_hosts_count;
51
52         smpi_mpi_request_t *request;
53
54         smx_host_t dhost;
55
56         smx_action_t communicate_action;
57
58         smpi_received_message_t *message;
59
60         int drank;
61
62         smx_process_t receiver_process;
63
64         mutex = SIMIX_mutex_init();
65         cond  = SIMIX_cond_init();
66         self  = SIMIX_process_self();
67         shost = SIMIX_host_self();
68         rank  = smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, shost);
69
70         // make sure root is done before own initialization
71         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
72         if (!smpi_global->root_ready) {
73                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
74         }
75         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
76
77         request_queue       = smpi_global->pending_send_request_queues[rank];
78         request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[rank];
79         size                = smpi_mpi_comm_size(smpi_mpi_global->mpi_comm_world);
80
81         smpi_global->sender_processes[rank] = self;
82
83         // wait for all nodes to signal initializatin complete
84         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
85         smpi_global->ready_process_count++;
86         if (smpi_global->ready_process_count < 3 * size) {
87                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
88         } else {
89                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
90         }
91         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
92
93         do {
94
95                 SIMIX_mutex_lock(request_queue_mutex);
96                 request = xbt_fifo_shift(request_queue);
97                 SIMIX_mutex_unlock(request_queue_mutex);
98
99                 if (NULL == request) {
100                         SIMIX_process_suspend(self);
101                 } else {
102                         SIMIX_mutex_lock(request->mutex);
103
104                         dhost = request->comm->hosts[request->dst];
105
106                         communicate_action = SIMIX_action_communicate(shost, dhost,
107                                 "communication", request->datatype->size * request->count * 1.0, -1.0);
108
109                         SIMIX_register_condition_to_action(communicate_action, cond);
110                         SIMIX_register_action_to_condition(communicate_action, cond);
111
112                         SIMIX_mutex_lock(mutex);
113                         SIMIX_cond_wait(cond, mutex);
114                         SIMIX_mutex_unlock(mutex);
115
116                         // copy request to appropriate received queue
117                         message       = xbt_mallocator_get(smpi_global->message_mallocator);
118                         message->comm = request->comm;
119                         message->src  = request->src;
120                         message->dst  = request->dst;
121                         message->tag  = request->tag;
122                         message->buf  = xbt_malloc(request->datatype->size * request->count);
123                         memcpy(message->buf, request->buf, request->datatype->size * request->count);
124
125                         drank = smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, dhost);
126
127                         SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[drank]);
128                         xbt_fifo_push(smpi_global->received_message_queues[drank], message);
129                         SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[drank]);
130
131                         request->completed = 1;
132
133                         SIMIX_cond_broadcast(request->cond);
134
135                         SIMIX_mutex_unlock(request->mutex);
136
137                         // wake up receiver if necessary
138                         receiver_process = smpi_global->receiver_processes[drank];
139
140                         if (SIMIX_process_is_suspended(receiver_process)) {
141                                 SIMIX_process_resume(receiver_process);
142                         }
143
144                 }
145
146                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
147                 running_hosts_count = smpi_global->running_hosts_count;
148                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
149
150         } while (0 < running_hosts_count);
151
152         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
153         smpi_global->ready_process_count--;
154         if (smpi_global->ready_process_count == 0) {
155                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
156         } else if (smpi_global->ready_process_count < 0) {
157                 // FIXME: can't happen! abort!
158         }
159         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
160
161         SIMIX_mutex_destroy(mutex);
162         SIMIX_cond_destroy(cond);
163
164         return 0;
165 }
166
167 int smpi_receiver(int argc, char **argv)
168 {
169         smx_process_t self;
170         int rank;
171
172         xbt_fifo_t request_queue;
173         smx_mutex_t request_queue_mutex;
174         xbt_fifo_t message_queue;
175         smx_mutex_t message_queue_mutex;
176         int size;
177
178         int running_hosts_count;
179
180         smpi_mpi_request_t *request;
181         smpi_received_message_t *message;
182
183         xbt_fifo_item_t request_item;
184         xbt_fifo_item_t message_item;
185
186         smx_process_t waitproc;
187
188         self  = SIMIX_process_self();
189         rank  = smpi_mpi_comm_world_rank_self();
190
191         // make sure root is done before own initialization
192         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
193         if (!smpi_global->root_ready) {
194                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
195         }
196         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
197
198         request_queue       = smpi_global->pending_recv_request_queues[rank];
199         request_queue_mutex = smpi_global->pending_recv_request_queues_mutexes[rank];
200         message_queue       = smpi_global->received_message_queues[rank];
201         message_queue_mutex = smpi_global->received_message_queues_mutexes[rank];
202         size                = smpi_mpi_comm_size(smpi_mpi_global->mpi_comm_world);
203
204         smpi_global->receiver_processes[rank] = self;
205
206         // wait for all nodes to signal initializatin complete
207         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
208         smpi_global->ready_process_count++;
209         if (smpi_global->ready_process_count < 3 * size) {
210                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
211         } else {
212                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
213         }
214         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
215
216         do {
217                 request = NULL;
218                 message = NULL;
219
220                 // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
221
222                 // FIXME: not the best way to request multiple locks...
223                 SIMIX_mutex_lock(request_queue_mutex);
224                 SIMIX_mutex_lock(message_queue_mutex);
225                 for (request_item = xbt_fifo_get_first_item(request_queue);
226                         NULL != request_item;
227                         request_item = xbt_fifo_get_next_item(request_item)) {
228                         request = xbt_fifo_get_item_content(request_item);
229                         for (message_item = xbt_fifo_get_first_item(message_queue);
230                                 NULL != message_item;
231                                 message_item = xbt_fifo_get_next_item(message_item)) {
232                                 message = xbt_fifo_get_item_content(message_item);
233                                 if (request->comm == message->comm &&
234                                                 (MPI_ANY_SOURCE == request->src || request->src == message->src) &&
235                                                 request->tag == message->tag) {
236                                         xbt_fifo_remove_item(request_queue, request_item);
237                                         xbt_fifo_remove_item(message_queue, message_item);
238                                         goto stopsearch;
239                                 }
240                         }
241                 }
242 stopsearch:
243                 SIMIX_mutex_unlock(message_queue_mutex);
244                 SIMIX_mutex_unlock(request_queue_mutex);
245
246                 if (NULL == request || NULL == message) {
247                         SIMIX_process_suspend(self);
248                 } else {
249                         SIMIX_mutex_lock(request->mutex);
250
251                         memcpy(request->buf, message->buf, request->datatype->size * request->count);
252                         request->src = message->src;
253                         request->completed = 1;
254                         SIMIX_broadcast(request->cond);
255
256                         SIMIX_mutex_unlock(request->mutex);
257
258                         xbt_free(message->buf);
259                         xbt_mallocator_release(smpi_global->message_mallocator, message);
260                 }
261
262                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
263                 running_hosts_count = smpi_global->running_hosts_count;
264                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
265
266         } while (0 < running_hosts_count);
267
268         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
269         smpi_global->ready_process_count--;
270         if (smpi_global->ready_process_count == 0) {
271                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
272         } else if (smpi_global->ready_process_count < 0) {
273                 // FIXME: can't happen, abort!
274         }
275         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
276
277         return 0;
278 }
279
280 void *smpi_request_new()
281 {
282         smpi_mpi_request_t *request = xbt_new(smpi_mpi_request_t, 1);
283
284         request->completed = 0;
285         request->mutex     = SIMIX_mutex_init();
286         request->cond      = SIMIX_cond_init();
287
288         return request;
289 }
290
291 void smpi_request_free(void *pointer) {
292
293         smpi_mpi_request_t *request = pointer;
294
295         if (NULL != request) {
296                 SIMIX_cond_destroy(request->cond);
297                 SIMIX_mutex_destroy(request->mutex);
298                 xbt_free(request);
299         }
300
301         return;
302 }
303
304 void smpi_request_reset(void *pointer) {
305         return;
306 }
307
308
309 void *smpi_message_new()
310 {
311         return xbt_new(smpi_received_message_t, 1);
312 }
313
314 void smpi_message_free(void *pointer)
315 {
316         if (NULL != pointer) {
317                 xbt_free(pointer);
318         }
319
320         return;
321 }
322
323 void smpi_message_reset(void *pointer)
324 {
325         return;
326 }
327
328 void smpi_global_init()
329 {
330         int i;
331
332         int size = SIMIX_host_get_number();
333
334         smpi_global = xbt_new(s_SMPI_Global_t, 1);
335
336         // config variable
337         smpi_global->reference_speed                     = SMPI_DEFAULT_SPEED;
338
339         smpi_global->root_ready                          = 0;
340         smpi_global->ready_process_count                 = 0;
341
342         // start/stop
343         smpi_global->start_stop_mutex                    = SIMIX_mutex_init();
344         smpi_global->start_stop_cond                     = SIMIX_cond_init();
345
346         // processes
347         smpi_global->sender_processes                    = xbt_new(smx_process_t, size);
348         smpi_global->receiver_processes                  = xbt_new(smx_process_t, size);
349
350         // running hosts
351         smpi_global->running_hosts_count_mutex           = SIMIX_mutex_init();
352         smpi_global->running_hosts_count                 = 0;
353
354         // mallocators
355         smpi_global->request_mallocator                  = xbt_mallocator_new(SMPI_REQUEST_MALLOCATOR_SIZE,
356                                                              smpi_request_new, smpi_request_free, smpi_request_reset);
357         smpi_global->message_mallocator                  = xbt_mallocator_new(SMPI_MESSAGE_MALLOCATOR_SIZE,
358                                                              smpi_message_new, smpi_message_free, smpi_message_reset);
359         smpi_global->pending_send_request_queues         = xbt_new(xbt_fifo_t,  size);
360         smpi_global->pending_send_request_queues_mutexes = xbt_new(smx_mutex_t, size);
361         smpi_global->pending_recv_request_queues         = xbt_new(xbt_fifo_t,  size);
362         smpi_global->pending_recv_request_queues_mutexes = xbt_new(smx_mutex_t, size);
363         smpi_global->received_message_queues             = xbt_new(xbt_fifo_t,  size);
364         smpi_global->received_message_queues_mutexes     = xbt_new(smx_mutex_t, size);
365         smpi_global->timers                              = xbt_new(xbt_os_timer_t, size);
366         smpi_global->timers_mutexes                      = xbt_new(smx_mutex_t, size);
367
368
369         for(i = 0; i < size; i++) {
370                 smpi_global->pending_send_request_queues[i]         = xbt_fifo_new();
371                 smpi_global->pending_send_request_queues_mutexes[i] = SIMIX_mutex_init();
372                 smpi_global->pending_recv_request_queues[i]         = xbt_fifo_new();
373                 smpi_global->pending_recv_request_queues_mutexes[i] = SIMIX_mutex_init();
374                 smpi_global->received_message_queues[i]             = xbt_fifo_new();
375                 smpi_global->received_message_queues_mutexes[i]     = SIMIX_mutex_init();
376                 smpi_global->timers[i]                              = xbt_os_timer_new();
377                 smpi_global->timers_mutexes[i]                      = SIMIX_mutex_init();
378         }
379
380
381 }
382
383 void smpi_global_destroy()
384 {
385         int i;
386
387         int size = SIMIX_host_get_number();
388
389         // start/stop
390         SIMIX_mutex_destroy(smpi_global->start_stop_mutex);
391         SIMIX_cond_destroy(smpi_global->start_stop_cond);
392
393         // processes
394         xbt_free(smpi_global->sender_processes);
395         xbt_free(smpi_global->receiver_processes);
396
397         // running hosts
398         SIMIX_mutex_destroy(smpi_global->running_hosts_count_mutex);
399
400         // mallocators
401         xbt_mallocator_free(smpi_global->request_mallocator);
402         xbt_mallocator_free(smpi_global->message_mallocator);
403
404         for(i = 0; i < size; i++) {
405                 xbt_fifo_free(smpi_global->pending_send_request_queues[i]);
406                 SIMIX_mutex_destroy(smpi_global->pending_send_request_queues_mutexes[i]);
407                 xbt_fifo_free(smpi_global->pending_recv_request_queues[i]);
408                 SIMIX_mutex_destroy(smpi_global->pending_recv_request_queues_mutexes[i]);
409                 xbt_fifo_free(smpi_global->received_message_queues[i]);
410                 SIMIX_mutex_destroy(smpi_global->received_message_queues_mutexes[i]);
411                 xbt_os_timer_free(smpi_global->timers[i]);
412                 SIMIX_mutex_destroy(smpi_global->timers_mutexes[i]);
413         }
414
415         xbt_free(smpi_global->pending_send_request_queues);
416         xbt_free(smpi_global->pending_send_request_queues_mutexes);
417         xbt_free(smpi_global->pending_recv_request_queues);
418         xbt_free(smpi_global->pending_recv_request_queues_mutexes);
419         xbt_free(smpi_global->received_message_queues);
420         xbt_free(smpi_global->received_message_queues_mutexes);
421         xbt_free(smpi_global->timers);
422         xbt_free(smpi_global->timers_mutexes);
423
424         xbt_free(smpi_global);
425 }
426
427 int smpi_run_simulation(int argc, char **argv)
428 {
429         smx_cond_t   cond           = NULL;
430         smx_action_t action         = NULL;
431
432         xbt_fifo_t   actions_failed = xbt_fifo_new();
433         xbt_fifo_t   actions_done   = xbt_fifo_new();
434
435         srand(SMPI_RAND_SEED);
436
437         SIMIX_global_init(&argc, argv);
438
439         // initialize smpi_global
440         smpi_global_init();
441
442         SIMIX_function_register("smpi_simulated_main", smpi_simulated_main);
443         SIMIX_function_register("smpi_sender",         smpi_sender);
444         SIMIX_function_register("smpi_receiver",       smpi_receiver);
445
446         // FIXME: ought to verify these files...
447         SIMIX_create_environment(argv[1]);
448         SIMIX_launch_application(argv[2]);
449
450         /* Prepare to display some more info when dying on Ctrl-C pressing */
451         // FIXME: doesn't work
452         //signal(SIGINT, inthandler);
453
454         /* Clean IO before the run */
455         fflush(stdout);
456         fflush(stderr);
457
458         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
459                 while (action = xbt_fifo_pop(actions_failed)) {
460                         DEBUG1("** %s failed **", action->name);
461                         while (cond = xbt_fifo_pop(action->cond_list)) {
462                                 SIMIX_cond_broadcast(cond);
463                         }
464                         SIMIX_action_destroy(action);
465                 }
466                 while (action = xbt_fifo_pop(actions_done)) {
467                         DEBUG1("** %s done **",action->name);
468                         while (cond = xbt_fifo_pop(action->cond_list)) {
469                                 SIMIX_cond_broadcast(cond);
470                         }
471                         SIMIX_action_destroy(action);
472                 }
473         }
474
475         xbt_fifo_free(actions_failed);
476         xbt_fifo_free(actions_done);
477
478         INFO1("simulation time %g", SIMIX_get_clock());
479
480         smpi_global_destroy();
481
482         SIMIX_clean();
483
484         return 0;
485 }
486
487 void smpi_mpi_land_func(void *x, void *y, void *z)
488 {
489         *(int *)z = *(int *)x && *(int *)y;
490 }
491
492 void smpi_mpi_sum_func(void *x, void *y, void *z)
493 {
494         *(int *)z = *(int *)x + *(int *)y;
495 }
496
497
498 void smpi_mpi_init()
499 {
500         int i;
501         smx_process_t process;
502         smx_host_t host;
503         smx_host_t *hosts;
504         int size;
505         double duration;
506
507         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
508         smpi_global->running_hosts_count++;
509         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
510
511         // initialize some local variables
512         process = SIMIX_process_self();
513         host    = SIMIX_host_self();
514         hosts   = SIMIX_host_get_table();
515         size    = SIMIX_host_get_number();
516
517         // node 0 sets the globals
518         if (host == hosts[0]) {
519
520                 smpi_mpi_global                                = xbt_new(s_SMPI_MPI_Global_t, 1);
521
522                 // global communicator
523                 smpi_mpi_global->mpi_comm_world                = xbt_new(smpi_mpi_communicator_t, 1);
524                 smpi_mpi_global->mpi_comm_world->size          = size;
525                 smpi_mpi_global->mpi_comm_world->barrier_count = 0;
526                 smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
527                 smpi_mpi_global->mpi_comm_world->barrier_cond  = SIMIX_cond_init();
528                 smpi_mpi_global->mpi_comm_world->hosts         = hosts;
529                 smpi_mpi_global->mpi_comm_world->processes     = xbt_new(smx_process_t, size);
530                 smpi_mpi_global->mpi_comm_world->processes[0]  = process;
531
532                 // mpi datatypes
533                 smpi_mpi_global->mpi_byte                      = xbt_new(smpi_mpi_datatype_t, 1);
534                 smpi_mpi_global->mpi_byte->size                = (size_t)1;
535                 smpi_mpi_global->mpi_int                       = xbt_new(smpi_mpi_datatype_t, 1);
536                 smpi_mpi_global->mpi_int->size                 = sizeof(int);
537                 smpi_mpi_global->mpi_double                    = xbt_new(smpi_mpi_datatype_t, 1);
538                 smpi_mpi_global->mpi_double->size              = sizeof(double);
539
540                 // mpi operations
541                 smpi_mpi_global->mpi_land                      = xbt_new(smpi_mpi_op_t, 1);
542                 smpi_mpi_global->mpi_land->func                = smpi_mpi_land_func;
543                 smpi_mpi_global->mpi_sum                       = xbt_new(smpi_mpi_op_t, 1);
544                 smpi_mpi_global->mpi_sum->func                 = smpi_mpi_sum_func;
545
546                 // signal all nodes to perform initialization
547                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
548                 smpi_global->root_ready = 1;
549                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
550                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
551
552         } else {
553
554                 // make sure root is done before own initialization
555                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
556                 if (!smpi_global->root_ready) {
557                         SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
558                 }
559                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
560
561                 smpi_mpi_global->mpi_comm_world->processes[smpi_mpi_comm_world_rank_self()] = process;
562         }
563
564         // wait for all nodes to signal initializatin complete
565         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
566         smpi_global->ready_process_count++;
567         if (smpi_global->ready_process_count < 3 * size) {
568                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
569         } else {
570                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
571         }
572         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
573
574         return;
575 }
576
577 void smpi_mpi_finalize()
578 {
579         int i;
580
581         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
582         i = --smpi_global->running_hosts_count;
583         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
584
585         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
586         smpi_global->ready_process_count--;
587         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
588
589         if (0 >= i) {
590
591                 // wake up senders/receivers
592                 for (i = 0; i < smpi_mpi_global->mpi_comm_world->size; i++) {
593                         if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
594                                 SIMIX_process_resume(smpi_global->sender_processes[i]);
595                         }
596                         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
597                                 SIMIX_process_resume(smpi_global->receiver_processes[i]);
598                         }
599                 }
600
601                 // wait for senders/receivers to exit...
602                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
603                 if (smpi_global->ready_process_count > 0) {
604                         SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
605                 }
606                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
607
608                 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
609                 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
610                 xbt_free(smpi_mpi_global->mpi_comm_world->processes);
611                 xbt_free(smpi_mpi_global->mpi_comm_world);
612
613                 xbt_free(smpi_mpi_global->mpi_byte);
614                 xbt_free(smpi_mpi_global->mpi_int);
615                 xbt_free(smpi_mpi_global->mpi_double);
616
617                 xbt_free(smpi_mpi_global->mpi_land);
618                 xbt_free(smpi_mpi_global->mpi_sum);
619
620                 xbt_free(smpi_mpi_global);
621         }
622
623 }
624
625 // FIXME: could cause trouble with multithreaded procs on same host...
626 void smpi_bench_begin()
627 {
628         int rank = smpi_mpi_comm_world_rank_self();
629         SIMIX_mutex_lock(smpi_global->timers_mutexes[rank]);
630         xbt_os_timer_start(smpi_global->timers[rank]);
631         return;
632 }
633
634 void smpi_bench_end()
635 {
636         int rank = smpi_mpi_comm_world_rank_self();
637         double duration;
638         smx_host_t host;
639         smx_action_t compute_action;
640         smx_mutex_t mutex;
641         smx_cond_t cond;
642
643         xbt_os_timer_stop(smpi_global->timers[rank]);
644
645         duration       = xbt_os_timer_elapsed(smpi_global->timers[rank]);
646         SIMIX_mutex_unlock(smpi_global->timers_mutexes[rank]);
647
648         host           = smpi_mpi_global->mpi_comm_world->hosts[rank];
649         compute_action = SIMIX_action_execute(host, "computation", duration * SMPI_DEFAULT_SPEED);
650         mutex          = SIMIX_mutex_init();
651         cond           = SIMIX_cond_init();
652
653         SIMIX_register_condition_to_action(compute_action, cond);
654         SIMIX_register_action_to_condition(compute_action, cond);
655         SIMIX_mutex_lock(mutex);
656         SIMIX_cond_wait(cond, mutex);
657         SIMIX_mutex_unlock(mutex);
658
659         SIMIX_mutex_destroy(mutex);
660         SIMIX_cond_destroy(cond);
661
662         // FIXME: check for success/failure?
663
664         return;
665 }
666
667 void smpi_barrier(smpi_mpi_communicator_t *comm) {
668
669         SIMIX_mutex_lock(comm->barrier_mutex);
670         if(++comm->barrier_count < comm->size) {
671                 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
672         } else {
673                 comm->barrier_count = 0;
674                 SIMIX_cond_broadcast(comm->barrier_cond);
675         }
676         SIMIX_mutex_unlock(comm->barrier_mutex);
677
678         return;
679 }
680
681 // FIXME: smarter algorithm...
682 int smpi_comm_rank(smpi_mpi_communicator_t *comm, smx_host_t host)
683 {
684         int i;
685         for(i = 0; i < comm->size && host != comm->hosts[i]; i++);
686         if (i >= comm->size) i = -1;
687         return i;
688 }
689
690 int smpi_create_request(void *buf, int count, smpi_mpi_datatype_t *datatype,
691         int src, int dst, int tag, smpi_mpi_communicator_t *comm, smpi_mpi_request_t **request)
692 {
693         int retval = MPI_SUCCESS;
694
695         *request = NULL;
696
697         if (0 > count) {
698                 retval = MPI_ERR_COUNT;
699         } else if (NULL == buf) {
700                 retval = MPI_ERR_INTERN;
701         } else if (NULL == datatype) {
702                 retval = MPI_ERR_TYPE;
703         } else if (NULL == comm) {
704                 retval = MPI_ERR_COMM;
705         } else if (MPI_ANY_SOURCE != src && (0 > src || comm->size <= src)) {
706                 retval = MPI_ERR_RANK;
707         } else if (0 > dst || comm->size <= dst) {
708                 retval = MPI_ERR_RANK;
709         } else if (0 > tag) {
710                 retval = MPI_ERR_TAG;
711         } else {
712                 *request = xbt_mallocator_get(smpi_global->request_mallocator);
713                 (*request)->comm       = comm;
714                 (*request)->src        = src;
715                 (*request)->dst        = dst;
716                 (*request)->tag        = tag;
717                 (*request)->buf        = buf;
718                 (*request)->count      = count;
719                 (*request)->datatype   = datatype;
720         }
721         return retval;
722 }
723
724 int smpi_isend(smpi_mpi_request_t *request)
725 {
726         int retval = MPI_SUCCESS;
727         int rank   = smpi_mpi_comm_world_rank_self();
728
729         if (NULL != request) {
730                 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[rank]);
731                 xbt_fifo_push(smpi_global->pending_send_request_queues[rank], request);
732                 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[rank]);
733         }
734
735         if (SIMIX_process_is_suspended(smpi_global->sender_processes[rank])) {
736                 SIMIX_process_resume(smpi_global->sender_processes[rank]);
737         }
738
739         return retval;
740 }
741
742 int smpi_irecv(smpi_mpi_request_t *request)
743 {
744         int retval = MPI_SUCCESS;
745         int rank = smpi_mpi_comm_world_rank_self();
746
747         if (NULL != request) {
748                 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[rank]);
749                 xbt_fifo_push(smpi_global->pending_recv_request_queues[rank], request);
750                 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[rank]);
751         }
752
753         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[rank])) {
754                 SIMIX_process_resume(smpi_global->receiver_processes[rank]);
755         }
756
757         return retval;
758 }
759
760 void smpi_wait(smpi_mpi_request_t *request, smpi_mpi_status_t *status)
761 {
762         smx_process_t self = SIMIX_process_self();
763         int suspend = 0;
764
765         if (NULL != request) {
766                 SIMIX_mutex_lock(request->mutex);
767                 if (!request->completed) {
768                         SIMIX_cond_wait(request->cond, request->mutex);
769                 }
770                 status->MPI_SOURCE = request->src;
771                 SIMIX_mutex_unlock(request->mutex);
772         }
773 }
774
775 // FIXME: move into own file
776 int smpi_gettimeofday(struct timeval *tv, struct timezone *tz)
777 {
778         double now;
779         int retval = 0;
780         smpi_bench_end();
781         if (NULL == tv) {
782                 retval = -1;
783         } else {
784                 now = SIMIX_get_clock();
785                 tv->tv_sec  = now;
786                 tv->tv_usec = ((now - (double)tv->tv_sec) * 1000000.0);
787         }
788         smpi_bench_begin();
789         return retval;
790 }
791
792 unsigned int smpi_sleep(unsigned int seconds)
793 {
794         smx_mutex_t mutex;
795         smx_cond_t cond;
796         smx_host_t host;
797         smx_action_t sleep_action;
798
799         smpi_bench_end();
800         host         = SIMIX_host_self();
801         sleep_action = SIMIX_action_sleep(host, seconds);
802         mutex        = SIMIX_mutex_init();
803         cond         = SIMIX_cond_init();
804
805         SIMIX_register_condition_to_action(sleep_action, cond);
806         SIMIX_register_action_to_condition(sleep_action, cond);
807         SIMIX_mutex_lock(mutex);
808         SIMIX_cond_wait(cond, mutex);
809         SIMIX_mutex_unlock(mutex);
810
811         SIMIX_mutex_destroy(mutex);
812         SIMIX_cond_destroy(cond);
813
814         // FIXME: check for success/failure?
815
816         smpi_bench_begin();
817         return 0;
818 }
819
820 void smpi_exit(int status)
821 {
822         smpi_bench_end();
823         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
824         smpi_global->running_hosts_count--;
825         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
826         SIMIX_process_kill(SIMIX_process_self());
827         return;
828 }