Logo AND Algorithmique Numérique Distribuée

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