Logo AND Algorithmique Numérique Distribuée

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