Logo AND Algorithmique Numérique Distribuée

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