Logo AND Algorithmique Numérique Distribuée

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