Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add debug + incredibly stupid bug fix.
[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         char communicate[] = "communicate";
20         smx_action_t action;
21
22         smpi_received_message_t message;
23
24         int drank;
25
26         smx_process_t receiver_process;
27
28         self  = SIMIX_process_self();
29         shost = SIMIX_host_self();
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         rank = smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, shost);
39         size = smpi_mpi_comm_size(smpi_mpi_global->mpi_comm_world);
40
41         request_queue       = smpi_global->pending_send_request_queues[rank];
42         request_queue_mutex = smpi_global->pending_send_request_queues_mutexes[rank];
43
44         smpi_global->sender_processes[rank] = 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 * size) {
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->src;
72                         message->dst  = request->dst;
73                         message->tag  = request->tag;
74                         message->buf  = xbt_malloc(request->datatype->size * request->count);
75                         memcpy(message->buf, request->buf, request->datatype->size * request->count);
76
77                         dhost = request->comm->hosts[request->dst];
78                         drank = smpi_mpi_comm_rank(smpi_mpi_global->mpi_comm_world, dhost);
79
80                         SIMIX_mutex_lock(smpi_global->received_message_queues_mutexes[drank]);
81                         xbt_fifo_push(smpi_global->received_message_queues[drank], message);
82                         SIMIX_mutex_unlock(smpi_global->received_message_queues_mutexes[drank]);
83
84                         request->completed = 1;
85
86                         action = SIMIX_action_communicate(shost, dhost, communicate, request->datatype->size * request->count, -1.0);
87
88                         SIMIX_register_action_to_condition(action, request->cond);
89                         SIMIX_cond_wait(request->cond, request->mutex);
90                         SIMIX_unregister_action_to_condition(action, request->cond);
91
92                         SIMIX_mutex_unlock(request->mutex);
93
94                         SIMIX_action_destroy(action);
95
96                         // wake up receiver if necessary
97                         receiver_process = smpi_global->receiver_processes[drank];
98                         if (SIMIX_process_is_suspended(receiver_process)) {
99                                 SIMIX_process_resume(receiver_process);
100                         }
101
102                 }
103
104                 SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
105                 running_hosts_count = smpi_global->running_hosts_count;
106                 SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
107
108         } while (0 < running_hosts_count);
109
110         return 0;
111 }