Logo AND Algorithmique Numérique Distribuée

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