Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid into...
[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    XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
21
22    xbt_assert(ref, "Cannot match recv against null reference");
23    xbt_assert(req, "Cannot match recv against null request");
24    return (ref->src == MPI_ANY_SOURCE || req->src == ref->src)
25           && (ref->tag == MPI_ANY_TAG || req->tag == ref->tag);
26 }
27
28 static int match_send(void* a, void* b,smx_action_t ignored) {
29    MPI_Request ref = (MPI_Request)a;
30    MPI_Request req = (MPI_Request)b;
31    XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
32    xbt_assert(ref, "Cannot match send against null reference");
33    xbt_assert(req, "Cannot match send against null request");
34    return (req->src == MPI_ANY_SOURCE || req->src == ref->src)
35           && (req->tag == MPI_ANY_TAG || req->tag == ref->tag);
36 }
37
38 static MPI_Request build_request(void *buf, int count,
39                                  MPI_Datatype datatype, int src, int dst,
40                                  int tag, MPI_Comm comm, unsigned flags)
41 {
42   MPI_Request request;
43
44   request = xbt_new(s_smpi_mpi_request_t, 1);
45   request->buf = buf;
46   // FIXME: this will have to be changed to support non-contiguous datatypes
47   request->size = smpi_datatype_size(datatype) * count;
48   request->src = src;
49   request->dst = dst;
50   request->tag = tag;
51   request->comm = comm;
52   request->action = NULL;
53   request->flags = flags;
54 #ifdef HAVE_TRACING
55   request->send = 0;
56   request->recv = 0;
57 #endif
58   return request;
59 }
60
61 void smpi_action_trace_run(char *path)
62 {
63   char *name;
64   xbt_dynar_t todo;
65   xbt_dict_cursor_t cursor;
66
67   action_fp=NULL;
68   if (path) {
69     action_fp = fopen(path, "r");
70     xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
71                 strerror(errno));
72   }
73
74   if (!xbt_dict_is_empty(action_queues)) {
75     XBT_WARN
76         ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
77
78
79     xbt_dict_foreach(action_queues, cursor, name, todo) {
80       XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
81     }
82   }
83
84   if (path)
85     fclose(action_fp);
86   xbt_dict_free(&action_queues);
87   action_queues = xbt_dict_new_homogeneous(NULL);
88 }
89
90 static void smpi_mpi_request_free_voidp(void* request)
91 {
92   MPI_Request req = request;
93   smpi_mpi_request_free(&req);
94 }
95
96 /* MPI Low level calls */
97 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
98                                int dst, int tag, MPI_Comm comm)
99 {
100   MPI_Request request =
101       build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
102                     comm, PERSISTENT | SEND);
103
104   return request;
105 }
106
107 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
108                                int src, int tag, MPI_Comm comm)
109 {
110   MPI_Request request =
111       build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
112                     comm, PERSISTENT | RECV);
113
114   return request;
115 }
116
117 void smpi_mpi_start(MPI_Request request)
118 {
119   smx_rdv_t mailbox;
120   int detached = 0;
121
122   xbt_assert(!request->action,
123               "Cannot (re)start a non-finished communication");
124   if(request->flags & RECV) {
125     print_request("New recv", request);
126     if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres"))
127     mailbox = smpi_process_mailbox_small();
128     else
129     mailbox = smpi_process_mailbox();
130
131     // FIXME: SIMIX does not yet support non-contiguous datatypes
132     request->action = simcall_comm_irecv(mailbox, request->buf, &request->size, &match_recv, request);
133   } else {
134     print_request("New send", request);
135
136     if (request->size < xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")) { // eager mode => detached send (FIXME: this limit should be configurable)
137       mailbox = smpi_process_remote_mailbox_small(
138             smpi_group_index(smpi_comm_group(request->comm), request->dst));
139     }else{
140       XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
141       mailbox = smpi_process_remote_mailbox(
142                   smpi_group_index(smpi_comm_group(request->comm), request->dst));
143     }
144     if (request->size < 64*1024 ) { //(FIXME: this limit should be configurable)
145       void *oldbuf = request->buf;
146       detached = 1;
147       request->buf = malloc(request->size);
148       if (oldbuf)
149         memcpy(request->buf,oldbuf,request->size);
150       XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
151     }
152
153       request->action =
154       simcall_comm_isend(mailbox, request->size, -1.0,
155               request->buf, request->size,
156               &match_send,
157               &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
158               request,
159               // detach if msg size < eager/rdv switch limit
160               detached);
161
162   #ifdef HAVE_TRACING
163       /* FIXME: detached sends are not traceable (request->action == NULL) */
164       if (request->action)
165         simcall_set_category(request->action, TRACE_internal_smpi_get_category());
166   #endif
167
168     }
169
170 }
171
172 void smpi_mpi_startall(int count, MPI_Request * requests)
173 {
174     int i;
175
176   for(i = 0; i < count; i++) {
177     smpi_mpi_start(requests[i]);
178   }
179 }
180
181 void smpi_mpi_request_free(MPI_Request * request)
182 {
183   xbt_free(*request);
184   *request = MPI_REQUEST_NULL;
185 }
186
187 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
188                             int dst, int tag, MPI_Comm comm)
189 {
190   MPI_Request request =
191       build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
192                     comm, NON_PERSISTENT | SEND);
193
194   return request;
195 }
196
197 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
198                            int dst, int tag, MPI_Comm comm)
199 {
200   MPI_Request request =
201       smpi_isend_init(buf, count, datatype, dst, tag, comm);
202
203   smpi_mpi_start(request);
204   return request;
205 }
206
207 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
208                             int src, int tag, MPI_Comm comm)
209 {
210   MPI_Request request =
211       build_request(buf, count, datatype, src, smpi_comm_rank(comm), tag,
212                     comm, NON_PERSISTENT | RECV);
213
214   return request;
215 }
216
217 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
218                            int src, int tag, MPI_Comm comm)
219 {
220   MPI_Request request =
221       smpi_irecv_init(buf, count, datatype, src, tag, comm);
222
223   smpi_mpi_start(request);
224   return request;
225 }
226
227 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
228                    int tag, MPI_Comm comm, MPI_Status * status)
229 {
230   MPI_Request request;
231
232   request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
233   smpi_mpi_wait(&request, status);
234 }
235
236
237
238 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
239                    int tag, MPI_Comm comm)
240 {
241           MPI_Request request;
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   if ((*request)->action == NULL)
300     flag = 1;
301   else
302     flag = simcall_comm_test((*request)->action);
303   if(flag) {
304     finish_wait(request, status);
305   }
306   return flag;
307 }
308
309 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
310                      MPI_Status * status)
311 {
312   xbt_dynar_t comms;
313   int i, flag, size;
314   int* map;
315
316   *index = MPI_UNDEFINED;
317   flag = 0;
318   if(count > 0) {
319     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
320     map = xbt_new(int, count);
321     size = 0;
322     for(i = 0; i < count; i++) {
323       if(requests[i]->action) {
324          xbt_dynar_push(comms, &requests[i]->action);
325          map[size] = i;
326          size++;
327       }
328     }
329     if(size > 0) {
330       i = simcall_comm_testany(comms);
331       // FIXME: MPI_UNDEFINED or does SIMIX have a return code?
332       if(i != MPI_UNDEFINED) {
333         *index = map[i];
334         smpi_mpi_wait(&requests[*index], status);
335         flag = 1;
336       }
337     }
338     xbt_free(map);
339     xbt_dynar_free(&comms);
340   }
341   return flag;
342 }
343
344
345 int smpi_mpi_testall(int count, MPI_Request requests[],
346                      MPI_Status status[])
347 {
348   int flag=1;
349   int i;
350   for(i=0; i<count; i++)
351     if(requests[i]!= MPI_REQUEST_NULL)
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   while(flag==0){
363         smpi_mpi_iprobe(source, tag, comm, &flag, status);
364         XBT_DEBUG("Busy Waiting on probing : %d", flag);
365         if(!flag) {
366             simcall_process_sleep(0.0001);
367         }
368   }
369 }
370
371 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
372   MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
373             comm, NON_PERSISTENT | RECV);
374   // behave like a receive, but don't do it
375   smx_rdv_t mailbox;
376
377   print_request("New iprobe", request);
378   // We have to test both mailboxes as we don't know if we will receive one one or another
379     if (xbt_cfg_get_int(_surf_cfg_set, "smpi/async_small_thres")>0){
380         mailbox = smpi_process_mailbox_small();
381         XBT_DEBUG("trying to probe the perm recv mailbox");
382         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
383     }
384     if (request->action==NULL){
385         mailbox = smpi_process_mailbox();
386         XBT_DEBUG("trying to probe the other mailbox");
387         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
388     }
389
390   if(request->action){
391     MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
392     *flag=true;
393     if(status != MPI_STATUS_IGNORE) {
394       status->MPI_SOURCE = req->src;
395       status->MPI_TAG = req->tag;
396       status->MPI_ERROR = MPI_SUCCESS;
397       status->count = req->size;
398     }
399   }
400   else *flag=false;
401   smpi_mpi_request_free(&request);
402
403   return;
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_DEBUG("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 }