Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
renamed some functions and moved some data structures to make things more
[simgrid.git] / src / smpi / smpi_base.c
1 #include "private.h"
2
3 smpi_mpi_global_t smpi_mpi_global = NULL;
4
5 void smpi_mpi_land_func(void *x, void *y, void *z);
6
7 void smpi_mpi_land_func(void *x, void *y, void *z)
8 {
9         *(int *)z = *(int *)x && *(int *)y;
10 }
11
12 void smpi_mpi_sum_func(void *x, void *y, void *z);
13
14 void smpi_mpi_sum_func(void *x, void *y, void *z)
15 {
16         *(int *)z = *(int *)x + *(int *)y;
17 }
18
19 int inline smpi_mpi_comm_size(smpi_mpi_communicator_t comm)
20 {
21         return comm->size;
22 }
23
24 // FIXME: smarter algorithm?
25 int smpi_mpi_comm_rank(smpi_mpi_communicator_t comm, smx_host_t host)
26 {
27         int i;
28
29         for(i = comm->size - 1; i > 0 && host != comm->hosts[i]; i--);
30
31         return i;
32 }
33
34 int inline smpi_mpi_comm_rank_self(smpi_mpi_communicator_t comm)
35 {
36         return smpi_mpi_comm_rank(comm, SIMIX_host_self());
37 }
38
39 void smpi_mpi_init()
40 {
41         smx_process_t process;
42         smx_host_t host;
43         smx_host_t *hosts;
44         int size;
45
46         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
47         smpi_global->running_hosts_count++;
48         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
49
50         // initialize some local variables
51         process = SIMIX_process_self();
52         host    = SIMIX_host_self();
53         hosts   = SIMIX_host_get_table();
54         size    = SIMIX_host_get_number();
55
56         // node 0 sets the globals
57         if (host == hosts[0]) {
58
59                 smpi_mpi_global                                = xbt_new(s_smpi_mpi_global_t, 1);
60
61                 // global communicator
62                 smpi_mpi_global->mpi_comm_world                = xbt_new(s_smpi_mpi_communicator_t, 1);
63                 smpi_mpi_global->mpi_comm_world->size          = size;
64                 smpi_mpi_global->mpi_comm_world->hosts         = hosts;
65                 smpi_mpi_global->mpi_comm_world->processes     = xbt_new(smx_process_t, size);
66                 smpi_mpi_global->mpi_comm_world->processes[0]  = process;
67                 smpi_mpi_global->mpi_comm_world->barrier_count = 0;
68                 smpi_mpi_global->mpi_comm_world->barrier_mutex = SIMIX_mutex_init();
69                 smpi_mpi_global->mpi_comm_world->barrier_cond  = SIMIX_cond_init();
70
71                 // mpi datatypes
72                 smpi_mpi_global->mpi_byte                      = xbt_new(s_smpi_mpi_datatype_t, 1);
73                 smpi_mpi_global->mpi_byte->size                = (size_t)1;
74                 smpi_mpi_global->mpi_int                       = xbt_new(s_smpi_mpi_datatype_t, 1);
75                 smpi_mpi_global->mpi_int->size                 = sizeof(int);
76                 smpi_mpi_global->mpi_double                    = xbt_new(s_smpi_mpi_datatype_t, 1);
77                 smpi_mpi_global->mpi_double->size              = sizeof(double);
78
79                 // mpi operations
80                 smpi_mpi_global->mpi_land                      = xbt_new(s_smpi_mpi_op_t, 1);
81                 smpi_mpi_global->mpi_land->func                = smpi_mpi_land_func;
82                 smpi_mpi_global->mpi_sum                       = xbt_new(s_smpi_mpi_op_t, 1);
83                 smpi_mpi_global->mpi_sum->func                 = smpi_mpi_sum_func;
84
85                 // signal all nodes to perform initialization
86                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
87                 smpi_global->root_ready = 1;
88                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
89                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
90
91         } else {
92
93                 // make sure root is done before own initialization
94                 SIMIX_mutex_lock(smpi_global->start_stop_mutex);
95                 if (!smpi_global->root_ready) {
96                         SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
97                 }
98                 SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
99
100                 smpi_mpi_global->mpi_comm_world->processes[smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world)] = process;
101         }
102
103         // wait for all nodes to signal initializatin complete
104         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
105         smpi_global->ready_process_count++;
106         if (smpi_global->ready_process_count < 3 * size) {
107                 SIMIX_cond_wait(smpi_global->start_stop_cond, smpi_global->start_stop_mutex);
108         } else {
109                 SIMIX_cond_broadcast(smpi_global->start_stop_cond);
110         }
111         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
112
113         return;
114 }
115
116 void smpi_mpi_finalize()
117 {
118         int i;
119
120         SIMIX_mutex_lock(smpi_global->running_hosts_count_mutex);
121         i = --smpi_global->running_hosts_count;
122         SIMIX_mutex_unlock(smpi_global->running_hosts_count_mutex);
123
124         SIMIX_mutex_lock(smpi_global->start_stop_mutex);
125         smpi_global->ready_process_count--;
126         SIMIX_mutex_unlock(smpi_global->start_stop_mutex);
127
128         if (0 >= i) {
129
130                 // wake up senders/receivers
131                 for (i = 0; i < smpi_mpi_global->mpi_comm_world->size; i++) {
132                         if (SIMIX_process_is_suspended(smpi_global->sender_processes[i])) {
133                                 SIMIX_process_resume(smpi_global->sender_processes[i]);
134                         }
135                         if (SIMIX_process_is_suspended(smpi_global->receiver_processes[i])) {
136                                 SIMIX_process_resume(smpi_global->receiver_processes[i]);
137                         }
138                 }
139
140                 SIMIX_mutex_destroy(smpi_mpi_global->mpi_comm_world->barrier_mutex);
141                 SIMIX_cond_destroy(smpi_mpi_global->mpi_comm_world->barrier_cond);
142                 xbt_free(smpi_mpi_global->mpi_comm_world->processes);
143                 xbt_free(smpi_mpi_global->mpi_comm_world);
144
145                 xbt_free(smpi_mpi_global->mpi_byte);
146                 xbt_free(smpi_mpi_global->mpi_int);
147                 xbt_free(smpi_mpi_global->mpi_double);
148
149                 xbt_free(smpi_mpi_global->mpi_land);
150                 xbt_free(smpi_mpi_global->mpi_sum);
151
152                 xbt_free(smpi_mpi_global);
153
154         }
155
156 }
157
158 int smpi_mpi_barrier(smpi_mpi_communicator_t comm)
159 {
160
161         SIMIX_mutex_lock(comm->barrier_mutex);
162         if(++comm->barrier_count < comm->size) {
163                 SIMIX_cond_wait(comm->barrier_cond, comm->barrier_mutex);
164         } else {
165                 comm->barrier_count = 0;
166                 SIMIX_cond_broadcast(comm->barrier_cond);
167         }
168         SIMIX_mutex_unlock(comm->barrier_mutex);
169
170         return MPI_SUCCESS;
171 }
172
173 int smpi_mpi_isend(smpi_mpi_request_t request)
174 {
175         int retval = MPI_SUCCESS;
176         int rank   = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
177
178         if (NULL == request) {
179                 retval = MPI_ERR_INTERN;
180         } else {
181                 SIMIX_mutex_lock(smpi_global->pending_send_request_queues_mutexes[rank]);
182                 xbt_fifo_push(smpi_global->pending_send_request_queues[rank], request);
183                 SIMIX_mutex_unlock(smpi_global->pending_send_request_queues_mutexes[rank]);
184
185                 if (SIMIX_process_is_suspended(smpi_global->sender_processes[rank])) {
186                         SIMIX_process_resume(smpi_global->sender_processes[rank]);
187                 }
188         }
189
190         return retval;
191 }
192
193 int smpi_mpi_irecv(smpi_mpi_request_t request)
194 {
195         int retval = MPI_SUCCESS;
196         int rank = smpi_mpi_comm_rank_self(smpi_mpi_global->mpi_comm_world);
197
198         if (NULL == request) {
199                 retval = MPI_ERR_INTERN;
200         } else {
201                 SIMIX_mutex_lock(smpi_global->pending_recv_request_queues_mutexes[rank]);
202                 xbt_fifo_push(smpi_global->pending_recv_request_queues[rank], request);
203                 SIMIX_mutex_unlock(smpi_global->pending_recv_request_queues_mutexes[rank]);
204
205                 if (SIMIX_process_is_suspended(smpi_global->receiver_processes[rank])) {
206                         SIMIX_process_resume(smpi_global->receiver_processes[rank]);
207                 }
208         }
209
210         return retval;
211 }
212
213 int smpi_mpi_wait(smpi_mpi_request_t request, smpi_mpi_status_t *status)
214 {
215         int retval = MPI_SUCCESS;
216
217         if (NULL == request) {
218                 retval = MPI_ERR_INTERN;
219         } else {
220                 SIMIX_mutex_lock(request->mutex);
221                 if (!request->completed) {
222                         SIMIX_cond_wait(request->cond, request->mutex);
223                 }
224                 if (NULL != status) {
225                         status->MPI_SOURCE = request->src;
226                         status->MPI_TAG    = request->tag;
227                         status->MPI_ERROR  = MPI_SUCCESS;
228                 }
229                 SIMIX_mutex_unlock(request->mutex);
230         }
231
232         return retval;
233 }