Logo AND Algorithmique Numérique Distribuée

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