Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0035d63897a7732394f3d9afb42a01a167c52119
[simgrid.git] / src / smpi / smpi_sender.c
1 #include "private.h"
2
3 int smpi_sender(int argc, char **argv)
4 {
5         smx_process_t self;
6         smx_host_t shost;
7         int index;
8
9         xbt_fifo_t request_queue;
10         smx_mutex_t request_queue_mutex;
11
12         int running_hosts_count;
13
14         smpi_mpi_request_t request;
15
16         smx_host_t dhost;
17
18         smx_action_t action;
19
20         smpi_received_message_t message;
21
22         int dindex;
23
24         smx_process_t receiver_process;
25
26         self  = SIMIX_process_self();
27         shost = SIMIX_host_self();
28
29         // make sure root is done before own initialization
30         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
31         if (!smpi_global->root_ready) {
32                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
33         }
34         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
35
36         index = smpi_host_index();
37
38         request_queue       = smpi_global->pending_send_request_queues[index];
39         request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[index];
40
41         smpi_global->sender_processes[index] = self;
42
43         // wait for all nodes to signal initializatin complete
44         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
45         smpi_global->ready_process_count++;
46         if (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
47                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
48         } else {
49                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
50         }
51         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
52
53         do {
54
55                 SIMIX_mutex_lock(request_queue_mutex);
56                 request = xbt_fifo_shift(request_queue);
57                 SIMIX_mutex_unlock(request_queue_mutex);
58
59                 if (NULL == request) {
60                         SIMIX_process_suspend(self);
61                 } else {
62
63                         message          = xbt_mallocator_get(smpi_global->message_mallocator);
64
65                         SIMIX_mutex_lock(request->mutex);
66
67                         message->comm    = request->comm;
68                         message->src     = request->comm->index_to_rank_map[index];
69                         message->tag     = request->tag;
70                         message->data    = request->data;
71                         message->buf     = xbt_malloc(request->datatype->size * request->count);
72                         memcpy(message->buf, request->buf, request->datatype->size * request->count);
73
74                         dindex = request->comm->rank_to_index_map[request->dst];
75                         dhost  = smpi_global->hosts[dindex];
76
77                         message->forward = (request->forward - 1) / 2;
78                         request->forward = request->forward / 2;
79
80                         SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[dindex]);
81                         xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
82                         SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[dindex]);
83
84                         if (0 < request->forward) {
85                                 request->dst = (request->dst + message->forward + 1) % request->comm->size;
86                                 SIMIX_mutex_lock(request_queue_mutex);
87                                 xbt_fifo_push(request_queue, request);
88                                 SIMIX_mutex_unlock(request_queue_mutex);
89                         } else {
90                                 request->completed = 1;
91                         }
92
93                         action = SIMIX_action_communicate(shost, dhost, "communication", request->datatype->size * request->count, -1.0);
94
95                         SIMIX_register_action_to_condition(action, request->cond);
96                         SIMIX_cond_wait(request->cond, request->mutex);
97                         SIMIX_unregister_action_to_condition(action, request->cond);
98
99                         SIMIX_mutex_unlock(request->mutex);
100
101                         // wake up receiver if necessary
102                         receiver_process = smpi_global->receiver_processes[dindex];
103                         if (SIMIX_process_is_suspended(receiver_process)) {
104                                 SIMIX_process_resume(receiver_process);
105                         }
106
107                 }
108
109                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
110                 running_hosts_count = smpi_global->running_hosts_count;
111                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
112
113         } while (0 < running_hosts_count);
114
115         return 0;
116 }