Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix error: passing argument 7 of ‘SIMIX_req_comm_isend’ from incompatible pointer...
[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 #include "mc/mc.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
12                                 "Logging specific to SMPI (base)");
13 XBT_LOG_EXTERNAL_CATEGORY(smpi_base);
14 XBT_LOG_EXTERNAL_CATEGORY(smpi_bench);
15 XBT_LOG_EXTERNAL_CATEGORY(smpi_kernel);
16 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi);
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_mpi_dt);
18 XBT_LOG_EXTERNAL_CATEGORY(smpi_coll);
19 XBT_LOG_EXTERNAL_CATEGORY(smpi_receiver);
20 XBT_LOG_EXTERNAL_CATEGORY(smpi_sender);
21 XBT_LOG_EXTERNAL_CATEGORY(smpi_util);
22
23 static int match_recv(void* a, void* b) {
24    MPI_Request ref = (MPI_Request)a;
25    MPI_Request req = (MPI_Request)b;
26
27    xbt_assert(ref, "Cannot match recv against null reference");
28    xbt_assert(req, "Cannot match recv against null request");
29    return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
30           && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
31 }
32
33 static int match_send(void* a, void* b) {
34    MPI_Request ref = (MPI_Request)a;
35    MPI_Request req = (MPI_Request)b;
36
37    xbt_assert(ref, "Cannot match send against null reference");
38    xbt_assert(req, "Cannot match send against null request");
39    return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
40           && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
41 }
42
43 static MPI_Request build_request(void *buf, int count,
44                                  MPI_Datatype datatype, int src, int dst,
45                                  int tag, MPI_Comm comm, unsigned flags)
46 {
47   MPI_Request request;
48
49   request = xbt_new(s_smpi_mpi_request_t, 1);
50   request->buf = buf;
51   // FIXME: this will have to be changed to support non-contiguous datatypes
52   request->size = smpi_datatype_size(datatype) * count;
53   request->src = src;
54   request->dst = dst;
55   request->tag = tag;
56   request->comm = comm;
57   request->action = NULL;
58   request->flags = flags;
59 #ifdef HAVE_TRACING
60   request->send = 0;
61   request->recv = 0;
62 #endif
63   return request;
64 }
65
66 static void smpi_mpi_request_free_voidp(void* request)
67 {
68   smpi_mpi_request_free(request);
69 }
70
71 /* MPI Low level calls */
72 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
73                                int dst, int tag, MPI_Comm comm)
74 {
75   MPI_Request request =
76       build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
77                     comm, PERSISTENT | SEND);
78
79   return request;
80 }
81
82 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
83                                int src, int tag, MPI_Comm comm)
84 {
85   MPI_Request request =
86       build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
87                     comm, PERSISTENT | RECV);
88
89   return request;
90 }
91
92 void smpi_mpi_start(MPI_Request request)
93 {
94   smx_rdv_t mailbox;
95   int detached = 0;
96
97   xbt_assert(!request->action,
98               "Cannot (re)start a non-finished communication");
99   if(request->flags & RECV) {
100     print_request("New recv", request);
101     mailbox = smpi_process_mailbox();
102     // FIXME: SIMIX does not yet support non-contiguous datatypes
103     request->action = SIMIX_req_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
104   } else {
105     print_request("New send", request);
106     mailbox = smpi_process_remote_mailbox(
107                           smpi_group_index(smpi_comm_group(request->comm), request->dst));
108     // FIXME: SIMIX does not yet support non-contiguous datatypes
109
110     if (request->size < 64*1024 ) { // eager mode => detached send (FIXME: this limit should be configurable)
111         void *oldbuf = request->buf;
112         detached = 1;
113         request->buf = malloc(request->size);
114         memcpy(request->buf,oldbuf,request->size);
115         XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
116     } else {
117         XBT_DEBUG("Send request %p is not detached (buf: %p)",request,request->buf);
118     }
119     request->action = 
120                 SIMIX_req_comm_isend(mailbox, request->size, -1.0,
121                                     request->buf, request->size,
122                                     &match_send,
123                                     &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
124                                     request,
125                                     // detach if msg size < eager/rdv switch limit
126                                     detached);
127
128 #ifdef HAVE_TRACING
129     SIMIX_req_set_category (request->action, TRACE_internal_smpi_get_category());
130 #endif
131   }
132 }
133
134 void smpi_mpi_startall(int count, MPI_Request * requests)
135 {
136           int i;
137
138   for(i = 0; i < count; i++) {
139     smpi_mpi_start(requests[i]);
140   }
141 }
142
143 void smpi_mpi_request_free(MPI_Request * request)
144 {
145   xbt_free(*request);
146   *request = MPI_REQUEST_NULL;
147 }
148
149 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
150                             int dst, int tag, MPI_Comm comm)
151 {
152   MPI_Request request =
153       build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
154                     comm, NON_PERSISTENT | SEND);
155
156   return request;
157 }
158
159 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
160                            int dst, int tag, MPI_Comm comm)
161 {
162   MPI_Request request =
163       smpi_isend_init(buf, count, datatype, dst, tag, comm);
164
165   smpi_mpi_start(request);
166   return request;
167 }
168
169 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
170                             int src, int tag, MPI_Comm comm)
171 {
172   MPI_Request request =
173       build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
174                     comm, NON_PERSISTENT | RECV);
175
176   return request;
177 }
178
179 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
180                            int src, int tag, MPI_Comm comm)
181 {
182   MPI_Request request =
183       smpi_irecv_init(buf, count, datatype, src, tag, comm);
184
185   smpi_mpi_start(request);
186   return request;
187 }
188
189 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
190                    int tag, MPI_Comm comm, MPI_Status * status)
191 {
192   MPI_Request request;
193
194   request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
195   smpi_mpi_wait(&request, status);
196 }
197
198 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
199                    int tag, MPI_Comm comm)
200 {
201   MPI_Request request;
202
203   request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
204   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
205 }
206
207 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
208                        int dst, int sendtag, void *recvbuf, int recvcount,
209                        MPI_Datatype recvtype, int src, int recvtag,
210                        MPI_Comm comm, MPI_Status * status)
211 {
212   MPI_Request requests[2];
213   MPI_Status stats[2];
214
215   requests[0] =
216       smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
217   requests[1] =
218       smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
219   smpi_mpi_startall(2, requests);
220   smpi_mpi_waitall(2, requests, stats);
221   if(status != MPI_STATUS_IGNORE) {
222     // Copy receive status
223     memcpy(status, &stats[1], sizeof(MPI_Status));
224   }
225 }
226
227 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
228 {
229   return status->count / smpi_datatype_size(datatype);
230 }
231
232 static void finish_wait(MPI_Request * request, MPI_Status * status)
233 {
234   MPI_Request req = *request;
235
236   if(status != MPI_STATUS_IGNORE) {
237     status->MPI_SOURCE = req->src;
238     status->MPI_TAG = req->tag;
239     status->MPI_ERROR = MPI_SUCCESS;
240     // FIXME: really this should just contain the count of receive-type blocks,
241     // right?
242     status->count = req->size;
243   }
244   print_request("Finishing", req);
245   if(req->flags & NON_PERSISTENT) {
246     smpi_mpi_request_free(request);
247   } else {
248     req->action = NULL;
249   }
250 }
251
252 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
253 int flag;
254
255    if ((*request)->action == NULL)
256         flag = 1;
257    else 
258     flag = SIMIX_req_comm_test((*request)->action);
259    if(flag) {
260                     smpi_mpi_wait(request, status);
261           }
262           return flag;
263 }
264
265 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
266                      MPI_Status * status)
267 {
268   xbt_dynar_t comms;
269   int i, flag, size;
270   int* map;
271
272   *index = MPI_UNDEFINED;
273   flag = 0;
274   if(count > 0) {
275     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
276     map = xbt_new(int, count);
277     size = 0;
278     for(i = 0; i < count; i++) {
279       if(requests[i]->action) {
280          xbt_dynar_push(comms, &requests[i]->action);
281          map[size] = i;
282          size++;
283       }
284     }
285     if(size > 0) {
286       i = SIMIX_req_comm_testany(comms);
287       // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
288       if(i != MPI_UNDEFINED) {
289         *index = map[i];
290         smpi_mpi_wait(&requests[*index], status);
291         flag = 1;
292       }
293     }
294     xbt_free(map);
295     xbt_dynar_free(&comms);
296   }
297   return flag;
298 }
299
300 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
301 {
302   print_request("Waiting", *request);
303   if ((*request)->action != NULL) { // this is not a detached send
304     SIMIX_req_comm_wait((*request)->action, -1.0);
305     finish_wait(request, status);
306   }
307   // FIXME for a detached send, finish_wait is not called:
308 }
309
310 int smpi_mpi_waitany(int count, MPI_Request requests[],
311                      MPI_Status * status)
312 {
313   xbt_dynar_t comms;
314   int i, size, index;
315   int *map;
316
317   index = MPI_UNDEFINED;
318   if(count > 0) {
319     // Wait for a request to complete
320     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
321     map = xbt_new(int, count);
322     size = 0;
323     XBT_DEBUG("Wait for one of");
324     for(i = 0; i < count; i++) {
325       if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
326         print_request("   ", requests[i]);
327         xbt_dynar_push(comms, &requests[i]->action);
328         map[size] = i;
329         size++;
330       }
331     }
332     if(size > 0) {
333       i = SIMIX_req_comm_waitany(comms);
334       // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
335       if (i != MPI_UNDEFINED) {
336         index = map[i];
337         finish_wait(&requests[index], status);
338       }
339     }
340     xbt_free(map);
341     xbt_dynar_free(&comms);
342   }
343   return index;
344 }
345
346 void smpi_mpi_waitall(int count, MPI_Request requests[],
347                       MPI_Status status[])
348 {
349   int index, c;
350   MPI_Status stat;
351   MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
352
353   for(c = 0; c < count; c++) {
354     if(MC_IS_ENABLED) {
355       smpi_mpi_wait(&requests[c], pstat);
356       index = c;
357     } else {
358       index = smpi_mpi_waitany(count, requests, pstat);
359       if(index == MPI_UNDEFINED) {
360         break;
361       }
362     }
363     if(status != MPI_STATUS_IGNORE) {
364       memcpy(&status[index], pstat, sizeof(*pstat));
365     }
366   }
367 }
368
369 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
370                       MPI_Status status[])
371 {
372   int i, count, index;
373
374   count = 0;
375   for(i = 0; i < incount; i++) {
376      if(smpi_mpi_testany(incount, requests, &index, status)) {
377        indices[count] = index;
378        count++;
379      }
380   }
381   return count;
382 }
383
384 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
385                     MPI_Comm comm)
386 {
387   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
388   nary_tree_bcast(buf, count, datatype, root, comm, 4);
389 }
390
391 void smpi_mpi_barrier(MPI_Comm comm)
392 {
393   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
394   nary_tree_barrier(comm, 4);
395 }
396
397 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
398                      void *recvbuf, int recvcount, MPI_Datatype recvtype,
399                      int root, MPI_Comm comm)
400 {
401   int system_tag = 666;
402   int rank, size, src, index;
403   MPI_Aint lb = 0, recvext = 0;
404   MPI_Request *requests;
405
406   rank = smpi_comm_rank(comm);
407   size = smpi_comm_size(comm);
408   if(rank != root) {
409     // Send buffer to root
410     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
411   } else {
412     // FIXME: check for errors
413     smpi_datatype_extent(recvtype, &lb, &recvext);
414     // Local copy from root
415     smpi_datatype_copy(sendbuf, sendcount, sendtype, 
416         (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
417     // Receive buffers from senders
418     requests = xbt_new(MPI_Request, size - 1);
419     index = 0;
420     for(src = 0; src < size; src++) {
421       if(src != root) {
422         requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext, 
423                                           recvcount, recvtype, 
424                                           src, system_tag, comm);
425         index++;
426       }
427     }
428     // Wait for completion of irecv's.
429     smpi_mpi_startall(size - 1, requests);
430     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
431     xbt_free(requests);
432   }
433 }
434
435 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
436                       void *recvbuf, int *recvcounts, int *displs,
437                       MPI_Datatype recvtype, int root, MPI_Comm comm)
438 {
439   int system_tag = 666;
440   int rank, size, src, index;
441   MPI_Aint lb = 0, recvext = 0;
442   MPI_Request *requests;
443
444   rank = smpi_comm_rank(comm);
445   size = smpi_comm_size(comm);
446   if(rank != root) {
447     // Send buffer to root
448     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
449   } else {
450     // FIXME: check for errors
451     smpi_datatype_extent(recvtype, &lb, &recvext);
452     // Local copy from root
453     smpi_datatype_copy(sendbuf, sendcount, sendtype, 
454                        (char *)recvbuf + displs[root] * recvext, 
455                        recvcounts[root], recvtype);
456     // Receive buffers from senders
457     requests = xbt_new(MPI_Request, size - 1);
458     index = 0;
459     for(src = 0; src < size; src++) {
460       if(src != root) {
461         requests[index] =
462             smpi_irecv_init((char *)recvbuf + displs[src] * recvext, 
463                             recvcounts[src], recvtype, src, system_tag, comm);
464         index++;
465       }
466     }
467     // Wait for completion of irecv's.
468     smpi_mpi_startall(size - 1, requests);
469     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
470     xbt_free(requests);
471   }
472 }
473
474 void smpi_mpi_allgather(void *sendbuf, int sendcount,
475                         MPI_Datatype sendtype, void *recvbuf,
476                         int recvcount, MPI_Datatype recvtype,
477                         MPI_Comm comm)
478 {
479   int system_tag = 666;
480   int rank, size, other, index;
481   MPI_Aint lb = 0, recvext = 0;
482   MPI_Request *requests;
483
484   rank = smpi_comm_rank(comm);
485   size = smpi_comm_size(comm);
486   // FIXME: check for errors
487   smpi_datatype_extent(recvtype, &lb, &recvext);
488   // Local copy from self
489   smpi_datatype_copy(sendbuf, sendcount, sendtype, 
490                      (char *)recvbuf + rank * recvcount * recvext, recvcount, 
491                      recvtype);
492   // Send/Recv buffers to/from others;
493   requests = xbt_new(MPI_Request, 2 * (size - 1));
494   index = 0;
495   for(other = 0; other < size; other++) {
496     if(other != rank) {
497       requests[index] =
498           smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
499                           comm);
500       index++;
501       requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext, 
502                                         recvcount, recvtype, other, 
503                                         system_tag, comm);
504       index++;
505     }
506   }
507   // Wait for completion of all comms.
508   smpi_mpi_startall(2 * (size - 1), requests);
509   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
510   xbt_free(requests);
511 }
512
513 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
514                          MPI_Datatype sendtype, void *recvbuf,
515                          int *recvcounts, int *displs,
516                          MPI_Datatype recvtype, MPI_Comm comm)
517 {
518   int system_tag = 666;
519   int rank, size, other, index;
520   MPI_Aint lb = 0, recvext = 0;
521   MPI_Request *requests;
522
523   rank = smpi_comm_rank(comm);
524   size = smpi_comm_size(comm);
525   // FIXME: check for errors
526   smpi_datatype_extent(recvtype, &lb, &recvext);
527   // Local copy from self
528   smpi_datatype_copy(sendbuf, sendcount, sendtype, 
529                      (char *)recvbuf + displs[rank] * recvext, 
530                      recvcounts[rank], recvtype);
531   // Send buffers to others;
532   requests = xbt_new(MPI_Request, 2 * (size - 1));
533   index = 0;
534   for(other = 0; other < size; other++) {
535     if(other != rank) {
536       requests[index] =
537           smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
538                           comm);
539       index++;
540       requests[index] =
541           smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
542                           recvtype, other, system_tag, comm);
543       index++;
544     }
545   }
546   // Wait for completion of all comms.
547   smpi_mpi_startall(2 * (size - 1), requests);
548   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
549   xbt_free(requests);
550 }
551
552 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
553                       void *recvbuf, int recvcount, MPI_Datatype recvtype,
554                       int root, MPI_Comm comm)
555 {
556   int system_tag = 666;
557   int rank, size, dst, index;
558   MPI_Aint lb = 0, sendext = 0;
559   MPI_Request *requests;
560
561   rank = smpi_comm_rank(comm);
562   size = smpi_comm_size(comm);
563   if(rank != root) {
564     // Recv buffer from root
565     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
566                   MPI_STATUS_IGNORE);
567   } else {
568     // FIXME: check for errors
569     smpi_datatype_extent(sendtype, &lb, &sendext);
570     // Local copy from root
571     smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
572       sendcount, sendtype, recvbuf, recvcount, recvtype);
573     // Send buffers to receivers
574     requests = xbt_new(MPI_Request, size - 1);
575     index = 0;
576     for(dst = 0; dst < size; dst++) {
577       if(dst != root) {
578         requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext, 
579                                           sendcount, sendtype, dst,
580                                           system_tag, comm);
581         index++;
582       }
583     }
584     // Wait for completion of isend's.
585     smpi_mpi_startall(size - 1, requests);
586     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
587     xbt_free(requests);
588   }
589 }
590
591 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
592                        MPI_Datatype sendtype, void *recvbuf, int recvcount,
593                        MPI_Datatype recvtype, int root, MPI_Comm comm)
594 {
595   int system_tag = 666;
596   int rank, size, dst, index;
597   MPI_Aint lb = 0, sendext = 0;
598   MPI_Request *requests;
599
600   rank = smpi_comm_rank(comm);
601   size = smpi_comm_size(comm);
602   if(rank != root) {
603     // Recv buffer from root
604     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
605                   MPI_STATUS_IGNORE);
606   } else {
607     // FIXME: check for errors
608     smpi_datatype_extent(sendtype, &lb, &sendext);
609     // Local copy from root
610     smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root], 
611                        sendtype, recvbuf, recvcount, recvtype);
612     // Send buffers to receivers
613     requests = xbt_new(MPI_Request, size - 1);
614     index = 0;
615     for(dst = 0; dst < size; dst++) {
616       if(dst != root) {
617         requests[index] =
618             smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst], 
619                             sendtype, dst, system_tag, comm);
620         index++;
621       }
622     }
623     // Wait for completion of isend's.
624     smpi_mpi_startall(size - 1, requests);
625     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
626     xbt_free(requests);
627   }
628 }
629
630 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
631                      MPI_Datatype datatype, MPI_Op op, int root,
632                      MPI_Comm comm)
633 {
634   int system_tag = 666;
635   int rank, size, src, index;
636   MPI_Aint lb = 0, dataext = 0;
637   MPI_Request *requests;
638   void **tmpbufs;
639
640   rank = smpi_comm_rank(comm);
641   size = smpi_comm_size(comm);
642   if(rank != root) {
643     // Send buffer to root
644     smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
645   } else {
646     // FIXME: check for errors
647     smpi_datatype_extent(datatype, &lb, &dataext);
648     // Local copy from root
649     smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
650     // Receive buffers from senders
651     //TODO: make a MPI_barrier here ?
652     requests = xbt_new(MPI_Request, size - 1);
653     tmpbufs = xbt_new(void *, size - 1);
654     index = 0;
655     for(src = 0; src < size; src++) {
656       if(src != root) {
657         // FIXME: possibly overkill we we have contiguous/noncontiguous data
658         //  mapping...
659         tmpbufs[index] = xbt_malloc(count * dataext);
660         requests[index] =
661             smpi_irecv_init(tmpbufs[index], count, datatype, src,
662                             system_tag, comm);
663         index++;
664       }
665     }
666     // Wait for completion of irecv's.
667     smpi_mpi_startall(size - 1, requests);
668     for(src = 0; src < size - 1; src++) {
669       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
670       if(index == MPI_UNDEFINED) {
671         break;
672       }
673       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
674     }
675     for(index = 0; index < size - 1; index++) {
676       xbt_free(tmpbufs[index]);
677     }
678     xbt_free(tmpbufs);
679     xbt_free(requests);
680   }
681 }
682
683 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
684                         MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
685 {
686   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
687   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
688 }
689
690 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
691                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
692 {
693   int system_tag = 666;
694   int rank, size, other, index;
695   MPI_Aint lb = 0, dataext = 0;
696   MPI_Request *requests;
697   void **tmpbufs;
698
699   rank = smpi_comm_rank(comm);
700   size = smpi_comm_size(comm);
701
702   // FIXME: check for errors
703   smpi_datatype_extent(datatype, &lb, &dataext);
704
705   // Local copy from self
706   smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
707
708   // Send/Recv buffers to/from others;
709   requests = xbt_new(MPI_Request, size - 1);
710   tmpbufs = xbt_new(void *, rank);
711   index = 0;
712   for(other = 0; other < rank; other++) {
713     // FIXME: possibly overkill we we have contiguous/noncontiguous data 
714     // mapping...
715     tmpbufs[index] = xbt_malloc(count * dataext);
716     requests[index] =
717         smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
718                         comm);
719     index++;
720   }
721   for(other = rank + 1; other < size; other++) {
722     requests[index] =
723         smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
724     index++;
725   }
726   // Wait for completion of all comms.
727   smpi_mpi_startall(size - 1, requests);
728   for(other = 0; other < size - 1; other++) {
729     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
730     if(index == MPI_UNDEFINED) {
731       break;
732     }
733     if(index < rank) {
734       // #Request is below rank: it's a irecv
735       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
736     }
737   }
738   for(index = 0; index < rank; index++) {
739     xbt_free(tmpbufs[index]);
740   }
741   xbt_free(tmpbufs);
742   xbt_free(requests);
743 }