Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid into...
[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_Start(MPI_Request * request)
866 {
867   int retval;
868
869   smpi_bench_end();
870   if (request == NULL || *request == MPI_REQUEST_NULL) {
871     retval = MPI_ERR_ARG;
872   } else {
873     smpi_mpi_start(*request);
874     retval = MPI_SUCCESS;
875   }
876   smpi_bench_begin();
877   return retval;
878 }
879
880 int PMPI_Startall(int count, MPI_Request * requests)
881 {
882   int retval;
883
884   smpi_bench_end();
885   if (requests == NULL) {
886     retval = MPI_ERR_ARG;
887   } else {
888     smpi_mpi_startall(count, requests);
889     retval = MPI_SUCCESS;
890   }
891   smpi_bench_begin();
892   return retval;
893 }
894
895 int PMPI_Request_free(MPI_Request * request)
896 {
897   int retval;
898
899   smpi_bench_end();
900   if (request == MPI_REQUEST_NULL) {
901     retval = MPI_ERR_ARG;
902   } else {
903     smpi_mpi_request_free(request);
904     retval = MPI_SUCCESS;
905   }
906   smpi_bench_begin();
907   return retval;
908 }
909
910 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src,
911               int tag, MPI_Comm comm, MPI_Request * request)
912 {
913   int retval;
914
915   smpi_bench_end();
916
917   if (request == NULL) {
918     retval = MPI_ERR_ARG;
919   } else if (comm == MPI_COMM_NULL) {
920     retval = MPI_ERR_COMM;
921   } else if (src == MPI_PROC_NULL) {
922     *request = MPI_REQUEST_NULL;
923     retval = MPI_SUCCESS;
924   } else if (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0)){
925     retval = MPI_ERR_COMM;
926   } else if (count < 0) {
927     retval = MPI_ERR_COUNT;
928   } else if (buf==NULL && count > 0) {
929     retval = MPI_ERR_COUNT;
930   } else if (datatype == MPI_DATATYPE_NULL){
931     retval = MPI_ERR_TYPE;
932   } else if(tag<0 && tag !=  MPI_ANY_TAG){
933     retval = MPI_ERR_TAG;
934   } else {
935
936 #ifdef HAVE_TRACING
937   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
938   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
939   TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__);
940 #endif
941
942     *request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
943     retval = MPI_SUCCESS;
944
945 #ifdef HAVE_TRACING
946   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
947   (*request)->recv = 1;
948 #endif
949   }
950
951   smpi_bench_begin();
952   return retval;
953 }
954
955
956 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst,
957               int tag, MPI_Comm comm, MPI_Request * request)
958 {
959   int retval;
960
961   smpi_bench_end();
962   if (request == NULL) {
963     retval = MPI_ERR_ARG;
964   } else if (comm == MPI_COMM_NULL) {
965     retval = MPI_ERR_COMM;
966   } else if (dst == MPI_PROC_NULL) {
967     *request = MPI_REQUEST_NULL;
968     retval = MPI_SUCCESS;
969   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
970     retval = MPI_ERR_COMM;
971   } else if (count < 0) {
972     retval = MPI_ERR_COUNT;
973   } else if (buf==NULL && count > 0) {
974     retval = MPI_ERR_COUNT;
975   } else if (datatype == MPI_DATATYPE_NULL){
976     retval = MPI_ERR_TYPE;
977   } else if(tag<0 && tag !=  MPI_ANY_TAG){
978     retval = MPI_ERR_TAG;
979   } else {
980
981 #ifdef HAVE_TRACING
982   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
983   TRACE_smpi_computing_out(rank);
984   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
985   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__);
986   TRACE_smpi_send(rank, rank, dst_traced);
987 #endif
988
989     *request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
990     retval = MPI_SUCCESS;
991
992 #ifdef HAVE_TRACING
993   TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
994   (*request)->send = 1;
995   TRACE_smpi_computing_in(rank);
996 #endif
997   }
998
999   smpi_bench_begin();
1000   return retval;
1001 }
1002
1003
1004
1005 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag,
1006              MPI_Comm comm, MPI_Status * status)
1007 {
1008   int retval;
1009
1010   smpi_bench_end();
1011
1012   if (comm == MPI_COMM_NULL) {
1013     retval = MPI_ERR_COMM;
1014   } else if (src == MPI_PROC_NULL) {
1015     smpi_empty_status(status);
1016     status->MPI_SOURCE = MPI_PROC_NULL;
1017     retval = MPI_SUCCESS;
1018   } else if (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0)){
1019     retval = MPI_ERR_COMM;
1020   } else if (count < 0) {
1021     retval = MPI_ERR_COUNT;
1022   } else if (buf==NULL && count > 0) {
1023     retval = MPI_ERR_COUNT;
1024   } else if (datatype == MPI_DATATYPE_NULL){
1025     retval = MPI_ERR_TYPE;
1026   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1027     retval = MPI_ERR_TAG;
1028   } else {
1029 #ifdef HAVE_TRACING
1030   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1031   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
1032   TRACE_smpi_computing_out(rank);
1033
1034   TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__);
1035 #endif
1036
1037     smpi_mpi_recv(buf, count, datatype, src, tag, comm, status);
1038     retval = MPI_SUCCESS;
1039
1040 #ifdef HAVE_TRACING
1041   //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1042   if(status!=MPI_STATUS_IGNORE)src_traced = smpi_group_index(smpi_comm_group(comm), status->MPI_SOURCE);
1043   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
1044   TRACE_smpi_recv(rank, src_traced, rank);
1045   TRACE_smpi_computing_in(rank);
1046 #endif
1047   }
1048
1049   smpi_bench_begin();
1050   return retval;
1051 }
1052
1053 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag,
1054              MPI_Comm comm)
1055 {
1056   int retval;
1057
1058   smpi_bench_end();
1059
1060   if (comm == MPI_COMM_NULL) {
1061     retval = MPI_ERR_COMM;
1062   } else if (dst == MPI_PROC_NULL) {
1063     retval = MPI_SUCCESS;
1064   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1065     retval = MPI_ERR_COMM;
1066   } else if (count < 0) {
1067     retval = MPI_ERR_COUNT;
1068   } else if (buf==NULL && count > 0) {
1069     retval = MPI_ERR_COUNT;
1070   } else if (datatype == MPI_DATATYPE_NULL){
1071     retval = MPI_ERR_TYPE;
1072   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1073     retval = MPI_ERR_TAG;
1074   } else {
1075
1076 #ifdef HAVE_TRACING
1077   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1078   TRACE_smpi_computing_out(rank);
1079   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1080   TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__);
1081   TRACE_smpi_send(rank, rank, dst_traced);
1082 #endif
1083
1084     smpi_mpi_send(buf, count, datatype, dst, tag, comm);
1085     retval = MPI_SUCCESS;
1086
1087 #ifdef HAVE_TRACING
1088   TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1089   TRACE_smpi_computing_in(rank);
1090 #endif
1091   }
1092
1093   smpi_bench_begin();
1094   return retval;
1095 }
1096
1097 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1098                  int dst, int sendtag, void *recvbuf, int recvcount,
1099                  MPI_Datatype recvtype, int src, int recvtag,
1100                  MPI_Comm comm, MPI_Status * status)
1101 {
1102   int retval;
1103
1104   smpi_bench_end();
1105
1106   if (comm == MPI_COMM_NULL) {
1107     retval = MPI_ERR_COMM;
1108   } else if (sendtype == MPI_DATATYPE_NULL
1109              || recvtype == MPI_DATATYPE_NULL) {
1110     retval = MPI_ERR_TYPE;
1111   } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1112       smpi_empty_status(status);
1113       status->MPI_SOURCE = MPI_PROC_NULL;
1114       retval = MPI_SUCCESS;
1115   }else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0 ||
1116       (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0))){
1117     retval = MPI_ERR_COMM;
1118   } else if (sendcount < 0 || recvcount<0) {
1119       retval = MPI_ERR_COUNT;
1120   } else if ((sendbuf==NULL && sendcount > 0)||(recvbuf==NULL && recvcount>0)) {
1121     retval = MPI_ERR_COUNT;
1122   } else if((sendtag<0 && sendtag !=  MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1123     retval = MPI_ERR_TAG;
1124   } else {
1125
1126 #ifdef HAVE_TRACING
1127   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1128   TRACE_smpi_computing_out(rank);
1129   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1130   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
1131   TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__);
1132   TRACE_smpi_send(rank, rank, dst_traced);
1133   TRACE_smpi_send(rank, src_traced, rank);
1134 #endif
1135
1136
1137     smpi_mpi_sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf,
1138                       recvcount, recvtype, src, recvtag, comm, status);
1139     retval = MPI_SUCCESS;
1140
1141 #ifdef HAVE_TRACING
1142   TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1143   TRACE_smpi_recv(rank, rank, dst_traced);
1144   TRACE_smpi_recv(rank, src_traced, rank);
1145   TRACE_smpi_computing_in(rank);
1146 #endif
1147
1148   }
1149
1150   smpi_bench_begin();
1151   return retval;
1152 }
1153
1154 int PMPI_Sendrecv_replace(void *buf, int count, MPI_Datatype datatype,
1155                          int dst, int sendtag, int src, int recvtag,
1156                          MPI_Comm comm, MPI_Status * status)
1157 {
1158   //TODO: suboptimal implementation
1159   void *recvbuf;
1160   int retval;
1161   if ((datatype == MPI_DATATYPE_NULL)||(datatype->has_subtype==1)) {
1162       retval = MPI_ERR_TYPE;
1163   } else if (count < 0) {
1164       retval = MPI_ERR_COUNT;
1165   } else {
1166     int size = smpi_datatype_size(datatype) * count;
1167     recvbuf = xbt_new(char, size);
1168     retval =
1169         MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count,
1170                      datatype, src, recvtag, comm, status);
1171     if(retval==MPI_SUCCESS){
1172         memcpy(buf, recvbuf, size * sizeof(char));
1173     }
1174     xbt_free(recvbuf);
1175
1176   }
1177   return retval;
1178 }
1179
1180 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1181 {
1182   int retval;
1183
1184   smpi_bench_end();
1185   if (request == MPI_REQUEST_NULL || flag == NULL) {
1186     retval = MPI_ERR_ARG;
1187   } else if (*request == MPI_REQUEST_NULL) {
1188     *flag= TRUE;
1189     retval = MPI_ERR_REQUEST;
1190   } else {
1191     *flag = smpi_mpi_test(request, status);
1192     retval = MPI_SUCCESS;
1193   }
1194   smpi_bench_begin();
1195   return retval;
1196 }
1197
1198 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag,
1199                 MPI_Status * status)
1200 {
1201   int retval;
1202
1203   smpi_bench_end();
1204   if (index == NULL || flag == NULL) {
1205     retval = MPI_ERR_ARG;
1206   } else {
1207     *flag = smpi_mpi_testany(count, requests, index, status);
1208     retval = MPI_SUCCESS;
1209   }
1210   smpi_bench_begin();
1211   return retval;
1212 }
1213
1214 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1215 {
1216   int retval;
1217
1218   smpi_bench_end();
1219   if (flag == NULL) {
1220     retval = MPI_ERR_ARG;
1221   } else {
1222     *flag = smpi_mpi_testall(count, requests, statuses);
1223     retval = MPI_SUCCESS;
1224   }
1225   smpi_bench_begin();
1226   return retval;
1227 }
1228
1229 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1230   int retval;
1231   smpi_bench_end();
1232
1233   if (status == NULL) {
1234     retval = MPI_ERR_ARG;
1235   } else if (comm == MPI_COMM_NULL) {
1236     retval = MPI_ERR_COMM;
1237   } else if (source == MPI_PROC_NULL) {
1238     smpi_empty_status(status);
1239     status->MPI_SOURCE = MPI_PROC_NULL;
1240     retval = MPI_SUCCESS;
1241   } else {
1242     smpi_mpi_probe(source, tag, comm, status);
1243     retval = MPI_SUCCESS;
1244   }
1245   smpi_bench_begin();
1246   return retval;
1247 }
1248
1249
1250 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1251   int retval;
1252   smpi_bench_end();
1253
1254   if (flag == NULL) {
1255     retval = MPI_ERR_ARG;
1256   } else if (status == NULL) {
1257     retval = MPI_ERR_ARG;
1258   } else if (comm == MPI_COMM_NULL) {
1259     retval = MPI_ERR_COMM;
1260   } else if (source == MPI_PROC_NULL) {
1261     smpi_empty_status(status);
1262     status->MPI_SOURCE = MPI_PROC_NULL;
1263     retval = MPI_SUCCESS;
1264   } else {
1265     smpi_mpi_iprobe(source, tag, comm, flag, status);
1266     retval = MPI_SUCCESS;
1267   }
1268   smpi_bench_begin();
1269   return retval;
1270 }
1271
1272 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1273 {
1274   int retval;
1275
1276   smpi_bench_end();
1277
1278   if (request == NULL) {
1279     retval = MPI_ERR_ARG;
1280   } else if (*request == MPI_REQUEST_NULL) {
1281     retval = MPI_ERR_REQUEST;
1282   } else {
1283
1284 #ifdef HAVE_TRACING
1285   int rank = request && (*request)->comm != MPI_COMM_NULL
1286       ? smpi_process_index()
1287       : -1;
1288   TRACE_smpi_computing_out(rank);
1289
1290   MPI_Group group = smpi_comm_group((*request)->comm);
1291   int src_traced = smpi_group_index(group, (*request)->src);
1292   int dst_traced = smpi_group_index(group, (*request)->dst);
1293   int is_wait_for_receive = (*request)->recv;
1294   TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__);
1295 #endif
1296
1297     smpi_mpi_wait(request, status);
1298     retval = MPI_SUCCESS;
1299
1300 #ifdef HAVE_TRACING
1301   TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1302   if (is_wait_for_receive) {
1303     TRACE_smpi_recv(rank, src_traced, dst_traced);
1304   }
1305   TRACE_smpi_computing_in(rank);
1306 #endif
1307
1308   }
1309
1310   smpi_bench_begin();
1311   return retval;
1312 }
1313
1314 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1315 {
1316   int retval;
1317
1318   smpi_bench_end();
1319 #ifdef HAVE_TRACING
1320   //save requests information for tracing
1321   int i;
1322   xbt_dynar_t srcs = xbt_dynar_new(sizeof(int), NULL);
1323   xbt_dynar_t dsts = xbt_dynar_new(sizeof(int), NULL);
1324   xbt_dynar_t recvs = xbt_dynar_new(sizeof(int), NULL);
1325   for (i = 0; i < count; i++) {
1326     MPI_Request req = requests[i];      //already received requests are no longer valid
1327     if (req) {
1328       int *asrc = xbt_new(int, 1);
1329       int *adst = xbt_new(int, 1);
1330       int *arecv = xbt_new(int, 1);
1331       *asrc = req->src;
1332       *adst = req->dst;
1333       *arecv = req->recv;
1334       xbt_dynar_insert_at(srcs, i, asrc);
1335       xbt_dynar_insert_at(dsts, i, adst);
1336       xbt_dynar_insert_at(recvs, i, arecv);
1337       xbt_free(asrc);
1338       xbt_free(adst);
1339       xbt_free(arecv);
1340     } else {
1341       int *t = xbt_new(int, 1);
1342       xbt_dynar_insert_at(srcs, i, t);
1343       xbt_dynar_insert_at(dsts, i, t);
1344       xbt_dynar_insert_at(recvs, i, t);
1345       xbt_free(t);
1346     }
1347   }
1348   int rank_traced = smpi_process_index();
1349   TRACE_smpi_computing_out(rank_traced);
1350
1351   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__);
1352
1353 #endif
1354   if (index == NULL) {
1355     retval = MPI_ERR_ARG;
1356   } else {
1357     *index = smpi_mpi_waitany(count, requests, status);
1358     retval = MPI_SUCCESS;
1359   }
1360 #ifdef HAVE_TRACING
1361   if(*index!=MPI_UNDEFINED){
1362     int src_traced, dst_traced, is_wait_for_receive;
1363     xbt_dynar_get_cpy(srcs, *index, &src_traced);
1364     xbt_dynar_get_cpy(dsts, *index, &dst_traced);
1365     xbt_dynar_get_cpy(recvs, *index, &is_wait_for_receive);
1366     if (is_wait_for_receive) {
1367       TRACE_smpi_recv(rank_traced, src_traced, dst_traced);
1368     }
1369     TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1370     //clean-up of dynars
1371     xbt_dynar_free(&srcs);
1372     xbt_dynar_free(&dsts);
1373     xbt_dynar_free(&recvs);
1374   }
1375   TRACE_smpi_computing_in(rank_traced);
1376
1377
1378 #endif
1379   smpi_bench_begin();
1380   return retval;
1381 }
1382
1383 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1384 {
1385
1386   smpi_bench_end();
1387 #ifdef HAVE_TRACING
1388   //save information from requests
1389   int i;
1390   xbt_dynar_t srcs = xbt_dynar_new(sizeof(int), NULL);
1391   xbt_dynar_t dsts = xbt_dynar_new(sizeof(int), NULL);
1392   xbt_dynar_t recvs = xbt_dynar_new(sizeof(int), NULL);
1393   for (i = 0; i < count; i++) {
1394     MPI_Request req = requests[i];
1395     if(req){
1396       int *asrc = xbt_new(int, 1);
1397       int *adst = xbt_new(int, 1);
1398       int *arecv = xbt_new(int, 1);
1399       *asrc = req->src;
1400       *adst = req->dst;
1401       *arecv = req->recv;
1402       xbt_dynar_insert_at(srcs, i, asrc);
1403       xbt_dynar_insert_at(dsts, i, adst);
1404       xbt_dynar_insert_at(recvs, i, arecv);
1405       xbt_free(asrc);
1406       xbt_free(adst);
1407       xbt_free(arecv);
1408     }else {
1409       int *t = xbt_new(int, 1);
1410       xbt_dynar_insert_at(srcs, i, t);
1411       xbt_dynar_insert_at(dsts, i, t);
1412       xbt_dynar_insert_at(recvs, i, t);
1413       xbt_free(t);
1414     }
1415   }
1416   int rank_traced = smpi_process_index();
1417   TRACE_smpi_computing_out(rank_traced);
1418
1419   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__);
1420 #endif
1421   int retval = smpi_mpi_waitall(count, requests, status);
1422 #ifdef HAVE_TRACING
1423   for (i = 0; i < count; i++) {
1424     int src_traced, dst_traced, is_wait_for_receive;
1425     xbt_dynar_get_cpy(srcs, i, &src_traced);
1426     xbt_dynar_get_cpy(dsts, i, &dst_traced);
1427     xbt_dynar_get_cpy(recvs, i, &is_wait_for_receive);
1428     if (is_wait_for_receive) {
1429       TRACE_smpi_recv(rank_traced, src_traced, dst_traced);
1430     }
1431   }
1432   TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1433   //clean-up of dynars
1434   xbt_dynar_free(&srcs);
1435   xbt_dynar_free(&dsts);
1436   xbt_dynar_free(&recvs);
1437   TRACE_smpi_computing_in(rank_traced);
1438 #endif
1439   smpi_bench_begin();
1440   return retval;
1441 }
1442
1443 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount,
1444                  int *indices, MPI_Status status[])
1445 {
1446   int retval;
1447
1448   smpi_bench_end();
1449   if (outcount == NULL || indices == NULL) {
1450     retval = MPI_ERR_ARG;
1451   } else {
1452     *outcount = smpi_mpi_waitsome(incount, requests, indices, status);
1453     retval = MPI_SUCCESS;
1454   }
1455   smpi_bench_begin();
1456   return retval;
1457 }
1458
1459 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount,
1460                  int* indices, MPI_Status status[])
1461 {
1462   int retval;
1463
1464    smpi_bench_end();
1465    if (outcount == NULL || indices == NULL) {
1466      retval = MPI_ERR_ARG;
1467    } else {
1468      *outcount = smpi_mpi_testsome(incount, requests, indices, status);
1469      retval = MPI_SUCCESS;
1470    }
1471    smpi_bench_begin();
1472    return retval;
1473 }
1474
1475
1476 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1477 {
1478   int retval;
1479
1480   smpi_bench_end();
1481 #ifdef HAVE_TRACING
1482   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1483   TRACE_smpi_computing_out(rank);
1484   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1485   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1486 #endif
1487   if (comm == MPI_COMM_NULL) {
1488     retval = MPI_ERR_COMM;
1489   } else {
1490     smpi_mpi_bcast(buf, count, datatype, root, comm);
1491     retval = MPI_SUCCESS;
1492   }
1493 #ifdef HAVE_TRACING
1494   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1495   TRACE_smpi_computing_in(rank);
1496 #endif
1497   smpi_bench_begin();
1498   return retval;
1499 }
1500
1501 int PMPI_Barrier(MPI_Comm comm)
1502 {
1503   int retval;
1504
1505   smpi_bench_end();
1506 #ifdef HAVE_TRACING
1507   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1508   TRACE_smpi_computing_out(rank);
1509   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1510 #endif
1511   if (comm == MPI_COMM_NULL) {
1512     retval = MPI_ERR_COMM;
1513   } else {
1514     smpi_mpi_barrier(comm);
1515     retval = MPI_SUCCESS;
1516   }
1517 #ifdef HAVE_TRACING
1518   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1519   TRACE_smpi_computing_in(rank);
1520 #endif
1521   smpi_bench_begin();
1522   return retval;
1523 }
1524
1525 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1526                void *recvbuf, int recvcount, MPI_Datatype recvtype,
1527                int root, MPI_Comm comm)
1528 {
1529   int retval;
1530
1531   smpi_bench_end();
1532 #ifdef HAVE_TRACING
1533   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1534   TRACE_smpi_computing_out(rank);
1535   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1536   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1537 #endif
1538   if (comm == MPI_COMM_NULL) {
1539     retval = MPI_ERR_COMM;
1540   } else if (sendtype == MPI_DATATYPE_NULL
1541              || recvtype == MPI_DATATYPE_NULL) {
1542     retval = MPI_ERR_TYPE;
1543   } else {
1544     smpi_mpi_gather(sendbuf, sendcount, sendtype, recvbuf, recvcount,
1545                     recvtype, root, comm);
1546     retval = MPI_SUCCESS;
1547   }
1548 #ifdef HAVE_TRACING
1549   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1550   TRACE_smpi_computing_in(rank);
1551 #endif
1552   smpi_bench_begin();
1553   return retval;
1554 }
1555
1556 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1557                 void *recvbuf, int *recvcounts, int *displs,
1558                 MPI_Datatype recvtype, int root, MPI_Comm comm)
1559 {
1560   int retval;
1561
1562   smpi_bench_end();
1563 #ifdef HAVE_TRACING
1564   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1565   TRACE_smpi_computing_out(rank);
1566   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1567   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1568 #endif
1569   if (comm == MPI_COMM_NULL) {
1570     retval = MPI_ERR_COMM;
1571   } else if (sendtype == MPI_DATATYPE_NULL
1572              || recvtype == MPI_DATATYPE_NULL) {
1573     retval = MPI_ERR_TYPE;
1574   } else if (recvcounts == NULL || displs == NULL) {
1575     retval = MPI_ERR_ARG;
1576   } else {
1577     smpi_mpi_gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts,
1578                      displs, recvtype, root, comm);
1579     retval = MPI_SUCCESS;
1580   }
1581 #ifdef HAVE_TRACING
1582   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1583   TRACE_smpi_computing_in(rank);
1584 #endif
1585   smpi_bench_begin();
1586   return retval;
1587 }
1588
1589 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1590                   void *recvbuf, int recvcount, MPI_Datatype recvtype,
1591                   MPI_Comm comm)
1592 {
1593   int retval;
1594
1595   smpi_bench_end();
1596 #ifdef HAVE_TRACING
1597   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1598   TRACE_smpi_computing_out(rank);
1599   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1600 #endif
1601   if (comm == MPI_COMM_NULL) {
1602     retval = MPI_ERR_COMM;
1603   } else if (sendtype == MPI_DATATYPE_NULL
1604              || recvtype == MPI_DATATYPE_NULL) {
1605     retval = MPI_ERR_TYPE;
1606   } else {
1607     smpi_mpi_allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount,
1608                        recvtype, comm);
1609     retval = MPI_SUCCESS;
1610   }
1611 #ifdef HAVE_TRACING
1612   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1613 #endif
1614   smpi_bench_begin();
1615   return retval;
1616 }
1617
1618 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1619                    void *recvbuf, int *recvcounts, int *displs,
1620                    MPI_Datatype recvtype, MPI_Comm comm)
1621 {
1622   int retval;
1623
1624   smpi_bench_end();
1625 #ifdef HAVE_TRACING
1626   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1627   TRACE_smpi_computing_out(rank);
1628   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1629 #endif
1630   if (comm == MPI_COMM_NULL) {
1631     retval = MPI_ERR_COMM;
1632   } else if (sendtype == MPI_DATATYPE_NULL
1633              || recvtype == MPI_DATATYPE_NULL) {
1634     retval = MPI_ERR_TYPE;
1635   } else if (recvcounts == NULL || displs == NULL) {
1636     retval = MPI_ERR_ARG;
1637   } else {
1638     smpi_mpi_allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts,
1639                         displs, recvtype, comm);
1640     retval = MPI_SUCCESS;
1641   }
1642 #ifdef HAVE_TRACING
1643   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1644   TRACE_smpi_computing_in(rank);
1645 #endif
1646   smpi_bench_begin();
1647   return retval;
1648 }
1649
1650 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1651                 void *recvbuf, int recvcount, MPI_Datatype recvtype,
1652                 int root, MPI_Comm comm)
1653 {
1654   int retval;
1655
1656   smpi_bench_end();
1657 #ifdef HAVE_TRACING
1658   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1659   TRACE_smpi_computing_out(rank);
1660   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1661
1662   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1663 #endif
1664   if (comm == MPI_COMM_NULL) {
1665     retval = MPI_ERR_COMM;
1666   } else if (sendtype == MPI_DATATYPE_NULL
1667              || recvtype == MPI_DATATYPE_NULL) {
1668     retval = MPI_ERR_TYPE;
1669   } else {
1670     smpi_mpi_scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount,
1671                      recvtype, root, comm);
1672     retval = MPI_SUCCESS;
1673   }
1674 #ifdef HAVE_TRACING
1675   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1676   TRACE_smpi_computing_in(rank);
1677 #endif
1678   smpi_bench_begin();
1679   return retval;
1680 }
1681
1682 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1683                  MPI_Datatype sendtype, void *recvbuf, int recvcount,
1684                  MPI_Datatype recvtype, int root, MPI_Comm comm)
1685 {
1686   int retval;
1687
1688   smpi_bench_end();
1689 #ifdef HAVE_TRACING
1690   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1691   TRACE_smpi_computing_out(rank);
1692   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1693   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1694 #endif
1695   if (comm == MPI_COMM_NULL) {
1696     retval = MPI_ERR_COMM;
1697   } else if (sendtype == MPI_DATATYPE_NULL
1698              || recvtype == MPI_DATATYPE_NULL) {
1699     retval = MPI_ERR_TYPE;
1700   } else if (sendcounts == NULL || displs == NULL) {
1701     retval = MPI_ERR_ARG;
1702   } else {
1703     smpi_mpi_scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf,
1704                       recvcount, recvtype, root, comm);
1705     retval = MPI_SUCCESS;
1706   }
1707 #ifdef HAVE_TRACING
1708   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1709   TRACE_smpi_computing_in(rank);
1710 #endif
1711   smpi_bench_begin();
1712   return retval;
1713 }
1714
1715 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count,
1716                MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1717 {
1718   int retval;
1719
1720   smpi_bench_end();
1721 #ifdef HAVE_TRACING
1722   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1723   TRACE_smpi_computing_out(rank);
1724   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1725   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__);
1726 #endif
1727   if (comm == MPI_COMM_NULL) {
1728     retval = MPI_ERR_COMM;
1729   } else if (datatype == MPI_DATATYPE_NULL || op == MPI_OP_NULL) {
1730     retval = MPI_ERR_ARG;
1731   } else {
1732     smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1733     retval = MPI_SUCCESS;
1734   }
1735 #ifdef HAVE_TRACING
1736   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1737   TRACE_smpi_computing_in(rank);
1738 #endif
1739   smpi_bench_begin();
1740   return retval;
1741 }
1742
1743 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count,
1744                   MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1745 {
1746   int retval;
1747
1748   smpi_bench_end();
1749 #ifdef HAVE_TRACING
1750   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1751   TRACE_smpi_computing_out(rank);
1752   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1753 #endif
1754   if (comm == MPI_COMM_NULL) {
1755     retval = MPI_ERR_COMM;
1756   } else if (datatype == MPI_DATATYPE_NULL) {
1757     retval = MPI_ERR_TYPE;
1758   } else if (op == MPI_OP_NULL) {
1759     retval = MPI_ERR_OP;
1760   } else {
1761     smpi_mpi_allreduce(sendbuf, recvbuf, count, datatype, op, comm);
1762     retval = MPI_SUCCESS;
1763   }
1764 #ifdef HAVE_TRACING
1765   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1766   TRACE_smpi_computing_in(rank);
1767 #endif
1768   smpi_bench_begin();
1769   return retval;
1770 }
1771
1772 int PMPI_Scan(void *sendbuf, void *recvbuf, int count,
1773              MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1774 {
1775   int retval;
1776
1777   smpi_bench_end();
1778 #ifdef HAVE_TRACING
1779   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1780   TRACE_smpi_computing_out(rank);
1781   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1782 #endif
1783   if (comm == MPI_COMM_NULL) {
1784     retval = MPI_ERR_COMM;
1785   } else if (datatype == MPI_DATATYPE_NULL) {
1786     retval = MPI_ERR_TYPE;
1787   } else if (op == MPI_OP_NULL) {
1788     retval = MPI_ERR_OP;
1789   } else {
1790     smpi_mpi_scan(sendbuf, recvbuf, count, datatype, op, comm);
1791     retval = MPI_SUCCESS;
1792   }
1793 #ifdef HAVE_TRACING
1794   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1795   TRACE_smpi_computing_in(rank);
1796 #endif
1797   smpi_bench_begin();
1798   return retval;
1799 }
1800
1801 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts,
1802                        MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
1803 {
1804   int retval, i, size, count;
1805   int *displs;
1806   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1807
1808   smpi_bench_end();
1809 #ifdef HAVE_TRACING
1810   TRACE_smpi_computing_out(rank);
1811   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1812 #endif
1813   if (comm == MPI_COMM_NULL) {
1814     retval = MPI_ERR_COMM;
1815   } else if (datatype == MPI_DATATYPE_NULL) {
1816     retval = MPI_ERR_TYPE;
1817   } else if (op == MPI_OP_NULL) {
1818     retval = MPI_ERR_OP;
1819   } else if (recvcounts == NULL) {
1820     retval = MPI_ERR_ARG;
1821   } else {
1822     /* arbitrarily choose root as rank 0 */
1823     /* TODO: faster direct implementation ? */
1824     size = smpi_comm_size(comm);
1825     count = 0;
1826     displs = xbt_new(int, size);
1827     for (i = 0; i < size; i++) {
1828       count += recvcounts[i];
1829       displs[i] = 0;
1830     }
1831     smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1832     smpi_mpi_scatterv(recvbuf, recvcounts, displs, datatype, recvbuf,
1833                       recvcounts[rank], datatype, 0, comm);
1834     xbt_free(displs);
1835     retval = MPI_SUCCESS;
1836   }
1837 #ifdef HAVE_TRACING
1838   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1839   TRACE_smpi_computing_in(rank);
1840 #endif
1841   smpi_bench_begin();
1842   return retval;
1843 }
1844
1845 int PMPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1846                  void *recvbuf, int recvcount, MPI_Datatype recvtype,
1847                  MPI_Comm comm)
1848 {
1849   int retval, size, sendsize;
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 (sendtype == MPI_DATATYPE_NULL
1860              || recvtype == MPI_DATATYPE_NULL) {
1861     retval = MPI_ERR_TYPE;
1862   } else {
1863     size = smpi_comm_size(comm);
1864     sendsize = smpi_datatype_size(sendtype) * sendcount;
1865     if (sendsize < 200 && size > 12) {
1866       retval =
1867           smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype,
1868                                          recvbuf, recvcount, recvtype,
1869                                          comm);
1870     } else if (sendsize < 3000) {
1871       retval =
1872           smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount,
1873                                                 sendtype, recvbuf,
1874                                                 recvcount, recvtype, comm);
1875     } else {
1876       retval =
1877           smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, sendtype,
1878                                             recvbuf, recvcount, recvtype,
1879                                             comm);
1880     }
1881   }
1882 #ifdef HAVE_TRACING
1883   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1884   TRACE_smpi_computing_in(rank);
1885 #endif
1886   smpi_bench_begin();
1887   return retval;
1888 }
1889
1890 int PMPI_Alltoallv(void *sendbuf, int *sendcounts, int *senddisps,
1891                   MPI_Datatype sendtype, void *recvbuf, int *recvcounts,
1892                   int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
1893 {
1894   int retval;
1895
1896   smpi_bench_end();
1897 #ifdef HAVE_TRACING
1898   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1899   TRACE_smpi_computing_out(rank);
1900   TRACE_smpi_collective_in(rank, -1, __FUNCTION__);
1901 #endif
1902   if (comm == MPI_COMM_NULL) {
1903     retval = MPI_ERR_COMM;
1904   } else if (sendtype == MPI_DATATYPE_NULL
1905              || recvtype == MPI_DATATYPE_NULL) {
1906     retval = MPI_ERR_TYPE;
1907   } else if (sendcounts == NULL || senddisps == NULL || recvcounts == NULL
1908              || recvdisps == NULL) {
1909     retval = MPI_ERR_ARG;
1910   } else {
1911     retval =
1912         smpi_coll_basic_alltoallv(sendbuf, sendcounts, senddisps, sendtype,
1913                                   recvbuf, recvcounts, recvdisps, recvtype,
1914                                   comm);
1915   }
1916 #ifdef HAVE_TRACING
1917   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1918   TRACE_smpi_computing_in(rank);
1919 #endif
1920   smpi_bench_begin();
1921   return retval;
1922 }
1923
1924
1925 int PMPI_Get_processor_name(char *name, int *resultlen)
1926 {
1927   int retval = MPI_SUCCESS;
1928
1929   smpi_bench_end();
1930   strncpy(name, SIMIX_host_get_name(SIMIX_host_self()),
1931           strlen(SIMIX_host_get_name(SIMIX_host_self())) < MPI_MAX_PROCESSOR_NAME - 1 ?
1932           strlen(SIMIX_host_get_name(SIMIX_host_self())) +1 :
1933           MPI_MAX_PROCESSOR_NAME - 1 );
1934   *resultlen =
1935       strlen(name) >
1936       MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
1937
1938   smpi_bench_begin();
1939   return retval;
1940 }
1941
1942 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
1943 {
1944   int retval = MPI_SUCCESS;
1945   size_t size;
1946
1947   smpi_bench_end();
1948   if (status == NULL || count == NULL) {
1949     retval = MPI_ERR_ARG;
1950   } else if (datatype == MPI_DATATYPE_NULL) {
1951     retval = MPI_ERR_TYPE;
1952   } else {
1953     size = smpi_datatype_size(datatype);
1954     if (size == 0) {
1955       *count = 0;
1956     } else if (status->count % size != 0) {
1957       retval = MPI_UNDEFINED;
1958     } else {
1959       *count = smpi_mpi_get_count(status, datatype);
1960     }
1961   }
1962   smpi_bench_begin();
1963   return retval;
1964 }
1965
1966 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
1967   int retval;
1968
1969   smpi_bench_end();
1970   if (old_type == MPI_DATATYPE_NULL) {
1971     retval = MPI_ERR_TYPE;
1972   } else if (count<0){
1973     retval = MPI_ERR_COUNT;
1974   } else {
1975     retval = smpi_datatype_contiguous(count, old_type, new_type);
1976   }
1977   smpi_bench_begin();
1978   return retval;
1979 }
1980
1981 int PMPI_Type_commit(MPI_Datatype* datatype) {
1982   int retval;
1983
1984   smpi_bench_end();
1985   if (datatype == MPI_DATATYPE_NULL) {
1986     retval = MPI_ERR_TYPE;
1987   } else {
1988     smpi_datatype_commit(datatype);
1989     retval = MPI_SUCCESS;
1990   }
1991   smpi_bench_begin();
1992   return retval;
1993 }
1994
1995
1996 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
1997   int retval;
1998
1999   smpi_bench_end();
2000   if (old_type == MPI_DATATYPE_NULL) {
2001     retval = MPI_ERR_TYPE;
2002   } else if (count<0 || blocklen<0){
2003     retval = MPI_ERR_COUNT;
2004   } else {
2005     retval = smpi_datatype_vector(count, blocklen, stride, old_type, new_type);
2006   }
2007   smpi_bench_begin();
2008   return retval;
2009 }
2010
2011 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2012   int retval;
2013
2014   smpi_bench_end();
2015   if (old_type == MPI_DATATYPE_NULL) {
2016     retval = MPI_ERR_TYPE;
2017   } else if (count<0 || blocklen<0){
2018     retval = MPI_ERR_COUNT;
2019   } else {
2020     retval = smpi_datatype_hvector(count, blocklen, stride, old_type, new_type);
2021   }
2022   smpi_bench_begin();
2023   return retval;
2024 }
2025
2026
2027 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2028   int retval;
2029
2030   smpi_bench_end();
2031   if (old_type == MPI_DATATYPE_NULL) {
2032     retval = MPI_ERR_TYPE;
2033   } else if (count<0){
2034     retval = MPI_ERR_COUNT;
2035   } else {
2036     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2037   }
2038   smpi_bench_begin();
2039   return retval;
2040 }
2041
2042 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2043   int retval;
2044
2045   smpi_bench_end();
2046   if (old_type == MPI_DATATYPE_NULL) {
2047     retval = MPI_ERR_TYPE;
2048   } else if (count<0){
2049     retval = MPI_ERR_COUNT;
2050   } else {
2051     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2052   }
2053   smpi_bench_begin();
2054   return retval;
2055 }
2056
2057
2058 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2059   int retval;
2060
2061   smpi_bench_end();
2062   if (count<0){
2063     retval = MPI_ERR_COUNT;
2064   } else {
2065     retval = smpi_datatype_struct(count, blocklens, indices, old_types, new_type);
2066   }
2067   smpi_bench_begin();
2068   return retval;}
2069
2070 int PMPI_Error_class(int errorcode, int* errorclass) {
2071   // assume smpi uses only standard mpi error codes
2072   *errorclass=errorcode;
2073   return MPI_SUCCESS;
2074 }
2075
2076 /* The following calls are not yet implemented and will fail at runtime. */
2077 /* Once implemented, please move them above this notice. */
2078
2079 static int not_yet_implemented(void) {
2080           XBT_WARN("Not yet implemented");
2081    return MPI_SUCCESS;
2082 }
2083
2084 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
2085    return not_yet_implemented();
2086 }
2087
2088 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2089    return not_yet_implemented();
2090 }
2091
2092 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periods, int reorder, MPI_Comm* comm_cart) {
2093    return not_yet_implemented();
2094 }
2095
2096 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2097    return not_yet_implemented();
2098 }
2099
2100 int PMPI_Cart_map(MPI_Comm comm_old, int ndims, int* dims, int* periods, int* newrank) {
2101    return not_yet_implemented();
2102 }
2103
2104 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2105    return not_yet_implemented();
2106 }
2107
2108 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2109    return not_yet_implemented();
2110 }
2111
2112 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2113    return not_yet_implemented();
2114 }
2115
2116 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2117    return not_yet_implemented();
2118 }
2119
2120 int PMPI_Graph_create(MPI_Comm comm_old, int nnodes, int* index, int* edges, int reorder, MPI_Comm* comm_graph) {
2121    return not_yet_implemented();
2122 }
2123
2124 int PMPI_Graph_get(MPI_Comm comm, int maxindex, int maxedges, int* index, int* edges) {
2125    return not_yet_implemented();
2126 }
2127
2128 int PMPI_Graph_map(MPI_Comm comm_old, int nnodes, int* index, int* edges, int* newrank) {
2129    return not_yet_implemented();
2130 }
2131
2132 int PMPI_Graph_neighbors(MPI_Comm comm, int rank, int maxneighbors, int* neighbors) {
2133    return not_yet_implemented();
2134 }
2135
2136 int PMPI_Graph_neighbors_count(MPI_Comm comm, int rank, int* nneighbors) {
2137    return not_yet_implemented();
2138 }
2139
2140 int PMPI_Graphdims_get(MPI_Comm comm, int* nnodes, int* nedges) {
2141    return not_yet_implemented();
2142 }
2143
2144 int PMPI_Topo_test(MPI_Comm comm, int* top_type) {
2145    return not_yet_implemented();
2146 }
2147
2148 int PMPI_Errhandler_create(MPI_Handler_function* function, MPI_Errhandler* errhandler) {
2149    return not_yet_implemented();
2150 }
2151
2152 int PMPI_Errhandler_free(MPI_Errhandler* errhandler) {
2153    return not_yet_implemented();
2154 }
2155
2156 int PMPI_Errhandler_get(MPI_Comm comm, MPI_Errhandler* errhandler) {
2157    return not_yet_implemented();
2158 }
2159
2160 int PMPI_Error_string(int errorcode, char* string, int* resultlen) {
2161    return not_yet_implemented();
2162 }
2163
2164 int PMPI_Errhandler_set(MPI_Comm comm, MPI_Errhandler errhandler) {
2165    return not_yet_implemented();
2166 }
2167
2168
2169 int PMPI_Cancel(MPI_Request* request) {
2170    return not_yet_implemented();
2171 }
2172
2173 int PMPI_Buffer_attach(void* buffer, int size) {
2174    return not_yet_implemented();
2175 }
2176
2177 int PMPI_Buffer_detach(void* buffer, int* size) {
2178    return not_yet_implemented();
2179 }
2180
2181 int PMPI_Comm_test_inter(MPI_Comm comm, int* flag) {
2182    return not_yet_implemented();
2183 }
2184
2185 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
2186 {
2187    return not_yet_implemented();
2188 }
2189
2190 int PMPI_Pcontrol(const int level )
2191 {
2192    return not_yet_implemented();
2193 }
2194
2195 int PMPI_Unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
2196    return not_yet_implemented();
2197 }
2198
2199 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) {
2200    return not_yet_implemented();
2201 }
2202
2203 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2204    return not_yet_implemented();
2205 }
2206
2207 int PMPI_Intercomm_create(MPI_Comm local_comm, int local_leader, MPI_Comm peer_comm, int remote_leader, int tag, MPI_Comm* comm_out) {
2208    return not_yet_implemented();
2209 }
2210
2211 int PMPI_Intercomm_merge(MPI_Comm comm, int high, MPI_Comm* comm_out) {
2212    return not_yet_implemented();
2213 }
2214
2215 int PMPI_Bsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) {
2216    return not_yet_implemented();
2217 }
2218
2219 int PMPI_Bsend_init(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2220    return not_yet_implemented();
2221 }
2222
2223 int PMPI_Ibsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2224    return not_yet_implemented();
2225 }
2226
2227 int PMPI_Comm_remote_group(MPI_Comm comm, MPI_Group* group) {
2228    return not_yet_implemented();
2229 }
2230
2231 int PMPI_Comm_remote_size(MPI_Comm comm, int* size) {
2232    return not_yet_implemented();
2233 }
2234
2235 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2236    return not_yet_implemented();
2237 }
2238
2239
2240 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
2241    return not_yet_implemented();
2242 }
2243
2244 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
2245    return not_yet_implemented();
2246 }
2247
2248 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
2249    return not_yet_implemented();
2250 }
2251
2252 int PMPI_Rsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) {
2253    return not_yet_implemented();
2254 }
2255
2256 int PMPI_Rsend_init(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2257    return not_yet_implemented();
2258 }
2259
2260 int PMPI_Irsend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request* request) {
2261    return not_yet_implemented();
2262 }
2263
2264 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
2265    return not_yet_implemented();
2266 }
2267
2268 int PMPI_Keyval_free(int* keyval) {
2269    return not_yet_implemented();
2270 }
2271
2272 int PMPI_Test_cancelled(MPI_Status* status, int* flag) {
2273    return not_yet_implemented();
2274 }
2275
2276 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
2277    return not_yet_implemented();
2278 }
2279
2280 int PMPI_Get_elements(MPI_Status* status, MPI_Datatype datatype, int* elements) {
2281    return not_yet_implemented();
2282 }
2283
2284 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2285    return not_yet_implemented();
2286 }
2287
2288 int PMPI_Initialized(int* flag) {
2289    return not_yet_implemented();
2290 }