Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
refactored smpi into multiple source files.
[simgrid.git] / src / smpi / smpi_receiver.c
1 #include "private.h"
2
3 int smpi_receiver(int argc, char **argv)
4 {
5         smx_process_t self;
6         int rank;
7
8         xbt_fifo_t request_queue;
9         smx_mutex_t request_queue_mutex;
10         xbt_fifo_t message_queue;
11         smx_mutex_t message_queue_mutex;
12         int size;
13
14         int running_hosts_count;
15
16         smpi_mpi_request_t *request;
17         smpi_received_message_t *message;
18
19         xbt_fifo_item_t request_item;
20         xbt_fifo_item_t message_item;
21
22         self  = SIMIX_process_self();
23         rank  = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
24
25         // make sure root is done before own initialization
26         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
27         if (!smpi_global->root_ready) {
28                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
29         }
30         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
31
32         request_queue       = smpi_global->pending_recv_request_queues[rank];
33         request_queue_mutex = smpi_global->pending_recv_request_queues_mutexes[rank];
34         message_queue       = smpi_global->received_message_queues[rank];
35         message_queue_mutex = smpi_global->received_message_queues_mutexes[rank];
36         size                = smpi_mpi_comm_size(smpi_mpi_global->mpi_comm_world);
37
38         smpi_global->receiver_processes[rank] = self;
39
40         // wait for all nodes to signal initializatin complete
41         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
42         smpi_global->ready_process_count++;
43         if (smpi_global->ready_process_count < 3 * size) {
44                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
45         } else {
46                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
47         }
48         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
49
50         do {
51                 request = NULL;
52                 message = NULL;
53
54                 // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
55
56                 // FIXME: not the best way to request multiple locks...
57                 SIMIX_mutex_lock(request_queue_mutex);
58                 SIMIX_mutex_lock(message_queue_mutex);
59                 for (request_item = xbt_fifo_get_first_item(request_queue);
60                         NULL != request_item;
61                         request_item = xbt_fifo_get_next_item(request_item)) {
62                         request = xbt_fifo_get_item_content(request_item);
63                         for (message_item = xbt_fifo_get_first_item(message_queue);
64                                 NULL != message_item;
65                                 message_item = xbt_fifo_get_next_item(message_item)) {
66                                 message = xbt_fifo_get_item_content(message_item);
67                                 if (request->comm == message->comm &&
68                                                 (MPI_ANY_SOURCE == request->src || request->src == message->src) &&
69                                                 request->tag == message->tag) {
70                                         xbt_fifo_remove_item(request_queue, request_item);
71                                         xbt_fifo_remove_item(message_queue, message_item);
72                                         goto stopsearch;
73                                 }
74                         }
75                 }
76 stopsearch:
77                 SIMIX_mutex_unlock(message_queue_mutex);
78                 SIMIX_mutex_unlock(request_queue_mutex);
79
80                 if (NULL == request || NULL == message) {
81                         SIMIX_process_suspend(self);
82                 } else {
83                         SIMIX_mutex_lock(request->simdata->mutex);
84
85                         memcpy(request->buf, message->buf, request->datatype->size * request->count);
86                         request->src = message->src;
87                         request->completed = 1;
88                         SIMIX_cond_broadcast(request->simdata->cond);
89
90                         SIMIX_mutex_unlock(request->simdata->mutex);
91
92                         xbt_free(message->buf);
93                         xbt_mallocator_release(smpi_global->message_mallocator, message);
94                 }
95
96                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
97                 running_hosts_count = smpi_global->running_hosts_count;
98                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
99
100         } while (0 < running_hosts_count);
101
102         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
103         smpi_global->ready_process_count--;
104         if (smpi_global->ready_process_count == 0) {
105                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
106         } else if (smpi_global->ready_process_count < 0) {
107                 // FIXME: can't happen, abort!
108         }
109         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
110
111         return 0;
112 }