Logo AND Algorithmique Numérique Distribuée

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