Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: Kill the global list of senders and receivers
[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         smpi_host_data_t mydata = SIMIX_process_get_data(SIMIX_process_self());
9   smx_process_t self;
10   int index = mydata->index;
11
12   xbt_fifo_t request_queue;
13   xbt_fifo_t message_queue;
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   request_queue = mydata->pending_recv_request_queue;
26   message_queue = smpi_global->received_message_queues[index];
27
28   do {
29
30     // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
31
32         xbt_fifo_foreach(request_queue,request_item,request,smpi_mpi_request_t){
33           xbt_fifo_foreach(message_queue,message_item,message, smpi_received_message_t) {
34
35         if (request->comm == message->comm &&
36             (MPI_ANY_SOURCE == request->src || request->src == message->src)
37             && (MPI_ANY_TAG == request->tag || request->tag == message->tag)) {
38           xbt_fifo_remove_item(request_queue, request_item);
39           xbt_fifo_free_item(request_item);
40           xbt_fifo_remove_item(message_queue, message_item);
41           xbt_fifo_free_item(message_item);
42           goto stopsearch;
43         }
44       }
45     }
46
47     request = NULL;
48     message = NULL;
49
50   stopsearch:
51     if (NULL == request || NULL == message) {
52       SIMIX_process_suspend(self);
53     } else {
54
55       SIMIX_mutex_lock(request->mutex);
56       memcpy(request->buf, message->buf,
57              request->datatype->size * request->count);
58       request->src = message->src;
59       request->data = message->data;
60       request->forward = message->forward;
61
62       if (0 == request->forward) {
63         request->completed = 1;
64         SIMIX_cond_broadcast(request->cond);
65       } else {
66         request->src = request->comm->index_to_rank_map[index];
67         request->dst = (request->src + 1) % request->comm->size;
68         smpi_mpi_isend(request);
69       }
70
71       SIMIX_mutex_unlock(request->mutex);
72
73       xbt_free(message->buf);
74       xbt_mallocator_release(smpi_global->message_mallocator, message);
75
76     }
77
78     running_hosts_count = smpi_global->running_hosts_count;
79
80   } while (0 < running_hosts_count);
81
82   return 0;
83 }