Logo AND Algorithmique Numérique Distribuée

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