Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code cleanup, streamlining, removed some redundant function calls.
[simgrid.git] / src / smpi / smpi_mpi.c
1 // FIXME: remove
2 #include <stdio.h>
3 #include "private.h"
4
5 int MPI_Init(int *argc, char ***argv)
6 {
7         smpi_mpi_init();
8         smpi_bench_begin();
9         return MPI_SUCCESS;
10 }
11
12 int MPI_Finalize()
13 {
14         smpi_bench_end();
15         smpi_mpi_finalize();
16         return MPI_SUCCESS;
17 }
18
19 // right now this just exits the current node, should send abort signal to all
20 // hosts in the communicator;
21 int MPI_Abort(MPI_Comm comm, int errorcode)
22 {
23         smpi_exit(errorcode);
24         return 0;
25 }
26
27 int MPI_Comm_size(MPI_Comm comm, int *size)
28 {
29         int retval = MPI_SUCCESS;
30
31         smpi_bench_end();
32
33         if (NULL == comm) {
34                 retval = MPI_ERR_COMM;
35         } else if (NULL == size) {
36                 retval = MPI_ERR_ARG;
37         } else {
38                 *size = comm->size;
39         }
40
41         smpi_bench_begin();
42
43         return retval;
44 }
45
46 int MPI_Comm_rank(MPI_Comm comm, int *rank)
47 {
48         int retval = MPI_SUCCESS;
49
50         smpi_bench_end();
51
52         if (NULL == comm) {
53                 retval = MPI_ERR_COMM;
54         } else if (NULL == rank) {
55                 retval = MPI_ERR_ARG;
56         } else {
57                 *rank = smpi_mpi_comm_rank(comm);
58         }
59
60         smpi_bench_begin();
61
62         return retval;
63 }
64
65 int MPI_Type_size(MPI_Datatype datatype, size_t *size)
66 {
67         int retval = MPI_SUCCESS;
68
69         smpi_bench_end();
70
71         if (NULL == datatype) {
72                 retval = MPI_ERR_TYPE;
73         } else if (NULL == size) {
74                 retval = MPI_ERR_ARG;
75         } else {
76                 *size = datatype->size;
77         }
78
79         smpi_bench_begin();
80
81         return retval;
82 }
83
84 int MPI_Barrier(MPI_Comm comm)
85 {
86         int retval = MPI_SUCCESS;
87
88         smpi_bench_end();
89
90         if (NULL == comm) {
91                 retval = MPI_ERR_COMM;
92         } else {
93                 retval = smpi_mpi_barrier(comm);
94         }
95
96         smpi_bench_begin();
97
98         return retval;
99 }
100
101 int MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request *request)
102 {
103         int retval = MPI_SUCCESS;
104
105         smpi_bench_end();
106
107         retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm, request);
108         if (NULL != *request && MPI_SUCCESS == retval) {
109                 retval = smpi_mpi_irecv(*request);
110         }
111
112         smpi_bench_begin();
113
114         return retval;
115 }
116
117 int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status *status)
118 {
119         int retval = MPI_SUCCESS;
120         smpi_mpi_request_t request;
121
122         smpi_bench_end();
123
124         retval = smpi_create_request(buf, count, datatype, src, 0, tag, comm, &request);
125         if (NULL != request && MPI_SUCCESS == retval) {
126                 retval = smpi_mpi_irecv(request);
127                 if (MPI_SUCCESS == retval) {
128                         retval = smpi_mpi_wait(request, status);
129                 }
130                 xbt_mallocator_release(smpi_global->request_mallocator, request);
131         }
132
133         smpi_bench_begin();
134
135         return retval;
136 }
137
138 int MPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request *request)
139 {
140         int retval = MPI_SUCCESS;
141
142         smpi_bench_end();
143
144         retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm, request);
145         if (NULL != *request && MPI_SUCCESS == retval) {
146                 retval = smpi_mpi_isend(*request);
147         }
148
149         smpi_bench_begin();
150
151         return retval;
152 }
153
154 int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
155 {
156         int retval = MPI_SUCCESS;
157         smpi_mpi_request_t request;
158
159         smpi_bench_end();
160
161         retval = smpi_create_request(buf, count, datatype, 0, dst, tag, comm, &request);
162         if (NULL != request && MPI_SUCCESS == retval) {
163                 retval = smpi_mpi_isend(request);
164                 if (MPI_SUCCESS == retval) {
165                         smpi_mpi_wait(request, MPI_STATUS_IGNORE);
166                 }
167                 xbt_mallocator_release(smpi_global->request_mallocator, request);
168         }
169
170         smpi_bench_begin();
171
172         return retval;
173 }
174
175 int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
176
177         int retval = MPI_SUCCESS;
178         int rank;
179         smpi_mpi_request_t request;
180
181         smpi_bench_end();
182
183         rank = smpi_mpi_comm_rank(comm);
184
185         if (rank == root) {
186                 retval = smpi_create_request(buf, count, datatype, root, (root + 1) % comm->size, 0, comm, &request);
187                 request->forward = comm->size - 1;
188                 smpi_mpi_isend(request);
189         } else {
190                 retval = smpi_create_request(buf, count, datatype, MPI_ANY_SOURCE, rank, 0, comm, &request);
191                 smpi_mpi_irecv(request);
192         }
193
194         smpi_mpi_wait(request, MPI_STATUS_IGNORE);
195         xbt_mallocator_release(smpi_global->request_mallocator, request);
196
197         smpi_bench_begin();
198
199         return retval;
200 }
201
202 // FIXME: needs to return null in event of MPI_UNDEFINED color...
203 // FIXME: seriously, this isn't pretty
204 int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm *comm_out)
205 {
206         int retval = MPI_SUCCESS;
207
208         int index, rank;
209         smpi_mpi_request_t request;
210         int colorkey[2];
211         smpi_mpi_status_t status;
212
213         smpi_bench_end();
214
215         // FIXME: need to test parameters
216
217         index = smpi_host_index();
218         rank  = comm->index_to_rank_map[index];
219
220         if (0 == rank) {
221                 int *colors = xbt_new(int, comm->size);
222                 int *keys   = xbt_new(int, comm->size);
223                 int i, j, k;
224                 smpi_mpi_communicator_t tempcomm = NULL;
225                 int colortmp;
226                 int keycount;
227                 int *keystmp  = xbt_new(int, comm->size);
228                 int *rankstmp = xbt_new(int, comm->size);
229                 int tmpval;
230                 int indextmp;
231
232                 colors[0] = color;
233                 keys[0]   = key;
234
235                 // FIXME: not efficient
236                 for (i = 1; i < comm->size; i++) {
237                         retval = smpi_create_request(colorkey, 2, MPI_INT, MPI_ANY_SOURCE, rank, MPI_ANY_TAG, comm, &request);
238                         smpi_mpi_irecv(request);
239                         smpi_mpi_wait(request, &status);
240                         xbt_mallocator_release(smpi_global->request_mallocator, request);
241                         colors[i] = colorkey[0];
242                         keys[i]   = colorkey[1];
243                 }
244
245                 for (i = 0; i < comm->size; i++) {
246                         if (-1 == colors[i]) {
247                                 continue;
248                         }
249                         colortmp = colors[i];
250                         keycount = 0;
251                         for (j = i; j < comm->size; j++) {
252                                 if(colortmp == colors[j]) {
253                                         colors[j] = -1;
254                                         keystmp[keycount] = keys[j];
255                                         rankstmp[keycount] = j;
256                                         keycount++;
257                                 }
258                         }
259                         if (0 < keycount) {
260                                 // FIXME: yes, mock me, bubble sort...
261                                 for (j = 0; j < keycount; j++) {
262                                         for (k = keycount - 1; k > j; k--) {
263                                                 if (keystmp[k] < keystmp[k - 1]) {
264                                                         tmpval          = keystmp[k];
265                                                         keystmp[k]      = keystmp[k - 1];
266                                                         keystmp[k - 1]  = tmpval;
267
268                                                         tmpval          = rankstmp[k];
269                                                         rankstmp[k]     = rankstmp[k - 1];
270                                                         rankstmp[k - 1] = tmpval;
271                                                 }
272                                         }
273                                 }
274                                 tempcomm                    = xbt_new(s_smpi_mpi_communicator_t, 1);
275                                 tempcomm->barrier_count     = 0;
276                                 tempcomm->barrier_mutex     = SIMIX_mutex_init();
277                                 tempcomm->barrier_cond      = SIMIX_cond_init();
278                                 tempcomm->rank_to_index_map = xbt_new(int, keycount);
279                                 tempcomm->index_to_rank_map = xbt_new(int, smpi_global->host_count);
280                                 for (j = 0; j < smpi_global->host_count; j++) {
281                                         tempcomm->index_to_rank_map[j] = -1;
282                                 }
283                                 for (j = 0; j < keycount; j++) {
284                                         indextmp = comm->rank_to_index_map[rankstmp[j]];
285                                         tempcomm->rank_to_index_map[j]        = indextmp;
286                                         tempcomm->index_to_rank_map[indextmp] = j;
287                                 }
288                                 for (j = 0; j < keycount; j++) {
289                                         retval = smpi_create_request(&j, 1, MPI_INT, 0, rankstmp[j], 0, comm, &request);
290                                         request->data = tempcomm;
291                                         smpi_mpi_isend(request);
292                                         smpi_mpi_wait(request, &status);
293                                         xbt_mallocator_release(smpi_global->request_mallocator, request);
294                                 }
295                         }
296                 }
297         } else {
298                 colorkey[0] = color;
299                 colorkey[1] = key;
300                 retval = smpi_create_request(colorkey, 2, MPI_INT, rank, 0, 0, comm, &request);
301                 smpi_mpi_isend(request);
302                 smpi_mpi_wait(request, &status);
303                 xbt_mallocator_release(smpi_global->request_mallocator, request);
304                 retval = smpi_create_request(colorkey, 1, MPI_INT, 0, rank, 0, comm, &request);
305                 smpi_mpi_irecv(request);
306                 smpi_mpi_wait(request, &status);
307                 *comm_out = request->data;
308         }
309
310         smpi_bench_begin();
311
312         return retval;
313 }