Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fixed nasty border condition bug.
[simgrid.git] / src / smpi / smpi_receiver.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_receiver, smpi, "Logging specific to SMPI (receiver)");
4
5 int smpi_receiver(int argc, char **argv)
6 {
7         smx_process_t self;
8         int index;
9
10         xbt_fifo_t request_queue;
11         smx_mutex_t request_queue_mutex;
12         xbt_fifo_t message_queue;
13         smx_mutex_t message_queue_mutex;
14
15         int running_hosts_count;
16
17         smpi_mpi_request_t request;
18         smpi_received_message_t message;
19
20         xbt_fifo_item_t request_item;
21         xbt_fifo_item_t message_item;
22
23         self = SIMIX_process_self();
24
25         // make sure root is done before own initialization
26         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
27         while (!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         index = smpi_host_index();
33
34         request_queue       = smpi_global->pending_recv_request_queues[index];
35         request_queue_mutex = smpi_global->pending_recv_request_queues_mutexes[index];
36         message_queue       = smpi_global->received_message_queues[index];
37         message_queue_mutex = smpi_global->received_message_queues_mutexes[index];
38
39         smpi_global->receiver_processes[index] = self;
40
41         // wait for all nodes to signal initializatin complete
42         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
43         smpi_global->ready_process_count++;
44         if (smpi_global->ready_process_count >= 3 * smpi_global->host_count) {
45                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
46         }
47         while (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
48                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
49         }
50         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
51
52         do {
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 (
68                                         request->comm == message->comm &&
69                                         (MPI_ANY_SOURCE == request->src || request->src == message->src) &&
70                                         (MPI_ANY_TAG == request->tag || request->tag == message->tag)) {
71                                         xbt_fifo_remove_item(request_queue, request_item);
72                                         xbt_fifo_free_item(request_item);
73                                         xbt_fifo_remove_item(message_queue, message_item);
74                                         xbt_fifo_free_item(message_item);
75                                         goto stopsearch;
76                                 }
77                         }
78                 }
79
80                 request = NULL;
81                 message = NULL;
82
83 stopsearch:
84                 SIMIX_mutex_unlock(message_queue_mutex);
85                 SIMIX_mutex_unlock(request_queue_mutex);
86
87                 if (NULL == request || NULL == message) {
88                         SIMIX_process_suspend(self);
89                 } else {
90
91                         SIMIX_mutex_lock(request->mutex);
92                         memcpy(request->buf, message->buf, request->datatype->size * request->count);
93                         request->src = message->src;
94                         request->data = message->data;
95                         request->forward = message->forward;
96
97                         if (0 == request->forward) {
98                                 request->completed = 1;
99                                 SIMIX_cond_broadcast(request->cond);
100                         } else {
101                                 request->src = request->comm->index_to_rank_map[index];
102                                 request->dst = (request->src + 1) % request->comm->size;
103                                 smpi_mpi_isend(request);
104                         }
105
106                         SIMIX_mutex_unlock(request->mutex);
107
108                         xbt_free(message->buf);
109                         xbt_mallocator_release(smpi_global->message_mallocator, message);
110
111                 }
112
113                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
114                 running_hosts_count = smpi_global->running_hosts_count;
115                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
116
117         } while (0 < running_hosts_count);
118
119         return 0;
120 }