Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI: Kill a whole bunch of unneeded synchronization: processes run in exclusive...
[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   smx_process_t self;
9   smx_host_t shost;
10
11   int index;
12
13   xbt_fifo_t request_queue;
14
15   int running_hosts_count;
16
17   smpi_mpi_request_t request;
18
19   smx_host_t dhost;
20
21   smx_action_t action;
22
23   e_surf_action_state_t state;
24
25   smpi_received_message_t message;
26
27   int dindex;
28
29   smx_process_t receiver_process;
30
31   self = SIMIX_process_self();
32   shost = SIMIX_host_self();
33
34   index = smpi_host_index();
35
36   request_queue = smpi_global->pending_send_request_queues[index];
37
38   smpi_global->sender_processes[index] = self;
39
40   do {
41
42     request = xbt_fifo_shift(request_queue);
43
44     if (NULL == request) {
45       SIMIX_process_suspend(self);
46     } else {
47
48       message = xbt_mallocator_get(smpi_global->message_mallocator);
49
50       SIMIX_mutex_lock(request->mutex);
51
52       message->comm = request->comm;
53       message->src = request->comm->index_to_rank_map[index];
54       message->tag = request->tag;
55       message->data = request->data;
56       message->buf = xbt_malloc(request->datatype->size * request->count);
57       memcpy(message->buf, request->buf,
58              request->datatype->size * request->count);
59
60       dindex = request->comm->rank_to_index_map[request->dst];
61       dhost = smpi_global->hosts[dindex];
62
63       message->forward = (request->forward - 1) / 2;
64       request->forward = request->forward / 2;
65
66       if (0 < request->forward) {
67         request->dst =
68           (request->dst + message->forward + 1) % request->comm->size;
69         xbt_fifo_push(request_queue, request);
70       } else {
71         request->completed = 1;
72       }
73
74       action =
75         SIMIX_action_communicate(shost, dhost, "communication",
76                                  request->datatype->size * request->count,
77                                  -1.0);
78
79       SIMIX_register_action_to_condition(action, request->cond);
80
81       for (state = SIMIX_action_get_state(action);
82            state == SURF_ACTION_READY ||
83            state == SURF_ACTION_RUNNING;
84            state = SIMIX_action_get_state(action)
85         ) {
86         SIMIX_cond_wait(request->cond, request->mutex);
87       }
88
89       xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
90
91       SIMIX_unregister_action_to_condition(action, request->cond);
92       SIMIX_action_destroy(action);
93
94       SIMIX_mutex_unlock(request->mutex);
95
96       // wake up receiver if necessary
97       receiver_process = smpi_global->receiver_processes[dindex];
98       if (SIMIX_process_is_suspended(receiver_process)) {
99         SIMIX_process_resume(receiver_process);
100       }
101
102     }
103
104     SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
105     running_hosts_count = smpi_global->running_hosts_count;
106     SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
107
108   } while (0 < running_hosts_count);
109
110   return 0;
111 }