Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Throw unimplemented.
[simgrid.git] / src / smpi / smpi_base.c
1 /* Copyright (c) 2007-2013. 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 #include "simgrid/sg_config.h"
15 #include "colls/colls.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_base, smpi, "Logging specific to SMPI (base)");
18
19
20 static int match_recv(void* a, void* b, smx_action_t ignored) {
21    MPI_Request ref = (MPI_Request)a;
22    MPI_Request req = (MPI_Request)b;
23    XBT_DEBUG("Trying to match a recv of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
24
25   xbt_assert(ref, "Cannot match recv against null reference");
26   xbt_assert(req, "Cannot match recv against null request");
27   if((ref->src == MPI_ANY_SOURCE || req->src == ref->src)
28     && ((ref->tag == MPI_ANY_TAG && req->tag >=0) || req->tag == ref->tag)){
29     //we match, we can transfer some values
30     // FIXME : move this to the copy function ?
31     if(ref->src == MPI_ANY_SOURCE)ref->real_src = req->src;
32     if(ref->tag == MPI_ANY_TAG)ref->real_tag = req->tag;
33     if(ref->real_size < req->real_size) ref->truncated = 1;
34     if(req->detached==1){
35         ref->detached_sender=req; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
36     }
37     XBT_DEBUG("match succeeded");
38     return 1;
39   }else return 0;
40 }
41
42 static int match_send(void* a, void* b,smx_action_t ignored) {
43    MPI_Request ref = (MPI_Request)a;
44    MPI_Request req = (MPI_Request)b;
45    XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d",ref->src,req->src, ref->tag, req->tag);
46    xbt_assert(ref, "Cannot match send against null reference");
47    xbt_assert(req, "Cannot match send against null request");
48
49    if((req->src == MPI_ANY_SOURCE || req->src == ref->src)
50              && ((req->tag == MPI_ANY_TAG && ref->tag >=0)|| req->tag == ref->tag))
51    {
52      if(req->src == MPI_ANY_SOURCE)req->real_src = ref->src;
53      if(req->tag == MPI_ANY_TAG)req->real_tag = ref->tag;
54      if(req->real_size < ref->real_size) req->truncated = 1;
55      if(ref->detached==1){
56          req->detached_sender=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
57      }
58     XBT_DEBUG("match succeeded");
59      return 1;
60    } else return 0;
61 }
62
63
64 typedef struct s_smpi_factor *smpi_factor_t;
65 typedef struct s_smpi_factor {
66   long factor;
67   int nb_values;
68   double values[4];//arbitrary set to 4
69 } s_smpi_factor_t;
70 xbt_dynar_t smpi_os_values = NULL;
71 xbt_dynar_t smpi_or_values = NULL;
72 xbt_dynar_t smpi_ois_values = NULL;
73
74 // Methods used to parse and store the values for timing injections in smpi
75 // These are taken from surf/network.c and generalized to have more factors
76 // These methods should be merged with those in surf/network.c (moved somewhere in xbt ?)
77
78 static int factor_cmp(const void *pa, const void *pb)
79 {
80   return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor) ? 1 :
81          (((s_smpi_factor_t*)pa)->factor < ((s_smpi_factor_t*)pb)->factor) ? -1 : 0;
82 }
83
84
85 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
86 {
87   char *value = NULL;
88   unsigned int iter = 0;
89   s_smpi_factor_t fact;
90   fact.nb_values=0;
91   int i=0;
92   xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
93
94   smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
95   radical_elements = xbt_str_split(smpi_coef_string, ";");
96   xbt_dynar_foreach(radical_elements, iter, value) {
97     memset(&fact, 0, sizeof(s_smpi_factor_t));
98     radical_elements2 = xbt_str_split(value, ":");
99     if (xbt_dynar_length(radical_elements2) <2 || xbt_dynar_length(radical_elements2) > 5)
100       xbt_die("Malformed radical for smpi factor!");
101     for(i =0; i<xbt_dynar_length(radical_elements2);i++ ){
102         if (i==0){
103            fact.factor = atol(xbt_dynar_get_as(radical_elements2, i, char *));
104         }else{
105            fact.values[fact.nb_values] = atof(xbt_dynar_get_as(radical_elements2, i, char *));
106            fact.nb_values++;
107         }
108     }
109
110     xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
111     XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
112     xbt_dynar_free(&radical_elements2);
113   }
114   xbt_dynar_free(&radical_elements);
115   iter=0;
116   xbt_dynar_sort(smpi_factor, &factor_cmp);
117   xbt_dynar_foreach(smpi_factor, iter, fact) {
118     XBT_DEBUG("smpi_factor:\t%ld : %d values, first: %f", fact.factor, fact.nb_values ,fact.values[0]);
119   }
120   return smpi_factor;
121 }
122
123 static double smpi_os(double size)
124 {
125   if (!smpi_os_values) {
126     smpi_os_values = parse_factor(sg_cfg_get_string("smpi/os"));
127     smpi_register_static(smpi_os_values, xbt_dynar_free_voidp);
128   }
129   unsigned int iter = 0;
130   s_smpi_factor_t fact;
131   double current=0.0;
132   xbt_dynar_foreach(smpi_os_values, iter, fact) {
133     if (size <= fact.factor) {
134         XBT_DEBUG("os : %f <= %ld return %f", size, fact.factor, current);
135       return current;
136     }else{
137       current=fact.values[0]+fact.values[1]*size;
138     }
139   }
140   XBT_DEBUG("os : %f > %ld return %f", size, fact.factor, current);
141
142   return current;
143 }
144
145 static double smpi_ois(double size)
146 {
147   if (!smpi_ois_values) {
148     smpi_ois_values = parse_factor(sg_cfg_get_string("smpi/ois"));
149     smpi_register_static(smpi_ois_values, xbt_dynar_free_voidp);
150   }
151   unsigned int iter = 0;
152   s_smpi_factor_t fact;
153   double current=0.0;
154   xbt_dynar_foreach(smpi_ois_values, iter, fact) {
155     if (size <= fact.factor) {
156         XBT_DEBUG("ois : %f <= %ld return %f", size, fact.factor, current);
157       return current;
158     }else{
159       current=fact.values[0]+fact.values[1]*size;
160     }
161   }
162   XBT_DEBUG("ois : %f > %ld return %f", size, fact.factor, current);
163
164   return current;
165 }
166
167 static double smpi_or(double size)
168 {
169   if (!smpi_or_values) {
170     smpi_or_values = parse_factor(sg_cfg_get_string("smpi/or"));
171     smpi_register_static(smpi_or_values, xbt_dynar_free_voidp);
172   }
173   unsigned int iter = 0;
174   s_smpi_factor_t fact;
175   double current=0.0;
176   xbt_dynar_foreach(smpi_or_values, iter, fact) {
177     if (size <= fact.factor) {
178         XBT_DEBUG("or : %f <= %ld return %f", size, fact.factor, current);
179       return current;
180     }else
181       current=fact.values[0]+fact.values[1]*size;
182   }
183   XBT_DEBUG("or : %f > %ld return %f", size, fact.factor, current);
184
185   return current;
186 }
187
188 static MPI_Request build_request(void *buf, int count,
189                                  MPI_Datatype datatype, int src, int dst,
190                                  int tag, MPI_Comm comm, unsigned flags)
191 {
192   MPI_Request request = NULL;
193
194   void *old_buf = NULL;
195
196   request = xbt_new(s_smpi_mpi_request_t, 1);
197
198   s_smpi_subtype_t *subtype = datatype->substruct;
199
200   if(datatype->has_subtype == 1){
201     // This part handles the problem of non-contiguous memory
202     old_buf = buf;
203     buf = count==0 ? NULL : xbt_malloc(count*smpi_datatype_size(datatype));
204     if (flags & SEND) {
205       subtype->serialize(old_buf, buf, count, datatype->substruct);
206     }
207   }
208
209   request->buf = buf;
210   // This part handles the problem of non-contiguous memory (for the
211   // unserialisation at the reception)
212   request->old_buf = old_buf;
213   request->old_type = datatype;
214
215   request->size = smpi_datatype_size(datatype) * count;
216   request->src = src;
217   request->dst = dst;
218   request->tag = tag;
219   request->comm = comm;
220   request->action = NULL;
221   request->flags = flags;
222   request->detached = 0;
223   request->detached_sender = NULL;
224   request->real_src = 0;
225
226   request->truncated = 0;
227   request->real_size = 0;
228   request->real_tag = 0;
229
230   request->refcount=1;
231 #ifdef HAVE_TRACING
232   request->send = 0;
233   request->recv = 0;
234 #endif
235   if (flags & SEND) smpi_datatype_unuse(datatype);
236
237   return request;
238 }
239
240
241 void smpi_empty_status(MPI_Status * status)
242 {
243   if(status != MPI_STATUS_IGNORE) {
244     status->MPI_SOURCE = MPI_ANY_SOURCE;
245     status->MPI_TAG = MPI_ANY_TAG;
246     status->MPI_ERROR = MPI_SUCCESS;
247     status->count=0;
248   }
249 }
250
251 void smpi_action_trace_run(char *path)
252 {
253   char *name;
254   xbt_dynar_t todo;
255   xbt_dict_cursor_t cursor;
256
257   action_fp=NULL;
258   if (path) {
259     action_fp = fopen(path, "r");
260     xbt_assert(action_fp != NULL, "Cannot open %s: %s", path,
261                strerror(errno));
262   }
263
264   if (!xbt_dict_is_empty(action_queues)) {
265     XBT_WARN
266       ("Not all actions got consumed. If the simulation ended successfully (without deadlock), you may want to add new processes to your deployment file.");
267
268
269     xbt_dict_foreach(action_queues, cursor, name, todo) {
270       XBT_WARN("Still %lu actions for %s", xbt_dynar_length(todo), name);
271     }
272   }
273
274   if (path)
275     fclose(action_fp);
276   xbt_dict_free(&action_queues);
277   action_queues = xbt_dict_new_homogeneous(NULL);
278 }
279
280 static void smpi_mpi_request_free_voidp(void* request)
281 {
282   MPI_Request req = request;
283   smpi_mpi_request_free(&req);
284 }
285
286 /* MPI Low level calls */
287 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
288                                int dst, int tag, MPI_Comm comm)
289 {
290   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
291   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
292                           comm, PERSISTENT | SEND | PREPARED);
293   request->refcount++;
294   return request;
295 }
296
297 MPI_Request smpi_mpi_ssend_init(void *buf, int count, MPI_Datatype datatype,
298                                int dst, int tag, MPI_Comm comm)
299 {
300   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
301   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
302                           comm, PERSISTENT | SSEND | SEND | PREPARED);
303   request->refcount++;
304   return request;
305 }
306
307 MPI_Request smpi_mpi_recv_init(void *buf, int count, MPI_Datatype datatype,
308                                int src, int tag, MPI_Comm comm)
309 {
310   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
311   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
312                           comm, PERSISTENT | RECV | PREPARED);
313   request->refcount++;
314   return request;
315 }
316
317 void smpi_mpi_start(MPI_Request request)
318 {
319   smx_rdv_t mailbox;
320
321   xbt_assert(!request->action, "Cannot (re)start a non-finished communication");
322   request->flags &= ~PREPARED;
323   if (request->flags & RECV) {
324     print_request("New recv", request);
325     //FIXME: if receive is posted with a large size, but send is smaller, mailboxes may not match !
326     if (request->size < sg_cfg_get_int("smpi/async_small_thres"))
327       mailbox = smpi_process_mailbox_small();
328     else
329       mailbox = smpi_process_mailbox();
330     // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
331     request->real_size=request->size;
332     smpi_datatype_use(request->old_type);
333     smpi_comm_use(request->comm);
334     request->action = simcall_comm_irecv(mailbox, request->buf, &request->real_size, &match_recv, request);
335
336     //integrate pseudo-timing for buffering of small messages, do not bother to execute the simcall if 0
337     double sleeptime = request->detached ? smpi_or(request->size) : 0.0;
338     if(sleeptime!=0.0){
339         simcall_process_sleep(sleeptime);
340         XBT_DEBUG("receiving size of %zu : sleep %f ", request->size, smpi_or(request->size));
341     }
342
343   } else {
344
345
346     int receiver = request->dst;//smpi_group_index(smpi_comm_group(request->comm), request->dst);
347
348     #ifdef HAVE_TRACING
349       int rank = smpi_process_index();
350       if (TRACE_smpi_view_internals()) {
351         TRACE_smpi_send(rank, rank, receiver,request->size);
352       }
353     #endif
354 /*    if(receiver == MPI_UNDEFINED) {*/
355 /*      XBT_WARN("Trying to send a message to a wrong rank");*/
356 /*      return;*/
357 /*    }*/
358     print_request("New send", request);
359     if (request->size < sg_cfg_get_int("smpi/async_small_thres")) { // eager mode
360       mailbox = smpi_process_remote_mailbox_small(receiver);
361     }else{
362       XBT_DEBUG("Send request %p is not in the permanent receive mailbox (buf: %p)",request,request->buf);
363       mailbox = smpi_process_remote_mailbox(receiver);
364     }
365     if ( (! (request->flags & SSEND)) && (request->size < sg_cfg_get_int("smpi/send_is_detached_thres"))) {
366       void *oldbuf = NULL;
367       request->detached = 1;
368       request->refcount++;
369       if(request->old_type->has_subtype == 0){
370         oldbuf = request->buf;
371         if (oldbuf && request->size!=0){
372           request->buf = xbt_malloc(request->size);
373           memcpy(request->buf,oldbuf,request->size);
374         }
375       }
376       XBT_DEBUG("Send request %p is detached; buf %p copied into %p",request,oldbuf,request->buf);
377     }
378
379     // we make a copy here, as the size is modified by simix, and we may reuse the request in another receive later
380     request->real_size=request->size;
381     smpi_datatype_use(request->old_type);
382     smpi_comm_use(request->comm);
383
384     //if we are giving back the control to the user without waiting for completion, we have to inject timings
385     double sleeptime =0.0;
386     if(request->detached || (request->flags & (ISEND|SSEND))){// issend should be treated as isend
387       //isend and send timings may be different
388       sleeptime = (request->flags & ISEND)? smpi_ois(request->size) : smpi_os(request->size);
389     }
390
391     if(sleeptime!=0.0){
392         simcall_process_sleep(sleeptime);
393         XBT_DEBUG("sending size of %zu : sleep %f ", request->size, smpi_os(request->size));
394     }
395
396     request->action =
397       simcall_comm_isend(mailbox, request->size, -1.0,
398                          request->buf, request->real_size,
399                          &match_send,
400                          &smpi_mpi_request_free_voidp, // how to free the userdata if a detached send fails
401                          request,
402                          // detach if msg size < eager/rdv switch limit
403                          request->detached);
404
405 #ifdef HAVE_TRACING
406     /* FIXME: detached sends are not traceable (request->action == NULL) */
407     if (request->action)
408       simcall_set_category(request->action, TRACE_internal_smpi_get_category());
409
410 #endif
411
412   }
413
414 }
415
416 void smpi_mpi_startall(int count, MPI_Request * requests)
417 {
418   int i;
419   if(requests==NULL) return;
420
421   for(i = 0; i < count; i++) {
422     smpi_mpi_start(requests[i]);
423   }
424 }
425
426 void smpi_mpi_request_free(MPI_Request * request)
427 {
428   if((*request) != MPI_REQUEST_NULL){
429     (*request)->refcount--;
430     if((*request)->refcount<0) xbt_die("wrong refcount");
431
432     if((*request)->refcount==0){
433         print_request("Destroying", (*request));
434         xbt_free(*request);
435         *request = MPI_REQUEST_NULL;
436     }else{
437         print_request("Decrementing", (*request));
438
439     }
440   }else{
441       xbt_die("freeing an already free request");
442   }
443 }
444
445 MPI_Request smpi_isend_init(void *buf, int count, MPI_Datatype datatype,
446                             int dst, int tag, MPI_Comm comm)
447 {
448   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
449   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf , count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
450                           comm, NON_PERSISTENT | ISEND | SEND | PREPARED);
451   return request;
452 }
453
454 MPI_Request smpi_mpi_isend(void *buf, int count, MPI_Datatype datatype,
455                            int dst, int tag, MPI_Comm comm)
456 {
457   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
458   request =  build_request(buf==MPI_BOTTOM?(void*)0:buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
459                            comm, NON_PERSISTENT | ISEND | SEND);
460   smpi_mpi_start(request);
461   return request;
462 }
463
464 MPI_Request smpi_mpi_issend(void *buf, int count, MPI_Datatype datatype,
465                            int dst, int tag, MPI_Comm comm)
466 {
467   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
468   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
469                           comm, NON_PERSISTENT | ISEND | SSEND | SEND);
470   smpi_mpi_start(request);
471   return request;
472 }
473
474
475
476 MPI_Request smpi_irecv_init(void *buf, int count, MPI_Datatype datatype,
477                             int src, int tag, MPI_Comm comm)
478 {
479   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
480   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
481                           comm, NON_PERSISTENT | RECV | PREPARED);
482   return request;
483 }
484
485 MPI_Request smpi_mpi_irecv(void *buf, int count, MPI_Datatype datatype,
486                            int src, int tag, MPI_Comm comm)
487 {
488   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
489   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, src == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), src), smpi_process_index(), tag,
490                           comm, NON_PERSISTENT | RECV);
491   smpi_mpi_start(request);
492   return request;
493 }
494
495 void smpi_mpi_recv(void *buf, int count, MPI_Datatype datatype, int src,
496                    int tag, MPI_Comm comm, MPI_Status * status)
497 {
498   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
499   request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
500   smpi_mpi_wait(&request, status);
501   request = NULL;
502 }
503
504
505
506 void smpi_mpi_send(void *buf, int count, MPI_Datatype datatype, int dst,
507                    int tag, MPI_Comm comm)
508 {
509   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
510   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
511                           comm, NON_PERSISTENT | SEND);
512
513   smpi_mpi_start(request);
514   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
515   request = NULL;
516 }
517
518 void smpi_mpi_ssend(void *buf, int count, MPI_Datatype datatype,
519                            int dst, int tag, MPI_Comm comm)
520 {
521   MPI_Request request = NULL; /* MC needs the comm to be set to NULL during the call */
522   request = build_request(buf==MPI_BOTTOM ? (void*)0 : buf, count, datatype, smpi_process_index(), smpi_group_index(smpi_comm_group(comm), dst), tag,
523                 comm, NON_PERSISTENT | SSEND | SEND);
524
525   smpi_mpi_start(request);
526   smpi_mpi_wait(&request, MPI_STATUS_IGNORE);
527   request = NULL;
528 }
529
530 void smpi_mpi_sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
531                        int dst, int sendtag, void *recvbuf, int recvcount,
532                        MPI_Datatype recvtype, int src, int recvtag,
533                        MPI_Comm comm, MPI_Status * status)
534 {
535   MPI_Request requests[2];
536   MPI_Status stats[2];
537   int myid=smpi_process_index();
538   if ((smpi_group_index(smpi_comm_group(comm), dst) == myid) && (smpi_group_index(smpi_comm_group(comm), src) == myid)) {
539       smpi_datatype_copy(sendbuf, sendcount, sendtype,
540                                      recvbuf, recvcount, recvtype);
541       return;
542   }
543   requests[0] =
544     smpi_isend_init(sendbuf, sendcount, sendtype, dst, sendtag, comm);
545   requests[1] =
546     smpi_irecv_init(recvbuf, recvcount, recvtype, src, recvtag, comm);
547   smpi_mpi_startall(2, requests);
548   smpi_mpi_waitall(2, requests, stats);
549   if(status != MPI_STATUS_IGNORE) {
550     // Copy receive status
551     *status = stats[1];
552   }
553 }
554
555 int smpi_mpi_get_count(MPI_Status * status, MPI_Datatype datatype)
556 {
557   return status->count / smpi_datatype_size(datatype);
558 }
559
560 static void finish_wait(MPI_Request * request, MPI_Status * status)
561 {
562   MPI_Request req = *request;
563   smpi_empty_status(status);
564
565   if(!(req->detached && req->flags & SEND) && !(req->flags & PREPARED)){
566     if(status != MPI_STATUS_IGNORE) {
567       int src = req->src == MPI_ANY_SOURCE ? req->real_src : req->src;
568       status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(req->comm), src);
569       status->MPI_TAG = req->tag == MPI_ANY_TAG ? req->real_tag : req->tag;
570       status->MPI_ERROR = req->truncated ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
571       // this handles the case were size in receive differs from size in send
572       // FIXME: really this should just contain the count of receive-type blocks,
573       // right?
574       status->count = req->real_size;
575     }
576
577     print_request("Finishing", req);
578     MPI_Datatype datatype = req->old_type;
579
580     if(datatype->has_subtype == 1){
581       // This part handles the problem of non-contignous memory
582       // the unserialization at the reception
583       s_smpi_subtype_t *subtype = datatype->substruct;
584       if(req->flags & RECV) {
585         subtype->unserialize(req->buf, req->old_buf, req->real_size/smpi_datatype_size(datatype) , datatype->substruct);
586       }
587       if(req->detached == 0) free(req->buf);
588     }
589     smpi_comm_unuse(req->comm);
590     smpi_datatype_unuse(datatype);
591
592   }
593
594 #ifdef HAVE_TRACING
595   if (TRACE_smpi_view_internals()) {
596     if(req->flags & RECV){
597       int rank = smpi_process_index();
598       int src_traced = (req->src == MPI_ANY_SOURCE ? req->real_src : req->src);
599       TRACE_smpi_recv(rank, src_traced, rank);
600     }
601   }
602 #endif
603
604   if(req->detached_sender!=NULL){
605     smpi_mpi_request_free(&(req->detached_sender));
606   }
607
608   if(req->flags & NON_PERSISTENT) {
609     smpi_mpi_request_free(request);
610   } else {
611     req->action = NULL;
612   }
613 }
614
615 int smpi_mpi_test(MPI_Request * request, MPI_Status * status) {
616   int flag;
617
618   //assume that request is not MPI_REQUEST_NULL (filtered in PMPI_Test or smpi_mpi_testall before)
619   smpi_empty_status(status);
620   flag = 1;
621   if (!((*request)->flags & PREPARED)) {
622     if ((*request)->action != NULL)
623       flag = simcall_comm_test((*request)->action);
624     if (flag) {
625       finish_wait(request, status);
626       *request = MPI_REQUEST_NULL;
627     }
628   }
629   return flag;
630 }
631
632 int smpi_mpi_testany(int count, MPI_Request requests[], int *index,
633                      MPI_Status * status)
634 {
635   xbt_dynar_t comms;
636   int i, flag, size;
637   int* map;
638
639   *index = MPI_UNDEFINED;
640   flag = 0;
641   comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
642   map = xbt_new(int, count);
643   size = 0;
644   for(i = 0; i < count; i++) {
645     if ((requests[i] != MPI_REQUEST_NULL) && requests[i]->action &&
646         !(requests[i]->flags & PREPARED)) {
647        xbt_dynar_push(comms, &requests[i]->action);
648        map[size] = i;
649        size++;
650     }
651   }
652   if(size > 0) {
653     i = simcall_comm_testany(comms);
654     // not MPI_UNDEFINED, as this is a simix return code
655     if(i != -1) {
656       *index = map[i];
657       finish_wait(&requests[*index], status);
658       requests[*index] = MPI_REQUEST_NULL;
659       flag = 1;
660     }
661   }else{
662       //all requests are null or inactive, return true
663       flag=1;
664       smpi_empty_status(status);
665   }
666   xbt_free(map);
667   xbt_dynar_free(&comms);
668
669   return flag;
670 }
671
672
673 int smpi_mpi_testall(int count, MPI_Request requests[],
674                      MPI_Status status[])
675 {
676   MPI_Status stat;
677   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
678   int flag=1;
679   int i;
680   for(i=0; i<count; i++){
681     if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
682       if (smpi_mpi_test(&requests[i], pstat)!=1){
683         flag=0;
684       }else{
685           requests[i]=MPI_REQUEST_NULL;
686       }
687     }else{
688       smpi_empty_status(pstat);
689     }
690     if(status != MPI_STATUSES_IGNORE) {
691       status[i] = *pstat;
692     }
693   }
694   return flag;
695 }
696
697 void smpi_mpi_probe(int source, int tag, MPI_Comm comm, MPI_Status* status){
698   int flag=0;
699   //FIXME find another wait to avoid busy waiting ?
700   // the issue here is that we have to wait on a nonexistent comm
701   while(flag==0){
702     smpi_mpi_iprobe(source, tag, comm, &flag, status);
703     XBT_DEBUG("Busy Waiting on probing : %d", flag);
704   }
705 }
706
707 void smpi_mpi_iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status){
708
709   MPI_Request request =build_request(NULL, 0, MPI_CHAR, source == MPI_ANY_SOURCE ? MPI_ANY_SOURCE : smpi_group_index(smpi_comm_group(comm), source), smpi_comm_rank(comm), tag,
710             comm, NON_PERSISTENT | RECV);
711
712   //to avoid deadlock, we have to sleep some time here, or the timer won't advance and we will only do iprobe simcalls
713   double sleeptime= sg_cfg_get_double("smpi/iprobe");
714   //multiplier to the sleeptime, to increase speed of execution, each failed iprobe will increase it
715   static int nsleeps = 1;
716
717   simcall_process_sleep(sleeptime);
718
719   // behave like a receive, but don't do it
720   smx_rdv_t mailbox;
721
722   print_request("New iprobe", request);
723   // We have to test both mailboxes as we don't know if we will receive one one or another
724     if (sg_cfg_get_int("smpi/async_small_thres")>0){
725         mailbox = smpi_process_mailbox_small();
726         XBT_DEBUG("trying to probe the perm recv mailbox");
727         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
728     }
729     if (request->action==NULL){
730         mailbox = smpi_process_mailbox();
731         XBT_DEBUG("trying to probe the other mailbox");
732         request->action = simcall_comm_iprobe(mailbox, request->src, request->tag, &match_recv, (void*)request);
733     }
734
735   if(request->action){
736     MPI_Request req = (MPI_Request)SIMIX_comm_get_src_data(request->action);
737     *flag = 1;
738     if(status != MPI_STATUS_IGNORE && !(req->flags & PREPARED)) {
739       status->MPI_SOURCE = smpi_group_rank(smpi_comm_group(comm), req->src);
740       status->MPI_TAG = req->tag;
741       status->MPI_ERROR = MPI_SUCCESS;
742       status->count = req->real_size;
743     }
744     nsleeps=1;//reset the number of sleeps we will do next time
745   }
746   else {
747       *flag = 0;
748       nsleeps++;
749   }
750   smpi_mpi_request_free(&request);
751
752   return;
753 }
754
755 void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
756 {
757   print_request("Waiting", *request);
758   if ((*request)->flags & PREPARED) {
759     smpi_empty_status(status);
760     return;
761   }
762
763   if ((*request)->action != NULL) { // this is not a detached send
764     simcall_comm_wait((*request)->action, -1.0);
765   }
766
767 #ifdef HAVE_MC
768   if(MC_is_active())
769     (*request)->action->comm.dst_data = NULL; // dangling pointer : dst_data is freed with a wait, need to set it to NULL for system state comparison
770 #endif
771
772   finish_wait(request, status);
773   *request = MPI_REQUEST_NULL;
774   // FIXME for a detached send, finish_wait is not called:
775 }
776
777 int smpi_mpi_waitany(int count, MPI_Request requests[],
778                      MPI_Status * status)
779 {
780   xbt_dynar_t comms;
781   int i, size, index;
782   int *map;
783
784   index = MPI_UNDEFINED;
785   if(count > 0) {
786     // Wait for a request to complete
787     comms = xbt_dynar_new(sizeof(smx_action_t), NULL);
788     map = xbt_new(int, count);
789     size = 0;
790     XBT_DEBUG("Wait for one of %d", count);
791     for(i = 0; i < count; i++) {
792       if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED)) {
793         if (requests[i]->action != NULL) {
794           XBT_DEBUG("Waiting any %p ", requests[i]);
795           xbt_dynar_push(comms, &requests[i]->action);
796           map[size] = i;
797           size++;
798         }else{
799          //This is a finished detached request, let's return this one
800          size=0;//so we free the dynar but don't do the waitany call
801          index=i;
802          finish_wait(&requests[i], status);//cleanup if refcount = 0
803          requests[i]=MPI_REQUEST_NULL;//set to null
804          break;
805          }
806       }
807     }
808     if(size > 0) {
809       i = simcall_comm_waitany(comms);
810
811       // not MPI_UNDEFINED, as this is a simix return code
812       if (i != -1) {
813         index = map[i];
814         finish_wait(&requests[index], status);
815         requests[index] = MPI_REQUEST_NULL;
816       }
817     }
818     xbt_free(map);
819     xbt_dynar_free(&comms);
820   }
821
822   if (index==MPI_UNDEFINED)
823     smpi_empty_status(status);
824
825   return index;
826 }
827
828 int smpi_mpi_waitall(int count, MPI_Request requests[],
829                       MPI_Status status[])
830 {
831   int  index, c;
832   MPI_Status stat;
833   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
834   int retvalue = MPI_SUCCESS;
835   //tag invalid requests in the set
836   if (status != MPI_STATUSES_IGNORE) {
837     for (c = 0; c < count; c++) {
838       if (requests[c] == MPI_REQUEST_NULL || requests[c]->dst == MPI_PROC_NULL ||
839           (requests[c]->flags & PREPARED)) {
840         smpi_empty_status(&status[c]);
841       } else if (requests[c]->src == MPI_PROC_NULL) {
842         smpi_empty_status(&status[c]);
843         status[c].MPI_SOURCE = MPI_PROC_NULL;
844       }
845     }
846   }
847   for(c = 0; c < count; c++) {
848     if (MC_is_active()) {
849       smpi_mpi_wait(&requests[c], pstat);
850       index = c;
851     } else {
852       index = smpi_mpi_waitany(count, requests, pstat);
853       if (index == MPI_UNDEFINED)
854         break;
855       requests[index]=MPI_REQUEST_NULL;
856     }
857     if (status != MPI_STATUSES_IGNORE) {
858       status[index] = *pstat;
859       if (status[index].MPI_ERROR == MPI_ERR_TRUNCATE)
860         retvalue = MPI_ERR_IN_STATUS;
861     }
862   }
863
864   return retvalue;
865 }
866
867 int smpi_mpi_waitsome(int incount, MPI_Request requests[], int *indices,
868                       MPI_Status status[])
869 {
870   int i, count, index;
871   MPI_Status stat;
872   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
873
874   count = 0;
875   for(i = 0; i < incount; i++)
876   {
877     index=smpi_mpi_waitany(incount, requests, pstat);
878     if(index!=MPI_UNDEFINED){
879       indices[count] = index;
880       count++;
881       if(status != MPI_STATUSES_IGNORE) {
882         status[index] = *pstat;
883       }
884      requests[index]=MPI_REQUEST_NULL;
885     }else{
886       return MPI_UNDEFINED;
887     }
888   }
889   return count;
890 }
891
892 int smpi_mpi_testsome(int incount, MPI_Request requests[], int *indices,
893                       MPI_Status status[])
894 {
895   int i, count, count_dead;
896   MPI_Status stat;
897   MPI_Status *pstat = status == MPI_STATUSES_IGNORE ? MPI_STATUS_IGNORE : &stat;
898
899   count = 0;
900   count_dead = 0;
901   for(i = 0; i < incount; i++) {
902     if((requests[i] != MPI_REQUEST_NULL)) {
903       if(smpi_mpi_test(&requests[i], pstat)) {
904          indices[i] = 1;
905          count++;
906          if(status != MPI_STATUSES_IGNORE) {
907            status[i] = *pstat;
908          }
909          requests[i]=MPI_REQUEST_NULL;
910
911       }
912     }else{
913       count_dead++;
914     }
915   }
916   if(count_dead==incount)return MPI_UNDEFINED;
917   else return count;
918 }
919
920 void smpi_mpi_bcast(void *buf, int count, MPI_Datatype datatype, int root,
921                     MPI_Comm comm)
922 {
923   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
924   nary_tree_bcast(buf, count, datatype, root, comm, 4);
925 }
926
927 void smpi_mpi_barrier(MPI_Comm comm)
928 {
929   // arity=2: a binary tree, arity=4 seem to be a good setting (see P2P-MPI))
930   nary_tree_barrier(comm, 4);
931 }
932
933 void smpi_mpi_gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
934                      void *recvbuf, int recvcount, MPI_Datatype recvtype,
935                      int root, MPI_Comm comm)
936 {
937   int system_tag = COLL_TAG_GATHER;
938   int rank, size, src, index;
939   MPI_Aint lb = 0, recvext = 0;
940   MPI_Request *requests;
941
942   rank = smpi_comm_rank(comm);
943   size = smpi_comm_size(comm);
944   if(rank != root) {
945     // Send buffer to root
946     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
947   } else {
948     // FIXME: check for errors
949     smpi_datatype_extent(recvtype, &lb, &recvext);
950     // Local copy from root
951     smpi_datatype_copy(sendbuf, sendcount, sendtype,
952                        (char *)recvbuf + root * recvcount * recvext, recvcount, recvtype);
953     // Receive buffers from senders
954     requests = xbt_new(MPI_Request, size - 1);
955     index = 0;
956     for(src = 0; src < size; src++) {
957       if(src != root) {
958         requests[index] = smpi_irecv_init((char *)recvbuf + src * recvcount * recvext,
959                                           recvcount, recvtype,
960                                           src, system_tag, comm);
961         index++;
962       }
963     }
964     // Wait for completion of irecv's.
965     smpi_mpi_startall(size - 1, requests);
966     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
967     xbt_free(requests);
968   }
969 }
970
971
972 void smpi_mpi_reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
973                        MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
974 {
975     int i, size, count;
976     int *displs;
977     int rank = smpi_process_index();
978     void *tmpbuf;
979
980     /* arbitrarily choose root as rank 0 */
981     size = smpi_comm_size(comm);
982     count = 0;
983     displs = xbt_new(int, size);
984     for (i = 0; i < size; i++) {
985       displs[i] = count;
986       count += recvcounts[i];
987     }
988     tmpbuf=(void*)xbt_malloc(count*smpi_datatype_get_extent(datatype));
989     mpi_coll_reduce_fun(sendbuf, tmpbuf, count, datatype, op, 0, comm);
990     smpi_mpi_scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf,
991                       recvcounts[rank], datatype, 0, comm);
992     xbt_free(displs);
993     xbt_free(tmpbuf);
994 }
995
996 void smpi_mpi_gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
997                       void *recvbuf, int *recvcounts, int *displs,
998                       MPI_Datatype recvtype, int root, MPI_Comm comm)
999 {
1000   int system_tag = COLL_TAG_GATHERV;
1001   int rank, size, src, index;
1002   MPI_Aint lb = 0, recvext = 0;
1003   MPI_Request *requests;
1004
1005   rank = smpi_comm_rank(comm);
1006   size = smpi_comm_size(comm);
1007   if(rank != root) {
1008     // Send buffer to root
1009     smpi_mpi_send(sendbuf, sendcount, sendtype, root, system_tag, comm);
1010   } else {
1011     // FIXME: check for errors
1012     smpi_datatype_extent(recvtype, &lb, &recvext);
1013     // Local copy from root
1014     smpi_datatype_copy(sendbuf, sendcount, sendtype,
1015                        (char *)recvbuf + displs[root] * recvext,
1016                        recvcounts[root], recvtype);
1017     // Receive buffers from senders
1018     requests = xbt_new(MPI_Request, size - 1);
1019     index = 0;
1020     for(src = 0; src < size; src++) {
1021       if(src != root) {
1022         requests[index] =
1023           smpi_irecv_init((char *)recvbuf + displs[src] * recvext,
1024                           recvcounts[src], recvtype, src, system_tag, comm);
1025         index++;
1026       }
1027     }
1028     // Wait for completion of irecv's.
1029     smpi_mpi_startall(size - 1, requests);
1030     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1031     xbt_free(requests);
1032   }
1033 }
1034
1035 void smpi_mpi_allgather(void *sendbuf, int sendcount,
1036                         MPI_Datatype sendtype, void *recvbuf,
1037                         int recvcount, MPI_Datatype recvtype,
1038                         MPI_Comm comm)
1039 {
1040   int system_tag = COLL_TAG_ALLGATHER;
1041   int rank, size, other, index;
1042   MPI_Aint lb = 0, recvext = 0;
1043   MPI_Request *requests;
1044
1045   rank = smpi_comm_rank(comm);
1046   size = smpi_comm_size(comm);
1047   // FIXME: check for errors
1048   smpi_datatype_extent(recvtype, &lb, &recvext);
1049   // Local copy from self
1050   smpi_datatype_copy(sendbuf, sendcount, sendtype,
1051                      (char *)recvbuf + rank * recvcount * recvext, recvcount,
1052                      recvtype);
1053   // Send/Recv buffers to/from others;
1054   requests = xbt_new(MPI_Request, 2 * (size - 1));
1055   index = 0;
1056   for(other = 0; other < size; other++) {
1057     if(other != rank) {
1058       requests[index] =
1059         smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1060                         comm);
1061       index++;
1062       requests[index] = smpi_irecv_init((char *)recvbuf + other * recvcount * recvext,
1063                                         recvcount, recvtype, other,
1064                                         system_tag, comm);
1065       index++;
1066     }
1067   }
1068   // Wait for completion of all comms.
1069   smpi_mpi_startall(2 * (size - 1), requests);
1070   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1071   xbt_free(requests);
1072 }
1073
1074 void smpi_mpi_allgatherv(void *sendbuf, int sendcount,
1075                          MPI_Datatype sendtype, void *recvbuf,
1076                          int *recvcounts, int *displs,
1077                          MPI_Datatype recvtype, MPI_Comm comm)
1078 {
1079   int system_tag = COLL_TAG_ALLGATHERV;
1080   int rank, size, other, index;
1081   MPI_Aint lb = 0, recvext = 0;
1082   MPI_Request *requests;
1083
1084   rank = smpi_comm_rank(comm);
1085   size = smpi_comm_size(comm);
1086   // FIXME: check for errors
1087   smpi_datatype_extent(recvtype, &lb, &recvext);
1088   // Local copy from self
1089   smpi_datatype_copy(sendbuf, sendcount, sendtype,
1090                      (char *)recvbuf + displs[rank] * recvext,
1091                      recvcounts[rank], recvtype);
1092   // Send buffers to others;
1093   requests = xbt_new(MPI_Request, 2 * (size - 1));
1094   index = 0;
1095   for(other = 0; other < size; other++) {
1096     if(other != rank) {
1097       requests[index] =
1098         smpi_isend_init(sendbuf, sendcount, sendtype, other, system_tag,
1099                         comm);
1100       index++;
1101       requests[index] =
1102         smpi_irecv_init((char *)recvbuf + displs[other] * recvext, recvcounts[other],
1103                         recvtype, other, system_tag, comm);
1104       index++;
1105     }
1106   }
1107   // Wait for completion of all comms.
1108   smpi_mpi_startall(2 * (size - 1), requests);
1109   smpi_mpi_waitall(2 * (size - 1), requests, MPI_STATUS_IGNORE);
1110   xbt_free(requests);
1111 }
1112
1113 void smpi_mpi_scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1114                       void *recvbuf, int recvcount, MPI_Datatype recvtype,
1115                       int root, MPI_Comm comm)
1116 {
1117   int system_tag = COLL_TAG_SCATTER;
1118   int rank, size, dst, index;
1119   MPI_Aint lb = 0, sendext = 0;
1120   MPI_Request *requests;
1121
1122   rank = smpi_comm_rank(comm);
1123   size = smpi_comm_size(comm);
1124   if(rank != root) {
1125     // Recv buffer from root
1126     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1127                   MPI_STATUS_IGNORE);
1128   } else {
1129     // FIXME: check for errors
1130     smpi_datatype_extent(sendtype, &lb, &sendext);
1131     // Local copy from root
1132     if(recvbuf!=MPI_IN_PLACE){
1133         smpi_datatype_copy((char *)sendbuf + root * sendcount * sendext,
1134                            sendcount, sendtype, recvbuf, recvcount, recvtype);
1135     }
1136     // Send buffers to receivers
1137     requests = xbt_new(MPI_Request, size - 1);
1138     index = 0;
1139     for(dst = 0; dst < size; dst++) {
1140       if(dst != root) {
1141         requests[index] = smpi_isend_init((char *)sendbuf + dst * sendcount * sendext,
1142                                           sendcount, sendtype, dst,
1143                                           system_tag, comm);
1144         index++;
1145       }
1146     }
1147     // Wait for completion of isend's.
1148     smpi_mpi_startall(size - 1, requests);
1149     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1150     xbt_free(requests);
1151   }
1152 }
1153
1154 void smpi_mpi_scatterv(void *sendbuf, int *sendcounts, int *displs,
1155                        MPI_Datatype sendtype, void *recvbuf, int recvcount,
1156                        MPI_Datatype recvtype, int root, MPI_Comm comm)
1157 {
1158   int system_tag = COLL_TAG_SCATTERV;
1159   int rank, size, dst, index;
1160   MPI_Aint lb = 0, sendext = 0;
1161   MPI_Request *requests;
1162
1163   rank = smpi_comm_rank(comm);
1164   size = smpi_comm_size(comm);
1165   if(rank != root) {
1166     // Recv buffer from root
1167     smpi_mpi_recv(recvbuf, recvcount, recvtype, root, system_tag, comm,
1168                   MPI_STATUS_IGNORE);
1169   } else {
1170     // FIXME: check for errors
1171     smpi_datatype_extent(sendtype, &lb, &sendext);
1172     // Local copy from root
1173     if(recvbuf!=MPI_IN_PLACE){
1174       smpi_datatype_copy((char *)sendbuf + displs[root] * sendext, sendcounts[root],
1175                        sendtype, recvbuf, recvcount, recvtype);
1176     }
1177     // Send buffers to receivers
1178     requests = xbt_new(MPI_Request, size - 1);
1179     index = 0;
1180     for(dst = 0; dst < size; dst++) {
1181       if(dst != root) {
1182         requests[index] =
1183           smpi_isend_init((char *)sendbuf + displs[dst] * sendext, sendcounts[dst],
1184                           sendtype, dst, system_tag, comm);
1185         index++;
1186       }
1187     }
1188     // Wait for completion of isend's.
1189     smpi_mpi_startall(size - 1, requests);
1190     smpi_mpi_waitall(size - 1, requests, MPI_STATUS_IGNORE);
1191     xbt_free(requests);
1192   }
1193 }
1194
1195 void smpi_mpi_reduce(void *sendbuf, void *recvbuf, int count,
1196                      MPI_Datatype datatype, MPI_Op op, int root,
1197                      MPI_Comm comm)
1198 {
1199   int system_tag = COLL_TAG_REDUCE;
1200   int rank, size, src, index;
1201   MPI_Aint lb = 0, dataext = 0;
1202   MPI_Request *requests;
1203   void **tmpbufs;
1204
1205
1206   char* sendtmpbuf = (char*) sendbuf;
1207   if( sendbuf == MPI_IN_PLACE ) {
1208     sendtmpbuf = (char *)xbt_malloc(count*smpi_datatype_get_extent(datatype));
1209     smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
1210   }
1211
1212   rank = smpi_comm_rank(comm);
1213   size = smpi_comm_size(comm);
1214   //non commutative case, use a working algo from openmpi
1215   if(!smpi_op_is_commute(op)){
1216     smpi_coll_tuned_reduce_ompi_basic_linear(sendtmpbuf, recvbuf, count,
1217                      datatype, op, root, comm);
1218     return;
1219   }
1220   
1221   if(rank != root) {
1222     // Send buffer to root
1223     smpi_mpi_send(sendtmpbuf, count, datatype, root, system_tag, comm);
1224   } else {
1225     // FIXME: check for errors
1226     smpi_datatype_extent(datatype, &lb, &dataext);
1227     // Local copy from root
1228     if (sendtmpbuf && recvbuf)
1229       smpi_datatype_copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
1230     // Receive buffers from senders
1231     //TODO: make a MPI_barrier here ?
1232     requests = xbt_new(MPI_Request, size - 1);
1233     tmpbufs = xbt_new(void *, size - 1);
1234     index = 0;
1235     for(src = 0; src < size; src++) {
1236       if(src != root) {
1237         // FIXME: possibly overkill we we have contiguous/noncontiguous data
1238         //  mapping...
1239         tmpbufs[index] = xbt_malloc(count * dataext);
1240         requests[index] =
1241           smpi_irecv_init(tmpbufs[index], count, datatype, src,
1242                           system_tag, comm);
1243         index++;
1244       }
1245     }
1246     // Wait for completion of irecv's.
1247     smpi_mpi_startall(size - 1, requests);
1248     for(src = 0; src < size - 1; src++) {
1249       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1250       XBT_DEBUG("finished waiting any request with index %d", index);
1251       if(index == MPI_UNDEFINED) {
1252         break;
1253       }
1254       if(op) /* op can be MPI_OP_NULL that does nothing */
1255         smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1256     }
1257     for(index = 0; index < size - 1; index++) {
1258       xbt_free(tmpbufs[index]);
1259     }
1260     xbt_free(tmpbufs);
1261     xbt_free(requests);
1262
1263     if( sendbuf == MPI_IN_PLACE ) {
1264       xbt_free(sendtmpbuf);
1265     }
1266   }
1267 }
1268
1269 void smpi_mpi_allreduce(void *sendbuf, void *recvbuf, int count,
1270                         MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1271 {
1272   smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1273   smpi_mpi_bcast(recvbuf, count, datatype, 0, comm);
1274 }
1275
1276 void smpi_mpi_scan(void *sendbuf, void *recvbuf, int count,
1277                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1278 {
1279   int system_tag = -888;
1280   int rank, size, other, index;
1281   MPI_Aint lb = 0, dataext = 0;
1282   MPI_Request *requests;
1283   void **tmpbufs;
1284
1285   rank = smpi_comm_rank(comm);
1286   size = smpi_comm_size(comm);
1287
1288   // FIXME: check for errors
1289   smpi_datatype_extent(datatype, &lb, &dataext);
1290
1291   // Local copy from self
1292   smpi_datatype_copy(sendbuf, count, datatype, recvbuf, count, datatype);
1293
1294   // Send/Recv buffers to/from others;
1295   requests = xbt_new(MPI_Request, size - 1);
1296   tmpbufs = xbt_new(void *, rank);
1297   index = 0;
1298   for(other = 0; other < rank; other++) {
1299     // FIXME: possibly overkill we we have contiguous/noncontiguous data
1300     // mapping...
1301     tmpbufs[index] = xbt_malloc(count * dataext);
1302     requests[index] =
1303       smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1304                       comm);
1305     index++;
1306   }
1307   for(other = rank + 1; other < size; other++) {
1308     requests[index] =
1309       smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1310     index++;
1311   }
1312   // Wait for completion of all comms.
1313   smpi_mpi_startall(size - 1, requests);
1314
1315   if(smpi_op_is_commute(op)){
1316     for(other = 0; other < size - 1; other++) {
1317       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1318       if(index == MPI_UNDEFINED) {
1319         break;
1320       }
1321       if(index < rank) {
1322         // #Request is below rank: it's a irecv
1323         smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1324       }
1325     }
1326   }else{
1327     //non commutative case, wait in order
1328     for(other = 0; other < size - 1; other++) {
1329       smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1330       if(index < rank) {
1331         smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1332       }
1333     }
1334   }
1335   for(index = 0; index < rank; index++) {
1336     xbt_free(tmpbufs[index]);
1337   }
1338   xbt_free(tmpbufs);
1339   xbt_free(requests);
1340 }
1341
1342 void smpi_mpi_exscan(void *sendbuf, void *recvbuf, int count,
1343                    MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1344 {
1345   int system_tag = -888;
1346   int rank, size, other, index;
1347   MPI_Aint lb = 0, dataext = 0;
1348   MPI_Request *requests;
1349   void **tmpbufs;
1350   int recvbuf_is_empty=1;
1351   rank = smpi_comm_rank(comm);
1352   size = smpi_comm_size(comm);
1353
1354   // FIXME: check for errors
1355   smpi_datatype_extent(datatype, &lb, &dataext);
1356
1357   // Send/Recv buffers to/from others;
1358   requests = xbt_new(MPI_Request, size - 1);
1359   tmpbufs = xbt_new(void *, rank);
1360   index = 0;
1361   for(other = 0; other < rank; other++) {
1362     // FIXME: possibly overkill we we have contiguous/noncontiguous data
1363     // mapping...
1364     tmpbufs[index] = xbt_malloc(count * dataext);
1365     requests[index] =
1366       smpi_irecv_init(tmpbufs[index], count, datatype, other, system_tag,
1367                       comm);
1368     index++;
1369   }
1370   for(other = rank + 1; other < size; other++) {
1371     requests[index] =
1372       smpi_isend_init(sendbuf, count, datatype, other, system_tag, comm);
1373     index++;
1374   }
1375   // Wait for completion of all comms.
1376   smpi_mpi_startall(size - 1, requests);
1377   if(smpi_op_is_commute(op)){
1378     for(other = 0; other < size - 1; other++) {
1379       index = smpi_mpi_waitany(size - 1, requests, MPI_STATUS_IGNORE);
1380       if(index == MPI_UNDEFINED) {
1381         break;
1382       }
1383       if(index < rank) {
1384         if(recvbuf_is_empty){
1385           smpi_datatype_copy(tmpbufs[index], count, datatype, recvbuf, count, datatype);
1386           recvbuf_is_empty=0;
1387         }else
1388         // #Request is below rank: it's a irecv
1389         smpi_op_apply(op, tmpbufs[index], recvbuf, &count, &datatype);
1390       }
1391     }
1392   }else{
1393     //non commutative case, wait in order
1394     for(other = 0; other < size - 1; other++) {
1395       smpi_mpi_wait(&(requests[other]), MPI_STATUS_IGNORE);
1396       if(index < rank) {
1397           if(recvbuf_is_empty){
1398             smpi_datatype_copy(tmpbufs[other], count, datatype, recvbuf, count, datatype);
1399             recvbuf_is_empty=0;
1400           }else smpi_op_apply(op, tmpbufs[other], recvbuf, &count, &datatype);
1401       }
1402     }
1403   }
1404   for(index = 0; index < rank; index++) {
1405     xbt_free(tmpbufs[index]);
1406   }
1407   xbt_free(tmpbufs);
1408   xbt_free(requests);
1409 }