Logo AND Algorithmique Numérique Distribuée

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