Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use xbt_XXX_is_empty() instead of testing xbt_XXX_length() against 0.
[simgrid.git] / src / smpi / smpi_pmpi.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 "smpi_mpi_dt_private.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_pmpi, smpi,
11                                 "Logging specific to SMPI (pmpi)");
12
13 #ifdef HAVE_TRACING
14 //this function need to be here because of the calls to smpi_bench
15 void TRACE_smpi_set_category(const char *category)
16 {
17   //need to end bench otherwise categories for execution tasks are wrong
18   smpi_bench_end();
19   TRACE_internal_smpi_set_category (category);
20   //begin bench after changing process's category
21   smpi_bench_begin();
22 }
23 #endif
24
25 /* PMPI User level calls */
26
27 int PMPI_Init(int *argc, char ***argv)
28 {
29   smpi_process_init(argc, argv);
30 #ifdef HAVE_TRACING
31   int rank = smpi_process_index();
32   TRACE_smpi_init(rank);
33
34   TRACE_smpi_computing_init(rank);
35 #endif
36   smpi_bench_begin();
37   return MPI_SUCCESS;
38 }
39
40 int PMPI_Finalize(void)
41 {
42   smpi_process_finalize();
43   smpi_bench_end();
44 #ifdef HAVE_TRACING
45   int rank = smpi_process_index();
46   TRACE_smpi_computing_out(rank);
47   TRACE_smpi_finalize(smpi_process_index());
48 #endif
49   smpi_process_destroy();
50   return MPI_SUCCESS;
51 }
52
53 int PMPI_Init_thread(int *argc, char ***argv, int required, int *provided)
54 {
55   if (provided != NULL) {
56     *provided = MPI_THREAD_MULTIPLE;
57   }
58   return MPI_Init(argc, argv);
59 }
60
61 int PMPI_Query_thread(int *provided)
62 {
63   int retval;
64
65   smpi_bench_end();
66   if (provided == NULL) {
67     retval = MPI_ERR_ARG;
68   } else {
69     *provided = MPI_THREAD_MULTIPLE;
70     retval = MPI_SUCCESS;
71   }
72   smpi_bench_begin();
73   return retval;
74 }
75
76 int PMPI_Is_thread_main(int *flag)
77 {
78   int retval;
79
80   smpi_bench_end();
81   if (flag == NULL) {
82     retval = MPI_ERR_ARG;
83   } else {
84     *flag = smpi_process_index() == 0;
85     retval = MPI_SUCCESS;
86   }
87   smpi_bench_begin();
88   return retval;
89 }
90
91 int PMPI_Abort(MPI_Comm comm, int errorcode)
92 {
93   smpi_bench_end();
94   smpi_process_destroy();
95 #ifdef HAVE_TRACING
96   int rank = smpi_process_index();
97   TRACE_smpi_computing_out(rank);
98 #endif
99   // FIXME: should kill all processes in comm instead
100   simcall_process_kill(SIMIX_process_self());
101   return MPI_SUCCESS;
102 }
103
104 double PMPI_Wtime(void)
105 {
106   double time;
107
108   smpi_bench_end();
109   time = SIMIX_get_clock();
110   smpi_bench_begin();
111   return time;
112 }
113 extern double sg_maxmin_precision;
114 double PMPI_Wtick(void)
115 {
116   return sg_maxmin_precision;
117 }
118
119 int PMPI_Address(void *location, MPI_Aint * address)
120 {
121   int retval;
122
123   smpi_bench_end();
124   if (!address) {
125     retval = MPI_ERR_ARG;
126   } else {
127     *address = (MPI_Aint) location;
128     retval = MPI_SUCCESS;
129   }
130   smpi_bench_begin();
131   return retval;
132 }
133
134 int PMPI_Type_free(MPI_Datatype * datatype)
135 {
136   int retval;
137
138   smpi_bench_end();
139   if (!datatype) {
140     retval = MPI_ERR_ARG;
141   } else {
142     smpi_datatype_free(datatype);
143     retval = MPI_SUCCESS;
144   }
145   smpi_bench_begin();
146   return retval;
147 }
148
149 int PMPI_Type_size(MPI_Datatype datatype, int *size)
150 {
151   int retval;
152
153   smpi_bench_end();
154   if (datatype == MPI_DATATYPE_NULL) {
155     retval = MPI_ERR_TYPE;
156   } else if (size == NULL) {
157     retval = MPI_ERR_ARG;
158   } else {
159     *size = (int) smpi_datatype_size(datatype);
160     retval = MPI_SUCCESS;
161   }
162   smpi_bench_begin();
163   return retval;
164 }
165
166 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
167 {
168   int retval;
169
170   smpi_bench_end();
171   if (datatype == MPI_DATATYPE_NULL) {
172     retval = MPI_ERR_TYPE;
173   } else if (lb == NULL || extent == NULL) {
174     retval = MPI_ERR_ARG;
175   } else {
176     retval = smpi_datatype_extent(datatype, lb, extent);
177   }
178   smpi_bench_begin();
179   return retval;
180 }
181
182 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
183 {
184   int retval;
185
186   smpi_bench_end();
187   if (datatype == MPI_DATATYPE_NULL) {
188     retval = MPI_ERR_TYPE;
189   } else if (extent == NULL) {
190     retval = MPI_ERR_ARG;
191   } else {
192     *extent = smpi_datatype_get_extent(datatype);
193     retval = MPI_SUCCESS;
194   }
195   smpi_bench_begin();
196   return retval;
197 }
198
199 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
200 {
201   int retval;
202
203   smpi_bench_end();
204   if (datatype == MPI_DATATYPE_NULL) {
205     retval = MPI_ERR_TYPE;
206   } else if (disp == NULL) {
207     retval = MPI_ERR_ARG;
208   } else {
209     *disp = smpi_datatype_lb(datatype);
210     retval = MPI_SUCCESS;
211   }
212   smpi_bench_begin();
213   return retval;
214 }
215
216 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
217 {
218   int retval;
219
220   smpi_bench_end();
221   if (datatype == MPI_DATATYPE_NULL) {
222     retval = MPI_ERR_TYPE;
223   } else if (disp == NULL) {
224     retval = MPI_ERR_ARG;
225   } else {
226     *disp = smpi_datatype_ub(datatype);
227     retval = MPI_SUCCESS;
228   }
229   smpi_bench_begin();
230   return retval;
231 }
232
233 int PMPI_Op_create(MPI_User_function * function, int commute, MPI_Op * op)
234 {
235   int retval;
236
237   smpi_bench_end();
238   if (function == NULL || op == NULL) {
239     retval = MPI_ERR_ARG;
240   } else {
241     *op = smpi_op_new(function, commute);
242     retval = MPI_SUCCESS;
243   }
244   smpi_bench_begin();
245   return retval;
246 }
247
248 int PMPI_Op_free(MPI_Op * op)
249 {
250   int retval;
251
252   smpi_bench_end();
253   if (op == NULL) {
254     retval = MPI_ERR_ARG;
255   } else if (*op == MPI_OP_NULL) {
256     retval = MPI_ERR_OP;
257   } else {
258     smpi_op_destroy(*op);
259     *op = MPI_OP_NULL;
260     retval = MPI_SUCCESS;
261   }
262   smpi_bench_begin();
263   return retval;
264 }
265
266 int PMPI_Group_free(MPI_Group * group)
267 {
268   int retval;
269
270   smpi_bench_end();
271   if (group == NULL) {
272     retval = MPI_ERR_ARG;
273   } else {
274     if(*group!= smpi_comm_group(MPI_COMM_WORLD))// do not free the group of the comm_world
275     smpi_group_destroy(*group);
276     *group = MPI_GROUP_NULL;
277     retval = MPI_SUCCESS;
278   }
279   smpi_bench_begin();
280   return retval;
281 }
282
283 int PMPI_Group_size(MPI_Group group, int *size)
284 {
285   int retval;
286
287   smpi_bench_end();
288   if (group == MPI_GROUP_NULL) {
289     retval = MPI_ERR_GROUP;
290   } else if (size == NULL) {
291     retval = MPI_ERR_ARG;
292   } else {
293     *size = smpi_group_size(group);
294     retval = MPI_SUCCESS;
295   }
296   smpi_bench_begin();
297   return retval;
298 }
299
300 int PMPI_Group_rank(MPI_Group group, int *rank)
301 {
302   int retval;
303
304   smpi_bench_end();
305   if (group == MPI_GROUP_NULL) {
306     retval = MPI_ERR_GROUP;
307   } else if (rank == NULL) {
308     retval = MPI_ERR_ARG;
309   } else {
310     *rank = smpi_group_rank(group, smpi_process_index());
311     retval = MPI_SUCCESS;
312   }
313   smpi_bench_begin();
314   return retval;
315 }
316
317 int PMPI_Group_translate_ranks(MPI_Group group1, int n, int *ranks1,
318                               MPI_Group group2, int *ranks2)
319 {
320   int retval, i, index;
321
322   smpi_bench_end();
323   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
324     retval = MPI_ERR_GROUP;
325   } else {
326     for (i = 0; i < n; i++) {
327       index = smpi_group_index(group1, ranks1[i]);
328       ranks2[i] = smpi_group_rank(group2, index);
329     }
330     retval = MPI_SUCCESS;
331   }
332   smpi_bench_begin();
333   return retval;
334 }
335
336 int PMPI_Group_compare(MPI_Group group1, MPI_Group group2, int *result)
337 {
338   int retval;
339
340   smpi_bench_end();
341   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
342     retval = MPI_ERR_GROUP;
343   } else if (result == NULL) {
344     retval = MPI_ERR_ARG;
345   } else {
346     *result = smpi_group_compare(group1, group2);
347     retval = MPI_SUCCESS;
348   }
349   smpi_bench_begin();
350   return retval;
351 }
352
353 int PMPI_Group_union(MPI_Group group1, MPI_Group group2,
354                     MPI_Group * newgroup)
355 {
356   int retval, i, proc1, proc2, size, size2;
357
358   smpi_bench_end();
359   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
360     retval = MPI_ERR_GROUP;
361   } else if (newgroup == NULL) {
362     retval = MPI_ERR_ARG;
363   } else {
364     size = smpi_group_size(group1);
365     size2 = smpi_group_size(group2);
366     for (i = 0; i < size2; i++) {
367       proc2 = smpi_group_index(group2, i);
368       proc1 = smpi_group_rank(group1, proc2);
369       if (proc1 == MPI_UNDEFINED) {
370         size++;
371       }
372     }
373     if (size == 0) {
374       *newgroup = MPI_GROUP_EMPTY;
375     } else {
376       *newgroup = smpi_group_new(size);
377       size2 = smpi_group_size(group1);
378       for (i = 0; i < size2; i++) {
379         proc1 = smpi_group_index(group1, i);
380         smpi_group_set_mapping(*newgroup, proc1, i);
381       }
382       for (i = size2; i < size; i++) {
383         proc2 = smpi_group_index(group2, i - size2);
384         smpi_group_set_mapping(*newgroup, proc2, i);
385       }
386     }
387     smpi_group_use(*newgroup);
388     retval = MPI_SUCCESS;
389   }
390   smpi_bench_begin();
391   return retval;
392 }
393
394 int PMPI_Group_intersection(MPI_Group group1, MPI_Group group2,
395                            MPI_Group * newgroup)
396 {
397   int retval, i, proc1, proc2, size;
398
399   smpi_bench_end();
400   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
401     retval = MPI_ERR_GROUP;
402   } else if (newgroup == NULL) {
403     retval = MPI_ERR_ARG;
404   } else {
405     size = smpi_group_size(group2);
406     for (i = 0; i < size; i++) {
407       proc2 = smpi_group_index(group2, i);
408       proc1 = smpi_group_rank(group1, proc2);
409       if (proc1 == MPI_UNDEFINED) {
410         size--;
411       }
412     }
413     if (size == 0) {
414       *newgroup = MPI_GROUP_EMPTY;
415     } else {
416       *newgroup = smpi_group_new(size);
417       int j=0;
418       for (i = 0; i < smpi_group_size(group2); i++) {
419         proc2 = smpi_group_index(group2, i);
420         proc1 = smpi_group_rank(group1, proc2);
421         if (proc1 != MPI_UNDEFINED) {
422           smpi_group_set_mapping(*newgroup, proc2, j);
423           j++;
424         }
425       }
426     }
427     smpi_group_use(*newgroup);
428     retval = MPI_SUCCESS;
429   }
430   smpi_bench_begin();
431   return retval;
432 }
433
434 int PMPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
435 {
436   int retval, i, proc1, proc2, size, size2;
437
438   smpi_bench_end();
439   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
440     retval = MPI_ERR_GROUP;
441   } else if (newgroup == NULL) {
442     retval = MPI_ERR_ARG;
443   } else {
444     size = size2 = smpi_group_size(group1);
445     for (i = 0; i < size2; i++) {
446       proc1 = smpi_group_index(group1, i);
447       proc2 = smpi_group_rank(group2, proc1);
448       if (proc2 != MPI_UNDEFINED) {
449         size--;
450       }
451     }
452     if (size == 0) {
453       *newgroup = MPI_GROUP_EMPTY;
454     } else {
455       *newgroup = smpi_group_new(size);
456       for (i = 0; i < size2; i++) {
457         proc1 = smpi_group_index(group1, i);
458         proc2 = smpi_group_rank(group2, proc1);
459         if (proc2 == MPI_UNDEFINED) {
460           smpi_group_set_mapping(*newgroup, proc1, i);
461         }
462       }
463     }
464     smpi_group_use(*newgroup);
465     retval = MPI_SUCCESS;
466   }
467   smpi_bench_begin();
468   return retval;
469 }
470
471 int PMPI_Group_incl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
472 {
473   int retval, i, index;
474
475   smpi_bench_end();
476   if (group == MPI_GROUP_NULL) {
477     retval = MPI_ERR_GROUP;
478   } else if (newgroup == NULL) {
479     retval = MPI_ERR_ARG;
480   } else {
481     if (n == 0) {
482       *newgroup = MPI_GROUP_EMPTY;
483     } else if (n == smpi_group_size(group)) {
484       *newgroup = group;
485     } else {
486       *newgroup = smpi_group_new(n);
487       for (i = 0; i < n; i++) {
488         index = smpi_group_index(group, ranks[i]);
489         smpi_group_set_mapping(*newgroup, index, i);
490       }
491     }
492     smpi_group_use(*newgroup);
493     retval = MPI_SUCCESS;
494   }
495   smpi_bench_begin();
496   return retval;
497 }
498
499 int PMPI_Group_excl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
500 {
501   int retval, i, j, newsize, oldsize, index;
502
503   smpi_bench_end();
504   if (group == MPI_GROUP_NULL) {
505     retval = MPI_ERR_GROUP;
506   } else if (newgroup == NULL) {
507     retval = MPI_ERR_ARG;
508   } else {
509     if (n == 0) {
510       *newgroup = group;
511     } else if (n == smpi_group_size(group)) {
512       *newgroup = MPI_GROUP_EMPTY;
513     } else {
514       oldsize=smpi_group_size(group);
515       newsize = oldsize - n;
516       *newgroup = smpi_group_new(newsize);
517
518       int* to_exclude=xbt_new(int, smpi_group_size(group));
519       for(i=0; i<oldsize; i++)
520         to_exclude[i]=0;
521       for(i=0; i<n; i++)
522         to_exclude[ranks[i]]=1;
523
524       j=0;
525       for(i=0; i<oldsize; i++){
526         if(to_exclude[i]==0){
527           index = smpi_group_index(group, i);
528           smpi_group_set_mapping(*newgroup, index, j);
529           j++;
530         }
531       }
532
533       xbt_free(to_exclude);
534     }
535     smpi_group_use(*newgroup);
536     retval = MPI_SUCCESS;
537   }
538   smpi_bench_begin();
539   return retval;
540 }
541
542 int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3],
543                          MPI_Group * newgroup)
544 {
545   int retval, i, j, rank, size, index;
546
547   smpi_bench_end();
548   if (group == MPI_GROUP_NULL) {
549     retval = MPI_ERR_GROUP;
550   } else if (newgroup == NULL) {
551     retval = MPI_ERR_ARG;
552   } else {
553     if (n == 0) {
554       *newgroup = MPI_GROUP_EMPTY;
555     } else {
556       size = 0;
557       for (i = 0; i < n; i++) {
558         for (rank = ranges[i][0];       /* First */
559              rank >= 0 && rank <= ranges[i][1]; /* Last */
560              rank += ranges[i][2] /* Stride */ ) {
561           size++;
562         }
563       }
564       if (size == smpi_group_size(group)) {
565         *newgroup = group;
566       } else {
567         *newgroup = smpi_group_new(size);
568         j = 0;
569         for (i = 0; i < n; i++) {
570           for (rank = ranges[i][0];     /* First */
571                rank >= 0 && rank <= ranges[i][1];       /* Last */
572                rank += ranges[i][2] /* Stride */ ) {
573             index = smpi_group_index(group, rank);
574             smpi_group_set_mapping(*newgroup, index, j);
575             j++;
576           }
577         }
578       }
579     }
580     smpi_group_use(*newgroup);
581     retval = MPI_SUCCESS;
582   }
583   smpi_bench_begin();
584   return retval;
585 }
586
587 int PMPI_Group_range_excl(MPI_Group group, int n, int ranges[][3],
588                          MPI_Group * newgroup)
589 {
590   int retval, i, newrank, rank, size, index, add;
591
592   smpi_bench_end();
593   if (group == MPI_GROUP_NULL) {
594     retval = MPI_ERR_GROUP;
595   } else if (newgroup == NULL) {
596     retval = MPI_ERR_ARG;
597   } else {
598     if (n == 0) {
599       *newgroup = group;
600     } else {
601       size = smpi_group_size(group);
602       for (i = 0; i < n; i++) {
603         for (rank = ranges[i][0];       /* First */
604              rank >= 0 && rank <= ranges[i][1]; /* Last */
605              rank += ranges[i][2] /* Stride */ ) {
606           size--;
607         }
608       }
609       if (size == 0) {
610         *newgroup = MPI_GROUP_EMPTY;
611       } else {
612         *newgroup = smpi_group_new(size);
613         newrank = 0;
614         while (newrank < size) {
615           for (i = 0; i < n; i++) {
616             add = 1;
617             for (rank = ranges[i][0];   /* First */
618                  rank >= 0 && rank <= ranges[i][1];     /* Last */
619                  rank += ranges[i][2] /* Stride */ ) {
620               if (rank == newrank) {
621                 add = 0;
622                 break;
623               }
624             }
625             if (add == 1) {
626               index = smpi_group_index(group, newrank);
627               smpi_group_set_mapping(*newgroup, index, newrank);
628             }
629           }
630           newrank++; //added to avoid looping, need to be checked ..
631         }
632       }
633     }
634     smpi_group_use(*newgroup);
635     retval = MPI_SUCCESS;
636   }
637   smpi_bench_begin();
638   return retval;
639 }
640
641 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
642 {
643   int retval;
644
645   smpi_bench_end();
646   if (comm == MPI_COMM_NULL) {
647     retval = MPI_ERR_COMM;
648   } else if (rank == NULL) {
649     retval = MPI_ERR_ARG;
650   } else {
651     *rank = smpi_comm_rank(comm);
652     retval = MPI_SUCCESS;
653   }
654   smpi_bench_begin();
655   return retval;
656 }
657
658 int PMPI_Comm_size(MPI_Comm comm, int *size)
659 {
660   int retval;
661
662   smpi_bench_end();
663   if (comm == MPI_COMM_NULL) {
664     retval = MPI_ERR_COMM;
665   } else if (size == NULL) {
666     retval = MPI_ERR_ARG;
667   } else {
668     *size = smpi_comm_size(comm);
669     retval = MPI_SUCCESS;
670   }
671   smpi_bench_begin();
672   return retval;
673 }
674
675 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
676 {
677   int retval;
678
679   smpi_bench_end();
680   if (comm == MPI_COMM_NULL)  {
681     retval = MPI_ERR_COMM;
682   } else if (name == NULL || len == NULL)  {
683     retval = MPI_ERR_ARG;
684   } else {
685     smpi_comm_get_name(comm, name, len);
686     retval = MPI_SUCCESS;
687   }
688   smpi_bench_begin();
689   return retval;
690 }
691
692 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
693 {
694   int retval;
695
696   smpi_bench_end();
697   if (comm == MPI_COMM_NULL) {
698     retval = MPI_ERR_COMM;
699   } else if (group == NULL) {
700     retval = MPI_ERR_ARG;
701   } else {
702     *group = smpi_comm_group(comm);
703     retval = MPI_SUCCESS;
704   }
705   smpi_bench_begin();
706   return retval;
707 }
708
709 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
710 {
711   int retval;
712
713   smpi_bench_end();
714   if (comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
715     retval = MPI_ERR_COMM;
716   } else if (result == NULL) {
717     retval = MPI_ERR_ARG;
718   } else {
719     if (comm1 == comm2) {       /* Same communicators means same groups */
720       *result = MPI_IDENT;
721     } else {
722       *result =
723           smpi_group_compare(smpi_comm_group(comm1),
724                              smpi_comm_group(comm2));
725       if (*result == MPI_IDENT) {
726         *result = MPI_CONGRUENT;
727       }
728     }
729     retval = MPI_SUCCESS;
730   }
731   smpi_bench_begin();
732   return retval;
733 }
734
735 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
736 {
737   int retval;
738
739   smpi_bench_end();
740   if (comm == MPI_COMM_NULL) {
741     retval = MPI_ERR_COMM;
742   } else if (newcomm == NULL) {
743     retval = MPI_ERR_ARG;
744   } else {
745     *newcomm = smpi_comm_new(smpi_comm_group(comm));
746     retval = MPI_SUCCESS;
747   }
748   smpi_bench_begin();
749   return retval;
750 }
751
752 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
753 {
754   int retval;
755
756   smpi_bench_end();
757   if (comm == MPI_COMM_NULL) {
758     retval = MPI_ERR_COMM;
759   } else if (group == MPI_GROUP_NULL) {
760     retval = MPI_ERR_GROUP;
761   } else if (newcomm == NULL) {
762     retval = MPI_ERR_ARG;
763   } else {
764     *newcomm = smpi_comm_new(group);
765     retval = MPI_SUCCESS;
766   }
767   smpi_bench_begin();
768   return retval;
769 }
770
771 int PMPI_Comm_free(MPI_Comm * comm)
772 {
773   int retval;
774
775   smpi_bench_end();
776   if (comm == NULL) {
777     retval = MPI_ERR_ARG;
778   } else if (*comm == MPI_COMM_NULL) {
779     retval = MPI_ERR_COMM;
780   } else {
781     smpi_comm_destroy(*comm);
782     *comm = MPI_COMM_NULL;
783     retval = MPI_SUCCESS;
784   }
785   smpi_bench_begin();
786   return retval;
787 }
788
789 int PMPI_Comm_disconnect(MPI_Comm * comm)
790 {
791   /* TODO: wait until all communication in comm are done */
792   int retval;
793
794   smpi_bench_end();
795   if (comm == NULL) {
796     retval = MPI_ERR_ARG;
797   } else if (*comm == MPI_COMM_NULL) {
798     retval = MPI_ERR_COMM;
799   } else {
800     smpi_comm_destroy(*comm);
801     *comm = MPI_COMM_NULL;
802     retval = MPI_SUCCESS;
803   }
804   smpi_bench_begin();
805   return retval;
806 }
807
808 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
809 {
810   int retval;
811
812   smpi_bench_end();
813   if (comm_out == NULL) {
814     retval = MPI_ERR_ARG;
815   } else if (comm == MPI_COMM_NULL) {
816     retval = MPI_ERR_COMM;
817   } else {
818     *comm_out = smpi_comm_split(comm, color, key);
819     retval = MPI_SUCCESS;
820   }
821   smpi_bench_begin();
822   return retval;
823 }
824
825 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst,
826                   int tag, MPI_Comm comm, MPI_Request * request)
827 {
828   int retval;
829
830   smpi_bench_end();
831   if (request == NULL) {
832     retval = MPI_ERR_ARG;
833   } else if (comm == MPI_COMM_NULL) {
834     retval = MPI_ERR_COMM;
835   } else if (dst == MPI_PROC_NULL) {
836     retval = MPI_SUCCESS;
837   } else {
838     *request = smpi_mpi_send_init(buf, count, datatype, dst, tag, comm);
839     retval = MPI_SUCCESS;
840   }
841   smpi_bench_begin();
842   return retval;
843 }
844
845 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src,
846                   int tag, MPI_Comm comm, MPI_Request * request)
847 {
848   int retval;
849
850   smpi_bench_end();
851   if (request == NULL) {
852     retval = MPI_ERR_ARG;
853   } else if (comm == MPI_COMM_NULL) {
854     retval = MPI_ERR_COMM;
855   } else if (src == MPI_PROC_NULL) {
856       retval = MPI_SUCCESS;
857   } else {
858     *request = smpi_mpi_recv_init(buf, count, datatype, src, tag, comm);
859     retval = MPI_SUCCESS;
860   }
861   smpi_bench_begin();
862   return retval;
863 }
864
865 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request) {
866   int retval;
867
868     smpi_bench_end();
869     if (request == NULL) {
870       retval = MPI_ERR_ARG;
871     } else if (comm == MPI_COMM_NULL) {
872       retval = MPI_ERR_COMM;
873     } else if (dst == MPI_PROC_NULL) {
874       retval = MPI_SUCCESS;
875     } else {
876       *request = smpi_mpi_ssend_init(buf, count, datatype, dst, tag, comm);
877       retval = MPI_SUCCESS;
878     }
879     smpi_bench_begin();
880     return retval;
881 }
882
883 int PMPI_Start(MPI_Request * request)
884 {
885   int retval;
886
887   smpi_bench_end();
888   if (request == NULL || *request == MPI_REQUEST_NULL) {
889     retval = MPI_ERR_ARG;
890   } else {
891     smpi_mpi_start(*request);
892     retval = MPI_SUCCESS;
893   }
894   smpi_bench_begin();
895   return retval;
896 }
897
898 int PMPI_Startall(int count, MPI_Request * requests)
899 {
900   int retval;
901
902   smpi_bench_end();
903   if (requests == NULL) {
904     retval = MPI_ERR_ARG;
905   } else {
906     smpi_mpi_startall(count, requests);
907     retval = MPI_SUCCESS;
908   }
909   smpi_bench_begin();
910   return retval;
911 }
912
913 int PMPI_Request_free(MPI_Request * request)
914 {
915   int retval;
916
917   smpi_bench_end();
918   if (*request == MPI_REQUEST_NULL) {
919     retval = MPI_ERR_ARG;
920   } else {
921     if((*request)->flags & PERSISTENT)(*request)->refcount--;
922     smpi_mpi_request_free(request);
923     retval = MPI_SUCCESS;
924   }
925   smpi_bench_begin();
926   return retval;
927 }
928
929 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
930               int tag, MPI_Comm comm, MPI_Request * request)
931 {
932   int retval;
933
934   smpi_bench_end();
935
936   if (request == NULL) {
937     retval = MPI_ERR_ARG;
938   } else if (comm == MPI_COMM_NULL) {
939     retval = MPI_ERR_COMM;
940   } else if (src == MPI_PROC_NULL) {
941     *request = MPI_REQUEST_NULL;
942     retval = MPI_SUCCESS;
943   } else if (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0)){
944     retval = MPI_ERR_COMM;
945   } else if (count < 0) {
946     retval = MPI_ERR_COUNT;
947   } else if (buf==NULL && count > 0) {
948     retval = MPI_ERR_COUNT;
949   } else if (datatype == MPI_DATATYPE_NULL){
950     retval = MPI_ERR_TYPE;
951   } else if(tag<0 && tag !=  MPI_ANY_TAG){
952     retval = MPI_ERR_TAG;
953   } else {
954
955 #ifdef HAVE_TRACING
956   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
957   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
958   TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__);
959 #endif
960
961     *request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
962     retval = MPI_SUCCESS;
963
964 #ifdef HAVE_TRACING
965   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
966   (*request)->recv = 1;
967 #endif
968   }
969
970   smpi_bench_begin();
971   return retval;
972 }
973
974
975 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
976               int tag, MPI_Comm comm, MPI_Request * request)
977 {
978   int retval;
979
980   smpi_bench_end();
981   if (request == NULL) {
982     retval = MPI_ERR_ARG;
983   } else if (comm == MPI_COMM_NULL) {
984     retval = MPI_ERR_COMM;
985   } else if (dst == MPI_PROC_NULL) {
986     *request = MPI_REQUEST_NULL;
987     retval = MPI_SUCCESS;
988   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
989     retval = MPI_ERR_COMM;
990   } else if (count < 0) {
991     retval = MPI_ERR_COUNT;
992   } else if (buf==NULL && count > 0) {
993     retval = MPI_ERR_COUNT;
994   } else if (datatype == MPI_DATATYPE_NULL){
995     retval = MPI_ERR_TYPE;
996   } else if(tag<0 && tag !=  MPI_ANY_TAG){
997     retval = MPI_ERR_TAG;
998   } else {
999
1000 #ifdef HAVE_TRACING
1001   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1002   TRACE_smpi_computing_out(rank);
1003   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1004   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__);
1005   TRACE_smpi_send(rank, rank, dst_traced);
1006 #endif
1007
1008     *request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
1009     retval = MPI_SUCCESS;
1010
1011 #ifdef HAVE_TRACING
1012   TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1013   (*request)->send = 1;
1014   TRACE_smpi_computing_in(rank);
1015 #endif
1016   }
1017
1018   smpi_bench_begin();
1019   return retval;
1020 }
1021
1022 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request) {
1023   int retval;
1024
1025   smpi_bench_end();
1026   if (request == NULL) {
1027     retval = MPI_ERR_ARG;
1028   } else if (comm == MPI_COMM_NULL) {
1029     retval = MPI_ERR_COMM;
1030   } else if (dst == MPI_PROC_NULL) {
1031     *request = MPI_REQUEST_NULL;
1032     retval = MPI_SUCCESS;
1033   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1034     retval = MPI_ERR_COMM;
1035   } else if (count < 0) {
1036     retval = MPI_ERR_COUNT;
1037   } else if (buf==NULL && count > 0) {
1038     retval = MPI_ERR_COUNT;
1039   } else if (datatype == MPI_DATATYPE_NULL){
1040     retval = MPI_ERR_TYPE;
1041   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1042     retval = MPI_ERR_TAG;
1043   } else {
1044
1045 #ifdef HAVE_TRACING
1046   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1047   TRACE_smpi_computing_out(rank);
1048   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1049   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__);
1050   TRACE_smpi_send(rank, rank, dst_traced);
1051 #endif
1052
1053     *request = smpi_mpi_issend(buf, count, datatype, dst, tag, comm);
1054     retval = MPI_SUCCESS;
1055
1056 #ifdef HAVE_TRACING
1057   TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1058   (*request)->send = 1;
1059   TRACE_smpi_computing_in(rank);
1060 #endif
1061   }
1062
1063   smpi_bench_begin();
1064   return retval;
1065 }
1066
1067 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag,
1068              MPI_Comm comm, MPI_Status * status)
1069 {
1070   int retval;
1071
1072   smpi_bench_end();
1073   if (comm == MPI_COMM_NULL) {
1074     retval = MPI_ERR_COMM;
1075   } else if (src == MPI_PROC_NULL) {
1076     smpi_empty_status(status);
1077     status->MPI_SOURCE = MPI_PROC_NULL;
1078     retval = MPI_SUCCESS;
1079   } else if (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0)){
1080     retval = MPI_ERR_COMM;
1081   } else if (count < 0) {
1082     retval = MPI_ERR_COUNT;
1083   } else if (buf==NULL && count > 0) {
1084     retval = MPI_ERR_COUNT;
1085   } else if (datatype == MPI_DATATYPE_NULL){
1086     retval = MPI_ERR_TYPE;
1087   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1088     retval = MPI_ERR_TAG;
1089   } else {
1090 #ifdef HAVE_TRACING
1091   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1092   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
1093   TRACE_smpi_computing_out(rank);
1094
1095   TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__);
1096 #endif
1097
1098     smpi_mpi_recv(buf, count, datatype, src, tag, comm, status);
1099     retval = MPI_SUCCESS;
1100
1101 #ifdef HAVE_TRACING
1102   //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1103   if(status!=MPI_STATUS_IGNORE)src_traced = smpi_group_index(smpi_comm_group(comm), status->MPI_SOURCE);
1104   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
1105   TRACE_smpi_recv(rank, src_traced, rank);
1106   TRACE_smpi_computing_in(rank);
1107 #endif
1108   }
1109
1110   smpi_bench_begin();
1111   return retval;
1112 }
1113
1114 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag,
1115              MPI_Comm comm)
1116 {
1117   int retval;
1118
1119   smpi_bench_end();
1120
1121   if (comm == MPI_COMM_NULL) {
1122     retval = MPI_ERR_COMM;
1123   } else if (dst == MPI_PROC_NULL) {
1124     retval = MPI_SUCCESS;
1125   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1126     retval = MPI_ERR_COMM;
1127   } else if (count < 0) {
1128     retval = MPI_ERR_COUNT;
1129   } else if (buf==NULL && count > 0) {
1130     retval = MPI_ERR_COUNT;
1131   } else if (datatype == MPI_DATATYPE_NULL){
1132     retval = MPI_ERR_TYPE;
1133   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1134     retval = MPI_ERR_TAG;
1135   } else {
1136
1137 #ifdef HAVE_TRACING
1138   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1139   TRACE_smpi_computing_out(rank);
1140   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1141   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__);
1142   TRACE_smpi_send(rank, rank, dst_traced);
1143 #endif
1144
1145     smpi_mpi_send(buf, count, datatype, dst, tag, comm);
1146     retval = MPI_SUCCESS;
1147
1148 #ifdef HAVE_TRACING
1149   TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1150   TRACE_smpi_computing_in(rank);
1151 #endif
1152   }
1153
1154   smpi_bench_begin();
1155   return retval;
1156 }
1157
1158
1159
1160 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
1161   int retval;
1162
1163    smpi_bench_end();
1164
1165    if (comm == MPI_COMM_NULL) {
1166      retval = MPI_ERR_COMM;
1167    } else if (dst == MPI_PROC_NULL) {
1168      retval = MPI_SUCCESS;
1169    } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1170      retval = MPI_ERR_COMM;
1171    } else if (count < 0) {
1172      retval = MPI_ERR_COUNT;
1173    } else if (buf==NULL && count > 0) {
1174      retval = MPI_ERR_COUNT;
1175    } else if (datatype == MPI_DATATYPE_NULL){
1176      retval = MPI_ERR_TYPE;
1177    } else if(tag<0 && tag !=  MPI_ANY_TAG){
1178      retval = MPI_ERR_TAG;
1179    } else {
1180
1181  #ifdef HAVE_TRACING
1182    int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1183    TRACE_smpi_computing_out(rank);
1184    int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1185    TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__);
1186    TRACE_smpi_send(rank, rank, dst_traced);
1187  #endif
1188
1189      smpi_mpi_ssend(buf, count, datatype, dst, tag, comm);
1190      retval = MPI_SUCCESS;
1191
1192  #ifdef HAVE_TRACING
1193    TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1194    TRACE_smpi_computing_in(rank);
1195  #endif
1196    }
1197
1198    smpi_bench_begin();
1199    return retval;}
1200
1201
1202 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1203                  int dst, int sendtag, void *recvbuf, int recvcount,
1204                  MPI_Datatype recvtype, int src, int recvtag,
1205                  MPI_Comm comm, MPI_Status * status)
1206 {
1207   int retval;
1208
1209   smpi_bench_end();
1210
1211   if (comm == MPI_COMM_NULL) {
1212     retval = MPI_ERR_COMM;
1213   } else if (sendtype == MPI_DATATYPE_NULL
1214              || recvtype == MPI_DATATYPE_NULL) {
1215     retval = MPI_ERR_TYPE;
1216   } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1217       smpi_empty_status(status);
1218       status->MPI_SOURCE = MPI_PROC_NULL;
1219       retval = MPI_SUCCESS;
1220   }else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0 ||
1221       (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0))){
1222     retval = MPI_ERR_COMM;
1223   } else if (sendcount < 0 || recvcount<0) {
1224       retval = MPI_ERR_COUNT;
1225   } else if ((sendbuf==NULL && sendcount > 0)||(recvbuf==NULL && recvcount>0)) {
1226     retval = MPI_ERR_COUNT;
1227   } else if((sendtag<0 && sendtag !=  MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1228     retval = MPI_ERR_TAG;
1229   } else {
1230
1231 #ifdef HAVE_TRACING
1232   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1233   TRACE_smpi_computing_out(rank);
1234   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1235   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
1236   TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__);
1237   TRACE_smpi_send(rank, rank, dst_traced);
1238   TRACE_smpi_send(rank, src_traced, rank);
1239 #endif
1240
1241
1242     smpi_mpi_sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf,
1243                       recvcount, recvtype, src, recvtag, comm, status);
1244     retval = MPI_SUCCESS;
1245
1246 #ifdef HAVE_TRACING
1247   TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1248   TRACE_smpi_recv(rank, rank, dst_traced);
1249   TRACE_smpi_recv(rank, src_traced, rank);
1250   TRACE_smpi_computing_in(rank);
1251 #endif
1252
1253   }
1254
1255   smpi_bench_begin();
1256   return retval;
1257 }
1258
1259 int PMPI_Sendrecv_replace(void *buf, int count, MPI_Datatype datatype,
1260                          int dst, int sendtag, int src, int recvtag,
1261                          MPI_Comm comm, MPI_Status * status)
1262 {
1263   //TODO: suboptimal implementation
1264   void *recvbuf;
1265   int retval;
1266   if ((datatype == MPI_DATATYPE_NULL)||(datatype->has_subtype==1)) {
1267       retval = MPI_ERR_TYPE;
1268   } else if (count < 0) {
1269       retval = MPI_ERR_COUNT;
1270   } else {
1271     int size = smpi_datatype_size(datatype) * count;
1272     recvbuf = xbt_new(char, size);
1273     retval =
1274         MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count,
1275                      datatype, src, recvtag, comm, status);
1276     if(retval==MPI_SUCCESS){
1277         memcpy(buf, recvbuf, size * sizeof(char));
1278     }
1279     xbt_free(recvbuf);
1280
1281   }
1282   return retval;
1283 }
1284
1285 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1286 {
1287   int retval;
1288
1289   smpi_bench_end();
1290   if (request == MPI_REQUEST_NULL || flag == NULL) {
1291     retval = MPI_ERR_ARG;
1292   } else if (*request == MPI_REQUEST_NULL) {
1293     *flag= TRUE;
1294     retval = MPI_ERR_REQUEST;
1295   } else {
1296     *flag = smpi_mpi_test(request, status);
1297     retval = MPI_SUCCESS;
1298   }
1299   smpi_bench_begin();
1300   return retval;
1301 }
1302
1303 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag,
1304                 MPI_Status * status)
1305 {
1306   int retval;
1307
1308   smpi_bench_end();
1309   if (index == NULL || flag == NULL) {
1310     retval = MPI_ERR_ARG;
1311   } else {
1312     *flag = smpi_mpi_testany(count, requests, index, status);
1313     retval = MPI_SUCCESS;
1314   }
1315   smpi_bench_begin();
1316   return retval;
1317 }
1318
1319 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1320 {
1321   int retval;
1322
1323   smpi_bench_end();
1324   if (flag == NULL) {
1325     retval = MPI_ERR_ARG;
1326   } else {
1327     *flag = smpi_mpi_testall(count, requests, statuses);
1328     retval = MPI_SUCCESS;
1329   }
1330   smpi_bench_begin();
1331   return retval;
1332 }
1333
1334 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1335   int retval;
1336   smpi_bench_end();
1337
1338   if (status == NULL) {
1339     retval = MPI_ERR_ARG;
1340   } else if (comm == MPI_COMM_NULL) {
1341     retval = MPI_ERR_COMM;
1342   } else if (source == MPI_PROC_NULL) {
1343     smpi_empty_status(status);
1344     status->MPI_SOURCE = MPI_PROC_NULL;
1345     retval = MPI_SUCCESS;
1346   } else {
1347     smpi_mpi_probe(source, tag, comm, status);
1348     retval = MPI_SUCCESS;
1349   }
1350   smpi_bench_begin();
1351   return retval;
1352 }
1353
1354
1355 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1356   int retval;
1357   smpi_bench_end();
1358
1359   if (flag == NULL) {
1360     retval = MPI_ERR_ARG;
1361   } else if (status == NULL) {
1362     retval = MPI_ERR_ARG;
1363   } else if (comm == MPI_COMM_NULL) {
1364     retval = MPI_ERR_COMM;
1365   } else if (source == MPI_PROC_NULL) {
1366     smpi_empty_status(status);
1367     status->MPI_SOURCE = MPI_PROC_NULL;
1368     retval = MPI_SUCCESS;
1369   } else {
1370     smpi_mpi_iprobe(source, tag, comm, flag, status);
1371     retval = MPI_SUCCESS;
1372   }
1373   smpi_bench_begin();
1374   return retval;
1375 }
1376
1377 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1378 {
1379   int retval;
1380
1381   smpi_bench_end();
1382
1383   if (request == NULL) {
1384     retval = MPI_ERR_ARG;
1385   } else if (*request == MPI_REQUEST_NULL) {
1386     retval = MPI_ERR_REQUEST;
1387   } else {
1388
1389 #ifdef HAVE_TRACING
1390   int rank = request && (*request)->comm != MPI_COMM_NULL
1391       ? smpi_process_index()
1392       : -1;
1393   TRACE_smpi_computing_out(rank);
1394
1395   MPI_Group group = smpi_comm_group((*request)->comm);
1396   int src_traced = smpi_group_index(group, (*request)->src);
1397   int dst_traced = smpi_group_index(group, (*request)->dst);
1398   int is_wait_for_receive = (*request)->recv;
1399   TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__);
1400 #endif
1401
1402     smpi_mpi_wait(request, status);
1403     retval = MPI_SUCCESS;
1404
1405 #ifdef HAVE_TRACING
1406   TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1407   if (is_wait_for_receive) {
1408     TRACE_smpi_recv(rank, src_traced, dst_traced);
1409   }
1410   TRACE_smpi_computing_in(rank);
1411 #endif
1412
1413   }
1414
1415   smpi_bench_begin();
1416   return retval;
1417 }
1418
1419 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1420 {
1421   int retval;
1422
1423   smpi_bench_end();
1424 #ifdef HAVE_TRACING
1425   //save requests information for tracing
1426   int i;
1427   xbt_dynar_t srcs = xbt_dynar_new(sizeof(int), NULL);
1428   xbt_dynar_t dsts = xbt_dynar_new(sizeof(int), NULL);
1429   xbt_dynar_t recvs = xbt_dynar_new(sizeof(int), NULL);
1430   for (i = 0; i < count; i++) {
1431     MPI_Request req = requests[i];      //already received requests are no longer valid
1432     if (req) {
1433       int *asrc = xbt_new(int, 1);
1434       int *adst = xbt_new(int, 1);
1435       int *arecv = xbt_new(int, 1);
1436       *asrc = req->src;
1437       *adst = req->dst;
1438       *arecv = req->recv;
1439       xbt_dynar_insert_at(srcs, i, asrc);
1440       xbt_dynar_insert_at(dsts, i, adst);
1441       xbt_dynar_insert_at(recvs, i, arecv);
1442       xbt_free(asrc);
1443       xbt_free(adst);
1444       xbt_free(arecv);
1445     } else {
1446       int *t = xbt_new(int, 1);
1447       xbt_dynar_insert_at(srcs, i, t);
1448       xbt_dynar_insert_at(dsts, i, t);
1449       xbt_dynar_insert_at(recvs, i, t);
1450       xbt_free(t);
1451     }
1452   }
1453   int rank_traced = smpi_process_index();
1454   TRACE_smpi_computing_out(rank_traced);
1455
1456   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__);
1457
1458 #endif
1459   if (index == NULL) {
1460     retval = MPI_ERR_ARG;
1461   } else {
1462     *index = smpi_mpi_waitany(count, requests, status);
1463     retval = MPI_SUCCESS;
1464   }
1465 #ifdef HAVE_TRACING
1466   if(*index!=MPI_UNDEFINED){
1467     int src_traced, dst_traced, is_wait_for_receive;
1468     xbt_dynar_get_cpy(srcs, *index, &src_traced);
1469     xbt_dynar_get_cpy(dsts, *index, &dst_traced);
1470     xbt_dynar_get_cpy(recvs, *index, &is_wait_for_receive);
1471     if (is_wait_for_receive) {
1472       TRACE_smpi_recv(rank_traced, src_traced, dst_traced);
1473     }
1474     TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1475     //clean-up of dynars
1476     xbt_dynar_free(&srcs);
1477     xbt_dynar_free(&dsts);
1478     xbt_dynar_free(&recvs);
1479   }
1480   TRACE_smpi_computing_in(rank_traced);
1481 #endif
1482   smpi_bench_begin();
1483   return retval;
1484 }
1485
1486 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1487 {
1488
1489   smpi_bench_end();
1490 #ifdef HAVE_TRACING
1491   //save information from requests
1492   int i;
1493   xbt_dynar_t srcs = xbt_dynar_new(sizeof(int), NULL);
1494   xbt_dynar_t dsts = xbt_dynar_new(sizeof(int), NULL);
1495   xbt_dynar_t recvs = xbt_dynar_new(sizeof(int), NULL);
1496   for (i = 0; i < count; i++) {
1497     MPI_Request req = requests[i];
1498     if(req){
1499       int *asrc = xbt_new(int, 1);
1500       int *adst = xbt_new(int, 1);
1501       int *arecv = xbt_new(int, 1);
1502       *asrc = req->src;
1503       *adst = req->dst;
1504       *arecv = req->recv;
1505       xbt_dynar_insert_at(srcs, i, asrc);
1506       xbt_dynar_insert_at(dsts, i, adst);
1507       xbt_dynar_insert_at(recvs, i, arecv);
1508       xbt_free(asrc);
1509       xbt_free(adst);
1510       xbt_free(arecv);
1511     }else {
1512       int *t = xbt_new(int, 1);
1513       xbt_dynar_insert_at(srcs, i, t);
1514       xbt_dynar_insert_at(dsts, i, t);
1515       xbt_dynar_insert_at(recvs, i, t);
1516       xbt_free(t);
1517     }
1518   }
1519   int rank_traced = smpi_process_index();
1520   TRACE_smpi_computing_out(rank_traced);
1521
1522   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__);
1523 #endif
1524   int retval = smpi_mpi_waitall(count, requests, status);
1525 #ifdef HAVE_TRACING
1526   for (i = 0; i < count; i++) {
1527     int src_traced, dst_traced, is_wait_for_receive;
1528     xbt_dynar_get_cpy(srcs, i, &src_traced);
1529     xbt_dynar_get_cpy(dsts, i, &dst_traced);
1530     xbt_dynar_get_cpy(recvs, i, &is_wait_for_receive);
1531     if (is_wait_for_receive) {
1532       TRACE_smpi_recv(rank_traced, src_traced, dst_traced);
1533     }
1534   }
1535   TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1536   //clean-up of dynars
1537   xbt_dynar_free(&srcs);
1538   xbt_dynar_free(&dsts);
1539   xbt_dynar_free(&recvs);
1540   TRACE_smpi_computing_in(rank_traced);
1541 #endif
1542   smpi_bench_begin();
1543   return retval;
1544 }
1545
1546 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount,
1547                  int *indices, MPI_Status status[])
1548 {
1549   int retval;
1550
1551   smpi_bench_end();
1552   if (outcount == NULL || indices == NULL) {
1553     retval = MPI_ERR_ARG;
1554   } else {
1555     *outcount = smpi_mpi_waitsome(incount, requests, indices, status);
1556     retval = MPI_SUCCESS;
1557   }
1558   smpi_bench_begin();
1559   return retval;
1560 }
1561
1562 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount,
1563                  int* indices, MPI_Status status[])
1564 {
1565   int retval;
1566
1567    smpi_bench_end();
1568    if (outcount == NULL || indices == NULL) {
1569      retval = MPI_ERR_ARG;
1570    } else {
1571      *outcount = smpi_mpi_testsome(incount, requests, indices, status);
1572      retval = MPI_SUCCESS;
1573    }
1574    smpi_bench_begin();
1575    return retval;
1576 }
1577
1578
1579 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1580 {
1581   int retval;
1582
1583   smpi_bench_end();
1584 #ifdef HAVE_TRACING
1585   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1586   TRACE_smpi_computing_out(rank);
1587   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1588   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1589 #endif
1590   if (comm == MPI_COMM_NULL) {
1591     retval = MPI_ERR_COMM;
1592   } else {
1593     smpi_mpi_bcast(buf, count, datatype, root, comm);
1594     retval = MPI_SUCCESS;
1595   }
1596 #ifdef HAVE_TRACING
1597   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1598   TRACE_smpi_computing_in(rank);
1599 #endif
1600   smpi_bench_begin();
1601   return retval;
1602 }
1603
1604 int PMPI_Barrier(MPI_Comm comm)
1605 {
1606   int retval;
1607
1608   smpi_bench_end();
1609 #ifdef HAVE_TRACING
1610   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1611   TRACE_smpi_computing_out(rank);
1612   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1613 #endif
1614   if (comm == MPI_COMM_NULL) {
1615     retval = MPI_ERR_COMM;
1616   } else {
1617     smpi_mpi_barrier(comm);
1618     retval = MPI_SUCCESS;
1619   }
1620 #ifdef HAVE_TRACING
1621   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1622   TRACE_smpi_computing_in(rank);
1623 #endif
1624   smpi_bench_begin();
1625   return retval;
1626 }
1627
1628 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1629                void *recvbuf, int recvcount, MPI_Datatype recvtype,
1630                int root, MPI_Comm comm)
1631 {
1632   int retval;
1633
1634   smpi_bench_end();
1635 #ifdef HAVE_TRACING
1636   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1637   TRACE_smpi_computing_out(rank);
1638   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1639   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1640 #endif
1641   if (comm == MPI_COMM_NULL) {
1642     retval = MPI_ERR_COMM;
1643   } else if (sendtype == MPI_DATATYPE_NULL
1644              || recvtype == MPI_DATATYPE_NULL) {
1645     retval = MPI_ERR_TYPE;
1646   } else {
1647     smpi_mpi_gather(sendbuf, sendcount, sendtype, recvbuf, recvcount,
1648                     recvtype, root, comm);
1649     retval = MPI_SUCCESS;
1650   }
1651 #ifdef HAVE_TRACING
1652   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1653   TRACE_smpi_computing_in(rank);
1654 #endif
1655   smpi_bench_begin();
1656   return retval;
1657 }
1658
1659 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1660                 void *recvbuf, int *recvcounts, int *displs,
1661                 MPI_Datatype recvtype, int root, MPI_Comm comm)
1662 {
1663   int retval;
1664
1665   smpi_bench_end();
1666 #ifdef HAVE_TRACING
1667   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1668   TRACE_smpi_computing_out(rank);
1669   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1670   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1671 #endif
1672   if (comm == MPI_COMM_NULL) {
1673     retval = MPI_ERR_COMM;
1674   } else if (sendtype == MPI_DATATYPE_NULL
1675              || recvtype == MPI_DATATYPE_NULL) {
1676     retval = MPI_ERR_TYPE;
1677   } else if (recvcounts == NULL || displs == NULL) {
1678     retval = MPI_ERR_ARG;
1679   } else {
1680     smpi_mpi_gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts,
1681                      displs, recvtype, root, comm);
1682     retval = MPI_SUCCESS;
1683   }
1684 #ifdef HAVE_TRACING
1685   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1686   TRACE_smpi_computing_in(rank);
1687 #endif
1688   smpi_bench_begin();
1689   return retval;
1690 }
1691
1692 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1693                   void *recvbuf, int recvcount, MPI_Datatype recvtype,
1694                   MPI_Comm comm)
1695 {
1696   int retval;
1697
1698   smpi_bench_end();
1699 #ifdef HAVE_TRACING
1700   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1701   TRACE_smpi_computing_out(rank);
1702   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1703 #endif
1704   if (comm == MPI_COMM_NULL) {
1705     retval = MPI_ERR_COMM;
1706   } else if (sendtype == MPI_DATATYPE_NULL
1707              || recvtype == MPI_DATATYPE_NULL) {
1708     retval = MPI_ERR_TYPE;
1709   } else {
1710     smpi_mpi_allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount,
1711                        recvtype, comm);
1712     retval = MPI_SUCCESS;
1713   }
1714 #ifdef HAVE_TRACING
1715   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1716 #endif
1717   smpi_bench_begin();
1718   return retval;
1719 }
1720
1721 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1722                    void *recvbuf, int *recvcounts, int *displs,
1723                    MPI_Datatype recvtype, MPI_Comm comm)
1724 {
1725   int retval;
1726
1727   smpi_bench_end();
1728 #ifdef HAVE_TRACING
1729   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1730   TRACE_smpi_computing_out(rank);
1731   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1732 #endif
1733   if (comm == MPI_COMM_NULL) {
1734     retval = MPI_ERR_COMM;
1735   } else if (sendtype == MPI_DATATYPE_NULL
1736              || recvtype == MPI_DATATYPE_NULL) {
1737     retval = MPI_ERR_TYPE;
1738   } else if (recvcounts == NULL || displs == NULL) {
1739     retval = MPI_ERR_ARG;
1740   } else {
1741     smpi_mpi_allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts,
1742                         displs, recvtype, comm);
1743     retval = MPI_SUCCESS;
1744   }
1745 #ifdef HAVE_TRACING
1746   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1747   TRACE_smpi_computing_in(rank);
1748 #endif
1749   smpi_bench_begin();
1750   return retval;
1751 }
1752
1753 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1754                 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1755                 int root, MPI_Comm comm)
1756 {
1757   int retval;
1758
1759   smpi_bench_end();
1760 #ifdef HAVE_TRACING
1761   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1762   TRACE_smpi_computing_out(rank);
1763   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1764
1765   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1766 #endif
1767   if (comm == MPI_COMM_NULL) {
1768     retval = MPI_ERR_COMM;
1769   } else if (sendtype == MPI_DATATYPE_NULL
1770              || recvtype == MPI_DATATYPE_NULL) {
1771     retval = MPI_ERR_TYPE;
1772   } else {
1773     smpi_mpi_scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount,
1774                      recvtype, root, comm);
1775     retval = MPI_SUCCESS;
1776   }
1777 #ifdef HAVE_TRACING
1778   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1779   TRACE_smpi_computing_in(rank);
1780 #endif
1781   smpi_bench_begin();
1782   return retval;
1783 }
1784
1785 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1786                  MPI_Datatype sendtype, void *recvbuf, int recvcount,
1787                  MPI_Datatype recvtype, int root, MPI_Comm comm)
1788 {
1789   int retval;
1790
1791   smpi_bench_end();
1792 #ifdef HAVE_TRACING
1793   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1794   TRACE_smpi_computing_out(rank);
1795   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1796   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1797 #endif
1798   if (comm == MPI_COMM_NULL) {
1799     retval = MPI_ERR_COMM;
1800   } else if (sendtype == MPI_DATATYPE_NULL
1801              || recvtype == MPI_DATATYPE_NULL) {
1802     retval = MPI_ERR_TYPE;
1803   } else if (sendcounts == NULL || displs == NULL) {
1804     retval = MPI_ERR_ARG;
1805   } else {
1806     smpi_mpi_scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf,
1807                       recvcount, recvtype, root, comm);
1808     retval = MPI_SUCCESS;
1809   }
1810 #ifdef HAVE_TRACING
1811   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1812   TRACE_smpi_computing_in(rank);
1813 #endif
1814   smpi_bench_begin();
1815   return retval;
1816 }
1817
1818 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count,
1819                MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1820 {
1821   int retval;
1822
1823   smpi_bench_end();
1824 #ifdef HAVE_TRACING
1825   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1826   TRACE_smpi_computing_out(rank);
1827   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1828   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1829 #endif
1830   if (comm == MPI_COMM_NULL) {
1831     retval = MPI_ERR_COMM;
1832   } else if (datatype == MPI_DATATYPE_NULL || op == MPI_OP_NULL) {
1833     retval = MPI_ERR_ARG;
1834   } else {
1835     smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1836     retval = MPI_SUCCESS;
1837   }
1838 #ifdef HAVE_TRACING
1839   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1840   TRACE_smpi_computing_in(rank);
1841 #endif
1842   smpi_bench_begin();
1843   return retval;
1844 }
1845
1846 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count,
1847                   MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1848 {
1849   int retval;
1850
1851   smpi_bench_end();
1852 #ifdef HAVE_TRACING
1853   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1854   TRACE_smpi_computing_out(rank);
1855   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1856 #endif
1857   if (comm == MPI_COMM_NULL) {
1858     retval = MPI_ERR_COMM;
1859   } else if (datatype == MPI_DATATYPE_NULL) {
1860     retval = MPI_ERR_TYPE;
1861   } else if (op == MPI_OP_NULL) {
1862     retval = MPI_ERR_OP;
1863   } else {
1864     smpi_mpi_allreduce(sendbuf, recvbuf, count, datatype, op, comm);
1865     retval = MPI_SUCCESS;
1866   }
1867 #ifdef HAVE_TRACING
1868   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1869   TRACE_smpi_computing_in(rank);
1870 #endif
1871   smpi_bench_begin();
1872   return retval;
1873 }
1874
1875 int PMPI_Scan(void *sendbuf, void *recvbuf, int count,
1876              MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1877 {
1878   int retval;
1879
1880   smpi_bench_end();
1881 #ifdef HAVE_TRACING
1882   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1883   TRACE_smpi_computing_out(rank);
1884   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1885 #endif
1886   if (comm == MPI_COMM_NULL) {
1887     retval = MPI_ERR_COMM;
1888   } else if (datatype == MPI_DATATYPE_NULL) {
1889     retval = MPI_ERR_TYPE;
1890   } else if (op == MPI_OP_NULL) {
1891     retval = MPI_ERR_OP;
1892   } else {
1893     smpi_mpi_scan(sendbuf, recvbuf, count, datatype, op, comm);
1894     retval = MPI_SUCCESS;
1895   }
1896 #ifdef HAVE_TRACING
1897   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1898   TRACE_smpi_computing_in(rank);
1899 #endif
1900   smpi_bench_begin();
1901   return retval;
1902 }
1903
1904 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
1905                        MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1906 {
1907   int retval, i, size, count;
1908   int *displs;
1909   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1910
1911   smpi_bench_end();
1912 #ifdef HAVE_TRACING
1913   TRACE_smpi_computing_out(rank);
1914   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1915 #endif
1916   if (comm == MPI_COMM_NULL) {
1917     retval = MPI_ERR_COMM;
1918   } else if (datatype == MPI_DATATYPE_NULL) {
1919     retval = MPI_ERR_TYPE;
1920   } else if (op == MPI_OP_NULL) {
1921     retval = MPI_ERR_OP;
1922   } else if (recvcounts == NULL) {
1923     retval = MPI_ERR_ARG;
1924   } else {
1925     /* arbitrarily choose root as rank 0 */
1926     /* TODO: faster direct implementation ? */
1927     size = smpi_comm_size(comm);
1928     count = 0;
1929     displs = xbt_new(int, size);
1930     for (i = 0; i < size; i++) {
1931       count += recvcounts[i];
1932       displs[i] = 0;
1933     }
1934     smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1935     smpi_mpi_scatterv(recvbuf, recvcounts, displs, datatype, recvbuf,
1936                       recvcounts[rank], datatype, 0, comm);
1937     xbt_free(displs);
1938     retval = MPI_SUCCESS;
1939   }
1940 #ifdef HAVE_TRACING
1941   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1942   TRACE_smpi_computing_in(rank);
1943 #endif
1944   smpi_bench_begin();
1945   return retval;
1946 }
1947
1948 int PMPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1949                  void *recvbuf, int recvcount, MPI_Datatype recvtype,
1950                  MPI_Comm comm)
1951 {
1952   int retval, size, sendsize;
1953
1954   smpi_bench_end();
1955 #ifdef HAVE_TRACING
1956   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1957   TRACE_smpi_computing_out(rank);
1958   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1959 #endif
1960   if (comm == MPI_COMM_NULL) {
1961     retval = MPI_ERR_COMM;
1962   } else if (sendtype == MPI_DATATYPE_NULL
1963              || recvtype == MPI_DATATYPE_NULL) {
1964     retval = MPI_ERR_TYPE;
1965   } else {
1966     size = smpi_comm_size(comm);
1967     sendsize = smpi_datatype_size(sendtype) * sendcount;
1968     if (sendsize < 200 && size > 12) {
1969       retval =
1970           smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
1971                                          recvbuf, recvcount, recvtype,
1972                                          comm);
1973     } else if (sendsize < 3000) {
1974       retval =
1975           smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
1976                                                 sendtype, recvbuf,
1977                                                 recvcount, recvtype, comm);
1978     } else {
1979       retval =
1980           smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, sendtype,
1981                                             recvbuf, recvcount, recvtype,
1982                                             comm);
1983     }
1984   }
1985 #ifdef HAVE_TRACING
1986   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1987   TRACE_smpi_computing_in(rank);
1988 #endif
1989   smpi_bench_begin();
1990   return retval;
1991 }
1992
1993 int PMPI_Alltoallv(void *sendbuf, int *sendcounts, int *senddisps,
1994                   MPI_Datatype sendtype, void *recvbuf, int *recvcounts,
1995                   int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
1996 {
1997   int retval;
1998
1999   smpi_bench_end();
2000 #ifdef HAVE_TRACING
2001   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2002   TRACE_smpi_computing_out(rank);
2003   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
2004 #endif
2005   if (comm == MPI_COMM_NULL) {
2006     retval = MPI_ERR_COMM;
2007   } else if (sendtype == MPI_DATATYPE_NULL
2008              || recvtype == MPI_DATATYPE_NULL) {
2009     retval = MPI_ERR_TYPE;
2010   } else if (sendcounts == NULL || senddisps == NULL || recvcounts == NULL
2011              || recvdisps == NULL) {
2012     retval = MPI_ERR_ARG;
2013   } else {
2014     retval =
2015         smpi_coll_basic_alltoallv(sendbuf, sendcounts, senddisps, sendtype,
2016                                   recvbuf, recvcounts, recvdisps, recvtype,
2017                                   comm);
2018   }
2019 #ifdef HAVE_TRACING
2020   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2021   TRACE_smpi_computing_in(rank);
2022 #endif
2023   smpi_bench_begin();
2024   return retval;
2025 }
2026
2027
2028 int PMPI_Get_processor_name(char *name, int *resultlen)
2029 {
2030   int retval = MPI_SUCCESS;
2031
2032   smpi_bench_end();
2033   strncpy(name, SIMIX_host_get_name(SIMIX_host_self()),
2034           strlen(SIMIX_host_get_name(SIMIX_host_self())) < MPI_MAX_PROCESSOR_NAME - 1 ?
2035           strlen(SIMIX_host_get_name(SIMIX_host_self())) +1 :
2036           MPI_MAX_PROCESSOR_NAME - 1 );
2037   *resultlen =
2038       strlen(name) >
2039       MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2040
2041   smpi_bench_begin();
2042   return retval;
2043 }
2044
2045 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2046 {
2047   int retval = MPI_SUCCESS;
2048   size_t size;
2049
2050   smpi_bench_end();
2051   if (status == NULL || count == NULL) {
2052     retval = MPI_ERR_ARG;
2053   } else if (datatype == MPI_DATATYPE_NULL) {
2054     retval = MPI_ERR_TYPE;
2055   } else {
2056     size = smpi_datatype_size(datatype);
2057     if (size == 0) {
2058       *count = 0;
2059     } else if (status->count % size != 0) {
2060       retval = MPI_UNDEFINED;
2061     } else {
2062       *count = smpi_mpi_get_count(status, datatype);
2063     }
2064   }
2065   smpi_bench_begin();
2066   return retval;
2067 }
2068
2069 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2070   int retval;
2071
2072   smpi_bench_end();
2073   if (old_type == MPI_DATATYPE_NULL) {
2074     retval = MPI_ERR_TYPE;
2075   } else if (count<0){
2076     retval = MPI_ERR_COUNT;
2077   } else {
2078     retval = smpi_datatype_contiguous(count, old_type, new_type);
2079   }
2080   smpi_bench_begin();
2081   return retval;
2082 }
2083
2084 int PMPI_Type_commit(MPI_Datatype* datatype) {
2085   int retval;
2086
2087   smpi_bench_end();
2088   if (datatype == MPI_DATATYPE_NULL) {
2089     retval = MPI_ERR_TYPE;
2090   } else {
2091     smpi_datatype_commit(datatype);
2092     retval = MPI_SUCCESS;
2093   }
2094   smpi_bench_begin();
2095   return retval;
2096 }
2097
2098
2099 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2100   int retval;
2101
2102   smpi_bench_end();
2103   if (old_type == MPI_DATATYPE_NULL) {
2104     retval = MPI_ERR_TYPE;
2105   } else if (count<0 || blocklen<0){
2106     retval = MPI_ERR_COUNT;
2107   } else {
2108     retval = smpi_datatype_vector(count, blocklen, stride, old_type, new_type);
2109   }
2110   smpi_bench_begin();
2111   return retval;
2112 }
2113
2114 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2115   int retval;
2116
2117   smpi_bench_end();
2118   if (old_type == MPI_DATATYPE_NULL) {
2119     retval = MPI_ERR_TYPE;
2120   } else if (count<0 || blocklen<0){
2121     retval = MPI_ERR_COUNT;
2122   } else {
2123     retval = smpi_datatype_hvector(count, blocklen, stride, old_type, new_type);
2124   }
2125   smpi_bench_begin();
2126   return retval;
2127 }
2128
2129
2130 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2131   int retval;
2132
2133   smpi_bench_end();
2134   if (old_type == MPI_DATATYPE_NULL) {
2135     retval = MPI_ERR_TYPE;
2136   } else if (count<0){
2137     retval = MPI_ERR_COUNT;
2138   } else {
2139     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2140   }
2141   smpi_bench_begin();
2142   return retval;
2143 }
2144
2145 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2146   int retval;
2147
2148   smpi_bench_end();
2149   if (old_type == MPI_DATATYPE_NULL) {
2150     retval = MPI_ERR_TYPE;
2151   } else if (count<0){
2152     retval = MPI_ERR_COUNT;
2153   } else {
2154     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2155   }
2156   smpi_bench_begin();
2157   return retval;
2158 }
2159
2160
2161 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2162   int retval;
2163
2164   smpi_bench_end();
2165   if (count<0){
2166     retval = MPI_ERR_COUNT;
2167   } else {
2168     retval = smpi_datatype_struct(count, blocklens, indices, old_types, new_type);
2169   }
2170   smpi_bench_begin();
2171   return retval;}
2172
2173 int PMPI_Error_class(int errorcode, int* errorclass) {
2174   // assume smpi uses only standard mpi error codes
2175   *errorclass=errorcode;
2176   return MPI_SUCCESS;
2177 }
2178
2179
2180 int PMPI_Initialized(int* flag) {
2181    *flag=(smpi_process_data()!=NULL);
2182    return MPI_SUCCESS;
2183 }
2184
2185 /* The following calls are not yet implemented and will fail at runtime. */
2186 /* Once implemented, please move them above this notice. */
2187
2188 static int not_yet_implemented(void) {
2189           XBT_WARN("Not yet implemented");
2190    return MPI_SUCCESS;
2191 }
2192
2193 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
2194    return not_yet_implemented();
2195 }
2196
2197 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2198    return not_yet_implemented();
2199 }
2200
2201 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periods, int reorder, MPI_Comm* comm_cart) {
2202    return not_yet_implemented();
2203 }
2204
2205 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2206    return not_yet_implemented();
2207 }
2208
2209 int PMPI_Cart_map(MPI_Comm comm_old, int ndims, int* dims, int* periods, int* newrank) {
2210    return not_yet_implemented();
2211 }
2212
2213 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2214    return not_yet_implemented();
2215 }
2216
2217 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2218    return not_yet_implemented();
2219 }
2220
2221 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2222    return not_yet_implemented();
2223 }
2224
2225 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2226    return not_yet_implemented();
2227 }
2228
2229 int PMPI_Graph_create(MPI_Comm comm_old, int nnodes, int* index, int* edges, int reorder, MPI_Comm* comm_graph) {
2230    return not_yet_implemented();
2231 }
2232
2233 int PMPI_Graph_get(MPI_Comm comm, int maxindex, int maxedges, int* index, int* edges) {
2234    return not_yet_implemented();
2235 }
2236
2237 int PMPI_Graph_map(MPI_Comm comm_old, int nnodes, int* index, int* edges, int* newrank) {
2238    return not_yet_implemented();
2239 }
2240
2241 int PMPI_Graph_neighbors(MPI_Comm comm, int rank, int maxneighbors, int* neighbors) {
2242    return not_yet_implemented();
2243 }
2244
2245 int PMPI_Graph_neighbors_count(MPI_Comm comm, int rank, int* nneighbors) {
2246    return not_yet_implemented();
2247 }
2248
2249 int PMPI_Graphdims_get(MPI_Comm comm, int* nnodes, int* nedges) {
2250    return not_yet_implemented();
2251 }
2252
2253 int PMPI_Topo_test(MPI_Comm comm, int* top_type) {
2254    return not_yet_implemented();
2255 }
2256
2257 int PMPI_Errhandler_create(MPI_Handler_function* function, MPI_Errhandler* errhandler) {
2258    return not_yet_implemented();
2259 }
2260
2261 int PMPI_Errhandler_free(MPI_Errhandler* errhandler) {
2262    return not_yet_implemented();
2263 }
2264
2265 int PMPI_Errhandler_get(MPI_Comm comm, MPI_Errhandler* errhandler) {
2266    return not_yet_implemented();
2267 }
2268
2269 int PMPI_Error_string(int errorcode, char* string, int* resultlen) {
2270    return not_yet_implemented();
2271 }
2272
2273 int PMPI_Errhandler_set(MPI_Comm comm, MPI_Errhandler errhandler) {
2274    return not_yet_implemented();
2275 }
2276
2277
2278 int PMPI_Cancel(MPI_Request* request) {
2279    return not_yet_implemented();
2280 }
2281
2282 int PMPI_Buffer_attach(void* buffer, int size) {
2283    return not_yet_implemented();
2284 }
2285
2286 int PMPI_Buffer_detach(void* buffer, int* size) {
2287    return not_yet_implemented();
2288 }
2289
2290 int PMPI_Comm_test_inter(MPI_Comm comm, int* flag) {
2291    return not_yet_implemented();
2292 }
2293
2294 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
2295 {
2296    return not_yet_implemented();
2297 }
2298
2299 int PMPI_Pcontrol(const int level )
2300 {
2301    return not_yet_implemented();
2302 }
2303
2304 int PMPI_Unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
2305    return not_yet_implemented();
2306 }
2307
2308
2309
2310 int PMPI_Intercomm_create(MPI_Comm local_comm, int local_leader, MPI_Comm peer_comm, int remote_leader, int tag, MPI_Comm* comm_out) {
2311    return not_yet_implemented();
2312 }
2313
2314 int PMPI_Intercomm_merge(MPI_Comm comm, int high, MPI_Comm* comm_out) {
2315    return not_yet_implemented();
2316 }
2317
2318 int PMPI_Bsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) {
2319    return not_yet_implemented();
2320 }
2321
2322 int PMPI_Bsend_init(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2323    return not_yet_implemented();
2324 }
2325
2326 int PMPI_Ibsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2327    return not_yet_implemented();
2328 }
2329
2330 int PMPI_Comm_remote_group(MPI_Comm comm, MPI_Group* group) {
2331    return not_yet_implemented();
2332 }
2333
2334 int PMPI_Comm_remote_size(MPI_Comm comm, int* size) {
2335    return not_yet_implemented();
2336 }
2337
2338 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
2339    return not_yet_implemented();
2340 }
2341
2342 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
2343    return not_yet_implemented();
2344 }
2345
2346 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
2347    return not_yet_implemented();
2348 }
2349
2350 int PMPI_Rsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) {
2351    return not_yet_implemented();
2352 }
2353
2354 int PMPI_Rsend_init(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2355    return not_yet_implemented();
2356 }
2357
2358 int PMPI_Irsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2359    return not_yet_implemented();
2360 }
2361
2362 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
2363    return not_yet_implemented();
2364 }
2365
2366 int PMPI_Keyval_free(int* keyval) {
2367    return not_yet_implemented();
2368 }
2369
2370 int PMPI_Test_cancelled(MPI_Status* status, int* flag) {
2371    return not_yet_implemented();
2372 }
2373
2374 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
2375    return not_yet_implemented();
2376 }
2377
2378 int PMPI_Get_elements(MPI_Status* status, MPI_Datatype datatype, int* elements) {
2379    return not_yet_implemented();
2380 }
2381
2382 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2383    return not_yet_implemented();
2384 }
2385
2386