Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
accidently tried to compare action to state. now fixed.
[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         e_surf_action_state_t state;
24
25         self = SIMIX_process_self();
26
27         // make sure root is done before own initialization
28         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
29         while (!smpi_global->root_ready) {
30                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
31         }
32         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
33
34         index = smpi_host_index();
35
36         request_queue       = smpi_global->pending_recv_request_queues[index];
37         request_queue_mutex = smpi_global->pending_recv_request_queues_mutexes[index];
38         message_queue       = smpi_global->received_message_queues[index];
39         message_queue_mutex = smpi_global->received_message_queues_mutexes[index];
40
41         smpi_global->receiver_processes[index] = self;
42
43         // wait for all nodes to signal initializatin complete
44         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
45         smpi_global->ready_process_count++;
46         if (smpi_global->ready_process_count >= 3 * smpi_global->host_count) {
47                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
48         }
49         while (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
50                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
51         }
52         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
53
54         do {
55                 request = NULL;
56                 message = NULL;
57
58                 // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
59
60                 // FIXME: not the best way to request multiple locks...
61                 SIMIX_mutex_lock(request_queue_mutex);
62                 SIMIX_mutex_lock(message_queue_mutex);
63                 for (request_item = xbt_fifo_get_first_item(request_queue);
64                         NULL != request_item;
65                         request_item = xbt_fifo_get_next_item(request_item)) {
66                         request = xbt_fifo_get_item_content(request_item);
67                         for (message_item = xbt_fifo_get_first_item(message_queue);
68                                 NULL != message_item;
69                                 message_item = xbt_fifo_get_next_item(message_item)) {
70                                 message = xbt_fifo_get_item_content(message_item);
71                                 state   = SIMIX_action_get_state(message->action);
72                                 if (
73                                         request->comm == message->comm &&
74                                         (MPI_ANY_SOURCE == request->src || request->src == message->src) &&
75                                         (MPI_ANY_TAG == request->tag || request->tag == message->tag) &&
76                                         (state != SURF_ACTION_READY && state != SURF_ACTION_RUNNING)
77                                 ) {
78                                         xbt_fifo_remove_item(request_queue, request_item);
79                                         xbt_fifo_remove_item(message_queue, message_item);
80                                         goto stopsearch;
81                                 }
82                         }
83                 }
84 stopsearch:
85                 SIMIX_mutex_unlock(message_queue_mutex);
86                 SIMIX_mutex_unlock(request_queue_mutex);
87
88                 if (NULL == request || NULL == message) {
89                         SIMIX_process_suspend(self);
90                 } else {
91
92                         // FIXME: check action status for bad messages
93
94                         SIMIX_mutex_lock(request->mutex);
95                         memcpy(request->buf, message->buf, request->datatype->size * request->count);
96                         request->src = message->src;
97                         request->data = message->data;
98                         request->forward = message->forward;
99
100                         if (0 == request->forward) {
101                                 request->completed = 1;
102                                 SIMIX_cond_broadcast(request->cond);
103                         } else {
104                                 request->src = request->comm->index_to_rank_map[index];
105                                 request->dst = (request->src + 1) % request->comm->size;
106                                 smpi_mpi_isend(request);
107                         }
108
109                         SIMIX_mutex_unlock(request->mutex);
110
111                         xbt_free(message->buf);
112                         xbt_mallocator_release(smpi_global->message_mallocator, message);
113
114                 }
115
116                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
117                 running_hosts_count = smpi_global->running_hosts_count;
118                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
119
120         } while (0 < running_hosts_count);
121
122         return 0;
123 }