Logo AND Algorithmique Numérique Distribuée

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