Logo AND Algorithmique Numérique Distribuée

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