Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
renamed MPI_ functions to avoid namespace collisions in distributed code.
[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         smpi_received_message_t message;
24
25         int dindex;
26
27         smx_process_t receiver_process;
28
29         self  = SIMIX_process_self();
30         shost = SIMIX_host_self();
31
32         // make sure root is done before own initialization
33         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
34         if (!smpi_global->root_ready) {
35                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
36         }
37         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
38
39         index = smpi_host_index();
40
41         request_queue       = smpi_global->pending_send_request_queues[index];
42         request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[index];
43
44         smpi_global->sender_processes[index] = self;
45
46         // wait for all nodes to signal initializatin complete
47         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
48         smpi_global->ready_process_count++;
49         if (smpi_global->ready_process_count < 3 * smpi_global->host_count) {
50                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
51         } else {
52                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
53         }
54         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
55
56         do {
57
58                 SIMIX_mutex_lock(request_queue_mutex);
59                 request = xbt_fifo_shift(request_queue);
60                 SIMIX_mutex_unlock(request_queue_mutex);
61
62                 if (NULL == request) {
63                         SIMIX_process_suspend(self);
64                 } else {
65
66                         message          = xbt_mallocator_get(smpi_global->message_mallocator);
67
68                         SIMIX_mutex_lock(request->mutex);
69
70                         message->comm    = request->comm;
71                         message->src     = request->comm->index_to_rank_map[index];
72                         message->tag     = request->tag;
73                         message->data    = request->data;
74                         message->buf     = xbt_malloc(request->datatype->size * request->count);
75                         memcpy(message->buf, request->buf, request->datatype->size * request->count);
76
77                         dindex = request->comm->rank_to_index_map[request->dst];
78                         dhost  = smpi_global->hosts[dindex];
79
80                         message->forward = (request->forward - 1) / 2;
81                         request->forward = request->forward / 2;
82
83                         if (0 < request->forward) {
84                                 request->dst = (request->dst + message->forward + 1) % request->comm->size;
85                                 SIMIX_mutex_lock(request_queue_mutex);
86                                 xbt_fifo_push(request_queue, request);
87                                 SIMIX_mutex_unlock(request_queue_mutex);
88                         } else {
89                                 request->completed = 1;
90                         }
91
92                         action = SIMIX_action_communicate(shost, dhost, "communication", request->datatype->size * request->count, -1.0);
93
94                         SIMIX_register_action_to_condition(action, request->cond);
95                         SIMIX_cond_wait(request->cond, request->mutex);
96
97                         SIMIX_unregister_action_to_condition(action, request->cond);
98                         SIMIX_action_destroy(action);
99
100                         SIMIX_mutex_unlock(request->mutex);
101
102                         SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[dindex]);
103                         xbt_fifo_push(smpi_global->received_message_queues[dindex], message);
104                         SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[dindex]);
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 }