Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ooops. Forgot this new one.
[simgrid.git] / src / smpi / smpi_sender.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_sender, smpi,
4                                 "Logging specific to SMPI (sender)");
5
6 int smpi_sender(int argc, char *argv[])
7 {
8   smpi_process_data_t mydata = SIMIX_process_get_data(SIMIX_process_self());
9   smx_process_t self;
10   smx_host_t shost;
11
12   int index;
13
14   xbt_fifo_t request_queue;
15
16   smpi_mpi_request_t request;
17
18   smx_host_t dhost;
19
20   smx_action_t action;
21
22   e_surf_action_state_t state;
23
24   smpi_received_message_t message;
25
26   int dindex;
27
28   self = SIMIX_process_self();
29   shost = SIMIX_host_self();
30
31   index = mydata->index;
32
33   request_queue = mydata->pending_send_request_queue;
34
35   while (1) {
36     request = xbt_fifo_shift(request_queue);
37
38     if (NULL != request) {
39       message = xbt_mallocator_get(smpi_global->message_mallocator);
40
41       SIMIX_mutex_lock(request->mutex);
42
43       message->comm = request->comm;
44       message->src = request->comm->index_to_rank_map[index];
45       message->tag = request->tag;
46       message->data = request->data;
47       message->buf = xbt_malloc(request->datatype->size * request->count);
48       memcpy(message->buf, request->buf,
49              request->datatype->size * request->count);
50
51       dindex = request->comm->rank_to_index_map[request->dst];
52       smpi_process_data_t remote_process =
53         SIMIX_process_get_data(smpi_global->main_processes[dindex]);
54       dhost = SIMIX_process_get_host(smpi_global->main_processes[dindex]);
55
56       message->forward = (request->forward - 1) / 2;
57       request->forward = request->forward / 2;
58
59       if (0 < request->forward) {
60         request->dst =
61           (request->dst + message->forward + 1) % request->comm->size;
62         xbt_fifo_push(request_queue, request);
63       } else {
64 //#define DEBUG_MATCH
65 #ifdef DEBUG_MATCH
66  printf("**SENDER: request %p completed :=1\n",request);
67 #endif
68         request->completed = 1;
69       }
70
71       action =
72         SIMIX_action_communicate(shost, dhost, "communication",
73                                  request->datatype->size * request->count,
74                                  -1.0);
75
76       SIMIX_register_action_to_condition(action, request->cond);
77
78       for (state = SIMIX_action_get_state(action);
79            state == SURF_ACTION_READY ||
80            state == SURF_ACTION_RUNNING;
81            state = SIMIX_action_get_state(action)
82         ) {
83         SIMIX_cond_wait(request->cond, request->mutex);
84       }
85
86       xbt_fifo_push(remote_process->received_message_queue, message);
87
88       SIMIX_unregister_action_to_condition(action, request->cond);
89       SIMIX_action_destroy(action);
90
91       SIMIX_mutex_unlock(request->mutex);
92
93       // wake up receiver if necessary
94       SIMIX_process_resume(remote_process->receiver);
95
96     } else if (mydata->finalize > 0) {  /* main wants me to die and nothing to do */
97       mydata->finalize--;
98       SIMIX_cond_signal(mydata->cond);
99       return 0;
100     } else {
101       SIMIX_process_suspend(self);
102     }
103   }
104   return 0;
105 }