Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
579791161a4de8fac50c0b8d9d00ffed071f6f89
[simgrid.git] / src / smpi / smpi_receiver.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_receiver, smpi,
4                                 "Logging specific to SMPI (receiver)");
5
6 int smpi_receiver(int argc, char **argv)
7 {
8   smx_process_t self;
9   int index;
10
11   xbt_fifo_t request_queue;
12   xbt_fifo_t message_queue;
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
24   index = smpi_host_index();
25
26   request_queue = smpi_global->pending_recv_request_queues[index];
27   message_queue = smpi_global->received_message_queues[index];
28
29   smpi_global->receiver_processes[index] = self;
30
31   do {
32
33     // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
34
35     for (request_item = xbt_fifo_get_first_item(request_queue);
36          NULL != request_item;
37          request_item = xbt_fifo_get_next_item(request_item)) {
38       request = xbt_fifo_get_item_content(request_item);
39       for (message_item = xbt_fifo_get_first_item(message_queue);
40            NULL != message_item;
41            message_item = xbt_fifo_get_next_item(message_item)) {
42         message = xbt_fifo_get_item_content(message_item);
43         if (request->comm == message->comm &&
44             (MPI_ANY_SOURCE == request->src || request->src == message->src)
45             && (MPI_ANY_TAG == request->tag || request->tag == message->tag)) {
46           xbt_fifo_remove_item(request_queue, request_item);
47           xbt_fifo_free_item(request_item);
48           xbt_fifo_remove_item(message_queue, message_item);
49           xbt_fifo_free_item(message_item);
50           goto stopsearch;
51         }
52       }
53     }
54
55     request = NULL;
56     message = NULL;
57
58   stopsearch:
59     if (NULL == request || NULL == message) {
60       SIMIX_process_suspend(self);
61     } else {
62
63       SIMIX_mutex_lock(request->mutex);
64       memcpy(request->buf, message->buf,
65              request->datatype->size * request->count);
66       request->src = message->src;
67       request->data = message->data;
68       request->forward = message->forward;
69
70       if (0 == request->forward) {
71         request->completed = 1;
72         SIMIX_cond_broadcast(request->cond);
73       } else {
74         request->src = request->comm->index_to_rank_map[index];
75         request->dst = (request->src + 1) % request->comm->size;
76         smpi_mpi_isend(request);
77       }
78
79       SIMIX_mutex_unlock(request->mutex);
80
81       xbt_free(message->buf);
82       xbt_mallocator_release(smpi_global->message_mallocator, message);
83
84     }
85
86     SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
87     running_hosts_count = smpi_global->running_hosts_count;
88     SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
89
90   } while (0 < running_hosts_count);
91
92   return 0;
93 }