Logo AND Algorithmique Numérique Distribuée

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