Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
chaned global execute_mutex and execute_cond to host-specific and wrapped all
[simgrid.git] / src / smpi / smpi_sender.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_sender, smpi, "Logging specific to SMPI (sender)");
4
5 int smpi_sender(int argc, char **argv)
6 {
7         smx_process_t self;
8         smx_host_t shost;
9
10         int index;
11
12         xbt_fifo_t request_queue;
13         smx_mutex_t request_queue_mutex;
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         // make sure root is done before own initialization
35         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
36         while (!smpi_global->root_ready) {
37                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
38         }
39         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
40
41         index = smpi_host_index();
42
43         request_queue       = smpi_global->pending_send_request_queues[index];
44         request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[index];
45
46         smpi_global->sender_processes[index] = self;
47
48         // wait for all nodes to signal initializatin complete
49         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
50         smpi_global->ready_process_count++;
51         if (smpi_global->ready_process_count >= 3 * smpi_global->host_count) {
52                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
53         }
54         while (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
55                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
56         }
57         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
58
59         do {
60
61                 SIMIX_mutex_lock(request_queue_mutex);
62                 request = xbt_fifo_shift(request_queue);
63                 SIMIX_mutex_unlock(request_queue_mutex);
64
65                 if (NULL == request) {
66                         SIMIX_process_suspend(self);
67                 } else {
68
69                         message          = xbt_mallocator_get(smpi_global->message_mallocator);
70
71                         SIMIX_mutex_lock(request->mutex);
72
73                         message->comm    = request->comm;
74                         message->src     = request->comm->index_to_rank_map[index];
75                         message->tag     = request->tag;
76                         message->data    = request->data;
77                         message->buf     = xbt_malloc(request->datatype->size * request->count);
78                         memcpy(message->buf, request->buf, request->datatype->size * request->count);
79
80                         dindex = request->comm->rank_to_index_map[request->dst];
81                         dhost  = smpi_global->hosts[dindex];
82
83                         message->forward = (request->forward - 1) / 2;
84                         request->forward = request->forward / 2;
85
86                         if (0 < request->forward) {
87                                 request->dst = (request->dst + message->forward + 1) % request->comm->size;
88                                 SIMIX_mutex_lock(request_queue_mutex);
89                                 xbt_fifo_push(request_queue, request);
90                                 SIMIX_mutex_unlock(request_queue_mutex);
91                         } else {
92                                 request->completed = 1;
93                         }
94
95                         action = SIMIX_action_communicate(shost, dhost, "communication", request->datatype->size * request->count, -1.0);
96
97                         SIMIX_register_action_to_condition(action, request->cond);
98
99                         for (
100                                 state  = SIMIX_action_get_state(action);
101                                 state == SURF_ACTION_READY ||
102                                 state == SURF_ACTION_RUNNING;
103                                 state  = SIMIX_action_get_state(action)
104                         ) {
105                                 SIMIX_cond_wait(request->cond, request->mutex);
106                         }
107
108                         SIMIX_unregister_action_to_condition(action, request->cond);
109                         SIMIX_action_destroy(action);
110
111                         SIMIX_mutex_unlock(request->mutex);
112
113                         SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[dindex]);
114                         xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
115                         SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[dindex]);
116
117                         // wake up receiver if necessary
118                         receiver_process = smpi_global->receiver_processes[dindex];
119                         if (SIMIX_process_is_suspended(receiver_process)) {
120                                 SIMIX_process_resume(receiver_process);
121                         }
122
123                 }
124
125                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
126                 running_hosts_count = smpi_global->running_hosts_count;
127                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
128
129         } while (0 < running_hosts_count);
130
131         return 0;
132 }