Logo AND Algorithmique Numérique Distribuée

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