Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI tracing : match links with sender/receiver + tag, as in MPI, as messages could...
[simgrid.git] / src / smpi / smpi_pmpi.cpp
1
2 /* Copyright (c) 2007-2015. The SimGrid Team.
3  * All rights reserved.                                                     */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <xbt/ex.hpp>
9
10 #include "private.h"
11 #include "smpi_mpi_dt_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_pmpi, smpi, "Logging specific to SMPI (pmpi)");
14
15 //this function need to be here because of the calls to smpi_bench
16 void TRACE_smpi_set_category(const char *category)
17 {
18   //need to end bench otherwise categories for execution tasks are wrong
19   smpi_bench_end();
20   TRACE_internal_smpi_set_category (category);
21   //begin bench after changing process's category
22   smpi_bench_begin();
23 }
24
25 /* PMPI User level calls */
26
27 int PMPI_Init(int *argc, char ***argv)
28 {
29   // PMPI_Init is call only one time by only by SMPI process
30   int already_init;
31   MPI_Initialized(&already_init);
32   if(already_init == 0){
33     smpi_process_init(argc, argv);
34     smpi_process_mark_as_initialized();
35     int rank = smpi_process_index();
36     TRACE_smpi_init(rank);
37     TRACE_smpi_computing_init(rank);
38     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
39     extra->type = TRACING_INIT;
40     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
41     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
42     smpi_bench_begin();
43   }
44
45   smpi_mpi_init();
46
47   return MPI_SUCCESS;
48 }
49
50 int PMPI_Finalize()
51 {
52   smpi_bench_end();
53   int rank = smpi_process_index();
54   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
55   extra->type = TRACING_FINALIZE;
56   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
57
58   smpi_process_finalize();
59
60   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
61   TRACE_smpi_finalize(smpi_process_index());
62   smpi_process_destroy();
63   return MPI_SUCCESS;
64 }
65
66 int PMPI_Finalized(int* flag)
67 {
68   *flag=smpi_process_finalized();
69   return MPI_SUCCESS;
70 }
71
72 int PMPI_Get_version (int *version,int *subversion){
73   *version = MPI_VERSION;
74   *subversion= MPI_SUBVERSION;
75   return MPI_SUCCESS;
76 }
77
78 int PMPI_Get_library_version (char *version,int *len){
79   int retval = MPI_SUCCESS;
80   smpi_bench_end();
81   snprintf(version,MPI_MAX_LIBRARY_VERSION_STRING,"SMPI Version %d.%d. Copyright The Simgrid Team 2007-2015",
82            SIMGRID_VERSION_MAJOR, SIMGRID_VERSION_MINOR);
83   *len = strlen(version) > MPI_MAX_LIBRARY_VERSION_STRING ? MPI_MAX_LIBRARY_VERSION_STRING : strlen(version);
84   smpi_bench_begin();
85   return retval;
86 }
87
88 int PMPI_Init_thread(int *argc, char ***argv, int required, int *provided)
89 {
90   if (provided != nullptr) {
91     *provided = MPI_THREAD_SINGLE;
92   }
93   return MPI_Init(argc, argv);
94 }
95
96 int PMPI_Query_thread(int *provided)
97 {
98   int retval = 0;
99
100   if (provided == nullptr) {
101     retval = MPI_ERR_ARG;
102   } else {
103     *provided = MPI_THREAD_SINGLE;
104     retval = MPI_SUCCESS;
105   }
106   return retval;
107 }
108
109 int PMPI_Is_thread_main(int *flag)
110 {
111   int retval = 0;
112
113   if (flag == nullptr) {
114     retval = MPI_ERR_ARG;
115   } else {
116     *flag = smpi_process_index() == 0;
117     retval = MPI_SUCCESS;
118   }
119   return retval;
120 }
121
122 int PMPI_Abort(MPI_Comm comm, int errorcode)
123 {
124   smpi_bench_end();
125   smpi_process_destroy();
126   // FIXME: should kill all processes in comm instead
127   simcall_process_kill(SIMIX_process_self());
128   return MPI_SUCCESS;
129 }
130
131 double PMPI_Wtime()
132 {
133   return smpi_mpi_wtime();
134 }
135
136 extern double sg_maxmin_precision;
137 double PMPI_Wtick()
138 {
139   return sg_maxmin_precision;
140 }
141
142 int PMPI_Address(void *location, MPI_Aint * address)
143 {
144   int retval = 0;
145
146   if (address==nullptr) {
147     retval = MPI_ERR_ARG;
148   } else {
149     *address = reinterpret_cast<MPI_Aint>(location);
150     retval = MPI_SUCCESS;
151   }
152   return retval;
153 }
154
155 int PMPI_Get_address(void *location, MPI_Aint * address)
156 {
157   return PMPI_Address(location, address);
158 }
159
160 int PMPI_Type_free(MPI_Datatype * datatype)
161 {
162   int retval = 0;
163   /* Free a predefined datatype is an error according to the standard, and should be checked for */
164   if (*datatype == MPI_DATATYPE_NULL) {
165     retval = MPI_ERR_ARG;
166   } else {
167     smpi_datatype_unuse(*datatype);
168     retval = MPI_SUCCESS;
169   }
170   return retval;
171 }
172
173 int PMPI_Type_size(MPI_Datatype datatype, int *size)
174 {
175   int retval = 0;
176
177   if (datatype == MPI_DATATYPE_NULL) {
178     retval = MPI_ERR_TYPE;
179   } else if (size == nullptr) {
180     retval = MPI_ERR_ARG;
181   } else {
182     *size = static_cast<int>(smpi_datatype_size(datatype));
183     retval = MPI_SUCCESS;
184   }
185   return retval;
186 }
187
188 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
189 {
190   int retval = 0;
191
192   if (datatype == MPI_DATATYPE_NULL) {
193     retval = MPI_ERR_TYPE;
194   } else if (lb == nullptr || extent == nullptr) {
195     retval = MPI_ERR_ARG;
196   } else {
197     retval = smpi_datatype_extent(datatype, lb, extent);
198   }
199   return retval;
200 }
201
202 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
203 {
204   return PMPI_Type_get_extent(datatype, lb, extent);
205 }
206
207 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
208 {
209   int retval = 0;
210
211   if (datatype == MPI_DATATYPE_NULL) {
212     retval = MPI_ERR_TYPE;
213   } else if (extent == nullptr) {
214     retval = MPI_ERR_ARG;
215   } else {
216     *extent = smpi_datatype_get_extent(datatype);
217     retval = MPI_SUCCESS;
218   }
219   return retval;
220 }
221
222 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
223 {
224   int retval = 0;
225
226   if (datatype == MPI_DATATYPE_NULL) {
227     retval = MPI_ERR_TYPE;
228   } else if (disp == nullptr) {
229     retval = MPI_ERR_ARG;
230   } else {
231     *disp = smpi_datatype_lb(datatype);
232     retval = MPI_SUCCESS;
233   }
234   return retval;
235 }
236
237 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
238 {
239   int retval = 0;
240
241   if (datatype == MPI_DATATYPE_NULL) {
242     retval = MPI_ERR_TYPE;
243   } else if (disp == nullptr) {
244     retval = MPI_ERR_ARG;
245   } else {
246     *disp = smpi_datatype_ub(datatype);
247     retval = MPI_SUCCESS;
248   }
249   return retval;
250 }
251
252 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
253   int retval = 0;
254
255   if (datatype == MPI_DATATYPE_NULL) {
256     retval = MPI_ERR_TYPE;
257   } else {
258     retval = smpi_datatype_dup(datatype, newtype);
259   }
260   return retval;
261 }
262
263 int PMPI_Op_create(MPI_User_function * function, int commute, MPI_Op * op)
264 {
265   int retval = 0;
266
267   if (function == nullptr || op == nullptr) {
268     retval = MPI_ERR_ARG;
269   } else {
270     *op = smpi_op_new(function, (commute!=0));
271     retval = MPI_SUCCESS;
272   }
273   return retval;
274 }
275
276 int PMPI_Op_free(MPI_Op * op)
277 {
278   int retval = 0;
279
280   if (op == nullptr) {
281     retval = MPI_ERR_ARG;
282   } else if (*op == MPI_OP_NULL) {
283     retval = MPI_ERR_OP;
284   } else {
285     smpi_op_destroy(*op);
286     *op = MPI_OP_NULL;
287     retval = MPI_SUCCESS;
288   }
289   return retval;
290 }
291
292 int PMPI_Group_free(MPI_Group * group)
293 {
294   int retval = 0;
295
296   if (group == nullptr) {
297     retval = MPI_ERR_ARG;
298   } else {
299     smpi_group_destroy(*group);
300     *group = MPI_GROUP_NULL;
301     retval = MPI_SUCCESS;
302   }
303   return retval;
304 }
305
306 int PMPI_Group_size(MPI_Group group, int *size)
307 {
308   int retval = 0;
309
310   if (group == MPI_GROUP_NULL) {
311     retval = MPI_ERR_GROUP;
312   } else if (size == nullptr) {
313     retval = MPI_ERR_ARG;
314   } else {
315     *size = smpi_group_size(group);
316     retval = MPI_SUCCESS;
317   }
318   return retval;
319 }
320
321 int PMPI_Group_rank(MPI_Group group, int *rank)
322 {
323   int retval = 0;
324
325   if (group == MPI_GROUP_NULL) {
326     retval = MPI_ERR_GROUP;
327   } else if (rank == nullptr) {
328     retval = MPI_ERR_ARG;
329   } else {
330     *rank = smpi_group_rank(group, smpi_process_index());
331     retval = MPI_SUCCESS;
332   }
333   return retval;
334 }
335
336 int PMPI_Group_translate_ranks(MPI_Group group1, int n, int *ranks1, MPI_Group group2, int *ranks2)
337 {
338   int retval, i, index;
339   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
340     retval = MPI_ERR_GROUP;
341   } else {
342     for (i = 0; i < n; i++) {
343       if(ranks1[i]==MPI_PROC_NULL){
344         ranks2[i]=MPI_PROC_NULL;
345       }else{
346         index = smpi_group_index(group1, ranks1[i]);
347         ranks2[i] = smpi_group_rank(group2, index);
348       }
349     }
350     retval = MPI_SUCCESS;
351   }
352   return retval;
353 }
354
355 int PMPI_Group_compare(MPI_Group group1, MPI_Group group2, int *result)
356 {
357   int retval = 0;
358
359   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
360     retval = MPI_ERR_GROUP;
361   } else if (result == nullptr) {
362     retval = MPI_ERR_ARG;
363   } else {
364     *result = smpi_group_compare(group1, group2);
365     retval = MPI_SUCCESS;
366   }
367   return retval;
368 }
369
370 int PMPI_Group_union(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
371 {
372   int retval, i, proc1, proc2, size, size2;
373
374   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
375     retval = MPI_ERR_GROUP;
376   } else if (newgroup == nullptr) {
377     retval = MPI_ERR_ARG;
378   } else {
379     size = smpi_group_size(group1);
380     size2 = smpi_group_size(group2);
381     for (i = 0; i < size2; i++) {
382       proc2 = smpi_group_index(group2, i);
383       proc1 = smpi_group_rank(group1, proc2);
384       if (proc1 == MPI_UNDEFINED) {
385         size++;
386       }
387     }
388     if (size == 0) {
389       *newgroup = MPI_GROUP_EMPTY;
390     } else {
391       *newgroup = smpi_group_new(size);
392       size2 = smpi_group_size(group1);
393       for (i = 0; i < size2; i++) {
394         proc1 = smpi_group_index(group1, i);
395         smpi_group_set_mapping(*newgroup, proc1, i);
396       }
397       for (i = size2; i < size; i++) {
398         proc2 = smpi_group_index(group2, i - size2);
399         smpi_group_set_mapping(*newgroup, proc2, i);
400       }
401     }
402     retval = MPI_SUCCESS;
403   }
404   return retval;
405 }
406
407 int PMPI_Group_intersection(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
408 {
409   int retval, i, proc1, proc2, size;
410
411   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
412     retval = MPI_ERR_GROUP;
413   } else if (newgroup == nullptr) {
414     retval = MPI_ERR_ARG;
415   } else {
416     size = smpi_group_size(group2);
417     for (i = 0; i < size; i++) {
418       proc2 = smpi_group_index(group2, i);
419       proc1 = smpi_group_rank(group1, proc2);
420       if (proc1 == MPI_UNDEFINED) {
421         size--;
422       }
423     }
424     if (size == 0) {
425       *newgroup = MPI_GROUP_EMPTY;
426     } else {
427       *newgroup = smpi_group_new(size);
428       int j=0;
429       for (i = 0; i < smpi_group_size(group2); i++) {
430         proc2 = smpi_group_index(group2, i);
431         proc1 = smpi_group_rank(group1, proc2);
432         if (proc1 != MPI_UNDEFINED) {
433           smpi_group_set_mapping(*newgroup, proc2, j);
434           j++;
435         }
436       }
437     }
438     retval = MPI_SUCCESS;
439   }
440   return retval;
441 }
442
443 int PMPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group * newgroup)
444 {
445   int retval, i, proc1, proc2, size, size2;
446
447   if (group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
448     retval = MPI_ERR_GROUP;
449   } else if (newgroup == nullptr) {
450     retval = MPI_ERR_ARG;
451   } else {
452     size = smpi_group_size(group1);
453     size2 = size;
454     for (i = 0; i < size2; i++) {
455       proc1 = smpi_group_index(group1, i);
456       proc2 = smpi_group_rank(group2, proc1);
457       if (proc2 != MPI_UNDEFINED) {
458         size--;
459       }
460     }
461     if (size == 0) {
462       *newgroup = MPI_GROUP_EMPTY;
463     } else {
464       *newgroup = smpi_group_new(size);
465       for (i = 0; i < size2; i++) {
466         proc1 = smpi_group_index(group1, i);
467         proc2 = smpi_group_rank(group2, proc1);
468         if (proc2 == MPI_UNDEFINED) {
469           smpi_group_set_mapping(*newgroup, proc1, i);
470         }
471       }
472     }
473     retval = MPI_SUCCESS;
474   }
475   return retval;
476 }
477
478 int PMPI_Group_incl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
479 {
480   int retval;
481
482   if (group == MPI_GROUP_NULL) {
483     retval = MPI_ERR_GROUP;
484   } else if (newgroup == nullptr) {
485     retval = MPI_ERR_ARG;
486   } else {
487     retval = smpi_group_incl(group, n, ranks, newgroup);
488   }
489   return retval;
490 }
491
492 int PMPI_Group_excl(MPI_Group group, int n, int *ranks, MPI_Group * newgroup)
493 {
494   int retval, i, j, newsize, oldsize, index;
495
496   if (group == MPI_GROUP_NULL) {
497     retval = MPI_ERR_GROUP;
498   } else if (newgroup == nullptr) {
499     retval = MPI_ERR_ARG;
500   } else {
501     if (n == 0) {
502       *newgroup = group;
503       if(group!= smpi_comm_group(MPI_COMM_WORLD) && group != MPI_GROUP_NULL
504                 && group != smpi_comm_group(MPI_COMM_SELF) && group != MPI_GROUP_EMPTY)
505       smpi_group_use(group);
506     } else if (n == smpi_group_size(group)) {
507       *newgroup = MPI_GROUP_EMPTY;
508     } else {
509       oldsize=smpi_group_size(group);
510       newsize = oldsize - n;
511       *newgroup = smpi_group_new(newsize);
512
513       int* to_exclude=xbt_new0(int, smpi_group_size(group));
514       for(i=0; i<oldsize; i++)
515         to_exclude[i]=0;
516       for(i=0; i<n; i++)
517         to_exclude[ranks[i]]=1;
518
519       j=0;
520       for(i=0; i<oldsize; i++){
521         if(to_exclude[i]==0){
522           index = smpi_group_index(group, i);
523           smpi_group_set_mapping(*newgroup, index, j);
524           j++;
525         }
526       }
527
528       xbt_free(to_exclude);
529     }
530     retval = MPI_SUCCESS;
531   }
532   return retval;
533 }
534
535 int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
536 {
537   int retval, i, j, rank, size, index;
538
539   if (group == MPI_GROUP_NULL) {
540     retval = MPI_ERR_GROUP;
541   } else if (newgroup == nullptr) {
542     retval = MPI_ERR_ARG;
543   } else {
544     if (n == 0) {
545       *newgroup = MPI_GROUP_EMPTY;
546     } else {
547       size = 0;
548       for (i = 0; i < n; i++) {
549         for (rank = ranges[i][0];       /* First */
550              rank >= 0 && rank < smpi_group_size(group); /* Last */
551               ) {
552           size++;
553           if(rank == ranges[i][1]){/*already last ?*/
554             break;
555           }
556           rank += ranges[i][2]; /* Stride */
557     if (ranges[i][0]<ranges[i][1]){
558         if(rank > ranges[i][1])
559           break;
560     }else{
561         if(rank < ranges[i][1])
562           break;
563     }
564         }
565       }
566
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 < smpi_group_size(group); /* Last */
572              ) {
573           index = smpi_group_index(group, rank);
574           smpi_group_set_mapping(*newgroup, index, j);
575           j++;
576           if(rank == ranges[i][1]){/*already last ?*/
577             break;
578           }
579           rank += ranges[i][2]; /* Stride */
580     if (ranges[i][0]<ranges[i][1]){
581       if(rank > ranges[i][1])
582         break;
583     }else{
584       if(rank < ranges[i][1])
585         break;
586     }
587         }
588       }
589     }
590     retval = MPI_SUCCESS;
591   }
592   return retval;
593 }
594
595 int PMPI_Group_range_excl(MPI_Group group, int n, int ranges[][3], MPI_Group * newgroup)
596 {
597   int retval, i, rank, newrank,oldrank, size, index, add;
598
599   if (group == MPI_GROUP_NULL) {
600     retval = MPI_ERR_GROUP;
601   } else if (newgroup == nullptr) {
602     retval = MPI_ERR_ARG;
603   } else {
604     if (n == 0) {
605       *newgroup = group;
606       if(group!= smpi_comm_group(MPI_COMM_WORLD) && group != MPI_GROUP_NULL
607                 && group != smpi_comm_group(MPI_COMM_SELF) && group != MPI_GROUP_EMPTY)
608       smpi_group_use(group);
609     } else {
610       size = smpi_group_size(group);
611       for (i = 0; i < n; i++) {
612         for (rank = ranges[i][0];       /* First */
613              rank >= 0 && rank < smpi_group_size(group); /* Last */
614               ) {
615           size--;
616           if(rank == ranges[i][1]){/*already last ?*/
617             break;
618           }
619           rank += ranges[i][2]; /* Stride */
620     if (ranges[i][0]<ranges[i][1]){
621         if(rank > ranges[i][1])
622           break;
623     }else{
624         if(rank < ranges[i][1])
625           break;
626     }
627         }
628       }
629       if (size == 0) {
630         *newgroup = MPI_GROUP_EMPTY;
631       } else {
632         *newgroup = smpi_group_new(size);
633         newrank=0;
634         oldrank=0;
635         while (newrank < size) {
636           add=1;
637           for (i = 0; i < n; i++) {
638             for (rank = ranges[i][0];
639                 rank >= 0 && rank < smpi_group_size(group);
640                 ){
641               if(rank==oldrank){
642                   add=0;
643                   break;
644               }
645               if(rank == ranges[i][1]){/*already last ?*/
646                 break;
647               }
648               rank += ranges[i][2]; /* Stride */
649               if (ranges[i][0]<ranges[i][1]){
650                   if(rank > ranges[i][1])
651                     break;
652               }else{
653                   if(rank < ranges[i][1])
654                     break;
655               }
656             }
657           }
658           if(add==1){
659             index = smpi_group_index(group, oldrank);
660             smpi_group_set_mapping(*newgroup, index, newrank);
661             newrank++;
662           }
663           oldrank++;
664         }
665       }
666     }
667
668     retval = MPI_SUCCESS;
669   }
670   return retval;
671 }
672
673 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
674 {
675   int retval = 0;
676   if (comm == MPI_COMM_NULL) {
677     retval = MPI_ERR_COMM;
678   } else if (rank == nullptr) {
679     retval = MPI_ERR_ARG;
680   } else {
681     *rank = smpi_comm_rank(comm);
682     retval = MPI_SUCCESS;
683   }
684   return retval;
685 }
686
687 int PMPI_Comm_size(MPI_Comm comm, int *size)
688 {
689   int retval = 0;
690   if (comm == MPI_COMM_NULL) {
691     retval = MPI_ERR_COMM;
692   } else if (size == nullptr) {
693     retval = MPI_ERR_ARG;
694   } else {
695     *size = smpi_comm_size(comm);
696     retval = MPI_SUCCESS;
697   }
698   return retval;
699 }
700
701 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
702 {
703   int retval = 0;
704
705   if (comm == MPI_COMM_NULL)  {
706     retval = MPI_ERR_COMM;
707   } else if (name == nullptr || len == nullptr)  {
708     retval = MPI_ERR_ARG;
709   } else {
710     smpi_comm_get_name(comm, name, len);
711     retval = MPI_SUCCESS;
712   }
713   return retval;
714 }
715
716 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
717 {
718   int retval = 0;
719
720   if (comm == MPI_COMM_NULL) {
721     retval = MPI_ERR_COMM;
722   } else if (group == nullptr) {
723     retval = MPI_ERR_ARG;
724   } else {
725     *group = smpi_comm_group(comm);
726     if(*group!= smpi_comm_group(MPI_COMM_WORLD) && *group != MPI_GROUP_NULL
727               && *group != MPI_GROUP_EMPTY)
728     smpi_group_use(*group);
729     retval = MPI_SUCCESS;
730   }
731   return retval;
732 }
733
734 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
735 {
736   int retval = 0;
737
738   if (comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
739     retval = MPI_ERR_COMM;
740   } else if (result == nullptr) {
741     retval = MPI_ERR_ARG;
742   } else {
743     if (comm1 == comm2) {       /* Same communicators means same groups */
744       *result = MPI_IDENT;
745     } else {
746       *result = smpi_group_compare(smpi_comm_group(comm1), smpi_comm_group(comm2));
747       if (*result == MPI_IDENT) {
748         *result = MPI_CONGRUENT;
749       }
750     }
751     retval = MPI_SUCCESS;
752   }
753   return retval;
754 }
755
756 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
757 {
758   int retval = 0;
759
760   if (comm == MPI_COMM_NULL) {
761     retval = MPI_ERR_COMM;
762   } else if (newcomm == nullptr) {
763     retval = MPI_ERR_ARG;
764   } else {
765     retval = smpi_comm_dup(comm, newcomm);
766   }
767   return retval;
768 }
769
770 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
771 {
772   int retval = 0;
773
774   if (comm == MPI_COMM_NULL) {
775     retval = MPI_ERR_COMM;
776   } else if (group == MPI_GROUP_NULL) {
777     retval = MPI_ERR_GROUP;
778   } else if (newcomm == nullptr) {
779     retval = MPI_ERR_ARG;
780   } else if(smpi_group_rank(group,smpi_process_index())==MPI_UNDEFINED){
781     *newcomm= MPI_COMM_NULL;
782     retval = MPI_SUCCESS;
783   }else{
784     smpi_group_use(group);
785     *newcomm = smpi_comm_new(group, nullptr);
786     retval = MPI_SUCCESS;
787   }
788   return retval;
789 }
790
791 int PMPI_Comm_free(MPI_Comm * comm)
792 {
793   int retval = 0;
794
795   if (comm == nullptr) {
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   return retval;
805 }
806
807 int PMPI_Comm_disconnect(MPI_Comm * comm)
808 {
809   /* TODO: wait until all communication in comm are done */
810   int retval = 0;
811
812   if (comm == nullptr) {
813     retval = MPI_ERR_ARG;
814   } else if (*comm == MPI_COMM_NULL) {
815     retval = MPI_ERR_COMM;
816   } else {
817     smpi_comm_destroy(*comm);
818     *comm = MPI_COMM_NULL;
819     retval = MPI_SUCCESS;
820   }
821   return retval;
822 }
823
824 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
825 {
826   int retval = 0;
827   smpi_bench_end();
828
829   if (comm_out == nullptr) {
830     retval = MPI_ERR_ARG;
831   } else if (comm == MPI_COMM_NULL) {
832     retval = MPI_ERR_COMM;
833   } else {
834     *comm_out = smpi_comm_split(comm, color, key);
835     retval = MPI_SUCCESS;
836   }
837   smpi_bench_begin();
838
839   return retval;
840 }
841
842 int PMPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
843 {
844   int retval = 0;
845
846   smpi_bench_end();
847   if (request == nullptr) {
848       retval = MPI_ERR_ARG;
849   } else if (comm == MPI_COMM_NULL) {
850       retval = MPI_ERR_COMM;
851   } else if (!is_datatype_valid(datatype)) {
852       retval = MPI_ERR_TYPE;
853   } else if (dst == MPI_PROC_NULL) {
854       retval = MPI_SUCCESS;
855   } else {
856       *request = smpi_mpi_send_init(buf, count, datatype, dst, tag, comm);
857       retval = MPI_SUCCESS;
858   }
859   smpi_bench_begin();
860   if (retval != MPI_SUCCESS && request != nullptr)
861     *request = MPI_REQUEST_NULL;
862   return retval;
863 }
864
865 int PMPI_Recv_init(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
866 {
867   int retval = 0;
868
869   smpi_bench_end();
870   if (request == nullptr) {
871     retval = MPI_ERR_ARG;
872   } else if (comm == MPI_COMM_NULL) {
873     retval = MPI_ERR_COMM;
874   } else if (!is_datatype_valid(datatype)) {
875       retval = MPI_ERR_TYPE;
876   } else if (src == MPI_PROC_NULL) {
877     retval = MPI_SUCCESS;
878   } else {
879     *request = smpi_mpi_recv_init(buf, count, datatype, src, tag, comm);
880     retval = MPI_SUCCESS;
881   }
882   smpi_bench_begin();
883   if (retval != MPI_SUCCESS && request != nullptr)
884     *request = MPI_REQUEST_NULL;
885   return retval;
886 }
887
888 int PMPI_Ssend_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
889 {
890   int retval = 0;
891
892   smpi_bench_end();
893   if (request == nullptr) {
894     retval = MPI_ERR_ARG;
895   } else if (comm == MPI_COMM_NULL) {
896     retval = MPI_ERR_COMM;
897   } else if (!is_datatype_valid(datatype)) {
898       retval = MPI_ERR_TYPE;
899   } else if (dst == MPI_PROC_NULL) {
900     retval = MPI_SUCCESS;
901   } else {
902     *request = smpi_mpi_ssend_init(buf, count, datatype, dst, tag, comm);
903     retval = MPI_SUCCESS;
904   }
905   smpi_bench_begin();
906   if (retval != MPI_SUCCESS && request != nullptr)
907     *request = MPI_REQUEST_NULL;
908   return retval;
909 }
910
911 int PMPI_Start(MPI_Request * request)
912 {
913   int retval = 0;
914
915   smpi_bench_end();
916   if (request == nullptr || *request == MPI_REQUEST_NULL) {
917     retval = MPI_ERR_REQUEST;
918   } else {
919     smpi_mpi_start(*request);
920     retval = MPI_SUCCESS;
921   }
922   smpi_bench_begin();
923   return retval;
924 }
925
926 int PMPI_Startall(int count, MPI_Request * requests)
927 {
928   int retval;
929   int i = 0;
930   smpi_bench_end();
931   if (requests == nullptr) {
932     retval = MPI_ERR_ARG;
933   } else {
934     retval = MPI_SUCCESS;
935     for (i = 0 ;  i < count ; i++) {
936       if(requests[i] == MPI_REQUEST_NULL) {
937         retval = MPI_ERR_REQUEST;
938       }
939     }
940     if(retval != MPI_ERR_REQUEST) {
941       smpi_mpi_startall(count, requests);
942     }
943   }
944   smpi_bench_begin();
945   return retval;
946 }
947
948 int PMPI_Request_free(MPI_Request * request)
949 {
950   int retval = 0;
951
952   smpi_bench_end();
953   if (*request == MPI_REQUEST_NULL) {
954     retval = MPI_ERR_ARG;
955   } else {
956     smpi_mpi_request_free(request);
957     retval = MPI_SUCCESS;
958   }
959   smpi_bench_begin();
960   return retval;
961 }
962
963 int PMPI_Irecv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request * request)
964 {
965   int retval = 0;
966
967   smpi_bench_end();
968
969   if (request == nullptr) {
970     retval = MPI_ERR_ARG;
971   } else if (comm == MPI_COMM_NULL) {
972     retval = MPI_ERR_COMM;
973   } else if (src == MPI_PROC_NULL) {
974     *request = MPI_REQUEST_NULL;
975     retval = MPI_SUCCESS;
976   } else if (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0)){
977     retval = MPI_ERR_RANK;
978   } else if ((count < 0) || (buf==nullptr && count > 0)) {
979     retval = MPI_ERR_COUNT;
980   } else if (!is_datatype_valid(datatype)) {
981       retval = MPI_ERR_TYPE;
982   } else if(tag<0 && tag !=  MPI_ANY_TAG){
983     retval = MPI_ERR_TAG;
984   } else {
985
986     int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
987     int src_traced = smpi_group_index(smpi_comm_group(comm), src);
988
989     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
990     extra->type = TRACING_IRECV;
991     extra->src = src_traced;
992     extra->dst = rank;
993     int known=0;
994     extra->datatype1 = encode_datatype(datatype, &known);
995     int dt_size_send = 1;
996     if(known==0)
997       dt_size_send = smpi_datatype_size(datatype);
998     extra->send_size = count*dt_size_send;
999     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
1000
1001     *request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
1002     retval = MPI_SUCCESS;
1003
1004     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
1005     (*request)->recv = 1;
1006   }
1007
1008   smpi_bench_begin();
1009   if (retval != MPI_SUCCESS && request != nullptr)
1010     *request = MPI_REQUEST_NULL;
1011   return retval;
1012 }
1013
1014
1015 int PMPI_Isend(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request)
1016 {
1017   int retval = 0;
1018
1019   smpi_bench_end();
1020   if (request == nullptr) {
1021     retval = MPI_ERR_ARG;
1022   } else if (comm == MPI_COMM_NULL) {
1023     retval = MPI_ERR_COMM;
1024   } else if (dst == MPI_PROC_NULL) {
1025     *request = MPI_REQUEST_NULL;
1026     retval = MPI_SUCCESS;
1027   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1028     retval = MPI_ERR_RANK;
1029   } else if ((count < 0) || (buf==nullptr && count > 0)) {
1030     retval = MPI_ERR_COUNT;
1031   } else if (!is_datatype_valid(datatype)) {
1032       retval = MPI_ERR_TYPE;
1033   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1034     retval = MPI_ERR_TAG;
1035   } else {
1036
1037     int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1038     int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1039     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1040     extra->type = TRACING_ISEND;
1041     extra->src = rank;
1042     extra->dst = dst_traced;
1043     int known=0;
1044     extra->datatype1 = encode_datatype(datatype, &known);
1045     int dt_size_send = 1;
1046     if(known==0)
1047       dt_size_send = smpi_datatype_size(datatype);
1048     extra->send_size = count*dt_size_send;
1049     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1050     TRACE_smpi_send(rank, rank, dst_traced, tag, count*smpi_datatype_size(datatype));
1051
1052     *request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
1053     retval = MPI_SUCCESS;
1054
1055     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1056     (*request)->send = 1;
1057   }
1058
1059   smpi_bench_begin();
1060   if (retval != MPI_SUCCESS && request!=nullptr)
1061     *request = MPI_REQUEST_NULL;
1062   return retval;
1063 }
1064
1065 int PMPI_Issend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request)
1066 {
1067   int retval = 0;
1068
1069   smpi_bench_end();
1070   if (request == nullptr) {
1071     retval = MPI_ERR_ARG;
1072   } else if (comm == MPI_COMM_NULL) {
1073     retval = MPI_ERR_COMM;
1074   } else if (dst == MPI_PROC_NULL) {
1075     *request = MPI_REQUEST_NULL;
1076     retval = MPI_SUCCESS;
1077   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1078     retval = MPI_ERR_RANK;
1079   } else if ((count < 0)|| (buf==nullptr && count > 0)) {
1080     retval = MPI_ERR_COUNT;
1081   } else if (!is_datatype_valid(datatype)) {
1082       retval = MPI_ERR_TYPE;
1083   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1084     retval = MPI_ERR_TAG;
1085   } else {
1086
1087     int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1088     int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1089     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1090     extra->type = TRACING_ISSEND;
1091     extra->src = rank;
1092     extra->dst = dst_traced;
1093     int known=0;
1094     extra->datatype1 = encode_datatype(datatype, &known);
1095     int dt_size_send = 1;
1096     if(known==0)
1097       dt_size_send = smpi_datatype_size(datatype);
1098     extra->send_size = count*dt_size_send;
1099     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1100     TRACE_smpi_send(rank, rank, dst_traced, tag, count*smpi_datatype_size(datatype));
1101
1102     *request = smpi_mpi_issend(buf, count, datatype, dst, tag, comm);
1103     retval = MPI_SUCCESS;
1104
1105     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1106     (*request)->send = 1;
1107   }
1108
1109   smpi_bench_begin();
1110   if (retval != MPI_SUCCESS && request!=nullptr)
1111     *request = MPI_REQUEST_NULL;
1112   return retval;
1113 }
1114
1115 int PMPI_Recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status * status)
1116 {
1117   int retval = 0;
1118
1119   smpi_bench_end();
1120   if (comm == MPI_COMM_NULL) {
1121     retval = MPI_ERR_COMM;
1122   } else if (src == MPI_PROC_NULL) {
1123     smpi_empty_status(status);
1124     status->MPI_SOURCE = MPI_PROC_NULL;
1125     retval = MPI_SUCCESS;
1126   } else if (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0)){
1127     retval = MPI_ERR_RANK;
1128   } else if ((count < 0) || (buf==nullptr && count > 0)) {
1129     retval = MPI_ERR_COUNT;
1130   } else if (!is_datatype_valid(datatype)) {
1131       retval = MPI_ERR_TYPE;
1132   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1133     retval = MPI_ERR_TAG;
1134   } else {
1135   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1136   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
1137   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1138   extra->type = TRACING_RECV;
1139   extra->src = src_traced;
1140   extra->dst = rank;
1141   int known=0;
1142   extra->datatype1 = encode_datatype(datatype, &known);
1143   int dt_size_send = 1;
1144   if(known==0)
1145     dt_size_send = smpi_datatype_size(datatype);
1146   extra->send_size = count*dt_size_send;
1147   TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, extra);
1148
1149     smpi_mpi_recv(buf, count, datatype, src, tag, comm, status);
1150     retval = MPI_SUCCESS;
1151
1152   //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1153   if(status!=MPI_STATUS_IGNORE){
1154     src_traced = smpi_group_index(smpi_comm_group(comm), status->MPI_SOURCE);
1155     if (!TRACE_smpi_view_internals()) {
1156       TRACE_smpi_recv(rank, src_traced, rank, tag);
1157     }
1158   }
1159   TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
1160   }
1161
1162   smpi_bench_begin();
1163   return retval;
1164 }
1165
1166 int PMPI_Send(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm)
1167 {
1168   int retval = 0;
1169
1170   smpi_bench_end();
1171
1172   if (comm == MPI_COMM_NULL) {
1173     retval = MPI_ERR_COMM;
1174   } else if (dst == MPI_PROC_NULL) {
1175     retval = MPI_SUCCESS;
1176   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1177     retval = MPI_ERR_RANK;
1178   } else if ((count < 0) || (buf == nullptr && count > 0)) {
1179     retval = MPI_ERR_COUNT;
1180   } else if (!is_datatype_valid(datatype)) {
1181     retval = MPI_ERR_TYPE;
1182   } else if(tag < 0 && tag !=  MPI_ANY_TAG){
1183     retval = MPI_ERR_TAG;
1184   } else {
1185     int rank               = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1186     int dst_traced         = smpi_group_index(smpi_comm_group(comm), dst);
1187     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1188     extra->type            = TRACING_SEND;
1189     extra->src             = rank;
1190     extra->dst             = dst_traced;
1191     int known              = 0;
1192     extra->datatype1       = encode_datatype(datatype, &known);
1193     int dt_size_send       = 1;
1194     if (known == 0) {
1195       dt_size_send = smpi_datatype_size(datatype);
1196     }
1197     extra->send_size = count*dt_size_send;
1198     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1199     if (!TRACE_smpi_view_internals()) {
1200       TRACE_smpi_send(rank, rank, dst_traced, tag,count*smpi_datatype_size(datatype));
1201     }
1202
1203     smpi_mpi_send(buf, count, datatype, dst, tag, comm);
1204     retval = MPI_SUCCESS;
1205
1206     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1207   }
1208
1209   smpi_bench_begin();
1210   return retval;
1211 }
1212
1213 int PMPI_Ssend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
1214   int retval = 0;
1215
1216   smpi_bench_end();
1217
1218   if (comm == MPI_COMM_NULL) {
1219     retval = MPI_ERR_COMM;
1220   } else if (dst == MPI_PROC_NULL) {
1221     retval = MPI_SUCCESS;
1222   } else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0){
1223     retval = MPI_ERR_RANK;
1224   } else if ((count < 0) || (buf==nullptr && count > 0)) {
1225     retval = MPI_ERR_COUNT;
1226   } else if (!is_datatype_valid(datatype)){
1227     retval = MPI_ERR_TYPE;
1228   } else if(tag<0 && tag !=  MPI_ANY_TAG){
1229     retval = MPI_ERR_TAG;
1230   } else {
1231     int rank               = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1232     int dst_traced         = smpi_group_index(smpi_comm_group(comm), dst);
1233     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1234     extra->type            = TRACING_SSEND;
1235     extra->src             = rank;
1236     extra->dst             = dst_traced;
1237     int known              = 0;
1238     extra->datatype1       = encode_datatype(datatype, &known);
1239     int dt_size_send       = 1;
1240     if(known == 0) {
1241       dt_size_send = smpi_datatype_size(datatype);
1242     }
1243     extra->send_size = count*dt_size_send;
1244     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, extra);
1245     TRACE_smpi_send(rank, rank, dst_traced, tag,count*smpi_datatype_size(datatype));
1246   
1247     smpi_mpi_ssend(buf, count, datatype, dst, tag, comm);
1248     retval = MPI_SUCCESS;
1249   
1250     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
1251   }
1252
1253   smpi_bench_begin();
1254   return retval;
1255 }
1256
1257 int PMPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void *recvbuf,
1258                  int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status * status)
1259 {
1260   int retval = 0;
1261
1262   smpi_bench_end();
1263
1264   if (comm == MPI_COMM_NULL) {
1265     retval = MPI_ERR_COMM;
1266   } else if (!is_datatype_valid(sendtype)
1267              || !is_datatype_valid(recvtype)) {
1268     retval = MPI_ERR_TYPE;
1269   } else if (src == MPI_PROC_NULL || dst == MPI_PROC_NULL) {
1270       smpi_empty_status(status);
1271       status->MPI_SOURCE = MPI_PROC_NULL;
1272       retval = MPI_SUCCESS;
1273   }else if (dst >= smpi_group_size(smpi_comm_group(comm)) || dst <0 ||
1274       (src!=MPI_ANY_SOURCE && (src >= smpi_group_size(smpi_comm_group(comm)) || src <0))){
1275     retval = MPI_ERR_RANK;
1276   } else if ((sendcount < 0 || recvcount<0) || 
1277       (sendbuf==nullptr && sendcount > 0) || (recvbuf==nullptr && recvcount>0)) {
1278     retval = MPI_ERR_COUNT;
1279   } else if((sendtag<0 && sendtag !=  MPI_ANY_TAG)||(recvtag<0 && recvtag != MPI_ANY_TAG)){
1280     retval = MPI_ERR_TAG;
1281   } else {
1282
1283   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1284   int dst_traced = smpi_group_index(smpi_comm_group(comm), dst);
1285   int src_traced = smpi_group_index(smpi_comm_group(comm), src);
1286   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1287   extra->type = TRACING_SENDRECV;
1288   extra->src = src_traced;
1289   extra->dst = dst_traced;
1290   int known=0;
1291   extra->datatype1 = encode_datatype(sendtype, &known);
1292   int dt_size_send = 1;
1293   if(known==0)
1294     dt_size_send = smpi_datatype_size(sendtype);
1295   extra->send_size = sendcount*dt_size_send;
1296   extra->datatype2 = encode_datatype(recvtype, &known);
1297   int dt_size_recv = 1;
1298   if(known==0)
1299     dt_size_recv = smpi_datatype_size(recvtype);
1300   extra->recv_size = recvcount*dt_size_recv;
1301
1302   TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1303   TRACE_smpi_send(rank, rank, dst_traced, sendtag,sendcount*smpi_datatype_size(sendtype));
1304
1305     smpi_mpi_sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf,
1306                       recvcount, recvtype, src, recvtag, comm, status);
1307     retval = MPI_SUCCESS;
1308
1309   TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1310   TRACE_smpi_recv(rank, src_traced, rank, recvtag);
1311   }
1312
1313   smpi_bench_begin();
1314   return retval;
1315 }
1316
1317 int PMPI_Sendrecv_replace(void *buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag,
1318                          MPI_Comm comm, MPI_Status * status)
1319 {
1320   void *recvbuf;
1321   int retval = 0;
1322   if (!is_datatype_valid(datatype)) {
1323       retval = MPI_ERR_TYPE;
1324   } else if (count < 0) {
1325       retval = MPI_ERR_COUNT;
1326   } else {
1327     int size = smpi_datatype_get_extent(datatype) * count;
1328     recvbuf = xbt_new0(char, size);
1329     retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
1330     if(retval==MPI_SUCCESS){
1331         smpi_datatype_copy(recvbuf, count, datatype, buf, count, datatype);
1332     }
1333     xbt_free(recvbuf);
1334
1335   }
1336   return retval;
1337 }
1338
1339 int PMPI_Test(MPI_Request * request, int *flag, MPI_Status * status)
1340 {
1341   int retval = 0;
1342   smpi_bench_end();
1343   if (request == nullptr || flag == nullptr) {
1344     retval = MPI_ERR_ARG;
1345   } else if (*request == MPI_REQUEST_NULL) {
1346     *flag= true;
1347     smpi_empty_status(status);
1348     retval = MPI_SUCCESS;
1349   } else {
1350     int rank = (request!=nullptr && (*request)->comm != MPI_COMM_NULL) ? smpi_process_index() : -1;
1351
1352     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1353     extra->type = TRACING_TEST;
1354     TRACE_smpi_testing_in(rank, extra);
1355
1356     *flag = smpi_mpi_test(request, status);
1357
1358     TRACE_smpi_testing_out(rank);
1359     retval = MPI_SUCCESS;
1360   }
1361   smpi_bench_begin();
1362   return retval;
1363 }
1364
1365 int PMPI_Testany(int count, MPI_Request requests[], int *index, int *flag, MPI_Status * status)
1366 {
1367   int retval = 0;
1368
1369   smpi_bench_end();
1370   if (index == nullptr || flag == nullptr) {
1371     retval = MPI_ERR_ARG;
1372   } else {
1373     *flag = smpi_mpi_testany(count, requests, index, status);
1374     retval = MPI_SUCCESS;
1375   }
1376   smpi_bench_begin();
1377   return retval;
1378 }
1379
1380 int PMPI_Testall(int count, MPI_Request* requests, int* flag, MPI_Status* statuses)
1381 {
1382   int retval = 0;
1383
1384   smpi_bench_end();
1385   if (flag == nullptr) {
1386     retval = MPI_ERR_ARG;
1387   } else {
1388     *flag = smpi_mpi_testall(count, requests, statuses);
1389     retval = MPI_SUCCESS;
1390   }
1391   smpi_bench_begin();
1392   return retval;
1393 }
1394
1395 int PMPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status) {
1396   int retval = 0;
1397   smpi_bench_end();
1398
1399   if (status == nullptr) {
1400     retval = MPI_ERR_ARG;
1401   } else if (comm == MPI_COMM_NULL) {
1402     retval = MPI_ERR_COMM;
1403   } else if (source == MPI_PROC_NULL) {
1404     smpi_empty_status(status);
1405     status->MPI_SOURCE = MPI_PROC_NULL;
1406     retval = MPI_SUCCESS;
1407   } else {
1408     smpi_mpi_probe(source, tag, comm, status);
1409     retval = MPI_SUCCESS;
1410   }
1411   smpi_bench_begin();
1412   return retval;
1413 }
1414
1415 int PMPI_Iprobe(int source, int tag, MPI_Comm comm, int* flag, MPI_Status* status) {
1416   int retval = 0;
1417   smpi_bench_end();
1418
1419   if ((flag == nullptr) || (status == nullptr)) {
1420     retval = MPI_ERR_ARG;
1421   } else if (comm == MPI_COMM_NULL) {
1422     retval = MPI_ERR_COMM;
1423   } else if (source == MPI_PROC_NULL) {
1424     *flag=true;
1425     smpi_empty_status(status);
1426     status->MPI_SOURCE = MPI_PROC_NULL;
1427     retval = MPI_SUCCESS;
1428   } else {
1429     smpi_mpi_iprobe(source, tag, comm, flag, status);
1430     retval = MPI_SUCCESS;
1431   }
1432   smpi_bench_begin();
1433   return retval;
1434 }
1435
1436 int PMPI_Wait(MPI_Request * request, MPI_Status * status)
1437 {
1438   int retval = 0;
1439
1440   smpi_bench_end();
1441
1442   smpi_empty_status(status);
1443
1444   if (request == nullptr) {
1445     retval = MPI_ERR_ARG;
1446   } else if (*request == MPI_REQUEST_NULL) {
1447     retval = MPI_SUCCESS;
1448   } else {
1449
1450     int rank = (request!=nullptr && (*request)->comm != MPI_COMM_NULL) ? smpi_process_index() : -1;
1451
1452     int src_traced = (*request)->src;
1453     int dst_traced = (*request)->dst;
1454     int tag_traced= (*request)->tag;
1455     MPI_Comm comm = (*request)->comm;
1456     int is_wait_for_receive = (*request)->recv;
1457     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1458     extra->type = TRACING_WAIT;
1459     TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1460
1461     smpi_mpi_wait(request, status);
1462     retval = MPI_SUCCESS;
1463
1464     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1465     TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1466     if (is_wait_for_receive) {
1467       if(src_traced==MPI_ANY_SOURCE)
1468         src_traced = (status!=MPI_STATUS_IGNORE) ?
1469           smpi_group_rank(smpi_comm_group(comm), status->MPI_SOURCE) :
1470           src_traced;
1471       TRACE_smpi_recv(rank, src_traced, dst_traced, tag_traced);
1472     }
1473   }
1474
1475   smpi_bench_begin();
1476   return retval;
1477 }
1478
1479 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1480 {
1481   if (index == nullptr)
1482     return MPI_ERR_ARG;
1483
1484   smpi_bench_end();
1485   //save requests information for tracing
1486   int i;
1487   int *srcs = nullptr, *dsts = nullptr, *recvs = nullptr, *tags = nullptr;
1488   MPI_Comm* comms = nullptr;
1489   if(count>0){
1490     srcs = xbt_new0(int, count);
1491     dsts = xbt_new0(int, count);
1492     recvs = xbt_new0(int, count);
1493     tags = xbt_new0(int, count);
1494     comms = xbt_new0(MPI_Comm, count);
1495   }
1496   for (i = 0; i < count; i++) {
1497     MPI_Request req = requests[i];      //already received requests are no longer valid
1498     if (req) {
1499       srcs[i] = req->src;
1500       dsts[i] = req->dst;
1501       recvs[i] = req->recv;
1502       tags[i] = req->tag;
1503       comms[i] = req->comm;
1504     }
1505   }
1506   int rank_traced = smpi_process_index();
1507   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1508   extra->type = TRACING_WAITANY;
1509   extra->send_size=count;
1510   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1511
1512   *index = smpi_mpi_waitany(count, requests, status);
1513
1514   if(*index!=MPI_UNDEFINED){
1515     int src_traced = srcs[*index];
1516     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1517     int dst_traced = dsts[*index];
1518     int is_wait_for_receive = recvs[*index];
1519     if (is_wait_for_receive) {
1520       if(srcs[*index]==MPI_ANY_SOURCE)
1521         src_traced = (status!=MPI_STATUSES_IGNORE) ?
1522                       smpi_group_rank(smpi_comm_group(comms[*index]), status->MPI_SOURCE) : srcs[*index];
1523       TRACE_smpi_recv(rank_traced, src_traced, dst_traced, tags[*index]);
1524     }
1525     TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1526   }
1527   xbt_free(srcs);
1528   xbt_free(dsts);
1529   xbt_free(recvs);
1530   xbt_free(tags);
1531   xbt_free(comms);
1532
1533
1534   smpi_bench_begin();
1535   return MPI_SUCCESS;
1536 }
1537
1538 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1539 {
1540   smpi_bench_end();
1541   //save information from requests
1542   int i;
1543   int *srcs = xbt_new0(int, count);
1544   int *dsts = xbt_new0(int, count);
1545   int *recvs = xbt_new0(int, count);
1546   int *tags = xbt_new0(int, count);
1547   int *valid = xbt_new0(int, count);
1548   MPI_Comm *comms = xbt_new0(MPI_Comm, count);
1549
1550   for (i = 0; i < count; i++) {
1551     MPI_Request req = requests[i];
1552     if(req!=MPI_REQUEST_NULL){
1553       srcs[i] = req->src;
1554       dsts[i] = req->dst;
1555       recvs[i] = req->recv;
1556       tags[i] = req->tag;
1557       comms[i] = req->comm;
1558       valid[i]=1;;
1559     }else{
1560       valid[i]=0;
1561     }
1562   }
1563   int rank_traced = smpi_process_index();
1564   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1565   extra->type = TRACING_WAITALL;
1566   extra->send_size=count;
1567   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1568
1569   int retval = smpi_mpi_waitall(count, requests, status);
1570
1571   for (i = 0; i < count; i++) {
1572     if(valid[i]){
1573     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1574       int src_traced = srcs[i];
1575       int dst_traced = dsts[i];
1576       int is_wait_for_receive = recvs[i];
1577       if (is_wait_for_receive) {
1578         if(src_traced==MPI_ANY_SOURCE)
1579         src_traced = (status!=MPI_STATUSES_IGNORE) ?
1580                           smpi_group_rank(smpi_comm_group(comms[i]), status[i].MPI_SOURCE) : srcs[i];
1581         TRACE_smpi_recv(rank_traced, src_traced, dst_traced,tags[i]);
1582       }
1583     }
1584   }
1585   TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1586   xbt_free(srcs);
1587   xbt_free(dsts);
1588   xbt_free(recvs);
1589   xbt_free(valid);
1590   xbt_free(tags);
1591   xbt_free(comms);
1592
1593   smpi_bench_begin();
1594   return retval;
1595 }
1596
1597 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
1598 {
1599   int retval = 0;
1600
1601   smpi_bench_end();
1602   if (outcount == nullptr) {
1603     retval = MPI_ERR_ARG;
1604   } else {
1605     *outcount = smpi_mpi_waitsome(incount, requests, indices, status);
1606     retval = MPI_SUCCESS;
1607   }
1608   smpi_bench_begin();
1609   return retval;
1610 }
1611
1612 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
1613 {
1614   int retval = 0;
1615
1616    smpi_bench_end();
1617    if (outcount == nullptr) {
1618      retval = MPI_ERR_ARG;
1619    } else {
1620      *outcount = smpi_mpi_testsome(incount, requests, indices, status);
1621      retval = MPI_SUCCESS;
1622    }
1623    smpi_bench_begin();
1624    return retval;
1625 }
1626
1627
1628 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1629 {
1630   int retval = 0;
1631
1632   smpi_bench_end();
1633
1634   if (comm == MPI_COMM_NULL) {
1635     retval = MPI_ERR_COMM;
1636   } else if (!is_datatype_valid(datatype)) {
1637       retval = MPI_ERR_ARG;
1638   } else {
1639   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1640   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1641
1642   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1643   extra->type = TRACING_BCAST;
1644   extra->root = root_traced;
1645   int known=0;
1646   extra->datatype1 = encode_datatype(datatype, &known);
1647   int dt_size_send = 1;
1648   if(known==0)
1649     dt_size_send = smpi_datatype_size(datatype);
1650   extra->send_size = count*dt_size_send;
1651   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1652   if(smpi_comm_size(comm)>1)
1653     mpi_coll_bcast_fun(buf, count, datatype, root, comm);
1654   retval = MPI_SUCCESS;
1655
1656   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1657   }
1658
1659   smpi_bench_begin();
1660   return retval;
1661 }
1662
1663 int PMPI_Barrier(MPI_Comm comm)
1664 {
1665   int retval = 0;
1666
1667   smpi_bench_end();
1668
1669   if (comm == MPI_COMM_NULL) {
1670     retval = MPI_ERR_COMM;
1671   } else {
1672   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1673   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1674   extra->type = TRACING_BARRIER;
1675   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1676
1677   mpi_coll_barrier_fun(comm);
1678   retval = MPI_SUCCESS;
1679
1680   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1681   }
1682
1683   smpi_bench_begin();
1684   return retval;
1685 }
1686
1687 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
1688                 int root, MPI_Comm comm)
1689 {
1690   int retval = 0;
1691
1692   smpi_bench_end();
1693
1694   if (comm == MPI_COMM_NULL) {
1695     retval = MPI_ERR_COMM;
1696   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1697             ((smpi_comm_rank(comm) == root) && (recvtype == MPI_DATATYPE_NULL))){
1698     retval = MPI_ERR_TYPE;
1699   } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) || ((smpi_comm_rank(comm) == root) && (recvcount <0))){
1700     retval = MPI_ERR_COUNT;
1701   } else {
1702
1703     char* sendtmpbuf = static_cast<char*>(sendbuf);
1704     int sendtmpcount = sendcount;
1705     MPI_Datatype sendtmptype = sendtype;
1706     if( (smpi_comm_rank(comm) == root) && (sendbuf == MPI_IN_PLACE )) {
1707       sendtmpcount=0;
1708       sendtmptype=recvtype;
1709     }
1710   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1711   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1712   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1713   extra->type = TRACING_GATHER;
1714   extra->root = root_traced;
1715   int known=0;
1716   extra->datatype1 = encode_datatype(sendtmptype, &known);
1717   int dt_size_send = 1;
1718   if(known==0)
1719     dt_size_send = smpi_datatype_size(sendtmptype);
1720   extra->send_size = sendtmpcount*dt_size_send;
1721   extra->datatype2 = encode_datatype(recvtype, &known);
1722   int dt_size_recv = 1;
1723   if((smpi_comm_rank(comm)==root) && known==0)
1724     dt_size_recv = smpi_datatype_size(recvtype);
1725   extra->recv_size = recvcount*dt_size_recv;
1726
1727   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1728
1729   mpi_coll_gather_fun(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, root, comm);
1730
1731   retval = MPI_SUCCESS;
1732   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1733   }
1734
1735   smpi_bench_begin();
1736   return retval;
1737 }
1738
1739 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1740                 MPI_Datatype recvtype, int root, MPI_Comm comm)
1741 {
1742   int retval = 0;
1743
1744   smpi_bench_end();
1745
1746   if (comm == MPI_COMM_NULL) {
1747     retval = MPI_ERR_COMM;
1748   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1749             ((smpi_comm_rank(comm) == root) && (recvtype == MPI_DATATYPE_NULL))){
1750     retval = MPI_ERR_TYPE;
1751   } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1752     retval = MPI_ERR_COUNT;
1753   } else if (recvcounts == nullptr || displs == nullptr) {
1754     retval = MPI_ERR_ARG;
1755   } else {
1756     char* sendtmpbuf = static_cast<char*>(sendbuf);
1757     int sendtmpcount = sendcount;
1758     MPI_Datatype sendtmptype = sendtype;
1759     if( (smpi_comm_rank(comm) == root) && (sendbuf == MPI_IN_PLACE )) {
1760       sendtmpcount=0;
1761       sendtmptype=recvtype;
1762     }
1763
1764   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1765   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1766   int i=0;
1767   int size = smpi_comm_size(comm);
1768   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1769   extra->type = TRACING_GATHERV;
1770   extra->num_processes = size;
1771   extra->root = root_traced;
1772   int known=0;
1773   extra->datatype1 = encode_datatype(sendtmptype, &known);
1774   int dt_size_send = 1;
1775   if(known==0)
1776     dt_size_send = smpi_datatype_size(sendtype);
1777   extra->send_size = sendtmpcount*dt_size_send;
1778   extra->datatype2 = encode_datatype(recvtype, &known);
1779   int dt_size_recv = 1;
1780   if(known==0)
1781     dt_size_recv = smpi_datatype_size(recvtype);
1782   if((smpi_comm_rank(comm)==root)){
1783   extra->recvcounts= xbt_new(int,size);
1784   for(i=0; i< size; i++)//copy data to avoid bad free
1785     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
1786   }
1787   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
1788
1789   smpi_mpi_gatherv(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcounts, displs, recvtype, root, comm);
1790     retval = MPI_SUCCESS;
1791   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1792   }
1793
1794   smpi_bench_begin();
1795   return retval;
1796 }
1797
1798 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1799                    void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1800 {
1801   int retval = 0;
1802
1803   smpi_bench_end();
1804
1805   if (comm == MPI_COMM_NULL) {
1806     retval = MPI_ERR_COMM;
1807   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1808             (recvtype == MPI_DATATYPE_NULL)){
1809     retval = MPI_ERR_TYPE;
1810   } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) ||
1811             (recvcount <0)){
1812     retval = MPI_ERR_COUNT;
1813   } else {
1814     if(sendbuf == MPI_IN_PLACE) {
1815       sendbuf=static_cast<char*>(recvbuf)+smpi_datatype_get_extent(recvtype)*recvcount*smpi_comm_rank(comm);
1816       sendcount=recvcount;
1817       sendtype=recvtype;
1818     }
1819   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1820   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1821   extra->type = TRACING_ALLGATHER;
1822   int known=0;
1823   extra->datatype1 = encode_datatype(sendtype, &known);
1824   int dt_size_send = 1;
1825   if(known==0)
1826     dt_size_send = smpi_datatype_size(sendtype);
1827   extra->send_size = sendcount*dt_size_send;
1828   extra->datatype2 = encode_datatype(recvtype, &known);
1829   int dt_size_recv = 1;
1830   if(known==0)
1831     dt_size_recv = smpi_datatype_size(recvtype);
1832   extra->recv_size = recvcount*dt_size_recv;
1833
1834   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1835
1836   mpi_coll_allgather_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1837     retval = MPI_SUCCESS;
1838   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1839   }
1840   smpi_bench_begin();
1841   return retval;
1842 }
1843
1844 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1845                    void *recvbuf, int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1846 {
1847   int retval = 0;
1848
1849   smpi_bench_end();
1850
1851   if (comm == MPI_COMM_NULL) {
1852     retval = MPI_ERR_COMM;
1853   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1854             (recvtype == MPI_DATATYPE_NULL)){
1855     retval = MPI_ERR_TYPE;
1856   } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1857     retval = MPI_ERR_COUNT;
1858   } else if (recvcounts == nullptr || displs == nullptr) {
1859     retval = MPI_ERR_ARG;
1860   } else {
1861
1862     if(sendbuf == MPI_IN_PLACE) {
1863       sendbuf=static_cast<char*>(recvbuf)+smpi_datatype_get_extent(recvtype)*displs[smpi_comm_rank(comm)];
1864       sendcount=recvcounts[smpi_comm_rank(comm)];
1865       sendtype=recvtype;
1866     }
1867   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1868   int i=0;
1869   int size = smpi_comm_size(comm);
1870   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1871   extra->type = TRACING_ALLGATHERV;
1872   extra->num_processes = size;
1873   int known=0;
1874   extra->datatype1 = encode_datatype(sendtype, &known);
1875   int dt_size_send = 1;
1876   if(known==0)
1877     dt_size_send = smpi_datatype_size(sendtype);
1878   extra->send_size = sendcount*dt_size_send;
1879   extra->datatype2 = encode_datatype(recvtype, &known);
1880   int dt_size_recv = 1;
1881   if(known==0)
1882     dt_size_recv = smpi_datatype_size(recvtype);
1883   extra->recvcounts= xbt_new(int, size);
1884   for(i=0; i< size; i++)//copy data to avoid bad free
1885     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
1886
1887   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
1888
1889     mpi_coll_allgatherv_fun(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1890     retval = MPI_SUCCESS;
1891   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1892   }
1893
1894   smpi_bench_begin();
1895   return retval;
1896 }
1897
1898 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1899                 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1900 {
1901   int retval = 0;
1902
1903   smpi_bench_end();
1904
1905   if (comm == MPI_COMM_NULL) {
1906     retval = MPI_ERR_COMM;
1907   } else if (((smpi_comm_rank(comm)==root) && (!is_datatype_valid(sendtype)))
1908              || ((recvbuf !=MPI_IN_PLACE) && (!is_datatype_valid(recvtype)))){
1909     retval = MPI_ERR_TYPE;
1910   } else if ((sendbuf == recvbuf) ||
1911       ((smpi_comm_rank(comm)==root) && sendcount>0 && (sendbuf == nullptr))){
1912     retval = MPI_ERR_BUFFER;
1913   }else {
1914
1915     if (recvbuf == MPI_IN_PLACE) {
1916         recvtype=sendtype;
1917         recvcount=sendcount;
1918     }
1919   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1920   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1921   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1922   extra->type = TRACING_SCATTER;
1923   extra->root = root_traced;
1924   int known=0;
1925   extra->datatype1 = encode_datatype(sendtype, &known);
1926   int dt_size_send = 1;
1927   if((smpi_comm_rank(comm)==root) && known==0)
1928     dt_size_send = smpi_datatype_size(sendtype);
1929   extra->send_size = sendcount*dt_size_send;
1930   extra->datatype2 = encode_datatype(recvtype, &known);
1931   int dt_size_recv = 1;
1932   if(known==0)
1933     dt_size_recv = smpi_datatype_size(recvtype);
1934   extra->recv_size = recvcount*dt_size_recv;
1935   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
1936
1937   mpi_coll_scatter_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1938     retval = MPI_SUCCESS;
1939   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1940   }
1941
1942   smpi_bench_begin();
1943   return retval;
1944 }
1945
1946 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1947                  MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1948 {
1949   int retval = 0;
1950
1951   smpi_bench_end();
1952
1953   if (comm == MPI_COMM_NULL) {
1954     retval = MPI_ERR_COMM;
1955   } else if (sendcounts == nullptr || displs == nullptr) {
1956     retval = MPI_ERR_ARG;
1957   } else if (((smpi_comm_rank(comm)==root) && (sendtype == MPI_DATATYPE_NULL))
1958              || ((recvbuf !=MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL))) {
1959     retval = MPI_ERR_TYPE;
1960   } else {
1961     if (recvbuf == MPI_IN_PLACE) {
1962         recvtype=sendtype;
1963         recvcount=sendcounts[smpi_comm_rank(comm)];
1964     }
1965   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1966   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1967   int i=0;
1968   int size = smpi_comm_size(comm);
1969   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1970   extra->type = TRACING_SCATTERV;
1971   extra->num_processes = size;
1972   extra->root = root_traced;
1973   int known=0;
1974   extra->datatype1 = encode_datatype(sendtype, &known);
1975   int dt_size_send = 1;
1976   if(known==0)
1977     dt_size_send = smpi_datatype_size(sendtype);
1978   if((smpi_comm_rank(comm)==root)){
1979   extra->sendcounts= xbt_new(int, size);
1980   for(i=0; i< size; i++)//copy data to avoid bad free
1981     extra->sendcounts[i] = sendcounts[i]*dt_size_send;
1982   }
1983   extra->datatype2 = encode_datatype(recvtype, &known);
1984   int dt_size_recv = 1;
1985   if(known==0)
1986     dt_size_recv = smpi_datatype_size(recvtype);
1987   extra->recv_size = recvcount*dt_size_recv;
1988   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
1989
1990     smpi_mpi_scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1991
1992     retval = MPI_SUCCESS;
1993   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1994   }
1995
1996   smpi_bench_begin();
1997   return retval;
1998 }
1999
2000 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
2001 {
2002   int retval = 0;
2003
2004   smpi_bench_end();
2005
2006   if (comm == MPI_COMM_NULL) {
2007     retval = MPI_ERR_COMM;
2008   } else if (!is_datatype_valid(datatype) || op == MPI_OP_NULL) {
2009     retval = MPI_ERR_ARG;
2010   } else {
2011   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2012   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
2013   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2014   extra->type = TRACING_REDUCE;
2015   int known=0;
2016   extra->datatype1 = encode_datatype(datatype, &known);
2017   int dt_size_send = 1;
2018   if(known==0)
2019     dt_size_send = smpi_datatype_size(datatype);
2020   extra->send_size = count*dt_size_send;
2021   extra->root = root_traced;
2022
2023   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
2024
2025   mpi_coll_reduce_fun(sendbuf, recvbuf, count, datatype, op, root, comm);
2026
2027     retval = MPI_SUCCESS;
2028   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
2029   }
2030
2031   smpi_bench_begin();
2032   return retval;
2033 }
2034
2035 int PMPI_Reduce_local(void *inbuf, void *inoutbuf, int count, MPI_Datatype datatype, MPI_Op op){
2036   int retval = 0;
2037
2038     smpi_bench_end();
2039     if (!is_datatype_valid(datatype) || op == MPI_OP_NULL) {
2040       retval = MPI_ERR_ARG;
2041     } else {
2042       smpi_op_apply(op, inbuf, inoutbuf, &count, &datatype);
2043       retval=MPI_SUCCESS;
2044     }
2045     smpi_bench_begin();
2046     return retval;
2047 }
2048
2049 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2050 {
2051   int retval = 0;
2052
2053   smpi_bench_end();
2054
2055   if (comm == MPI_COMM_NULL) {
2056     retval = MPI_ERR_COMM;
2057   } else if (!is_datatype_valid(datatype)) {
2058     retval = MPI_ERR_TYPE;
2059   } else if (op == MPI_OP_NULL) {
2060     retval = MPI_ERR_OP;
2061   } else {
2062
2063     char* sendtmpbuf = static_cast<char*>(sendbuf);
2064     if( sendbuf == MPI_IN_PLACE ) {
2065       sendtmpbuf = static_cast<char*>(xbt_malloc(count*smpi_datatype_get_extent(datatype)));
2066       smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
2067     }
2068   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2069   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2070   extra->type = TRACING_ALLREDUCE;
2071   int known=0;
2072   extra->datatype1 = encode_datatype(datatype, &known);
2073   int dt_size_send = 1;
2074   if(known==0)
2075     dt_size_send = smpi_datatype_size(datatype);
2076   extra->send_size = count*dt_size_send;
2077
2078   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2079
2080   mpi_coll_allreduce_fun(sendtmpbuf, recvbuf, count, datatype, op, comm);
2081
2082     if( sendbuf == MPI_IN_PLACE )
2083       xbt_free(sendtmpbuf);
2084
2085     retval = MPI_SUCCESS;
2086   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2087   }
2088
2089   smpi_bench_begin();
2090   return retval;
2091 }
2092
2093 int PMPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2094 {
2095   int retval = 0;
2096
2097   smpi_bench_end();
2098
2099   if (comm == MPI_COMM_NULL) {
2100     retval = MPI_ERR_COMM;
2101   } else if (!is_datatype_valid(datatype)) {
2102     retval = MPI_ERR_TYPE;
2103   } else if (op == MPI_OP_NULL) {
2104     retval = MPI_ERR_OP;
2105   } else {
2106   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2107   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2108   extra->type = TRACING_SCAN;
2109   int known=0;
2110   extra->datatype1 = encode_datatype(datatype, &known);
2111   int dt_size_send = 1;
2112   if(known==0)
2113     dt_size_send = smpi_datatype_size(datatype);
2114   extra->send_size = count*dt_size_send;
2115
2116   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2117
2118   smpi_mpi_scan(sendbuf, recvbuf, count, datatype, op, comm);
2119
2120   retval = MPI_SUCCESS;
2121   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2122   }
2123
2124   smpi_bench_begin();
2125   return retval;
2126 }
2127
2128 int PMPI_Exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm){
2129   int retval = 0;
2130
2131   smpi_bench_end();
2132
2133   if (comm == MPI_COMM_NULL) {
2134     retval = MPI_ERR_COMM;
2135   } else if (!is_datatype_valid(datatype)) {
2136     retval = MPI_ERR_TYPE;
2137   } else if (op == MPI_OP_NULL) {
2138     retval = MPI_ERR_OP;
2139   } else {
2140   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2141   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2142   extra->type = TRACING_EXSCAN;
2143   int known=0;
2144   extra->datatype1 = encode_datatype(datatype, &known);
2145   int dt_size_send = 1;
2146   if(known==0)
2147     dt_size_send = smpi_datatype_size(datatype);
2148   extra->send_size = count*dt_size_send;
2149   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2150
2151   smpi_mpi_exscan(sendbuf, recvbuf, count, datatype, op, comm);
2152     retval = MPI_SUCCESS;
2153   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2154   }
2155
2156   smpi_bench_begin();
2157   return retval;
2158 }
2159
2160 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2161 {
2162   int retval = 0;
2163   smpi_bench_end();
2164
2165   if (comm == MPI_COMM_NULL) {
2166     retval = MPI_ERR_COMM;
2167   } else if (!is_datatype_valid(datatype)) {
2168     retval = MPI_ERR_TYPE;
2169   } else if (op == MPI_OP_NULL) {
2170     retval = MPI_ERR_OP;
2171   } else if (recvcounts == nullptr) {
2172     retval = MPI_ERR_ARG;
2173   } else {
2174   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2175   int i=0;
2176   int size = smpi_comm_size(comm);
2177   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2178   extra->type = TRACING_REDUCE_SCATTER;
2179   extra->num_processes = size;
2180   int known=0;
2181   extra->datatype1 = encode_datatype(datatype, &known);
2182   int dt_size_send = 1;
2183   if(known==0)
2184     dt_size_send = smpi_datatype_size(datatype);
2185   extra->send_size = 0;
2186   extra->recvcounts= xbt_new(int, size);
2187   for(i=0; i< size; i++)//copy data to avoid bad free
2188     extra->recvcounts[i] = recvcounts[i]*dt_size_send;
2189   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2190
2191   void* sendtmpbuf=sendbuf;
2192     if(sendbuf==MPI_IN_PLACE)
2193       sendtmpbuf=recvbuf;
2194
2195     mpi_coll_reduce_scatter_fun(sendtmpbuf, recvbuf, recvcounts, datatype,  op, comm);
2196     retval = MPI_SUCCESS;
2197   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2198   }
2199
2200   smpi_bench_begin();
2201   return retval;
2202 }
2203
2204 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
2205                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2206 {
2207   int retval,i;
2208   smpi_bench_end();
2209
2210   if (comm == MPI_COMM_NULL) {
2211     retval = MPI_ERR_COMM;
2212   } else if (!is_datatype_valid(datatype)) {
2213     retval = MPI_ERR_TYPE;
2214   } else if (op == MPI_OP_NULL) {
2215     retval = MPI_ERR_OP;
2216   } else if (recvcount < 0) {
2217     retval = MPI_ERR_ARG;
2218   } else {
2219     int count=smpi_comm_size(comm);
2220
2221   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2222   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2223   extra->type = TRACING_REDUCE_SCATTER;
2224   extra->num_processes = count;
2225   int known=0;
2226   extra->datatype1 = encode_datatype(datatype, &known);
2227   int dt_size_send = 1;
2228   if(known==0)
2229     dt_size_send = smpi_datatype_size(datatype);
2230   extra->send_size = 0;
2231   extra->recvcounts= xbt_new(int, count);
2232   for(i=0; i< count; i++)//copy data to avoid bad free
2233     extra->recvcounts[i] = recvcount*dt_size_send;
2234
2235   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2236
2237   int* recvcounts=static_cast<int*>(xbt_malloc(count));
2238     for (i=0; i<count;i++)
2239       recvcounts[i]=recvcount;
2240     mpi_coll_reduce_scatter_fun(sendbuf, recvbuf, recvcounts, datatype,  op, comm);
2241     xbt_free(recvcounts);
2242     retval = MPI_SUCCESS;
2243
2244     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2245   }
2246
2247   smpi_bench_begin();
2248   return retval;
2249 }
2250
2251 int PMPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype,
2252                  void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
2253 {
2254   int retval = 0;
2255
2256   smpi_bench_end();
2257
2258   if (comm == MPI_COMM_NULL) {
2259     retval = MPI_ERR_COMM;
2260   } else if (sendtype == MPI_DATATYPE_NULL
2261              || recvtype == MPI_DATATYPE_NULL) {
2262     retval = MPI_ERR_TYPE;
2263   } else {
2264   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2265   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2266   extra->type = TRACING_ALLTOALL;
2267   int known=0;
2268   extra->datatype1 = encode_datatype(sendtype, &known);
2269   if(known==0)
2270     extra->send_size = sendcount*smpi_datatype_size(sendtype);
2271   else
2272     extra->send_size = sendcount;
2273   extra->datatype2 = encode_datatype(recvtype, &known);
2274   if(known==0)
2275     extra->recv_size = recvcount*smpi_datatype_size(recvtype);
2276   else
2277     extra->recv_size = recvcount;
2278   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2279
2280   retval = mpi_coll_alltoall_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
2281
2282   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2283   }
2284
2285   smpi_bench_begin();
2286   return retval;
2287 }
2288
2289 int PMPI_Alltoallv(void *sendbuf, int *sendcounts, int *senddisps,MPI_Datatype sendtype, void *recvbuf, int *recvcounts,
2290                   int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2291 {
2292   int retval = 0;
2293
2294   smpi_bench_end();
2295
2296   if (comm == MPI_COMM_NULL) {
2297     retval = MPI_ERR_COMM;
2298   } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2299     retval = MPI_ERR_TYPE;
2300   } else if (sendcounts == nullptr || senddisps == nullptr || recvcounts == nullptr || recvdisps == nullptr) {
2301     retval = MPI_ERR_ARG;
2302   } else {
2303   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2304   int i=0;
2305   int size = smpi_comm_size(comm);
2306   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2307   extra->type = TRACING_ALLTOALLV;
2308   extra->send_size = 0;
2309   extra->recv_size = 0;
2310   extra->recvcounts= xbt_new(int, size);
2311   extra->sendcounts= xbt_new(int, size);
2312   int known=0;
2313   extra->datatype1 = encode_datatype(sendtype, &known);
2314   int dt_size_send = 1;
2315   if(known==0)
2316     dt_size_send = smpi_datatype_size(sendtype);
2317   int dt_size_recv = 1;
2318   extra->datatype2 = encode_datatype(recvtype, &known);
2319   if(known==0)
2320     dt_size_recv = smpi_datatype_size(recvtype);
2321   for(i=0; i< size; i++){//copy data to avoid bad free
2322     extra->send_size += sendcounts[i]*dt_size_send;
2323     extra->recv_size += recvcounts[i]*dt_size_recv;
2324
2325     extra->sendcounts[i] = sendcounts[i]*dt_size_send;
2326     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
2327   }
2328   extra->num_processes = size;
2329   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2330
2331   retval = mpi_coll_alltoallv_fun(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype,
2332                                   comm);
2333   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2334   }
2335
2336   smpi_bench_begin();
2337   return retval;
2338 }
2339
2340
2341 int PMPI_Get_processor_name(char *name, int *resultlen)
2342 {
2343   int retval = MPI_SUCCESS;
2344
2345   strncpy(name, sg_host_get_name(SIMIX_host_self()),
2346           strlen(sg_host_get_name(SIMIX_host_self())) < MPI_MAX_PROCESSOR_NAME - 1 ?
2347           strlen(sg_host_get_name(SIMIX_host_self())) +1 : MPI_MAX_PROCESSOR_NAME - 1 );
2348   *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2349
2350   return retval;
2351 }
2352
2353 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2354 {
2355   int retval = MPI_SUCCESS;
2356   size_t size;
2357
2358   if (status == nullptr || count == nullptr) {
2359     retval = MPI_ERR_ARG;
2360   } else if (!is_datatype_valid(datatype)) {
2361     retval = MPI_ERR_TYPE;
2362   } else {
2363     size = smpi_datatype_size(datatype);
2364     if (size == 0) {
2365       *count = 0;
2366     } else if (status->count % size != 0) {
2367       retval = MPI_UNDEFINED;
2368     } else {
2369       *count = smpi_mpi_get_count(status, datatype);
2370     }
2371   }
2372   return retval;
2373 }
2374
2375 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2376   int retval = 0;
2377
2378   if (old_type == MPI_DATATYPE_NULL) {
2379     retval = MPI_ERR_TYPE;
2380   } else if (count<0){
2381     retval = MPI_ERR_COUNT;
2382   } else {
2383     retval = smpi_datatype_contiguous(count, old_type, new_type, 0);
2384   }
2385   return retval;
2386 }
2387
2388 int PMPI_Type_commit(MPI_Datatype* datatype) {
2389   int retval = 0;
2390
2391   if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2392     retval = MPI_ERR_TYPE;
2393   } else {
2394     smpi_datatype_commit(datatype);
2395     retval = MPI_SUCCESS;
2396   }
2397   return retval;
2398 }
2399
2400 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2401   int retval = 0;
2402
2403   if (old_type == MPI_DATATYPE_NULL) {
2404     retval = MPI_ERR_TYPE;
2405   } else if (count<0 || blocklen<0){
2406     retval = MPI_ERR_COUNT;
2407   } else {
2408     retval = smpi_datatype_vector(count, blocklen, stride, old_type, new_type);
2409   }
2410   return retval;
2411 }
2412
2413 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2414   int retval = 0;
2415
2416   if (old_type == MPI_DATATYPE_NULL) {
2417     retval = MPI_ERR_TYPE;
2418   } else if (count<0 || blocklen<0){
2419     retval = MPI_ERR_COUNT;
2420   } else {
2421     retval = smpi_datatype_hvector(count, blocklen, stride, old_type, new_type);
2422   }
2423   return retval;
2424 }
2425
2426 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2427   return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2428 }
2429
2430 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2431   int retval = 0;
2432
2433   if (old_type == MPI_DATATYPE_NULL) {
2434     retval = MPI_ERR_TYPE;
2435   } else if (count<0){
2436     retval = MPI_ERR_COUNT;
2437   } else {
2438     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2439   }
2440   return retval;
2441 }
2442
2443 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2444   int retval = 0;
2445
2446   if (old_type == MPI_DATATYPE_NULL) {
2447     retval = MPI_ERR_TYPE;
2448   } else if (count<0){
2449     retval = MPI_ERR_COUNT;
2450   } else {
2451     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2452   }
2453   return retval;
2454 }
2455
2456 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2457                                    MPI_Datatype* new_type) {
2458   int retval,i;
2459
2460   if (old_type == MPI_DATATYPE_NULL) {
2461     retval = MPI_ERR_TYPE;
2462   } else if (count<0){
2463     retval = MPI_ERR_COUNT;
2464   } else {
2465     int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count));
2466     for (i=0; i<count;i++)
2467       blocklens[i]=blocklength;
2468     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2469     xbt_free(blocklens);
2470   }
2471   return retval;
2472 }
2473
2474 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2475   int retval = 0;
2476
2477   if (old_type == MPI_DATATYPE_NULL) {
2478     retval = MPI_ERR_TYPE;
2479   } else if (count<0){
2480     retval = MPI_ERR_COUNT;
2481   } else {
2482     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2483   }
2484   return retval;
2485 }
2486
2487 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2488                               MPI_Datatype* new_type) {
2489   return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2490 }
2491
2492 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2493                                     MPI_Datatype* new_type) {
2494   int retval,i;
2495
2496   if (old_type == MPI_DATATYPE_NULL) {
2497     retval = MPI_ERR_TYPE;
2498   } else if (count<0){
2499     retval = MPI_ERR_COUNT;
2500   } else {
2501     int* blocklens=(int*)xbt_malloc(blocklength*count);
2502     for (i=0; i<count;i++)blocklens[i]=blocklength;
2503     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2504     xbt_free(blocklens);
2505   }
2506   return retval;
2507 }
2508
2509 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2510   int retval = 0;
2511
2512   if (count<0){
2513     retval = MPI_ERR_COUNT;
2514   } else {
2515     retval = smpi_datatype_struct(count, blocklens, indices, old_types, new_type);
2516   }
2517   return retval;
2518 }
2519
2520 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2521                             MPI_Datatype* new_type) {
2522   return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2523 }
2524
2525 int PMPI_Error_class(int errorcode, int* errorclass) {
2526   // assume smpi uses only standard mpi error codes
2527   *errorclass=errorcode;
2528   return MPI_SUCCESS;
2529 }
2530
2531 int PMPI_Initialized(int* flag) {
2532    *flag=smpi_process_initialized();
2533    return MPI_SUCCESS;
2534 }
2535
2536 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2537  * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2538  *  MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2539
2540 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2541   int retval = 0;
2542   if (comm_old == MPI_COMM_NULL){
2543     retval =  MPI_ERR_COMM;
2544   } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2545     retval = MPI_ERR_ARG;
2546   } else{
2547     retval = smpi_mpi_cart_create(comm_old, ndims, dims, periodic, reorder, comm_cart);
2548   }
2549   return retval;
2550 }
2551
2552 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2553   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2554     return MPI_ERR_TOPOLOGY;
2555   }
2556   if (coords == nullptr) {
2557     return MPI_ERR_ARG;
2558   }
2559   return smpi_mpi_cart_rank(comm, coords, rank);
2560 }
2561
2562 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2563   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2564     return MPI_ERR_TOPOLOGY;
2565   }
2566   if (source == nullptr || dest == nullptr || direction < 0 ) {
2567     return MPI_ERR_ARG;
2568   }
2569   return smpi_mpi_cart_shift(comm, direction, displ, source, dest);
2570 }
2571
2572 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2573   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2574     return MPI_ERR_TOPOLOGY;
2575   }
2576   if (rank < 0 || rank >= smpi_comm_size(comm)) {
2577     return MPI_ERR_RANK;
2578   }
2579   if (maxdims <= 0) {
2580     return MPI_ERR_ARG;
2581   }
2582   if(coords == nullptr) {
2583     return MPI_ERR_ARG;
2584   }
2585   return smpi_mpi_cart_coords(comm, rank, maxdims, coords);
2586 }
2587
2588 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2589   if(comm == nullptr || smpi_comm_topo(comm) == nullptr) {
2590     return MPI_ERR_TOPOLOGY;
2591   }
2592   if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2593     return MPI_ERR_ARG;
2594   }
2595   return smpi_mpi_cart_get(comm, maxdims, dims, periods, coords);
2596 }
2597
2598 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2599   if (comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2600     return MPI_ERR_TOPOLOGY;
2601   }
2602   if (ndims == nullptr) {
2603     return MPI_ERR_ARG;
2604   }
2605   return smpi_mpi_cartdim_get(comm, ndims);
2606 }
2607
2608 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2609   if(dims == nullptr) {
2610     return MPI_ERR_ARG;
2611   }
2612   if (ndims < 1 || nnodes < 1) {
2613     return MPI_ERR_DIMS;
2614   }
2615
2616   return smpi_mpi_dims_create(nnodes, ndims, dims);
2617 }
2618
2619 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2620   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2621     return MPI_ERR_TOPOLOGY;
2622   }
2623   if (comm_new == nullptr) {
2624     return MPI_ERR_ARG;
2625   }
2626   return smpi_mpi_cart_sub(comm, remain_dims, comm_new);
2627 }
2628
2629 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2630     if(oldtype == MPI_DATATYPE_NULL) {
2631         return MPI_ERR_TYPE;
2632     }
2633     int blocks[3] = { 1, 1, 1 };
2634     MPI_Aint disps[3] = { lb, 0, lb+extent };
2635     MPI_Datatype types[3] = { MPI_LB, oldtype, MPI_UB };
2636
2637     s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocks, disps, 3, types);
2638     smpi_datatype_create(newtype,oldtype->size, lb, lb + extent, sizeof(s_smpi_mpi_struct_t) , subtype, DT_FLAG_VECTOR);
2639
2640     (*newtype)->flags &= ~DT_FLAG_COMMITED;
2641     return MPI_SUCCESS;
2642 }
2643
2644 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2645   int retval = 0;
2646   smpi_bench_end();
2647   if (comm == MPI_COMM_NULL) {
2648     retval= MPI_ERR_COMM;
2649   }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2650     retval= MPI_ERR_OTHER;
2651   }else{
2652     *win = smpi_mpi_win_create( base, size, disp_unit, info, comm);
2653     retval = MPI_SUCCESS;
2654   }
2655   smpi_bench_begin();
2656   return retval;
2657 }
2658
2659 int PMPI_Win_free( MPI_Win* win){
2660   int retval = 0;
2661   smpi_bench_end();
2662   if (win == nullptr || *win == MPI_WIN_NULL) {
2663     retval = MPI_ERR_WIN;
2664   }else{
2665     retval=smpi_mpi_win_free(win);
2666   }
2667   smpi_bench_begin();
2668   return retval;
2669 }
2670
2671 int PMPI_Win_set_name(MPI_Win  win, char * name)
2672 {
2673   int retval = 0;
2674   if (win == MPI_WIN_NULL)  {
2675     retval = MPI_ERR_TYPE;
2676   } else if (name == nullptr)  {
2677     retval = MPI_ERR_ARG;
2678   } else {
2679     smpi_mpi_win_set_name(win, name);
2680     retval = MPI_SUCCESS;
2681   }
2682   return retval;
2683 }
2684
2685 int PMPI_Win_get_name(MPI_Win  win, char * name, int* len)
2686 {
2687   int retval = MPI_SUCCESS;
2688
2689   if (win == MPI_WIN_NULL)  {
2690     retval = MPI_ERR_WIN;
2691   } else if (name == nullptr)  {
2692     retval = MPI_ERR_ARG;
2693   } else {
2694     smpi_mpi_win_get_name(win, name, len);
2695   }
2696   return retval;
2697 }
2698
2699 int PMPI_Win_get_group(MPI_Win  win, MPI_Group * group){
2700   int retval = MPI_SUCCESS;
2701   if (win == MPI_WIN_NULL)  {
2702     retval = MPI_ERR_WIN;
2703   }else {
2704     smpi_mpi_win_get_group(win, group);
2705     smpi_group_use(*group);
2706   }
2707   return retval;
2708 }
2709
2710
2711 int PMPI_Win_fence( int assert,  MPI_Win win){
2712   int retval = 0;
2713   smpi_bench_end();
2714   if (win == MPI_WIN_NULL) {
2715     retval = MPI_ERR_WIN;
2716   } else {
2717   int rank = smpi_process_index();
2718   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2719   retval = smpi_mpi_win_fence(assert, win);
2720   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2721   }
2722   smpi_bench_begin();
2723   return retval;
2724 }
2725
2726 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2727               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2728   int retval = 0;
2729   smpi_bench_end();
2730   if (win == MPI_WIN_NULL) {
2731     retval = MPI_ERR_WIN;
2732   } else if (target_rank == MPI_PROC_NULL) {
2733     retval = MPI_SUCCESS;
2734   } else if (target_rank <0){
2735     retval = MPI_ERR_RANK;
2736   } else if (target_disp <0){
2737       retval = MPI_ERR_ARG;
2738   } else if ((origin_count < 0 || target_count < 0) ||
2739              (origin_addr==nullptr && origin_count > 0)){
2740     retval = MPI_ERR_COUNT;
2741   } else if ((!is_datatype_valid(origin_datatype)) || (!is_datatype_valid(target_datatype))) {
2742     retval = MPI_ERR_TYPE;
2743   } else {
2744     int rank = smpi_process_index();
2745     MPI_Group group;
2746     smpi_mpi_win_get_group(win, &group);
2747     int src_traced = smpi_group_index(group, target_rank);
2748     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2749
2750     retval = smpi_mpi_get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2751                            target_datatype, win);
2752
2753     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2754   }
2755   smpi_bench_begin();
2756   return retval;
2757 }
2758
2759 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2760               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2761   int retval = 0;
2762   smpi_bench_end();
2763   if (win == MPI_WIN_NULL) {
2764     retval = MPI_ERR_WIN;
2765   } else if (target_rank == MPI_PROC_NULL) {
2766     retval = MPI_SUCCESS;
2767   } else if (target_rank <0){
2768     retval = MPI_ERR_RANK;
2769   } else if (target_disp <0){
2770     retval = MPI_ERR_ARG;
2771   } else if ((origin_count < 0 || target_count < 0) ||
2772             (origin_addr==nullptr && origin_count > 0)){
2773     retval = MPI_ERR_COUNT;
2774   } else if ((!is_datatype_valid(origin_datatype)) || (!is_datatype_valid(target_datatype))) {
2775     retval = MPI_ERR_TYPE;
2776   } else {
2777     int rank = smpi_process_index();
2778     MPI_Group group;
2779     smpi_mpi_win_get_group(win, &group);
2780     int dst_traced = smpi_group_index(group, target_rank);
2781     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2782     TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*smpi_datatype_size(origin_datatype));
2783
2784     retval = smpi_mpi_put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2785                            target_datatype, win);
2786
2787     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2788   }
2789   smpi_bench_begin();
2790   return retval;
2791 }
2792
2793 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2794               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2795   int retval = 0;
2796   smpi_bench_end();
2797   if (win == MPI_WIN_NULL) {
2798     retval = MPI_ERR_WIN;
2799   } else if (target_rank == MPI_PROC_NULL) {
2800     retval = MPI_SUCCESS;
2801   } else if (target_rank <0){
2802     retval = MPI_ERR_RANK;
2803   } else if (target_disp <0){
2804     retval = MPI_ERR_ARG;
2805   } else if ((origin_count < 0 || target_count < 0) ||
2806              (origin_addr==nullptr && origin_count > 0)){
2807     retval = MPI_ERR_COUNT;
2808   } else if ((!is_datatype_valid(origin_datatype)) ||
2809             (!is_datatype_valid(target_datatype))) {
2810     retval = MPI_ERR_TYPE;
2811   } else if (op == MPI_OP_NULL) {
2812     retval = MPI_ERR_OP;
2813   } else {
2814     int rank = smpi_process_index();
2815     MPI_Group group;
2816     smpi_mpi_win_get_group(win, &group);
2817     int src_traced = smpi_group_index(group, target_rank);
2818     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2819
2820     retval = smpi_mpi_accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2821                                   target_datatype, op, win);
2822
2823     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2824   }
2825   smpi_bench_begin();
2826   return retval;
2827 }
2828
2829 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2830   int retval = 0;
2831   smpi_bench_end();
2832   if (win == MPI_WIN_NULL) {
2833     retval = MPI_ERR_WIN;
2834   } else if (group==MPI_GROUP_NULL){
2835     retval = MPI_ERR_GROUP;
2836   }
2837   else {
2838     int rank = smpi_process_index();
2839     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2840     retval = smpi_mpi_win_post(group,assert,win);
2841     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2842   }
2843   smpi_bench_begin();
2844   return retval;
2845 }
2846
2847 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2848   int retval = 0;
2849   smpi_bench_end();
2850   if (win == MPI_WIN_NULL) {
2851     retval = MPI_ERR_WIN;
2852   } else if (group==MPI_GROUP_NULL){
2853     retval = MPI_ERR_GROUP;
2854   }
2855   else {
2856     int rank = smpi_process_index();
2857     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2858     retval = smpi_mpi_win_start(group,assert,win);
2859     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2860   }
2861   smpi_bench_begin();
2862   return retval;
2863 }
2864
2865 int PMPI_Win_complete(MPI_Win win){
2866   int retval = 0;
2867   smpi_bench_end();
2868   if (win == MPI_WIN_NULL) {
2869     retval = MPI_ERR_WIN;
2870   }
2871   else {
2872     int rank = smpi_process_index();
2873     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2874
2875     retval = smpi_mpi_win_complete(win);
2876
2877     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2878   }
2879   smpi_bench_begin();
2880   return retval;
2881 }
2882
2883 int PMPI_Win_wait(MPI_Win win){
2884   int retval = 0;
2885   smpi_bench_end();
2886   if (win == MPI_WIN_NULL) {
2887     retval = MPI_ERR_WIN;
2888   }
2889   else {
2890     int rank = smpi_process_index();
2891     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2892
2893     retval = smpi_mpi_win_wait(win);
2894
2895     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2896   }
2897   smpi_bench_begin();
2898   return retval;
2899 }
2900
2901 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
2902   void *ptr = xbt_malloc(size);
2903   if(ptr==nullptr)
2904     return MPI_ERR_NO_MEM;
2905   else {
2906     *static_cast<void**>(baseptr) = ptr;
2907     return MPI_SUCCESS;
2908   }
2909 }
2910
2911 int PMPI_Free_mem(void *baseptr){
2912   xbt_free(baseptr);
2913   return MPI_SUCCESS;
2914 }
2915
2916 int PMPI_Type_set_name(MPI_Datatype  datatype, char * name)
2917 {
2918   int retval = 0;
2919   if (datatype == MPI_DATATYPE_NULL)  {
2920     retval = MPI_ERR_TYPE;
2921   } else if (name == nullptr)  {
2922     retval = MPI_ERR_ARG;
2923   } else {
2924     smpi_datatype_set_name(datatype, name);
2925     retval = MPI_SUCCESS;
2926   }
2927   return retval;
2928 }
2929
2930 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
2931 {
2932   int retval = 0;
2933
2934   if (datatype == MPI_DATATYPE_NULL)  {
2935     retval = MPI_ERR_TYPE;
2936   } else if (name == nullptr)  {
2937     retval = MPI_ERR_ARG;
2938   } else {
2939     smpi_datatype_get_name(datatype, name, len);
2940     retval = MPI_SUCCESS;
2941   }
2942   return retval;
2943 }
2944
2945 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
2946   return smpi_type_f2c(datatype);
2947 }
2948
2949 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
2950   return smpi_type_c2f( datatype);
2951 }
2952
2953 MPI_Group PMPI_Group_f2c(MPI_Fint group){
2954   return smpi_group_f2c( group);
2955 }
2956
2957 MPI_Fint PMPI_Group_c2f(MPI_Group group){
2958   return smpi_group_c2f(group);
2959 }
2960
2961 MPI_Request PMPI_Request_f2c(MPI_Fint request){
2962   return smpi_request_f2c(request);
2963 }
2964
2965 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
2966   return smpi_request_c2f(request);
2967 }
2968
2969 MPI_Win PMPI_Win_f2c(MPI_Fint win){
2970   return smpi_win_f2c(win);
2971 }
2972
2973 MPI_Fint PMPI_Win_c2f(MPI_Win win){
2974   return smpi_win_c2f(win);
2975 }
2976
2977 MPI_Op PMPI_Op_f2c(MPI_Fint op){
2978   return smpi_op_f2c(op);
2979 }
2980
2981 MPI_Fint PMPI_Op_c2f(MPI_Op op){
2982   return smpi_op_c2f(op);
2983 }
2984
2985 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
2986   return smpi_comm_f2c(comm);
2987 }
2988
2989 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
2990   return smpi_comm_c2f(comm);
2991 }
2992
2993 MPI_Info PMPI_Info_f2c(MPI_Fint info){
2994   return smpi_info_f2c(info);
2995 }
2996
2997 MPI_Fint PMPI_Info_c2f(MPI_Info info){
2998   return smpi_info_c2f(info);
2999 }
3000
3001 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
3002   return smpi_comm_keyval_create(copy_fn, delete_fn, keyval, extra_state);
3003 }
3004
3005 int PMPI_Keyval_free(int* keyval) {
3006   return smpi_comm_keyval_free(keyval);
3007 }
3008
3009 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3010   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3011        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3012     return MPI_ERR_ARG;
3013   else if (comm==MPI_COMM_NULL)
3014     return MPI_ERR_COMM;
3015   else
3016     return smpi_comm_attr_delete(comm, keyval);
3017 }
3018
3019 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3020   static int one = 1;
3021   static int zero = 0;
3022   static int tag_ub = 1000000;
3023   static int last_used_code = MPI_ERR_LASTCODE;
3024
3025   if (comm==MPI_COMM_NULL){
3026     *flag = 0;
3027     return MPI_ERR_COMM;
3028   }
3029
3030   switch (keyval) {
3031   case MPI_HOST:
3032   case MPI_IO:
3033   case MPI_APPNUM:
3034     *flag = 1;
3035     *static_cast<int**>(attr_value) = &zero;
3036     return MPI_SUCCESS;
3037   case MPI_UNIVERSE_SIZE:
3038     *flag = 1;
3039     *static_cast<int**>(attr_value) = &smpi_universe_size;
3040     return MPI_SUCCESS;
3041   case MPI_LASTUSEDCODE:
3042     *flag = 1;
3043     *static_cast<int**>(attr_value) = &last_used_code;
3044     return MPI_SUCCESS;
3045   case MPI_TAG_UB:
3046     *flag=1;
3047     *static_cast<int**>(attr_value) = &tag_ub;
3048     return MPI_SUCCESS;
3049   case MPI_WTIME_IS_GLOBAL:
3050     *flag = 1;
3051     *static_cast<int**>(attr_value) = &one;
3052     return MPI_SUCCESS;
3053   default:
3054     return smpi_comm_attr_get(comm, keyval, attr_value, flag);
3055   }
3056 }
3057
3058 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3059   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3060        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3061     return MPI_ERR_ARG;
3062   else if (comm==MPI_COMM_NULL)
3063     return MPI_ERR_COMM;
3064   else
3065   return smpi_comm_attr_put(comm, keyval, attr_value);
3066 }
3067
3068 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3069 {
3070   return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3071 }
3072
3073 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3074 {
3075   return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3076 }
3077
3078 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3079 {
3080   return PMPI_Attr_delete(comm, comm_keyval);
3081 }
3082
3083 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3084                             void* extra_state)
3085 {
3086   return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3087 }
3088
3089 int PMPI_Comm_free_keyval(int* keyval) {
3090   return PMPI_Keyval_free(keyval);
3091 }
3092
3093 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3094 {
3095   if (type==MPI_DATATYPE_NULL)
3096     return MPI_ERR_TYPE;
3097   else
3098     return smpi_type_attr_get(type, type_keyval, attribute_val, flag);
3099 }
3100
3101 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3102 {
3103   if (type==MPI_DATATYPE_NULL)
3104     return MPI_ERR_TYPE;
3105   else
3106     return smpi_type_attr_put(type, type_keyval, attribute_val);
3107 }
3108
3109 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3110 {
3111   if (type==MPI_DATATYPE_NULL)
3112     return MPI_ERR_TYPE;
3113   else
3114     return smpi_type_attr_delete(type, type_keyval);
3115 }
3116
3117 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3118                             void* extra_state)
3119 {
3120   return smpi_type_keyval_create(copy_fn, delete_fn, keyval, extra_state);
3121 }
3122
3123 int PMPI_Type_free_keyval(int* keyval) {
3124   return smpi_type_keyval_free(keyval);
3125 }
3126
3127 int PMPI_Info_create( MPI_Info *info){
3128   if (info == nullptr)
3129     return MPI_ERR_ARG;
3130   *info = xbt_new(s_smpi_mpi_info_t, 1);
3131   (*info)->info_dict= xbt_dict_new_homogeneous(xbt_free_f);
3132   (*info)->refcount=1;
3133   return MPI_SUCCESS;
3134 }
3135
3136 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3137   if (info == nullptr || key == nullptr || value == nullptr)
3138     return MPI_ERR_ARG;
3139
3140   xbt_dict_set(info->info_dict, key, xbt_strdup(value), nullptr);
3141   return MPI_SUCCESS;
3142 }
3143
3144 int PMPI_Info_free( MPI_Info *info){
3145   if (info == nullptr || *info==nullptr)
3146     return MPI_ERR_ARG;
3147   (*info)->refcount--;
3148   if((*info)->refcount==0){
3149     xbt_dict_free(&((*info)->info_dict));
3150     xbt_free(*info);
3151   }
3152   *info=MPI_INFO_NULL;
3153   return MPI_SUCCESS;
3154 }
3155
3156 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3157   *flag=false;
3158   if (info == nullptr || key == nullptr || valuelen <0)
3159     return MPI_ERR_ARG;
3160   if (value == nullptr)
3161     return MPI_ERR_INFO_VALUE;
3162   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(info->info_dict, key));
3163   if(tmpvalue){
3164     memset(value, 0, valuelen);
3165     memcpy(value,tmpvalue, (strlen(tmpvalue) + 1 < static_cast<size_t>(valuelen)) ? strlen(tmpvalue) + 1 : valuelen);
3166     *flag=true;
3167   }
3168   return MPI_SUCCESS;
3169 }
3170
3171 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3172   if (info == nullptr || newinfo==nullptr)
3173     return MPI_ERR_ARG;
3174   *newinfo = xbt_new(s_smpi_mpi_info_t, 1);
3175   (*newinfo)->info_dict= xbt_dict_new_homogeneous(xbt_free_f);
3176   (*newinfo)->refcount=1;
3177   xbt_dict_cursor_t cursor = nullptr;
3178   int *key;
3179   void* data;
3180   xbt_dict_foreach(info->info_dict,cursor,key,data){
3181     xbt_dict_set((*newinfo)->info_dict, reinterpret_cast<char*>(key), xbt_strdup(reinterpret_cast<char*>(data)), nullptr);
3182   }
3183   return MPI_SUCCESS;
3184 }
3185
3186 int PMPI_Info_delete(MPI_Info info, char *key){
3187   if (info == nullptr || key==nullptr)
3188     return MPI_ERR_ARG;
3189   try {
3190     xbt_dict_remove(info->info_dict, key);
3191   }
3192   catch(xbt_ex& e){
3193     return MPI_ERR_INFO_NOKEY;
3194   }
3195   return MPI_SUCCESS;
3196 }
3197
3198 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3199   if (info == nullptr || nkeys==nullptr)
3200     return MPI_ERR_ARG;
3201   *nkeys=xbt_dict_size(info->info_dict);
3202   return MPI_SUCCESS;
3203 }
3204
3205 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3206   if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3207     return MPI_ERR_ARG;
3208
3209   xbt_dict_cursor_t cursor = nullptr;
3210   char *keyn;
3211   void* data;
3212   int num=0;
3213   xbt_dict_foreach(info->info_dict,cursor,keyn,data){
3214     if(num==n){
3215       strncpy(key,keyn,strlen(keyn)+1);
3216       xbt_dict_cursor_free(&cursor);
3217       return MPI_SUCCESS;
3218     }
3219     num++;
3220   }
3221   return MPI_ERR_ARG;
3222 }
3223
3224 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3225   *flag=false;
3226   if (info == nullptr || key == nullptr || valuelen==nullptr)
3227     return MPI_ERR_ARG;
3228   char* tmpvalue=(char*)xbt_dict_get_or_null(info->info_dict, key);
3229   if(tmpvalue){
3230     *valuelen=strlen(tmpvalue);
3231     *flag=true;
3232   }
3233   return MPI_SUCCESS;
3234 }
3235
3236 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3237   if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3238     return MPI_ERR_ARG;
3239   if(!is_datatype_valid(type))
3240     return MPI_ERR_TYPE;
3241   if(comm==MPI_COMM_NULL)
3242     return MPI_ERR_COMM;
3243   return smpi_mpi_unpack(inbuf, incount, position, outbuf,outcount,type, comm);
3244 }
3245
3246 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3247   if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3248     return MPI_ERR_ARG;
3249   if(!is_datatype_valid(type))
3250     return MPI_ERR_TYPE;
3251   if(comm==MPI_COMM_NULL)
3252     return MPI_ERR_COMM;
3253   return smpi_mpi_pack(inbuf, incount, type, outbuf,outcount,position, comm);
3254 }
3255
3256 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3257   if(incount<0)
3258     return MPI_ERR_ARG;
3259   if(!is_datatype_valid(datatype))
3260     return MPI_ERR_TYPE;
3261   if(comm==MPI_COMM_NULL)
3262     return MPI_ERR_COMM;
3263
3264   *size=incount*smpi_datatype_size(datatype);
3265
3266   return MPI_SUCCESS;
3267 }
3268