Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
haha! problem with call to get_remote_mailbox...code was using communicator rank
[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         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(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                        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(recvbuf + displs[src] * recvext, recvcounts[src], 
434                             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                      recvbuf + rank * recvcount * recvext, recvcount, recvtype);
462   // Send/Recv buffers to/from others;
463   requests = xbt_new(MPI_Request, 2 * (size - 1));
464   index = 0;
465   for(other = 0; other < size; other++) {
466     if(other != rank) {
467       requests[index] =
468           smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
469                           comm);
470       index++;
471       requests[index] = smpi_irecv_init(recvbuf + other * recvcount * recvext, 
472                                         recvcount, recvtype, other, 
473                                         system_tag, comm);
474       index++;
475     }
476   }
477   // Wait for completion of all comms.
478   smpi_mpi_startall(2 * (size - 1), requests);
479   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
480   xbt_free(requests);
481 }
482
483 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
484                          MPI_Datatype sendtype, void *recvbuf,
485                          int *recvcounts, int *displs,
486                          MPI_Datatype recvtype, MPI_Comm comm)
487 {
488   int system_tag = 666;
489   int rank, size, other, index;
490   MPI_Aint lb = 0, recvext = 0;
491   MPI_Request *requests;
492
493   rank = smpi_comm_rank(comm);
494   size = smpi_comm_size(comm);
495   // FIXME: check for errors
496   smpi_datatype_extent(recvtype, &lb, &recvext);
497   // Local copy from self
498   smpi_datatype_copy(sendbuf, sendcount, sendtype, 
499                      recvbuf + displs[rank] * recvext, 
500                      recvcounts[rank], recvtype);
501   // Send buffers to others;
502   requests = xbt_new(MPI_Request, 2 * (size - 1));
503   index = 0;
504   for(other = 0; other < size; other++) {
505     if(other != rank) {
506       requests[index] =
507           smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
508                           comm);
509       index++;
510       requests[index] =
511           smpi_irecv_init(recvbuf + displs[other] * recvext, recvcounts[other],
512                           recvtype, other, system_tag, comm);
513       index++;
514     }
515   }
516   // Wait for completion of all comms.
517   smpi_mpi_startall(2 * (size - 1), requests);
518   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
519   xbt_free(requests);
520 }
521
522 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
523                       void *recvbuf, int recvcount, MPI_Datatype recvtype,
524                       int root, MPI_Comm comm)
525 {
526   int system_tag = 666;
527   int rank, size, dst, index;
528   MPI_Aint lb = 0, sendext = 0;
529   MPI_Request *requests;
530
531   rank = smpi_comm_rank(comm);
532   size = smpi_comm_size(comm);
533   if(rank != root) {
534     // Recv buffer from root
535     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
536                   MPI_STATUS_IGNORE);
537   } else {
538     // FIXME: check for errors
539     smpi_datatype_extent(sendtype, &lb, &sendext);
540     // Local copy from root
541     smpi_datatype_copy(sendbuf + root * sendcount * sendext,
542       sendcount, sendtype, recvbuf, recvcount, recvtype);
543     // Send buffers to receivers
544     requests = xbt_new(MPI_Request, size - 1);
545     index = 0;
546     for(dst = 0; dst < size; dst++) {
547       if(dst != root) {
548         requests[index] = smpi_isend_init(sendbuf + dst * sendcount * sendext, 
549                                           sendcount, sendtype, dst,
550                                           system_tag, comm);
551         index++;
552       }
553     }
554     // Wait for completion of isend's.
555     smpi_mpi_startall(size - 1, requests);
556     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
557     xbt_free(requests);
558   }
559 }
560
561 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
562                        MPI_Datatype sendtype, void *recvbuf, int recvcount,
563                        MPI_Datatype recvtype, int root, MPI_Comm comm)
564 {
565   int system_tag = 666;
566   int rank, size, dst, index;
567   MPI_Aint lb = 0, sendext = 0;
568   MPI_Request *requests;
569
570   rank = smpi_comm_rank(comm);
571   size = smpi_comm_size(comm);
572   if(rank != root) {
573     // Recv buffer from root
574     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
575                   MPI_STATUS_IGNORE);
576   } else {
577     // FIXME: check for errors
578     smpi_datatype_extent(sendtype, &lb, &sendext);
579     // Local copy from root
580     smpi_datatype_copy(sendbuf + displs[root] * sendext, sendcounts[root], 
581                        sendtype, recvbuf, recvcount, recvtype);
582     // Send buffers to receivers
583     requests = xbt_new(MPI_Request, size - 1);
584     index = 0;
585     for(dst = 0; dst < size; dst++) {
586       if(dst != root) {
587         requests[index] =
588             smpi_isend_init(sendbuf + displs[dst] * sendext, sendcounts[dst], 
589                             sendtype, dst, system_tag, comm);
590         index++;
591       }
592     }
593     // Wait for completion of isend's.
594     smpi_mpi_startall(size - 1, requests);
595     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
596     xbt_free(requests);
597   }
598 }
599
600 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
601                      MPI_Datatype datatype, MPI_Op op, int root,
602                      MPI_Comm comm)
603 {
604   int system_tag = 666;
605   int rank, size, src, index;
606   MPI_Aint lb = 0, dataext = 0;
607   MPI_Request *requests;
608   void **tmpbufs;
609
610   rank = smpi_comm_rank(comm);
611   size = smpi_comm_size(comm);
612   if(rank != root) {
613     // Send buffer to root
614     smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
615   } else {
616     // FIXME: check for errors
617     smpi_datatype_extent(datatype, &lb, &dataext);
618     // Local copy from root
619     smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
620     // Receive buffers from senders
621     //TODO: make a MPI_barrier here ?
622     requests = xbt_new(MPI_Request, size - 1);
623     tmpbufs = xbt_new(void *, size - 1);
624     index = 0;
625     for(src = 0; src < size; src++) {
626       if(src != root) {
627         // FIXME: possibly overkill we we have contiguous/noncontiguous data
628         //  mapping...
629         tmpbufs[index] = xbt_malloc(count * dataext);
630         requests[index] =
631             smpi_irecv_init(tmpbufs[index], count, datatype, src,
632                             system_tag, comm);
633         index++;
634       }
635     }
636     // Wait for completion of irecv's.
637     smpi_mpi_startall(size - 1, requests);
638     for(src = 0; src < size - 1; src++) {
639       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
640       if(index == MPI_UNDEFINED) {
641         break;
642       }
643       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
644     }
645     for(index = 0; index < size - 1; index++) {
646       xbt_free(tmpbufs[index]);
647     }
648     xbt_free(tmpbufs);
649     xbt_free(requests);
650   }
651 }
652
653 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
654                         MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
655 {
656   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
657   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
658 }
659
660 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
661                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
662 {
663   int system_tag = 666;
664   int rank, size, other, index;
665   MPI_Aint lb = 0, dataext = 0;
666   MPI_Request *requests;
667   void **tmpbufs;
668
669   rank = smpi_comm_rank(comm);
670   size = smpi_comm_size(comm);
671
672   // FIXME: check for errors
673   smpi_datatype_extent(datatype, &lb, &dataext);
674
675   // Local copy from self
676   smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
677
678   // Send/Recv buffers to/from others;
679   requests = xbt_new(MPI_Request, size - 1);
680   tmpbufs = xbt_new(void *, rank);
681   index = 0;
682   for(other = 0; other < rank; other++) {
683     // FIXME: possibly overkill we we have contiguous/noncontiguous data 
684     // mapping...
685     tmpbufs[index] = xbt_malloc(count * dataext);
686     requests[index] =
687         smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
688                         comm);
689     index++;
690   }
691   for(other = rank + 1; other < size; other++) {
692     requests[index] =
693         smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
694     index++;
695   }
696   // Wait for completion of all comms.
697   smpi_mpi_startall(size - 1, requests);
698   for(other = 0; other < size - 1; other++) {
699     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
700     if(index == MPI_UNDEFINED) {
701       break;
702     }
703     if(index < rank) {
704       // #Request is below rank: it's a irecv
705       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
706     }
707   }
708   for(index = 0; index < rank; index++) {
709     xbt_free(tmpbufs[index]);
710   }
711   xbt_free(tmpbufs);
712   xbt_free(requests);
713 }