Logo AND Algorithmique Numérique Distribuée

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