Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bd0d13fd842757e88fbd62a806cd19236de9f7e7
[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   smpi_mpi_request_t request;
16   smpi_received_message_t message;
17
18   xbt_fifo_item_t request_item;
19   xbt_fifo_item_t message_item;
20
21   self = SIMIX_process_self();
22
23   request_queue = mydata->pending_recv_request_queue;
24   message_queue = smpi_global->received_message_queues[index];
25
26   while (1) {
27     // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
28
29         xbt_fifo_foreach(request_queue,request_item,request,smpi_mpi_request_t){
30           xbt_fifo_foreach(message_queue,message_item,message, smpi_received_message_t) {
31
32         if (request->comm == message->comm &&
33             (MPI_ANY_SOURCE == request->src || request->src == message->src)
34             && (MPI_ANY_TAG == request->tag || request->tag == message->tag)) {
35           xbt_fifo_remove_item(request_queue, request_item);
36           xbt_fifo_free_item(request_item);
37           xbt_fifo_remove_item(message_queue, message_item);
38           xbt_fifo_free_item(message_item);
39           goto stopsearch;
40         }
41       }
42     }
43
44     request = NULL;
45     message = NULL;
46
47   stopsearch:
48   if (NULL != request) {
49           if (NULL == message)
50                   DIE_IMPOSSIBLE;
51
52           SIMIX_mutex_lock(request->mutex);
53           memcpy(request->buf, message->buf,
54                           request->datatype->size * request->count);
55           request->src = message->src;
56           request->data = message->data;
57           request->forward = message->forward;
58
59           if (0 == request->forward) {
60                   request->completed = 1;
61                   SIMIX_cond_broadcast(request->cond);
62           } else {
63                   request->src = request->comm->index_to_rank_map[index];
64                   request->dst = (request->src + 1) % request->comm->size;
65                   smpi_mpi_isend(request);
66           }
67
68           SIMIX_mutex_unlock(request->mutex);
69
70           xbt_free(message->buf);
71           xbt_mallocator_release(smpi_global->message_mallocator, message);
72
73     } else if (mydata->finalize>0) { /* main wants me to die and nothing to do */
74         // FIXME: display the list of remaining requests and messages (user code synchronization faulty?)
75         mydata->finalize--;
76         SIMIX_cond_signal(mydata->cond);
77         return 0;
78     } else {
79         SIMIX_process_suspend(self);
80     }
81   }
82
83   return 0;
84 }