Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
alltoall implemented (almost opmpi algorithms)
[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_process_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 = mydata->received_message_queue;
25
26   DEBUG0("Up and running");
27
28
29   while (1) {
30     // FIXME: better algorithm, maybe some kind of balanced tree? or a heap?
31
32     DEBUG0("Look for matching");
33     xbt_fifo_foreach(request_queue, request_item, request, smpi_mpi_request_t) {
34       xbt_fifo_foreach(message_queue, message_item, message,
35                        smpi_received_message_t) {
36
37 //#define DEBUG_MATCH
38 #ifdef DEBUG_MATCH
39         printf("[%s] try match (req_src=%d,msg_src=%d)x(req_tag=%d,msg_tag=%d)\n",
40                         __FILE__,request->src,message->src,request->tag, message->tag);
41 #endif
42         if (request->comm == message->comm &&
43                         (MPI_ANY_SOURCE == request->src || request->src == message->src)
44                         && (MPI_ANY_TAG == request->tag || request->tag == message->tag)) {
45                 xbt_fifo_remove_item(request_queue, request_item);
46                 xbt_fifo_free_item(request_item);
47                 xbt_fifo_remove_item(message_queue, message_item);
48                 xbt_fifo_free_item(message_item);
49                 DEBUG5("found matching request %p: (req_src=%d,msg_src=%d)x(req_tag=%d,msg_tag=%d)",
50                                 request,request->src,message->src,request->tag, message->tag);
51                 goto stopsearch;
52         } else {
53                 DEBUG5("fail to match request %p: (req_src=%d,msg_src=%d)x(req_tag=%d,msg_tag=%d)",
54                                 request,request->src,message->src,request->tag, message->tag);
55         }
56       }
57     }
58
59     request = NULL;
60     message = NULL;
61
62   stopsearch:
63     if (NULL != request) {
64       if (NULL == message)
65         DIE_IMPOSSIBLE;
66
67       SIMIX_mutex_lock(request->mutex);
68       memcpy(request->buf, message->buf,
69              request->datatype->size * request->count);
70       request->src = message->src;
71       request->data = message->data;
72       request->forward = message->forward;
73
74       if (0 == request->forward) {
75         request->completed = 1;
76         SIMIX_cond_broadcast(request->cond);
77       } else {
78         request->src = request->comm->index_to_rank_map[index];
79         request->dst = (request->src + 1) % request->comm->size;
80         smpi_mpi_isend(request);
81       }
82
83       SIMIX_mutex_unlock(request->mutex);
84
85       xbt_free(message->buf);
86       xbt_mallocator_release(smpi_global->message_mallocator, message);
87
88     } else if (mydata->finalize > 0) {  /* main wants me to die and nothing to do */
89       // FIXME: display the list of remaining requests and messages (user code synchronization faulty?)
90       DEBUG0("Main wants me to die and I'm done. Bye, guys.");
91       mydata->finalize--;
92       SIMIX_cond_signal(mydata->cond);
93       return 0;
94     } else {
95       DEBUG0("Nothing to do. Let's get a nap");
96       SIMIX_process_suspend(self);
97       DEBUG0("=== Uh? Someone called me? ===");
98     }
99   }
100
101   return 0;
102 }