Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
458eb849cc87c2b3f420272196b50216fb7bf47c
[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 #include "xbt/replay.h"
11 #include <errno.h>
12 #include "surf/surf.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi,
15                                 "Logging specific to SMPI (base)");
16
17 static int match_recv(void* a, void* b, smx_action_t ignored) {
18    MPI_Request ref = (MPI_Request)a;
19    MPI_Request req = (MPI_Request)b;
20
21    xbt_assert(ref, "Cannot match recv against null reference");
22    xbt_assert(req, "Cannot match recv against null request");
23    return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
24           && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
25 }
26
27 static int match_send(void* a, void* b,smx_action_t ignored) {
28    MPI_Request ref = (MPI_Request)a;
29    MPI_Request req = (MPI_Request)b;
30
31    xbt_assert(ref, "Cannot match send against null reference");
32    xbt_assert(req, "Cannot match send against null request");
33    return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
34           && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
35 }
36
37 static MPI_Request build_request(void *buf, int count,
38                                  MPI_Datatype datatype, int src, int dst,
39                                  int tag, MPI_Comm comm, unsigned flags)
40 {
41   MPI_Request request;
42
43   request = xbt_new(s_smpi_mpi_request_t, 1);
44   request->buf = buf;
45   // FIXME: this will have to be changed to support non-contiguous datatypes
46   request->size = smpi_datatype_size(datatype) * count;
47   request->src = src;
48   request->dst = dst;
49   request->tag = tag;
50   request->comm = comm;
51   request->action = NULL;
52   request->flags = flags;
53 #ifdef HAVE_TRACING
54   request->send = 0;
55   request->recv = 0;
56 #endif
57   return request;
58 }
59
60 void smpi_action_trace_run(char *path)
61 {
62   char *name;
63   xbt_dynar_t todo;
64   xbt_dict_cursor_t cursor;
65
66   action_fp=NULL;
67   if (path) {
68     action_fp = fopen(path, "r");
69     xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
70                 strerror(errno));
71   }
72
73   if (!xbt_dict_is_empty(action_queues)) {
74     XBT_WARN
75         ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
76
77
78     xbt_dict_foreach(action_queues, cursor, name, todo) {
79       XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
80     }
81   }
82
83   if (path)
84     fclose(action_fp);
85   xbt_dict_free(&action_queues);
86   action_queues = xbt_dict_new_homogeneous(NULL);
87 }
88
89 static void smpi_mpi_request_free_voidp(void* request)
90 {
91   MPI_Request req = request;
92   smpi_mpi_request_free(&req);
93 }
94
95 /* MPI Low level calls */
96 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
97                                int dst, int tag, MPI_Comm comm)
98 {
99   MPI_Request request =
100       build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
101                     comm, PERSISTENT | SEND);
102
103   return request;
104 }
105
106 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
107                                int src, int tag, MPI_Comm comm)
108 {
109   MPI_Request request =
110       build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
111                     comm, PERSISTENT | RECV);
112
113   return request;
114 }
115
116 void smpi_mpi_start(MPI_Request request)
117 {
118   smx_rdv_t mailbox;
119   int detached = 0;
120
121   xbt_assert(!request->action,
122               "Cannot (re)start a non-finished communication");
123   if(request->flags & RECV) {
124     print_request("New recv", request);
125     if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres"))
126     mailbox = smpi_process_mailbox_small();
127     else
128     mailbox = smpi_process_mailbox();
129
130     // FIXME: SIMIX does not yet support non-contiguous datatypes
131     request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
132   } else {
133     print_request("New send", request);
134
135     if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")) { // eager mode => detached send (FIXME: this limit should be configurable)
136       mailbox = smpi_process_remote_mailbox_small(
137             smpi_group_index(smpi_comm_group(request->comm), request->dst));
138     }else{
139       XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
140       mailbox = smpi_process_remote_mailbox(
141                   smpi_group_index(smpi_comm_group(request->comm), request->dst));
142     }
143     if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
144       void *oldbuf = request->buf;
145       detached = 1;
146       request->buf = malloc(request->size);
147       if (oldbuf)
148         memcpy(request->buf,oldbuf,request->size);
149       XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
150     }
151
152       request->action =
153       simcall_comm_isend(mailbox, request->size, -1.0,
154               request->buf, request->size,
155               &match_send,
156               &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
157               request,
158               // detach if msg size < eager/rdv switch limit
159               detached);
160
161   #ifdef HAVE_TRACING
162       /* FIXME: detached sends are not traceable (request->action == NULL) */
163       if (request->action)
164         simcall_set_category(request->action, TRACE_internal_smpi_get_category());
165   #endif
166
167     }
168
169 }
170
171 void smpi_mpi_startall(int count, MPI_Request * requests)
172 {
173     int i;
174
175   for(i = 0; i < count; i++) {
176     smpi_mpi_start(requests[i]);
177   }
178 }
179
180 void smpi_mpi_request_free(MPI_Request * request)
181 {
182   xbt_free(*request);
183   *request = MPI_REQUEST_NULL;
184 }
185
186 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
187                             int dst, int tag, MPI_Comm comm)
188 {
189   MPI_Request request =
190       build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
191                     comm, NON_PERSISTENT | SEND);
192
193   return request;
194 }
195
196 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
197                            int dst, int tag, MPI_Comm comm)
198 {
199   MPI_Request request =
200       smpi_isend_init(buf, count, datatype, dst, tag, comm);
201
202   smpi_mpi_start(request);
203   return request;
204 }
205
206 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
207                             int src, int tag, MPI_Comm comm)
208 {
209   MPI_Request request =
210       build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
211                     comm, NON_PERSISTENT | RECV);
212
213   return request;
214 }
215
216 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
217                            int src, int tag, MPI_Comm comm)
218 {
219   MPI_Request request =
220       smpi_irecv_init(buf, count, datatype, src, tag, comm);
221
222   smpi_mpi_start(request);
223   return request;
224 }
225
226 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
227                    int tag, MPI_Comm comm, MPI_Status * status)
228 {
229   MPI_Request request;
230
231   request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
232   smpi_mpi_wait(&request, status);
233 }
234
235
236
237 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
238                    int tag, MPI_Comm comm)
239 {
240           MPI_Request request;
241
242           request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
243           smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
244 }
245
246 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
247                        int dst, int sendtag, void *recvbuf, int recvcount,
248                        MPI_Datatype recvtype, int src, int recvtag,
249                        MPI_Comm comm, MPI_Status * status)
250 {
251   MPI_Request requests[2];
252   MPI_Status stats[2];
253
254   requests[0] =
255       smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
256   requests[1] =
257       smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
258   smpi_mpi_startall(2, requests);
259   smpi_mpi_waitall(2, requests, stats);
260   if(status != MPI_STATUS_IGNORE) {
261     // Copy receive status
262     memcpy(status, &stats[1], sizeof(MPI_Status));
263   }
264 }
265
266 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
267 {
268   return status->count / smpi_datatype_size(datatype);
269 }
270
271 static void finish_wait(MPI_Request * request, MPI_Status * status)
272 {
273   MPI_Request req = *request;
274   // if we have a sender, we should use its data, and not the data from the receive
275   if((req->action)&&
276       (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
277     req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
278
279   if(status != MPI_STATUS_IGNORE) {
280     status->MPI_SOURCE = req->src;
281     status->MPI_TAG = req->tag;
282     status->MPI_ERROR = MPI_SUCCESS;
283     // FIXME: really this should just contain the count of receive-type blocks,
284     // right?
285     status->count = req->size;
286   }
287   req = *request;
288
289   print_request("Finishing", req);
290   if(req->flags & NON_PERSISTENT) {
291     smpi_mpi_request_free(request);
292   } else {
293     req->action = NULL;
294   }
295 }
296
297 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
298 int flag;
299
300    if ((*request)->action == NULL)
301   flag = 1;
302    else 
303     flag = simcall_comm_test((*request)->action);
304    if(flag) {
305        finish_wait(request, status);
306     }
307     return flag;
308 }
309
310 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
311                      MPI_Status * status)
312 {
313   xbt_dynar_t comms;
314   int i, flag, size;
315   int* map;
316
317   *index = MPI_UNDEFINED;
318   flag = 0;
319   if(count > 0) {
320     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
321     map = xbt_new(int, count);
322     size = 0;
323     for(i = 0; i < count; i++) {
324       if(requests[i]->action) {
325          xbt_dynar_push(comms, &requests[i]->action);
326          map[size] = i;
327          size++;
328       }
329     }
330     if(size > 0) {
331       i = simcall_comm_testany(comms);
332       // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
333       if(i != MPI_UNDEFINED) {
334         *index = map[i];
335         smpi_mpi_wait(&requests[*index], status);
336         flag = 1;
337       }
338     }
339     xbt_free(map);
340     xbt_dynar_free(&comms);
341   }
342   return flag;
343 }
344
345
346 int smpi_mpi_testall(int count, MPI_Request requests[],
347                      MPI_Status status[])
348 {
349   int flag=1;
350   int i;
351   for(i=0; i<count; i++)
352    if (smpi_mpi_test(&requests[i], &status[i])!=1)
353        flag=0;
354
355   return flag;
356 }
357
358 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
359   int flag=0;
360   //FIXME find another wait to avoid busy waiting ?
361   // the issue here is that we have to wait on a nonexistent comm
362   MPI_Request request;
363   while(flag==0){
364         request = smpi_mpi_iprobe(source, tag, comm, &flag, status);
365         XBT_DEBUG("Busy Waiting on probing : %d", flag);
366         if(!flag) {
367             smpi_mpi_request_free(&request);
368             simcall_process_sleep(0.0001);
369         }
370   }
371 }
372
373 MPI_Request smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
374   MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
375             comm, NON_PERSISTENT | RECV);
376   // behave like a receive, but don't do it
377   smx_rdv_t mailbox;
378
379   print_request("New iprobe", request);
380   // We have to test both mailboxes as we don't know if we will receive one one or another
381     if (xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")>0){
382         mailbox = smpi_process_mailbox_small();
383         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
384
385     }
386     if (request->action==NULL){
387         mailbox = smpi_process_mailbox();
388         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
389     }
390
391   if(request->action){
392     MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
393     *flag=true;
394     if(status != MPI_STATUS_IGNORE) {
395       status->MPI_SOURCE = req->src;
396       status->MPI_TAG = req->tag;
397       status->MPI_ERROR = MPI_SUCCESS;
398       status->count = req->size;
399     }
400   }
401   else *flag=false;
402
403   return request;
404 }
405
406 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
407 {
408   print_request("Waiting", *request);
409   if ((*request)->action != NULL) { // this is not a detached send
410     simcall_comm_wait((*request)->action, -1.0);
411     finish_wait(request, status);
412   }
413   // FIXME for a detached send, finish_wait is not called:
414 }
415
416 int smpi_mpi_waitany(int count, MPI_Request requests[],
417                      MPI_Status * status)
418 {
419   xbt_dynar_t comms;
420   int i, size, index;
421   int *map;
422
423   index = MPI_UNDEFINED;
424   if(count > 0) {
425     // Wait for a request to complete
426     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
427     map = xbt_new(int, count);
428     size = 0;
429     XBT_DEBUG("Wait for one of");
430     for(i = 0; i < count; i++) {
431       if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
432         print_request("Waiting any ", requests[i]);
433         xbt_dynar_push(comms, &requests[i]->action);
434         map[size] = i;
435         size++;
436       }
437     }
438     if(size > 0) {
439       i = simcall_comm_waitany(comms);
440
441       // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
442       if (i != MPI_UNDEFINED) {
443         index = map[i];
444         finish_wait(&requests[index], status);
445
446       }
447     }
448     xbt_free(map);
449     xbt_dynar_free(&comms);
450   }
451   return index;
452 }
453
454 void smpi_mpi_waitall(int count, MPI_Request requests[],
455                       MPI_Status status[])
456 {
457   int index, c;
458   MPI_Status stat;
459   MPI_Status *pstat = status == MPI_STATUS_IGNORE ? MPI_STATUS_IGNORE : &stat;
460
461   for(c = 0; c < count; c++) {
462     if(MC_IS_ENABLED) {
463       smpi_mpi_wait(&requests[c], pstat);
464       index = c;
465     } else {
466       index = smpi_mpi_waitany(count, requests, pstat);
467       if(index == MPI_UNDEFINED) {
468         break;
469       }
470     }
471     if(status != MPI_STATUS_IGNORE) {
472       memcpy(&status[index], pstat, sizeof(*pstat));
473     }
474   }
475 }
476
477 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
478                       MPI_Status status[])
479 {
480   int i, count, index;
481
482   count = 0;
483   for(i = 0; i < incount; i++) {
484      if(smpi_mpi_testany(incount, requests, &index, status)) {
485        indices[count] = index;
486        count++;
487      }
488   }
489   return count;
490 }
491
492 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
493                     MPI_Comm comm)
494 {
495   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
496   nary_tree_bcast(buf, count, datatype, root, comm, 4);
497 }
498
499 void smpi_mpi_barrier(MPI_Comm comm)
500 {
501   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
502   nary_tree_barrier(comm, 4);
503 }
504
505 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
506                      void *recvbuf, int recvcount, MPI_Datatype recvtype,
507                      int root, MPI_Comm comm)
508 {
509   int system_tag = 666;
510   int rank, size, src, index;
511   MPI_Aint lb = 0, recvext = 0;
512   MPI_Request *requests;
513
514   rank = smpi_comm_rank(comm);
515   size = smpi_comm_size(comm);
516   if(rank != root) {
517     // Send buffer to root
518     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
519   } else {
520     // FIXME: check for errors
521     smpi_datatype_extent(recvtype, &lb, &recvext);
522     // Local copy from root
523     smpi_datatype_copy(sendbuf, sendcount, sendtype, 
524         (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
525     // Receive buffers from senders
526     requests = xbt_new(MPI_Request, size - 1);
527     index = 0;
528     for(src = 0; src < size; src++) {
529       if(src != root) {
530         requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext, 
531                                           recvcount, recvtype, 
532                                           src, system_tag, comm);
533         index++;
534       }
535     }
536     // Wait for completion of irecv's.
537     smpi_mpi_startall(size - 1, requests);
538     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
539     xbt_free(requests);
540   }
541 }
542
543 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
544                       void *recvbuf, int *recvcounts, int *displs,
545                       MPI_Datatype recvtype, int root, MPI_Comm comm)
546 {
547   int system_tag = 666;
548   int rank, size, src, index;
549   MPI_Aint lb = 0, recvext = 0;
550   MPI_Request *requests;
551
552   rank = smpi_comm_rank(comm);
553   size = smpi_comm_size(comm);
554   if(rank != root) {
555     // Send buffer to root
556     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
557   } else {
558     // FIXME: check for errors
559     smpi_datatype_extent(recvtype, &lb, &recvext);
560     // Local copy from root
561     smpi_datatype_copy(sendbuf, sendcount, sendtype, 
562                        (char *)recvbuf + displs[root] * recvext, 
563                        recvcounts[root], recvtype);
564     // Receive buffers from senders
565     requests = xbt_new(MPI_Request, size - 1);
566     index = 0;
567     for(src = 0; src < size; src++) {
568       if(src != root) {
569         requests[index] =
570             smpi_irecv_init((char *)recvbuf + displs[src] * recvext, 
571                             recvcounts[src], recvtype, src, system_tag, comm);
572         index++;
573       }
574     }
575     // Wait for completion of irecv's.
576     smpi_mpi_startall(size - 1, requests);
577     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
578     xbt_free(requests);
579   }
580 }
581
582 void smpi_mpi_allgather(void *sendbuf, int sendcount,
583                         MPI_Datatype sendtype, void *recvbuf,
584                         int recvcount, MPI_Datatype recvtype,
585                         MPI_Comm comm)
586 {
587   int system_tag = 666;
588   int rank, size, other, index;
589   MPI_Aint lb = 0, recvext = 0;
590   MPI_Request *requests;
591
592   rank = smpi_comm_rank(comm);
593   size = smpi_comm_size(comm);
594   // FIXME: check for errors
595   smpi_datatype_extent(recvtype, &lb, &recvext);
596   // Local copy from self
597   smpi_datatype_copy(sendbuf, sendcount, sendtype, 
598                      (char *)recvbuf + rank * recvcount * recvext, recvcount, 
599                      recvtype);
600   // Send/Recv buffers to/from others;
601   requests = xbt_new(MPI_Request, 2 * (size - 1));
602   index = 0;
603   for(other = 0; other < size; other++) {
604     if(other != rank) {
605       requests[index] =
606           smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
607                           comm);
608       index++;
609       requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext, 
610                                         recvcount, recvtype, other, 
611                                         system_tag, comm);
612       index++;
613     }
614   }
615   // Wait for completion of all comms.
616   smpi_mpi_startall(2 * (size - 1), requests);
617   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
618   xbt_free(requests);
619 }
620
621 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
622                          MPI_Datatype sendtype, void *recvbuf,
623                          int *recvcounts, int *displs,
624                          MPI_Datatype recvtype, MPI_Comm comm)
625 {
626   int system_tag = 666;
627   int rank, size, other, index;
628   MPI_Aint lb = 0, recvext = 0;
629   MPI_Request *requests;
630
631   rank = smpi_comm_rank(comm);
632   size = smpi_comm_size(comm);
633   // FIXME: check for errors
634   smpi_datatype_extent(recvtype, &lb, &recvext);
635   // Local copy from self
636   smpi_datatype_copy(sendbuf, sendcount, sendtype, 
637                      (char *)recvbuf + displs[rank] * recvext, 
638                      recvcounts[rank], recvtype);
639   // Send buffers to others;
640   requests = xbt_new(MPI_Request, 2 * (size - 1));
641   index = 0;
642   for(other = 0; other < size; other++) {
643     if(other != rank) {
644       requests[index] =
645           smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
646                           comm);
647       index++;
648       requests[index] =
649           smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
650                           recvtype, other, system_tag, comm);
651       index++;
652     }
653   }
654   // Wait for completion of all comms.
655   smpi_mpi_startall(2 * (size - 1), requests);
656   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
657   xbt_free(requests);
658 }
659
660 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
661                       void *recvbuf, int recvcount, MPI_Datatype recvtype,
662                       int root, MPI_Comm comm)
663 {
664   int system_tag = 666;
665   int rank, size, dst, index;
666   MPI_Aint lb = 0, sendext = 0;
667   MPI_Request *requests;
668
669   rank = smpi_comm_rank(comm);
670   size = smpi_comm_size(comm);
671   if(rank != root) {
672     // Recv buffer from root
673     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
674                   MPI_STATUS_IGNORE);
675   } else {
676     // FIXME: check for errors
677     smpi_datatype_extent(sendtype, &lb, &sendext);
678     // Local copy from root
679     smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
680       sendcount, sendtype, recvbuf, recvcount, recvtype);
681     // Send buffers to receivers
682     requests = xbt_new(MPI_Request, size - 1);
683     index = 0;
684     for(dst = 0; dst < size; dst++) {
685       if(dst != root) {
686         requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext, 
687                                           sendcount, sendtype, dst,
688                                           system_tag, comm);
689         index++;
690       }
691     }
692     // Wait for completion of isend's.
693     smpi_mpi_startall(size - 1, requests);
694     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
695     xbt_free(requests);
696   }
697 }
698
699 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
700                        MPI_Datatype sendtype, void *recvbuf, int recvcount,
701                        MPI_Datatype recvtype, int root, MPI_Comm comm)
702 {
703   int system_tag = 666;
704   int rank, size, dst, index;
705   MPI_Aint lb = 0, sendext = 0;
706   MPI_Request *requests;
707
708   rank = smpi_comm_rank(comm);
709   size = smpi_comm_size(comm);
710   if(rank != root) {
711     // Recv buffer from root
712     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
713                   MPI_STATUS_IGNORE);
714   } else {
715     // FIXME: check for errors
716     smpi_datatype_extent(sendtype, &lb, &sendext);
717     // Local copy from root
718     smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root], 
719                        sendtype, recvbuf, recvcount, recvtype);
720     // Send buffers to receivers
721     requests = xbt_new(MPI_Request, size - 1);
722     index = 0;
723     for(dst = 0; dst < size; dst++) {
724       if(dst != root) {
725         requests[index] =
726             smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst], 
727                             sendtype, dst, system_tag, comm);
728         index++;
729       }
730     }
731     // Wait for completion of isend's.
732     smpi_mpi_startall(size - 1, requests);
733     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
734     xbt_free(requests);
735   }
736 }
737
738 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
739                      MPI_Datatype datatype, MPI_Op op, int root,
740                      MPI_Comm comm)
741 {
742   int system_tag = 666;
743   int rank, size, src, index;
744   MPI_Aint lb = 0, dataext = 0;
745   MPI_Request *requests;
746   void **tmpbufs;
747
748   rank = smpi_comm_rank(comm);
749   size = smpi_comm_size(comm);
750   if(rank != root) {
751     // Send buffer to root
752     smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
753   } else {
754     // FIXME: check for errors
755     smpi_datatype_extent(datatype, &lb, &dataext);
756     // Local copy from root
757     if (sendbuf && recvbuf)
758       smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
759     // Receive buffers from senders
760     //TODO: make a MPI_barrier here ?
761     requests = xbt_new(MPI_Request, size - 1);
762     tmpbufs = xbt_new(void *, size - 1);
763     index = 0;
764     for(src = 0; src < size; src++) {
765       if(src != root) {
766         // FIXME: possibly overkill we we have contiguous/noncontiguous data
767         //  mapping...
768         tmpbufs[index] = xbt_malloc(count * dataext);
769         requests[index] =
770             smpi_irecv_init(tmpbufs[index], count, datatype, src,
771                             system_tag, comm);
772         index++;
773       }
774     }
775     // Wait for completion of irecv's.
776     smpi_mpi_startall(size - 1, requests);
777     for(src = 0; src < size - 1; src++) {
778       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
779       XBT_VERB("finished waiting any request with index %d", index);
780       if(index == MPI_UNDEFINED) {
781         break;
782       }
783       if(op) /* op can be MPI_OP_NULL that does nothing */
784         smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
785     }
786     for(index = 0; index < size - 1; index++) {
787       xbt_free(tmpbufs[index]);
788     }
789     xbt_free(tmpbufs);
790     xbt_free(requests);
791   }
792 }
793
794 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
795                         MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
796 {
797   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
798   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
799 }
800
801 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
802                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
803 {
804   int system_tag = 666;
805   int rank, size, other, index;
806   MPI_Aint lb = 0, dataext = 0;
807   MPI_Request *requests;
808   void **tmpbufs;
809
810   rank = smpi_comm_rank(comm);
811   size = smpi_comm_size(comm);
812
813   // FIXME: check for errors
814   smpi_datatype_extent(datatype, &lb, &dataext);
815
816   // Local copy from self
817   smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
818
819   // Send/Recv buffers to/from others;
820   requests = xbt_new(MPI_Request, size - 1);
821   tmpbufs = xbt_new(void *, rank);
822   index = 0;
823   for(other = 0; other < rank; other++) {
824     // FIXME: possibly overkill we we have contiguous/noncontiguous data 
825     // mapping...
826     tmpbufs[index] = xbt_malloc(count * dataext);
827     requests[index] =
828         smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
829                         comm);
830     index++;
831   }
832   for(other = rank + 1; other < size; other++) {
833     requests[index] =
834         smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
835     index++;
836   }
837   // Wait for completion of all comms.
838   smpi_mpi_startall(size - 1, requests);
839   for(other = 0; other < size - 1; other++) {
840     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
841     if(index == MPI_UNDEFINED) {
842       break;
843     }
844     if(index < rank) {
845       // #Request is below rank: it's a irecv
846       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
847     }
848   }
849   for(index = 0; index < rank; index++) {
850     xbt_free(tmpbufs[index]);
851   }
852   xbt_free(tmpbufs);
853   xbt_free(requests);
854 }