Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added MPI_CHAR to predefined reduce operations (not in the standard, but convenient...
[simgrid.git] / src / smpi / smpi_base.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/time.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
11                                 "Logging specific to SMPI (base)");
12 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
15 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
16 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
18 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
19 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
20 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
21
22 void smpi_process_init(int* argc, char*** argv) {
23   int index;
24   smpi_process_data_t data;
25   smx_process_t proc;
26
27   proc = SIMIX_process_self();
28   index = atoi((*argv)[1]);
29   data = smpi_process_remote_data(index);
30   SIMIX_process_set_data(proc, data);
31   if (*argc > 2) {
32     free((*argv)[1]);
33     memmove(&(*argv)[1], &(*argv)[2], sizeof(char *) * (*argc - 2));
34     (*argv)[(*argc) - 1] = NULL;
35   }
36   (*argc)--;
37   DEBUG2("<%d> New process in the game: %p", index, proc);
38 }
39
40 void smpi_process_destroy(void) {
41   int index = smpi_process_index();
42
43   DEBUG1("<%d> Process left the game", index);
44 }
45
46 static MPI_Request build_request(void* buf, int count, MPI_Datatype datatype, int src, int dst, int tag, MPI_Comm comm, unsigned flags) {
47   MPI_Request request;
48
49   request = xbt_new(s_smpi_mpi_request_t, 1);
50   request->buf = buf;
51   request->size = smpi_datatype_size(datatype) * count;
52   request->src = src;
53   request->dst = dst;
54   request->tag = tag;
55   request->comm = comm;
56   request->rdv = NULL;
57   request->pair = NULL;
58   request->complete = 0;
59   request->match = MPI_REQUEST_NULL;
60   request->flags = flags;
61   return request;
62 }
63
64 /* MPI Low level calls */
65 MPI_Request smpi_mpi_send_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
66   MPI_Request request = build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag, comm, PERSISTENT | SEND);
67
68   return request;
69 }
70
71 MPI_Request smpi_mpi_recv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
72   MPI_Request request = build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag, comm, PERSISTENT | RECV);
73
74   return request;
75 }
76
77 void smpi_mpi_start(MPI_Request request) {
78   xbt_assert0(request->complete == 0, "Cannot start a non-finished communication");
79   if((request->flags & RECV) == RECV) {
80     smpi_process_post_recv(request);
81     print_request("New recv", request);
82     request->pair = SIMIX_network_irecv(request->rdv, request->buf, &request->size);
83   } else {
84     smpi_process_post_send(request->comm, request); // FIXME
85     print_request("New send", request);
86     request->pair = SIMIX_network_isend(request->rdv, request->size, -1.0, request->buf, request->size, NULL);
87   }
88 }
89
90 void smpi_mpi_startall(int count, MPI_Request* requests) {
91   int i;
92
93   for(i = 0; i < count; i++) {
94     smpi_mpi_start(requests[i]);
95   }
96 }
97
98 void smpi_mpi_request_free(MPI_Request* request) {
99   xbt_free(*request);
100   *request = MPI_REQUEST_NULL;
101 }
102
103 MPI_Request smpi_isend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
104   MPI_Request request = build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag, comm, NON_PERSISTENT | SEND);
105
106   return request;
107 }
108
109 MPI_Request smpi_mpi_isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
110   MPI_Request request = smpi_isend_init(buf, count, datatype, dst, tag, comm);
111
112   smpi_mpi_start(request);
113   return request;
114 }
115
116 MPI_Request smpi_irecv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
117   MPI_Request request = build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag, comm, NON_PERSISTENT | RECV);
118
119   return request;
120 }
121
122 MPI_Request smpi_mpi_irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm) {
123   MPI_Request request = smpi_irecv_init(buf, count, datatype, src, tag, comm);
124
125   smpi_mpi_start(request);
126   return request;
127 }
128
129 void smpi_mpi_recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
130   MPI_Request request;
131
132   request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
133   smpi_mpi_wait(&request, status);
134 }
135
136 void smpi_mpi_send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
137   MPI_Request request;
138
139   request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
140   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
141 }
142
143 void smpi_mpi_sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status* status) {
144   MPI_Request requests[2];
145   MPI_Status stats[2];
146
147   requests[0] = smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
148   requests[1] = smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
149   smpi_mpi_startall(2, requests);
150   smpi_mpi_waitall(2, requests, stats);
151   if(status != MPI_STATUS_IGNORE) {
152     // Copy receive status
153     memcpy(status, &stats[1], sizeof(MPI_Status));
154   }
155 }
156
157 static void finish_wait(MPI_Request* request, MPI_Status* status) {
158   if(status != MPI_STATUS_IGNORE) {
159     status->MPI_SOURCE = (*request)->src;
160     status->MPI_TAG = (*request)->tag;
161     status->MPI_ERROR = MPI_SUCCESS;
162   }
163   print_request("finishing wait", *request);
164   if((*request)->complete == 1) {
165     SIMIX_rdv_destroy((*request)->rdv);
166   } else {
167     (*request)->match->complete = 1;
168     (*request)->match->match = MPI_REQUEST_NULL;
169   }
170   if(((*request)->flags & NON_PERSISTENT) == NON_PERSISTENT) {
171     smpi_mpi_request_free(request);
172   } else {
173     (*request)->rdv = NULL;
174     (*request)->pair = NULL;
175   }
176 }
177
178 int smpi_mpi_test(MPI_Request* request, MPI_Status* status) {
179   int flag = (*request)->complete;
180
181   if(flag) {
182     smpi_mpi_wait(request, status);
183   }
184   return flag;
185 }
186
187 int smpi_mpi_testany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
188   int i, flag;
189
190   *index = MPI_UNDEFINED;
191   flag = 0;
192   for(i = 0; i < count; i++) {
193     if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
194       smpi_mpi_wait(&requests[i], status);
195       *index = i;
196       flag = 1;
197       break;
198     }
199   }
200   return flag;
201 }
202
203 void smpi_mpi_wait(MPI_Request* request, MPI_Status* status) {
204   print_request("wait", *request);
205   SIMIX_network_wait((*request)->pair, -1.0);
206   finish_wait(request, status);
207 }
208
209 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status* status) {
210   xbt_dynar_t comms;
211   int i, size, index;
212   int* map;
213
214   index = MPI_UNDEFINED;
215   if(count > 0) {
216     // First check for already completed requests
217     for(i = 0; i < count; i++) {
218       if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
219         index = i;
220         smpi_mpi_wait(&requests[index], status);
221         break;
222       }
223     }
224     if(index == MPI_UNDEFINED) {
225       // Otherwise, wait for a request to complete
226       comms = xbt_dynar_new(sizeof(smx_comm_t), NULL);
227       map = xbt_new(int, count);
228       size = 0;
229       DEBUG0("Wait for one of");
230       for(i = 0; i < count; i++) {
231         if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete == 0) {
232           print_request("   ", requests[i]);
233           xbt_dynar_push(comms, &requests[i]->pair);
234           map[size] = i;
235           size++;
236         }
237       }
238       if(size > 0) {
239         index = SIMIX_network_waitany(comms);
240         index = map[index];
241         finish_wait(&requests[index], status);
242       }
243       xbt_free(map);
244       xbt_dynar_free(&comms);
245     }
246   }
247   return index;
248 }
249
250 void smpi_mpi_waitall(int count, MPI_Request requests[],  MPI_Status status[]) {
251   int index;
252   MPI_Status stat;
253
254   while(count > 0) {
255     index = smpi_mpi_waitany(count, requests, &stat);
256     if(index == MPI_UNDEFINED) {
257       break;
258     }
259     if(status != MPI_STATUS_IGNORE) {
260       memcpy(&status[index], &stat, sizeof(stat));
261     }
262     // FIXME: check this -v
263     // Move the last request to the found position
264     requests[index] = requests[count - 1];
265     requests[count - 1] = MPI_REQUEST_NULL;
266     count--;
267   }
268 }
269
270 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int* indices, MPI_Status status[]) {
271   int i, count;
272
273   count = 0;
274   for(i = 0; i < incount; i++) {
275     if(requests[i] != MPI_REQUEST_NULL && requests[i]->complete) {
276       smpi_mpi_wait(&requests[i], status != MPI_STATUS_IGNORE ? &status[i] : MPI_STATUS_IGNORE);
277       indices[count] = i;
278       count++;
279     }
280   }
281   return count;
282 }
283
284 void smpi_mpi_bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
285   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
286   nary_tree_bcast(buf, count, datatype, root, comm, 4);
287 }
288
289 void smpi_mpi_barrier(MPI_Comm comm) {
290   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
291   nary_tree_barrier(comm, 4);
292 }
293
294 void smpi_mpi_gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
295   int system_tag = 666;
296   int rank, size, src, index, sendsize, recvsize;
297   MPI_Request* requests;
298
299   rank = smpi_comm_rank(comm);
300   size = smpi_comm_size(comm);
301   if(rank != root) {
302     // Send buffer to root
303     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
304   } else {
305     sendsize = smpi_datatype_size(sendtype);
306     recvsize = smpi_datatype_size(recvtype);
307     // Local copy from root
308     memcpy(&((char*)recvbuf)[root * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
309     // Receive buffers from senders
310     requests = xbt_new(MPI_Request, size - 1);
311     index = 0;
312     for(src = 0; src < size; src++) {
313       if(src != root) {
314         requests[index] = smpi_irecv_init(&((char*)recvbuf)[src * recvcount * recvsize], recvcount, recvtype, src, system_tag, comm);
315         index++;
316       }
317     }
318     // Wait for completion of irecv's.
319     smpi_mpi_startall(size - 1, requests);
320     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
321     xbt_free(requests);
322   }
323 }
324
325 void smpi_mpi_gatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, int root, MPI_Comm comm) {
326   int system_tag = 666;
327   int rank, size, src, index, sendsize;
328   MPI_Request* requests;
329
330   rank = smpi_comm_rank(comm);
331   size = smpi_comm_size(comm);
332   if(rank != root) {
333     // Send buffer to root
334     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
335   } else {
336     sendsize = smpi_datatype_size(sendtype);
337     // Local copy from root
338     memcpy(&((char*)recvbuf)[displs[root]], sendbuf, sendcount * sendsize * sizeof(char));
339     // Receive buffers from senders
340     requests = xbt_new(MPI_Request, size - 1);
341     index = 0;
342     for(src = 0; src < size; src++) {
343       if(src != root) {
344         requests[index] = smpi_irecv_init(&((char*)recvbuf)[displs[src]], recvcounts[src], recvtype, src, system_tag, comm);
345         index++;
346       }
347     }
348     // Wait for completion of irecv's.
349     smpi_mpi_startall(size - 1, requests);
350     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
351     xbt_free(requests);
352   }
353 }
354
355 void smpi_mpi_allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
356   int system_tag = 666;
357   int rank, size, other, index, sendsize, recvsize;
358   MPI_Request* requests;
359
360   rank = smpi_comm_rank(comm);
361   size = smpi_comm_size(comm);
362   sendsize = smpi_datatype_size(sendtype);
363   recvsize = smpi_datatype_size(recvtype);
364   // Local copy from self
365   memcpy(&((char*)recvbuf)[rank * recvcount * recvsize], sendbuf, sendcount * sendsize * sizeof(char));
366   // Send/Recv buffers to/from others;
367   requests = xbt_new(MPI_Request, 2 * (size - 1));
368   index = 0;
369   for(other = 0; other < size; other++) {
370     if(other != rank) {
371       requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
372       index++;
373       requests[index] = smpi_irecv_init(&((char*)recvbuf)[other * recvcount * recvsize], recvcount, recvtype, other, system_tag, comm);
374       index++;
375     }
376   }
377   // Wait for completion of all comms.
378   smpi_mpi_startall(2 * (size - 1), requests);
379   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
380   xbt_free(requests);
381 }
382
383 void smpi_mpi_allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
384   int system_tag = 666;
385   int rank, size, other, index, sendsize, recvsize;
386   MPI_Request* requests;
387
388   rank = smpi_comm_rank(comm);
389   size = smpi_comm_size(comm);
390   sendsize = smpi_datatype_size(sendtype);
391   recvsize = smpi_datatype_size(recvtype);
392   // Local copy from self
393   memcpy(&((char*)recvbuf)[displs[rank]], sendbuf, sendcount * sendsize * sizeof(char));
394   // Send buffers to others;
395   requests = xbt_new(MPI_Request, 2 * (size - 1));
396   index = 0;
397   for(other = 0; other < size; other++) {
398     if(other != rank) {
399       requests[index] = smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag, comm);
400       index++;
401       requests[index] = smpi_irecv_init(&((char*)recvbuf)[displs[other]], recvcounts[other], recvtype, other, system_tag, comm);
402       index++;
403     }
404   }
405   // Wait for completion of all comms.
406   smpi_mpi_startall(2 * (size - 1), requests);
407   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
408   xbt_free(requests);
409 }
410
411 void smpi_mpi_scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
412   int system_tag = 666;
413   int rank, size, dst, index, sendsize, recvsize;
414   MPI_Request* requests;
415
416   rank = smpi_comm_rank(comm);
417   size = smpi_comm_size(comm);
418   if(rank != root) {
419     // Recv buffer from root
420     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
421   } else {
422     sendsize = smpi_datatype_size(sendtype);
423     recvsize = smpi_datatype_size(recvtype);
424     // Local copy from root
425     memcpy(recvbuf, &((char*)sendbuf)[root * sendcount * sendsize], recvcount * recvsize * sizeof(char));
426     // Send buffers to receivers
427     requests = xbt_new(MPI_Request, size - 1);
428     index = 0;
429     for(dst = 0; dst < size; dst++) {
430       if(dst != root) {
431         requests[index] = smpi_isend_init(&((char*)sendbuf)[dst * sendcount * sendsize], sendcount, sendtype, dst, system_tag, comm);
432         index++;
433       }
434     }
435     // Wait for completion of isend's.
436     smpi_mpi_startall(size - 1, requests);
437     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
438     xbt_free(requests);
439   }
440 }
441
442 void smpi_mpi_scatterv(void* sendbuf, int* sendcounts, int* displs, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
443   int system_tag = 666;
444   int rank, size, dst, index, sendsize, recvsize;
445   MPI_Request* requests;
446
447   rank = smpi_comm_rank(comm);
448   size = smpi_comm_size(comm);
449   if(rank != root) {
450     // Recv buffer from root
451     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm, MPI_STATUS_IGNORE);
452   } else {
453     sendsize = smpi_datatype_size(sendtype);
454     recvsize = smpi_datatype_size(recvtype);
455     // Local copy from root
456     memcpy(recvbuf, &((char*)sendbuf)[displs[root]], recvcount * recvsize * sizeof(char));
457     // Send buffers to receivers
458     requests = xbt_new(MPI_Request, size - 1);
459     index = 0;
460     for(dst = 0; dst < size; dst++) {
461       if(dst != root) {
462         requests[index] = smpi_isend_init(&((char*)sendbuf)[displs[dst]], sendcounts[dst], sendtype, dst, system_tag, comm);
463         index++;
464       }
465     }
466     // Wait for completion of isend's.
467     smpi_mpi_startall(size - 1, requests);
468     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
469     xbt_free(requests);
470   }
471 }
472
473 void smpi_mpi_reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
474   int system_tag = 666;
475   int rank, size, src, index, datasize;
476   MPI_Request* requests;
477   void** tmpbufs;
478
479   rank = smpi_comm_rank(comm);
480   size = smpi_comm_size(comm);
481   if(rank != root) {
482     // Send buffer to root
483     smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
484   } else {
485     datasize = smpi_datatype_size(datatype);
486     // Local copy from root
487     memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
488     // Receive buffers from senders
489     //TODO: make a MPI_barrier here ?
490     requests = xbt_new(MPI_Request, size - 1);
491     tmpbufs = xbt_new(void*, size - 1);
492     index = 0;
493     for(src = 0; src < size; src++) {
494       if(src != root) {
495         tmpbufs[index] = xbt_malloc(count * datasize);
496         requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
497         index++;
498       }
499     }
500     // Wait for completion of irecv's.
501     smpi_mpi_startall(size - 1, requests);
502     for(src = 0; src < size - 1; src++) {
503       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
504       if(index == MPI_UNDEFINED) {
505         break;
506       }
507       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
508     }
509     for(index = 0; index < size - 1; index++) {
510       xbt_free(tmpbufs[index]);
511     }
512     xbt_free(tmpbufs);
513     xbt_free(requests);
514   }
515 }
516
517 void smpi_mpi_allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
518   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
519   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
520
521 /*
522 FIXME: buggy implementation
523
524   int system_tag = 666;
525   int rank, size, other, index, datasize;
526   MPI_Request* requests;
527   void** tmpbufs;
528
529   rank = smpi_comm_rank(comm);
530   size = smpi_comm_size(comm);
531   datasize = smpi_datatype_size(datatype);
532   // Local copy from self
533   memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
534   // Send/Recv buffers to/from others;
535   //TODO: make a MPI_barrier here ?
536   requests = xbt_new(MPI_Request, 2 * (size - 1));
537   tmpbufs = xbt_new(void*, size - 1);
538   index = 0;
539   for(other = 0; other < size; other++) {
540     if(other != rank) {
541       tmpbufs[index / 2] = xbt_malloc(count * datasize);
542       requests[index] = smpi_mpi_isend(sendbuf, count, datatype, other, system_tag, comm);
543       requests[index + 1] = smpi_mpi_irecv(tmpbufs[index / 2], count, datatype, other, system_tag, comm);
544       index += 2;
545     }
546   }
547   // Wait for completion of all comms.
548   for(other = 0; other < 2 * (size - 1); other++) {
549     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
550     if(index == MPI_UNDEFINED) {
551       break;
552     }
553     if((index & 1) == 1) {
554       // Request is odd: it's a irecv
555       smpi_op_apply(op, tmpbufs[index / 2], recvbuf, &count, &datatype);
556     }
557   }
558   for(index = 0; index < size - 1; index++) {
559     xbt_free(tmpbufs[index]);
560   }
561   xbt_free(tmpbufs);
562   xbt_free(requests);
563 */
564 }
565
566 void smpi_mpi_scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
567   int system_tag = 666;
568   int rank, size, other, index, datasize;
569   int total;
570   MPI_Request* requests;
571   void** tmpbufs;
572
573   rank = smpi_comm_rank(comm);
574   size = smpi_comm_size(comm);
575   datasize = smpi_datatype_size(datatype);
576   // Local copy from self
577   memcpy(recvbuf, sendbuf, count * datasize * sizeof(char));
578   // Send/Recv buffers to/from others;
579   total = rank + (size - (rank + 1));
580   requests = xbt_new(MPI_Request, total);
581   tmpbufs = xbt_new(void*, rank);
582   index = 0;
583   for(other = 0; other < rank; other++) {
584     tmpbufs[index] = xbt_malloc(count * datasize);
585     requests[index] = smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag, comm);
586     index++;
587   }
588   for(other = rank + 1; other < size; other++) {
589     requests[index] = smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
590     index++;
591   }
592   // Wait for completion of all comms.
593   smpi_mpi_startall(size - 1, requests);
594   for(other = 0; other < total; other++) {
595     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
596     if(index == MPI_UNDEFINED) {
597       break;
598     }
599     if(index < rank) {
600       // #Request is below rank: it's a irecv
601       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
602     }
603   }
604   for(index = 0; index < size - 1; index++) {
605     xbt_free(tmpbufs[index]);
606   }
607   xbt_free(tmpbufs);
608   xbt_free(requests);
609 }