Logo AND Algorithmique Numérique Distribuée

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