Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0345cbbf034293e1f69f4e5079176bd3241ac467
[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-contiguous 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-contiguous 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     build_request(buf, count, datatype, smpi_comm_rank(comm), dst, tag,
277                   comm, NON_PERSISTENT | SEND | RECV_DELETE);
278
279   smpi_mpi_start(request);
280   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
281
282 }
283
284 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
285                        int dst, int sendtag, void *recvbuf, int recvcount,
286                        MPI_Datatype recvtype, int src, int recvtag,
287                        MPI_Comm comm, MPI_Status * status)
288 {
289   MPI_Request requests[2];
290   MPI_Status stats[2];
291
292   requests[0] =
293     smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
294   requests[1] =
295     smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
296   smpi_mpi_startall(2, requests);
297   smpi_mpi_waitall(2, requests, stats);
298   if(status != MPI_STATUS_IGNORE) {
299     // Copy receive status
300     memcpy(status, &stats[1], sizeof(MPI_Status));
301   }
302 }
303
304 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
305 {
306   return status->count / smpi_datatype_size(datatype);
307 }
308
309 static void finish_wait(MPI_Request * request, MPI_Status * status)
310 {
311   MPI_Request req = *request;
312   // if we have a sender, we should use its data, and not the data from the receive
313   //FIXME : mail fail if req->action has already been freed, the pointer being invalid
314   if((req->action)&&
315      (req->src==MPI_ANY_SOURCE || req->tag== MPI_ANY_TAG))
316     req = (MPI_Request)SIMIX_comm_get_src_data((*request)->action);
317
318   if(status != MPI_STATUS_IGNORE) {
319     status->MPI_SOURCE = req->src;
320     status->MPI_TAG = req->tag;
321     //if((*request)->action && ((MPI_Request)SIMIX_comm_get_src_data((*request)->action))->size == (*request)->size)
322     status->MPI_ERROR = MPI_SUCCESS;
323     //else status->MPI_ERROR = MPI_ERR_TRUNCATE;
324     // this handles the case were size in receive differs from size in send
325     // FIXME: really this should just contain the count of receive-type blocks,
326     // right?
327     status->count = req->size;
328   }
329   req = *request;
330
331   print_request("Finishing", req);
332   MPI_Datatype datatype = req->old_type;
333   if(datatype->has_subtype == 1){
334       // This part handles the problem of non-contignous memory
335       // the unserialization at the reception
336     s_smpi_subtype_t *subtype = datatype->substruct;
337     if(req->flags & RECV) {
338       subtype->unserialize(req->buf, req->old_buf, req->size/smpi_datatype_size(datatype) , datatype->substruct);
339     }
340     if(req->detached == 0) free(req->buf);
341   }
342
343
344
345   if(req->flags & NON_PERSISTENT) {
346     if(req->action &&
347       (req->action->state == SIMIX_DONE))
348     {
349       MPI_Request sender_request = (MPI_Request)SIMIX_comm_get_src_data(req->action);
350       if((sender_request!=MPI_REQUEST_NULL) &&
351         ( sender_request->detached ) &&
352         ( sender_request->flags & RECV_DELETE))
353       {
354         //we are in a receiver's wait from a detached send
355         //we have to clean the sender's side request here.... but only if done by a send, not an isend
356         //the request lives senderside for an isend. As detached is currently for send + isend, we use RECV_DELETE to separate them
357         //FIXME : see if just removing detached status for isend is also good
358         smpi_mpi_request_free(&sender_request);
359       }
360     }
361     smpi_mpi_request_free(request);
362   } else {
363     req->action = NULL;
364   }
365 }
366
367 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
368   int flag;
369
370   //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
371   if ((*request)->action == NULL)
372     flag = 1;
373   else
374     flag = simcall_comm_test((*request)->action);
375   if(flag) {
376     finish_wait(request, status);
377   }else{
378     smpi_empty_status(status);
379   }
380   return flag;
381 }
382
383 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
384                      MPI_Status * status)
385 {
386   xbt_dynar_t comms;
387   int i, flag, size;
388   int* map;
389
390   *index = MPI_UNDEFINED;
391   flag = 0;
392   if(count > 0) {
393     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
394     map = xbt_new(int, count);
395     size = 0;
396     for(i = 0; i < count; i++) {
397       if((requests[i]!=MPI_REQUEST_NULL) && requests[i]->action) {
398          xbt_dynar_push(comms, &requests[i]->action);
399          map[size] = i;
400          size++;
401       }
402     }
403     if(size > 0) {
404       i = simcall_comm_testany(comms);
405       // not MPI_UNDEFINED, as this is a simix return code
406       if(i != -1) {
407         *index = map[i];
408         finish_wait(&requests[*index], status);
409         flag = 1;
410       }
411     }else{
412         //all requests are null or inactive, return true
413         flag=1;
414         smpi_empty_status(status);
415     }
416     xbt_free(map);
417     xbt_dynar_free(&comms);
418   }
419
420   return flag;
421 }
422
423
424 int smpi_mpi_testall(int count, MPI_Request requests[],
425                      MPI_Status status[])
426 {
427   MPI_Status stat;
428   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
429   int flag=1;
430   int i;
431   for(i=0; i<count; i++){
432     if(requests[i]!= MPI_REQUEST_NULL){
433       if (smpi_mpi_test(&requests[i], pstat)!=1){
434         flag=0;
435       }
436     }else{
437       smpi_empty_status(pstat);
438     }
439     if(status != MPI_STATUSES_IGNORE) {
440       memcpy(&status[i], pstat, sizeof(*pstat));
441     }
442   }
443   return flag;
444 }
445
446 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
447   int flag=0;
448   //FIXME find another wait to avoid busy waiting ?
449   // the issue here is that we have to wait on a nonexistent comm
450   while(flag==0){
451     smpi_mpi_iprobe(source, tag, comm, &flag, status);
452     XBT_DEBUG("Busy Waiting on probing : %d", flag);
453     if(!flag) {
454       simcall_process_sleep(0.0001);
455     }
456   }
457 }
458
459 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
460   MPI_Request request =build_request(NULL, 0, MPI_CHAR, source, smpi_comm_rank(comm), tag,
461             comm, NON_PERSISTENT | RECV);
462
463   // behave like a receive, but don't do it
464   smx_rdv_t mailbox;
465
466   print_request("New iprobe", request);
467   // We have to test both mailboxes as we don't know if we will receive one one or another
468     if (surf_cfg_get_int("smpi/async_small_thres")>0){
469         mailbox = smpi_process_mailbox_small();
470         XBT_DEBUG("trying to probe the perm recv mailbox");
471         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
472     }
473     if (request->action==NULL){
474         mailbox = smpi_process_mailbox();
475         XBT_DEBUG("trying to probe the other mailbox");
476         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
477     }
478
479   if(request->action){
480     MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
481     *flag = 1;
482     if(status != MPI_STATUS_IGNORE) {
483       status->MPI_SOURCE = req->src;
484       status->MPI_TAG = req->tag;
485       status->MPI_ERROR = MPI_SUCCESS;
486       status->count = req->size;
487     }
488   }
489   else *flag = 0;
490   smpi_mpi_request_free(&request);
491
492   return;
493 }
494
495 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
496 {
497   print_request("Waiting", *request);
498   if ((*request)->action != NULL) { // this is not a detached send
499     simcall_comm_wait((*request)->action, -1.0);
500     finish_wait(request, status);
501   }
502   // FIXME for a detached send, finish_wait is not called:
503 }
504
505 int smpi_mpi_waitany(int count, MPI_Request requests[],
506                      MPI_Status * status)
507 {
508   xbt_dynar_t comms;
509   int i, size, index;
510   int *map;
511
512   index = MPI_UNDEFINED;
513   if(count > 0) {
514     // Wait for a request to complete
515     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
516     map = xbt_new(int, count);
517     size = 0;
518     XBT_DEBUG("Wait for one of");
519     for(i = 0; i < count; i++) {
520       if((requests[i] != MPI_REQUEST_NULL) && (requests[i]->action != NULL)) {
521         print_request("Waiting any ", requests[i]);
522         xbt_dynar_push(comms, &requests[i]->action);
523         map[size] = i;
524         size++;
525       }
526     }
527     if(size > 0) {
528       i = simcall_comm_waitany(comms);
529
530       // not MPI_UNDEFINED, as this is a simix return code
531       if (i != -1) {
532         index = map[i];
533         finish_wait(&requests[index], status);
534       }
535     }
536     xbt_free(map);
537     xbt_dynar_free(&comms);
538   }
539
540   if (index==MPI_UNDEFINED)
541     smpi_empty_status(status);
542
543   return index;
544 }
545
546 int smpi_mpi_waitall(int count, MPI_Request requests[],
547                       MPI_Status status[])
548 {
549   int  index, c;
550   MPI_Status stat;
551   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
552   int retvalue=MPI_SUCCESS;
553   //tag invalid requests in the set
554   for(c = 0; c < count; c++) {
555     if(requests[c]==MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ){
556       if(status != MPI_STATUSES_IGNORE)
557         smpi_empty_status(&status[c]);
558     }else if(requests[c]->src == MPI_PROC_NULL ){
559       if(status != MPI_STATUSES_IGNORE) {
560         smpi_empty_status(&status[c]);
561         status[c].MPI_SOURCE=MPI_PROC_NULL;
562       }
563     }
564   }
565
566   for(c = 0; c < count; c++) {
567       if(MC_is_active()) {
568         smpi_mpi_wait(&requests[c], pstat);
569         index = c;
570       } else {
571         index = smpi_mpi_waitany(count, requests, pstat);
572         if(index == MPI_UNDEFINED) {
573           break;
574        }
575       if(status != MPI_STATUSES_IGNORE) {
576         memcpy(&status[index], pstat, sizeof(*pstat));
577         if(status[index].MPI_ERROR==MPI_ERR_TRUNCATE)retvalue=MPI_ERR_IN_STATUS;
578
579       }
580     }
581   }
582   return retvalue;
583 }
584
585 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
586                       MPI_Status status[])
587 {
588   int i, count, index;
589   MPI_Status stat;
590   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
591
592   count = 0;
593   for(i = 0; i < incount; i++)
594   {
595     index=smpi_mpi_waitany(incount, requests, pstat);
596     if(index!=MPI_UNDEFINED){
597       indices[count] = index;
598       count++;
599       if(status != MPI_STATUSES_IGNORE) {
600         memcpy(&status[index], pstat, sizeof(*pstat));
601       }
602     }else{
603       return MPI_UNDEFINED;
604     }
605   }
606   return count;
607 }
608
609 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
610                       MPI_Status status[])
611 {
612   int i, count, count_dead;
613   MPI_Status stat;
614   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
615
616   count = 0;
617   count_dead = 0;
618   for(i = 0; i < incount; i++) {
619     if((requests[i] != MPI_REQUEST_NULL)) {
620       if(smpi_mpi_test(&requests[i], pstat)) {
621          indices[count] = i;
622          count++;
623          if(status != MPI_STATUSES_IGNORE) {
624             memcpy(&status[i], pstat, sizeof(*pstat));
625          }
626       }
627     }else{
628       count_dead++;
629     }
630   }
631   if(count_dead==incount)return MPI_UNDEFINED;
632   else return count;
633 }
634
635 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
636                     MPI_Comm comm)
637 {
638   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
639   nary_tree_bcast(buf, count, datatype, root, comm, 4);
640 }
641
642 void smpi_mpi_barrier(MPI_Comm comm)
643 {
644   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
645   nary_tree_barrier(comm, 4);
646 }
647
648 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
649                      void *recvbuf, int recvcount, MPI_Datatype recvtype,
650                      int root, MPI_Comm comm)
651 {
652   int system_tag = 666;
653   int rank, size, src, index;
654   MPI_Aint lb = 0, recvext = 0;
655   MPI_Request *requests;
656
657   rank = smpi_comm_rank(comm);
658   size = smpi_comm_size(comm);
659   if(rank != root) {
660     // Send buffer to root
661     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
662   } else {
663     // FIXME: check for errors
664     smpi_datatype_extent(recvtype, &lb, &recvext);
665     // Local copy from root
666     smpi_datatype_copy(sendbuf, sendcount, sendtype,
667                        (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
668     // Receive buffers from senders
669     requests = xbt_new(MPI_Request, size - 1);
670     index = 0;
671     for(src = 0; src < size; src++) {
672       if(src != root) {
673         requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
674                                           recvcount, recvtype,
675                                           src, system_tag, comm);
676         index++;
677       }
678     }
679     // Wait for completion of irecv's.
680     smpi_mpi_startall(size - 1, requests);
681     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
682     xbt_free(requests);
683   }
684 }
685
686 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
687                       void *recvbuf, int *recvcounts, int *displs,
688                       MPI_Datatype recvtype, int root, MPI_Comm comm)
689 {
690   int system_tag = 666;
691   int rank, size, src, index;
692   MPI_Aint lb = 0, recvext = 0;
693   MPI_Request *requests;
694
695   rank = smpi_comm_rank(comm);
696   size = smpi_comm_size(comm);
697   if(rank != root) {
698     // Send buffer to root
699     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
700   } else {
701     // FIXME: check for errors
702     smpi_datatype_extent(recvtype, &lb, &recvext);
703     // Local copy from root
704     smpi_datatype_copy(sendbuf, sendcount, sendtype,
705                        (char *)recvbuf + displs[root] * recvext,
706                        recvcounts[root], recvtype);
707     // Receive buffers from senders
708     requests = xbt_new(MPI_Request, size - 1);
709     index = 0;
710     for(src = 0; src < size; src++) {
711       if(src != root) {
712         requests[index] =
713           smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
714                           recvcounts[src], recvtype, src, system_tag, comm);
715         index++;
716       }
717     }
718     // Wait for completion of irecv's.
719     smpi_mpi_startall(size - 1, requests);
720     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
721     xbt_free(requests);
722   }
723 }
724
725 void smpi_mpi_allgather(void *sendbuf, int sendcount,
726                         MPI_Datatype sendtype, void *recvbuf,
727                         int recvcount, MPI_Datatype recvtype,
728                         MPI_Comm comm)
729 {
730   int system_tag = 666;
731   int rank, size, other, index;
732   MPI_Aint lb = 0, recvext = 0;
733   MPI_Request *requests;
734
735   rank = smpi_comm_rank(comm);
736   size = smpi_comm_size(comm);
737   // FIXME: check for errors
738   smpi_datatype_extent(recvtype, &lb, &recvext);
739   // Local copy from self
740   smpi_datatype_copy(sendbuf, sendcount, sendtype,
741                      (char *)recvbuf + rank * recvcount * recvext, recvcount,
742                      recvtype);
743   // Send/Recv buffers to/from others;
744   requests = xbt_new(MPI_Request, 2 * (size - 1));
745   index = 0;
746   for(other = 0; other < size; other++) {
747     if(other != rank) {
748       requests[index] =
749         smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
750                         comm);
751       index++;
752       requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
753                                         recvcount, recvtype, other,
754                                         system_tag, comm);
755       index++;
756     }
757   }
758   // Wait for completion of all comms.
759   smpi_mpi_startall(2 * (size - 1), requests);
760   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
761   xbt_free(requests);
762 }
763
764 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
765                          MPI_Datatype sendtype, void *recvbuf,
766                          int *recvcounts, int *displs,
767                          MPI_Datatype recvtype, MPI_Comm comm)
768 {
769   int system_tag = 666;
770   int rank, size, other, index;
771   MPI_Aint lb = 0, recvext = 0;
772   MPI_Request *requests;
773
774   rank = smpi_comm_rank(comm);
775   size = smpi_comm_size(comm);
776   // FIXME: check for errors
777   smpi_datatype_extent(recvtype, &lb, &recvext);
778   // Local copy from self
779   smpi_datatype_copy(sendbuf, sendcount, sendtype,
780                      (char *)recvbuf + displs[rank] * recvext,
781                      recvcounts[rank], recvtype);
782   // Send buffers to others;
783   requests = xbt_new(MPI_Request, 2 * (size - 1));
784   index = 0;
785   for(other = 0; other < size; other++) {
786     if(other != rank) {
787       requests[index] =
788         smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
789                         comm);
790       index++;
791       requests[index] =
792         smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
793                         recvtype, other, system_tag, comm);
794       index++;
795     }
796   }
797   // Wait for completion of all comms.
798   smpi_mpi_startall(2 * (size - 1), requests);
799   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
800   xbt_free(requests);
801 }
802
803 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
804                       void *recvbuf, int recvcount, MPI_Datatype recvtype,
805                       int root, MPI_Comm comm)
806 {
807   int system_tag = 666;
808   int rank, size, dst, index;
809   MPI_Aint lb = 0, sendext = 0;
810   MPI_Request *requests;
811
812   rank = smpi_comm_rank(comm);
813   size = smpi_comm_size(comm);
814   if(rank != root) {
815     // Recv buffer from root
816     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
817                   MPI_STATUS_IGNORE);
818   } else {
819     // FIXME: check for errors
820     smpi_datatype_extent(sendtype, &lb, &sendext);
821     // Local copy from root
822     smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
823                        sendcount, sendtype, recvbuf, recvcount, recvtype);
824     // Send buffers to receivers
825     requests = xbt_new(MPI_Request, size - 1);
826     index = 0;
827     for(dst = 0; dst < size; dst++) {
828       if(dst != root) {
829         requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
830                                           sendcount, sendtype, dst,
831                                           system_tag, comm);
832         index++;
833       }
834     }
835     // Wait for completion of isend's.
836     smpi_mpi_startall(size - 1, requests);
837     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
838     xbt_free(requests);
839   }
840 }
841
842 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
843                        MPI_Datatype sendtype, void *recvbuf, int recvcount,
844                        MPI_Datatype recvtype, int root, MPI_Comm comm)
845 {
846   int system_tag = 666;
847   int rank, size, dst, index;
848   MPI_Aint lb = 0, sendext = 0;
849   MPI_Request *requests;
850
851   rank = smpi_comm_rank(comm);
852   size = smpi_comm_size(comm);
853   if(rank != root) {
854     // Recv buffer from root
855     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
856                   MPI_STATUS_IGNORE);
857   } else {
858     // FIXME: check for errors
859     smpi_datatype_extent(sendtype, &lb, &sendext);
860     // Local copy from root
861     smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
862                        sendtype, recvbuf, recvcount, recvtype);
863     // Send buffers to receivers
864     requests = xbt_new(MPI_Request, size - 1);
865     index = 0;
866     for(dst = 0; dst < size; dst++) {
867       if(dst != root) {
868         requests[index] =
869           smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
870                           sendtype, dst, system_tag, comm);
871         index++;
872       }
873     }
874     // Wait for completion of isend's.
875     smpi_mpi_startall(size - 1, requests);
876     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
877     xbt_free(requests);
878   }
879 }
880
881 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
882                      MPI_Datatype datatype, MPI_Op op, int root,
883                      MPI_Comm comm)
884 {
885   int system_tag = 666;
886   int rank, size, src, index;
887   MPI_Aint lb = 0, dataext = 0;
888   MPI_Request *requests;
889   void **tmpbufs;
890
891   rank = smpi_comm_rank(comm);
892   size = smpi_comm_size(comm);
893   if(rank != root) {
894     // Send buffer to root
895     smpi_mpi_send(sendbuf, count, datatype, root, system_tag, comm);
896   } else {
897     // FIXME: check for errors
898     smpi_datatype_extent(datatype, &lb, &dataext);
899     // Local copy from root
900     if (sendbuf && recvbuf)
901       smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
902     // Receive buffers from senders
903     //TODO: make a MPI_barrier here ?
904     requests = xbt_new(MPI_Request, size - 1);
905     tmpbufs = xbt_new(void *, size - 1);
906     index = 0;
907     for(src = 0; src < size; src++) {
908       if(src != root) {
909         // FIXME: possibly overkill we we have contiguous/noncontiguous data
910         //  mapping...
911         tmpbufs[index] = xbt_malloc(count * dataext);
912         requests[index] =
913           smpi_irecv_init(tmpbufs[index], count, datatype, src,
914                           system_tag, comm);
915         index++;
916       }
917     }
918     // Wait for completion of irecv's.
919     smpi_mpi_startall(size - 1, requests);
920     for(src = 0; src < size - 1; src++) {
921       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
922       XBT_DEBUG("finished waiting any request with index %d", index);
923       if(index == MPI_UNDEFINED) {
924         break;
925       }
926       if(op) /* op can be MPI_OP_NULL that does nothing */
927         smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
928     }
929     for(index = 0; index < size - 1; index++) {
930       xbt_free(tmpbufs[index]);
931     }
932     xbt_free(tmpbufs);
933     xbt_free(requests);
934   }
935 }
936
937 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
938                         MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
939 {
940   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
941   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
942 }
943
944 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
945                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
946 {
947   int system_tag = 666;
948   int rank, size, other, index;
949   MPI_Aint lb = 0, dataext = 0;
950   MPI_Request *requests;
951   void **tmpbufs;
952
953   rank = smpi_comm_rank(comm);
954   size = smpi_comm_size(comm);
955
956   // FIXME: check for errors
957   smpi_datatype_extent(datatype, &lb, &dataext);
958
959   // Local copy from self
960   smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
961
962   // Send/Recv buffers to/from others;
963   requests = xbt_new(MPI_Request, size - 1);
964   tmpbufs = xbt_new(void *, rank);
965   index = 0;
966   for(other = 0; other < rank; other++) {
967     // FIXME: possibly overkill we we have contiguous/noncontiguous data
968     // mapping...
969     tmpbufs[index] = xbt_malloc(count * dataext);
970     requests[index] =
971       smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
972                       comm);
973     index++;
974   }
975   for(other = rank + 1; other < size; other++) {
976     requests[index] =
977       smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
978     index++;
979   }
980   // Wait for completion of all comms.
981   smpi_mpi_startall(size - 1, requests);
982   for(other = 0; other < size - 1; other++) {
983     index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
984     if(index == MPI_UNDEFINED) {
985       break;
986     }
987     if(index < rank) {
988       // #Request is below rank: it's a irecv
989       smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
990     }
991   }
992   for(index = 0; index < rank; index++) {
993     xbt_free(tmpbufs[index]);
994   }
995   xbt_free(tmpbufs);
996   xbt_free(requests);
997 }