Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a useless function
[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, 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, 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);
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,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,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,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);
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     MPI_Comm comm = (*request)->comm;
1455     int is_wait_for_receive = (*request)->recv;
1456     instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1457     extra->type = TRACING_WAIT;
1458     TRACE_smpi_ptp_in(rank, src_traced, dst_traced, __FUNCTION__, extra);
1459
1460     smpi_mpi_wait(request, status);
1461     retval = MPI_SUCCESS;
1462
1463     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1464     TRACE_smpi_ptp_out(rank, src_traced, dst_traced, __FUNCTION__);
1465     if (is_wait_for_receive) {
1466       if(src_traced==MPI_ANY_SOURCE)
1467         src_traced = (status!=MPI_STATUS_IGNORE) ?
1468           smpi_group_rank(smpi_comm_group(comm), status->MPI_SOURCE) :
1469           src_traced;
1470       TRACE_smpi_recv(rank, src_traced, dst_traced);
1471     }
1472   }
1473
1474   smpi_bench_begin();
1475   return retval;
1476 }
1477
1478 int PMPI_Waitany(int count, MPI_Request requests[], int *index, MPI_Status * status)
1479 {
1480   if (index == nullptr)
1481     return MPI_ERR_ARG;
1482
1483   smpi_bench_end();
1484   //save requests information for tracing
1485   int i;
1486   int *srcs = nullptr, *dsts = nullptr, *recvs = nullptr;
1487   MPI_Comm* comms = nullptr;
1488   if(count>0){
1489     srcs = xbt_new0(int, count);
1490     dsts = xbt_new0(int, count);
1491     recvs = xbt_new0(int, count);
1492     comms = xbt_new0(MPI_Comm, count);
1493   }
1494   for (i = 0; i < count; i++) {
1495     MPI_Request req = requests[i];      //already received requests are no longer valid
1496     if (req) {
1497       srcs[i] = req->src;
1498       dsts[i] = req->dst;
1499       recvs[i] = req->recv;
1500       comms[i] = req->comm;
1501     }
1502   }
1503   int rank_traced = smpi_process_index();
1504   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1505   extra->type = TRACING_WAITANY;
1506   extra->send_size=count;
1507   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1508
1509   *index = smpi_mpi_waitany(count, requests, status);
1510
1511   if(*index!=MPI_UNDEFINED){
1512     int src_traced = srcs[*index];
1513     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1514     int dst_traced = dsts[*index];
1515     int is_wait_for_receive = recvs[*index];
1516     if (is_wait_for_receive) {
1517       if(srcs[*index]==MPI_ANY_SOURCE)
1518         src_traced = (status!=MPI_STATUSES_IGNORE) ?
1519                       smpi_group_rank(smpi_comm_group(comms[*index]), status->MPI_SOURCE) : srcs[*index];
1520       TRACE_smpi_recv(rank_traced, src_traced, dst_traced);
1521     }
1522     TRACE_smpi_ptp_out(rank_traced, src_traced, dst_traced, __FUNCTION__);
1523   }
1524   xbt_free(srcs);
1525   xbt_free(dsts);
1526   xbt_free(recvs);
1527   xbt_free(comms);
1528
1529
1530   smpi_bench_begin();
1531   return MPI_SUCCESS;
1532 }
1533
1534 int PMPI_Waitall(int count, MPI_Request requests[], MPI_Status status[])
1535 {
1536   smpi_bench_end();
1537   //save information from requests
1538   int i;
1539   int *srcs = xbt_new0(int, count);
1540   int *dsts = xbt_new0(int, count);
1541   int *recvs = xbt_new0(int, count);
1542   int *valid = xbt_new0(int, count);
1543   MPI_Comm *comms = xbt_new0(MPI_Comm, count);
1544
1545   for (i = 0; i < count; i++) {
1546     MPI_Request req = requests[i];
1547     if(req!=MPI_REQUEST_NULL){
1548       srcs[i] = req->src;
1549       dsts[i] = req->dst;
1550       recvs[i] = req->recv;
1551       comms[i] = req->comm;
1552       valid[i]=1;;
1553     }else{
1554       valid[i]=0;
1555     }
1556   }
1557   int rank_traced = smpi_process_index();
1558   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1559   extra->type = TRACING_WAITALL;
1560   extra->send_size=count;
1561   TRACE_smpi_ptp_in(rank_traced, -1, -1, __FUNCTION__,extra);
1562
1563   int retval = smpi_mpi_waitall(count, requests, status);
1564
1565   for (i = 0; i < count; i++) {
1566     if(valid[i]){
1567     //the src may not have been known at the beginning of the recv (MPI_ANY_SOURCE)
1568       int src_traced = srcs[i];
1569       int dst_traced = dsts[i];
1570       int is_wait_for_receive = recvs[i];
1571       if (is_wait_for_receive) {
1572         if(src_traced==MPI_ANY_SOURCE)
1573         src_traced = (status!=MPI_STATUSES_IGNORE) ?
1574                           smpi_group_rank(smpi_comm_group(comms[i]), status[i].MPI_SOURCE) : srcs[i];
1575         TRACE_smpi_recv(rank_traced, src_traced, dst_traced);
1576       }
1577     }
1578   }
1579   TRACE_smpi_ptp_out(rank_traced, -1, -1, __FUNCTION__);
1580   xbt_free(srcs);
1581   xbt_free(dsts);
1582   xbt_free(recvs);
1583   xbt_free(valid);
1584   xbt_free(comms);
1585
1586   smpi_bench_begin();
1587   return retval;
1588 }
1589
1590 int PMPI_Waitsome(int incount, MPI_Request requests[], int *outcount, int *indices, MPI_Status status[])
1591 {
1592   int retval = 0;
1593
1594   smpi_bench_end();
1595   if (outcount == nullptr) {
1596     retval = MPI_ERR_ARG;
1597   } else {
1598     *outcount = smpi_mpi_waitsome(incount, requests, indices, status);
1599     retval = MPI_SUCCESS;
1600   }
1601   smpi_bench_begin();
1602   return retval;
1603 }
1604
1605 int PMPI_Testsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[])
1606 {
1607   int retval = 0;
1608
1609    smpi_bench_end();
1610    if (outcount == nullptr) {
1611      retval = MPI_ERR_ARG;
1612    } else {
1613      *outcount = smpi_mpi_testsome(incount, requests, indices, status);
1614      retval = MPI_SUCCESS;
1615    }
1616    smpi_bench_begin();
1617    return retval;
1618 }
1619
1620
1621 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
1622 {
1623   int retval = 0;
1624
1625   smpi_bench_end();
1626
1627   if (comm == MPI_COMM_NULL) {
1628     retval = MPI_ERR_COMM;
1629   } else if (!is_datatype_valid(datatype)) {
1630       retval = MPI_ERR_ARG;
1631   } else {
1632   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1633   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1634
1635   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1636   extra->type = TRACING_BCAST;
1637   extra->root = root_traced;
1638   int known=0;
1639   extra->datatype1 = encode_datatype(datatype, &known);
1640   int dt_size_send = 1;
1641   if(known==0)
1642     dt_size_send = smpi_datatype_size(datatype);
1643   extra->send_size = count*dt_size_send;
1644   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1645   if(smpi_comm_size(comm)>1)
1646     mpi_coll_bcast_fun(buf, count, datatype, root, comm);
1647   retval = MPI_SUCCESS;
1648
1649   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1650   }
1651
1652   smpi_bench_begin();
1653   return retval;
1654 }
1655
1656 int PMPI_Barrier(MPI_Comm comm)
1657 {
1658   int retval = 0;
1659
1660   smpi_bench_end();
1661
1662   if (comm == MPI_COMM_NULL) {
1663     retval = MPI_ERR_COMM;
1664   } else {
1665   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1666   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1667   extra->type = TRACING_BARRIER;
1668   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1669
1670   mpi_coll_barrier_fun(comm);
1671   retval = MPI_SUCCESS;
1672
1673   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1674   }
1675
1676   smpi_bench_begin();
1677   return retval;
1678 }
1679
1680 int PMPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
1681                 int root, MPI_Comm comm)
1682 {
1683   int retval = 0;
1684
1685   smpi_bench_end();
1686
1687   if (comm == MPI_COMM_NULL) {
1688     retval = MPI_ERR_COMM;
1689   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1690             ((smpi_comm_rank(comm) == root) && (recvtype == MPI_DATATYPE_NULL))){
1691     retval = MPI_ERR_TYPE;
1692   } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) || ((smpi_comm_rank(comm) == root) && (recvcount <0))){
1693     retval = MPI_ERR_COUNT;
1694   } else {
1695
1696     char* sendtmpbuf = static_cast<char*>(sendbuf);
1697     int sendtmpcount = sendcount;
1698     MPI_Datatype sendtmptype = sendtype;
1699     if( (smpi_comm_rank(comm) == root) && (sendbuf == MPI_IN_PLACE )) {
1700       sendtmpcount=0;
1701       sendtmptype=recvtype;
1702     }
1703   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1704   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1705   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1706   extra->type = TRACING_GATHER;
1707   extra->root = root_traced;
1708   int known=0;
1709   extra->datatype1 = encode_datatype(sendtmptype, &known);
1710   int dt_size_send = 1;
1711   if(known==0)
1712     dt_size_send = smpi_datatype_size(sendtmptype);
1713   extra->send_size = sendtmpcount*dt_size_send;
1714   extra->datatype2 = encode_datatype(recvtype, &known);
1715   int dt_size_recv = 1;
1716   if((smpi_comm_rank(comm)==root) && known==0)
1717     dt_size_recv = smpi_datatype_size(recvtype);
1718   extra->recv_size = recvcount*dt_size_recv;
1719
1720   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__, extra);
1721
1722   mpi_coll_gather_fun(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcount, recvtype, root, comm);
1723
1724   retval = MPI_SUCCESS;
1725   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1726   }
1727
1728   smpi_bench_begin();
1729   return retval;
1730 }
1731
1732 int PMPI_Gatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int *recvcounts, int *displs,
1733                 MPI_Datatype recvtype, int root, MPI_Comm comm)
1734 {
1735   int retval = 0;
1736
1737   smpi_bench_end();
1738
1739   if (comm == MPI_COMM_NULL) {
1740     retval = MPI_ERR_COMM;
1741   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1742             ((smpi_comm_rank(comm) == root) && (recvtype == MPI_DATATYPE_NULL))){
1743     retval = MPI_ERR_TYPE;
1744   } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1745     retval = MPI_ERR_COUNT;
1746   } else if (recvcounts == nullptr || displs == nullptr) {
1747     retval = MPI_ERR_ARG;
1748   } else {
1749     char* sendtmpbuf = static_cast<char*>(sendbuf);
1750     int sendtmpcount = sendcount;
1751     MPI_Datatype sendtmptype = sendtype;
1752     if( (smpi_comm_rank(comm) == root) && (sendbuf == MPI_IN_PLACE )) {
1753       sendtmpcount=0;
1754       sendtmptype=recvtype;
1755     }
1756
1757   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1758   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1759   int i=0;
1760   int size = smpi_comm_size(comm);
1761   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1762   extra->type = TRACING_GATHERV;
1763   extra->num_processes = size;
1764   extra->root = root_traced;
1765   int known=0;
1766   extra->datatype1 = encode_datatype(sendtmptype, &known);
1767   int dt_size_send = 1;
1768   if(known==0)
1769     dt_size_send = smpi_datatype_size(sendtype);
1770   extra->send_size = sendtmpcount*dt_size_send;
1771   extra->datatype2 = encode_datatype(recvtype, &known);
1772   int dt_size_recv = 1;
1773   if(known==0)
1774     dt_size_recv = smpi_datatype_size(recvtype);
1775   if((smpi_comm_rank(comm)==root)){
1776   extra->recvcounts= xbt_new(int,size);
1777   for(i=0; i< size; i++)//copy data to avoid bad free
1778     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
1779   }
1780   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
1781
1782   smpi_mpi_gatherv(sendtmpbuf, sendtmpcount, sendtmptype, recvbuf, recvcounts, displs, recvtype, root, comm);
1783     retval = MPI_SUCCESS;
1784   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1785   }
1786
1787   smpi_bench_begin();
1788   return retval;
1789 }
1790
1791 int PMPI_Allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1792                    void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
1793 {
1794   int retval = 0;
1795
1796   smpi_bench_end();
1797
1798   if (comm == MPI_COMM_NULL) {
1799     retval = MPI_ERR_COMM;
1800   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1801             (recvtype == MPI_DATATYPE_NULL)){
1802     retval = MPI_ERR_TYPE;
1803   } else if ((( sendbuf != MPI_IN_PLACE) && (sendcount <0)) ||
1804             (recvcount <0)){
1805     retval = MPI_ERR_COUNT;
1806   } else {
1807     if(sendbuf == MPI_IN_PLACE) {
1808       sendbuf=static_cast<char*>(recvbuf)+smpi_datatype_get_extent(recvtype)*recvcount*smpi_comm_rank(comm);
1809       sendcount=recvcount;
1810       sendtype=recvtype;
1811     }
1812   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1813   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1814   extra->type = TRACING_ALLGATHER;
1815   int known=0;
1816   extra->datatype1 = encode_datatype(sendtype, &known);
1817   int dt_size_send = 1;
1818   if(known==0)
1819     dt_size_send = smpi_datatype_size(sendtype);
1820   extra->send_size = sendcount*dt_size_send;
1821   extra->datatype2 = encode_datatype(recvtype, &known);
1822   int dt_size_recv = 1;
1823   if(known==0)
1824     dt_size_recv = smpi_datatype_size(recvtype);
1825   extra->recv_size = recvcount*dt_size_recv;
1826
1827   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, extra);
1828
1829   mpi_coll_allgather_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1830     retval = MPI_SUCCESS;
1831   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1832   }
1833   smpi_bench_begin();
1834   return retval;
1835 }
1836
1837 int PMPI_Allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1838                    void *recvbuf, int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
1839 {
1840   int retval = 0;
1841
1842   smpi_bench_end();
1843
1844   if (comm == MPI_COMM_NULL) {
1845     retval = MPI_ERR_COMM;
1846   } else if ((( sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
1847             (recvtype == MPI_DATATYPE_NULL)){
1848     retval = MPI_ERR_TYPE;
1849   } else if (( sendbuf != MPI_IN_PLACE) && (sendcount <0)){
1850     retval = MPI_ERR_COUNT;
1851   } else if (recvcounts == nullptr || displs == nullptr) {
1852     retval = MPI_ERR_ARG;
1853   } else {
1854
1855     if(sendbuf == MPI_IN_PLACE) {
1856       sendbuf=static_cast<char*>(recvbuf)+smpi_datatype_get_extent(recvtype)*displs[smpi_comm_rank(comm)];
1857       sendcount=recvcounts[smpi_comm_rank(comm)];
1858       sendtype=recvtype;
1859     }
1860   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1861   int i=0;
1862   int size = smpi_comm_size(comm);
1863   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1864   extra->type = TRACING_ALLGATHERV;
1865   extra->num_processes = size;
1866   int known=0;
1867   extra->datatype1 = encode_datatype(sendtype, &known);
1868   int dt_size_send = 1;
1869   if(known==0)
1870     dt_size_send = smpi_datatype_size(sendtype);
1871   extra->send_size = sendcount*dt_size_send;
1872   extra->datatype2 = encode_datatype(recvtype, &known);
1873   int dt_size_recv = 1;
1874   if(known==0)
1875     dt_size_recv = smpi_datatype_size(recvtype);
1876   extra->recvcounts= xbt_new(int, size);
1877   for(i=0; i< size; i++)//copy data to avoid bad free
1878     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
1879
1880   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
1881
1882     mpi_coll_allgatherv_fun(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1883     retval = MPI_SUCCESS;
1884   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
1885   }
1886
1887   smpi_bench_begin();
1888   return retval;
1889 }
1890
1891 int PMPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
1892                 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1893 {
1894   int retval = 0;
1895
1896   smpi_bench_end();
1897
1898   if (comm == MPI_COMM_NULL) {
1899     retval = MPI_ERR_COMM;
1900   } else if (((smpi_comm_rank(comm)==root) && (!is_datatype_valid(sendtype)))
1901              || ((recvbuf !=MPI_IN_PLACE) && (!is_datatype_valid(recvtype)))){
1902     retval = MPI_ERR_TYPE;
1903   } else if ((sendbuf == recvbuf) ||
1904       ((smpi_comm_rank(comm)==root) && sendcount>0 && (sendbuf == nullptr))){
1905     retval = MPI_ERR_BUFFER;
1906   }else {
1907
1908     if (recvbuf == MPI_IN_PLACE) {
1909         recvtype=sendtype;
1910         recvcount=sendcount;
1911     }
1912   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1913   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1914   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1915   extra->type = TRACING_SCATTER;
1916   extra->root = root_traced;
1917   int known=0;
1918   extra->datatype1 = encode_datatype(sendtype, &known);
1919   int dt_size_send = 1;
1920   if((smpi_comm_rank(comm)==root) && known==0)
1921     dt_size_send = smpi_datatype_size(sendtype);
1922   extra->send_size = sendcount*dt_size_send;
1923   extra->datatype2 = encode_datatype(recvtype, &known);
1924   int dt_size_recv = 1;
1925   if(known==0)
1926     dt_size_recv = smpi_datatype_size(recvtype);
1927   extra->recv_size = recvcount*dt_size_recv;
1928   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
1929
1930   mpi_coll_scatter_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1931     retval = MPI_SUCCESS;
1932   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1933   }
1934
1935   smpi_bench_begin();
1936   return retval;
1937 }
1938
1939 int PMPI_Scatterv(void *sendbuf, int *sendcounts, int *displs,
1940                  MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
1941 {
1942   int retval = 0;
1943
1944   smpi_bench_end();
1945
1946   if (comm == MPI_COMM_NULL) {
1947     retval = MPI_ERR_COMM;
1948   } else if (sendcounts == nullptr || displs == nullptr) {
1949     retval = MPI_ERR_ARG;
1950   } else if (((smpi_comm_rank(comm)==root) && (sendtype == MPI_DATATYPE_NULL))
1951              || ((recvbuf !=MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL))) {
1952     retval = MPI_ERR_TYPE;
1953   } else {
1954     if (recvbuf == MPI_IN_PLACE) {
1955         recvtype=sendtype;
1956         recvcount=sendcounts[smpi_comm_rank(comm)];
1957     }
1958   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
1959   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
1960   int i=0;
1961   int size = smpi_comm_size(comm);
1962   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
1963   extra->type = TRACING_SCATTERV;
1964   extra->num_processes = size;
1965   extra->root = root_traced;
1966   int known=0;
1967   extra->datatype1 = encode_datatype(sendtype, &known);
1968   int dt_size_send = 1;
1969   if(known==0)
1970     dt_size_send = smpi_datatype_size(sendtype);
1971   if((smpi_comm_rank(comm)==root)){
1972   extra->sendcounts= xbt_new(int, size);
1973   for(i=0; i< size; i++)//copy data to avoid bad free
1974     extra->sendcounts[i] = sendcounts[i]*dt_size_send;
1975   }
1976   extra->datatype2 = encode_datatype(recvtype, &known);
1977   int dt_size_recv = 1;
1978   if(known==0)
1979     dt_size_recv = smpi_datatype_size(recvtype);
1980   extra->recv_size = recvcount*dt_size_recv;
1981   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
1982
1983     smpi_mpi_scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1984
1985     retval = MPI_SUCCESS;
1986   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
1987   }
1988
1989   smpi_bench_begin();
1990   return retval;
1991 }
1992
1993 int PMPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
1994 {
1995   int retval = 0;
1996
1997   smpi_bench_end();
1998
1999   if (comm == MPI_COMM_NULL) {
2000     retval = MPI_ERR_COMM;
2001   } else if (!is_datatype_valid(datatype) || op == MPI_OP_NULL) {
2002     retval = MPI_ERR_ARG;
2003   } else {
2004   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2005   int root_traced = smpi_group_index(smpi_comm_group(comm), root);
2006   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2007   extra->type = TRACING_REDUCE;
2008   int known=0;
2009   extra->datatype1 = encode_datatype(datatype, &known);
2010   int dt_size_send = 1;
2011   if(known==0)
2012     dt_size_send = smpi_datatype_size(datatype);
2013   extra->send_size = count*dt_size_send;
2014   extra->root = root_traced;
2015
2016   TRACE_smpi_collective_in(rank, root_traced, __FUNCTION__,extra);
2017
2018   mpi_coll_reduce_fun(sendbuf, recvbuf, count, datatype, op, root, comm);
2019
2020     retval = MPI_SUCCESS;
2021   TRACE_smpi_collective_out(rank, root_traced, __FUNCTION__);
2022   }
2023
2024   smpi_bench_begin();
2025   return retval;
2026 }
2027
2028 int PMPI_Reduce_local(void *inbuf, void *inoutbuf, int count, MPI_Datatype datatype, MPI_Op op){
2029   int retval = 0;
2030
2031     smpi_bench_end();
2032     if (!is_datatype_valid(datatype) || op == MPI_OP_NULL) {
2033       retval = MPI_ERR_ARG;
2034     } else {
2035       smpi_op_apply(op, inbuf, inoutbuf, &count, &datatype);
2036       retval=MPI_SUCCESS;
2037     }
2038     smpi_bench_begin();
2039     return retval;
2040 }
2041
2042 int PMPI_Allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2043 {
2044   int retval = 0;
2045
2046   smpi_bench_end();
2047
2048   if (comm == MPI_COMM_NULL) {
2049     retval = MPI_ERR_COMM;
2050   } else if (!is_datatype_valid(datatype)) {
2051     retval = MPI_ERR_TYPE;
2052   } else if (op == MPI_OP_NULL) {
2053     retval = MPI_ERR_OP;
2054   } else {
2055
2056     char* sendtmpbuf = static_cast<char*>(sendbuf);
2057     if( sendbuf == MPI_IN_PLACE ) {
2058       sendtmpbuf = static_cast<char*>(xbt_malloc(count*smpi_datatype_get_extent(datatype)));
2059       smpi_datatype_copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
2060     }
2061   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2062   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2063   extra->type = TRACING_ALLREDUCE;
2064   int known=0;
2065   extra->datatype1 = encode_datatype(datatype, &known);
2066   int dt_size_send = 1;
2067   if(known==0)
2068     dt_size_send = smpi_datatype_size(datatype);
2069   extra->send_size = count*dt_size_send;
2070
2071   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2072
2073   mpi_coll_allreduce_fun(sendtmpbuf, recvbuf, count, datatype, op, comm);
2074
2075     if( sendbuf == MPI_IN_PLACE )
2076       xbt_free(sendtmpbuf);
2077
2078     retval = MPI_SUCCESS;
2079   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2080   }
2081
2082   smpi_bench_begin();
2083   return retval;
2084 }
2085
2086 int PMPI_Scan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2087 {
2088   int retval = 0;
2089
2090   smpi_bench_end();
2091
2092   if (comm == MPI_COMM_NULL) {
2093     retval = MPI_ERR_COMM;
2094   } else if (!is_datatype_valid(datatype)) {
2095     retval = MPI_ERR_TYPE;
2096   } else if (op == MPI_OP_NULL) {
2097     retval = MPI_ERR_OP;
2098   } else {
2099   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2100   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2101   extra->type = TRACING_SCAN;
2102   int known=0;
2103   extra->datatype1 = encode_datatype(datatype, &known);
2104   int dt_size_send = 1;
2105   if(known==0)
2106     dt_size_send = smpi_datatype_size(datatype);
2107   extra->send_size = count*dt_size_send;
2108
2109   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2110
2111   smpi_mpi_scan(sendbuf, recvbuf, count, datatype, op, comm);
2112
2113   retval = MPI_SUCCESS;
2114   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2115   }
2116
2117   smpi_bench_begin();
2118   return retval;
2119 }
2120
2121 int PMPI_Exscan(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm){
2122   int retval = 0;
2123
2124   smpi_bench_end();
2125
2126   if (comm == MPI_COMM_NULL) {
2127     retval = MPI_ERR_COMM;
2128   } else if (!is_datatype_valid(datatype)) {
2129     retval = MPI_ERR_TYPE;
2130   } else if (op == MPI_OP_NULL) {
2131     retval = MPI_ERR_OP;
2132   } else {
2133   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2134   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2135   extra->type = TRACING_EXSCAN;
2136   int known=0;
2137   extra->datatype1 = encode_datatype(datatype, &known);
2138   int dt_size_send = 1;
2139   if(known==0)
2140     dt_size_send = smpi_datatype_size(datatype);
2141   extra->send_size = count*dt_size_send;
2142   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2143
2144   smpi_mpi_exscan(sendbuf, recvbuf, count, datatype, op, comm);
2145     retval = MPI_SUCCESS;
2146   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2147   }
2148
2149   smpi_bench_begin();
2150   return retval;
2151 }
2152
2153 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2154 {
2155   int retval = 0;
2156   smpi_bench_end();
2157
2158   if (comm == MPI_COMM_NULL) {
2159     retval = MPI_ERR_COMM;
2160   } else if (!is_datatype_valid(datatype)) {
2161     retval = MPI_ERR_TYPE;
2162   } else if (op == MPI_OP_NULL) {
2163     retval = MPI_ERR_OP;
2164   } else if (recvcounts == nullptr) {
2165     retval = MPI_ERR_ARG;
2166   } else {
2167   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2168   int i=0;
2169   int size = smpi_comm_size(comm);
2170   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2171   extra->type = TRACING_REDUCE_SCATTER;
2172   extra->num_processes = size;
2173   int known=0;
2174   extra->datatype1 = encode_datatype(datatype, &known);
2175   int dt_size_send = 1;
2176   if(known==0)
2177     dt_size_send = smpi_datatype_size(datatype);
2178   extra->send_size = 0;
2179   extra->recvcounts= xbt_new(int, size);
2180   for(i=0; i< size; i++)//copy data to avoid bad free
2181     extra->recvcounts[i] = recvcounts[i]*dt_size_send;
2182   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2183
2184   void* sendtmpbuf=sendbuf;
2185     if(sendbuf==MPI_IN_PLACE)
2186       sendtmpbuf=recvbuf;
2187
2188     mpi_coll_reduce_scatter_fun(sendtmpbuf, recvbuf, recvcounts, datatype,  op, comm);
2189     retval = MPI_SUCCESS;
2190   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2191   }
2192
2193   smpi_bench_begin();
2194   return retval;
2195 }
2196
2197 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
2198                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2199 {
2200   int retval,i;
2201   smpi_bench_end();
2202
2203   if (comm == MPI_COMM_NULL) {
2204     retval = MPI_ERR_COMM;
2205   } else if (!is_datatype_valid(datatype)) {
2206     retval = MPI_ERR_TYPE;
2207   } else if (op == MPI_OP_NULL) {
2208     retval = MPI_ERR_OP;
2209   } else if (recvcount < 0) {
2210     retval = MPI_ERR_ARG;
2211   } else {
2212     int count=smpi_comm_size(comm);
2213
2214   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2215   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2216   extra->type = TRACING_REDUCE_SCATTER;
2217   extra->num_processes = count;
2218   int known=0;
2219   extra->datatype1 = encode_datatype(datatype, &known);
2220   int dt_size_send = 1;
2221   if(known==0)
2222     dt_size_send = smpi_datatype_size(datatype);
2223   extra->send_size = 0;
2224   extra->recvcounts= xbt_new(int, count);
2225   for(i=0; i< count; i++)//copy data to avoid bad free
2226     extra->recvcounts[i] = recvcount*dt_size_send;
2227
2228   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2229
2230   int* recvcounts=static_cast<int*>(xbt_malloc(count));
2231     for (i=0; i<count;i++)
2232       recvcounts[i]=recvcount;
2233     mpi_coll_reduce_scatter_fun(sendbuf, recvbuf, recvcounts, datatype,  op, comm);
2234     xbt_free(recvcounts);
2235     retval = MPI_SUCCESS;
2236
2237     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2238   }
2239
2240   smpi_bench_begin();
2241   return retval;
2242 }
2243
2244 int PMPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype,
2245                  void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
2246 {
2247   int retval = 0;
2248
2249   smpi_bench_end();
2250
2251   if (comm == MPI_COMM_NULL) {
2252     retval = MPI_ERR_COMM;
2253   } else if (sendtype == MPI_DATATYPE_NULL
2254              || recvtype == MPI_DATATYPE_NULL) {
2255     retval = MPI_ERR_TYPE;
2256   } else {
2257   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2258   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2259   extra->type = TRACING_ALLTOALL;
2260   int known=0;
2261   extra->datatype1 = encode_datatype(sendtype, &known);
2262   if(known==0)
2263     extra->send_size = sendcount*smpi_datatype_size(sendtype);
2264   else
2265     extra->send_size = sendcount;
2266   extra->datatype2 = encode_datatype(recvtype, &known);
2267   if(known==0)
2268     extra->recv_size = recvcount*smpi_datatype_size(recvtype);
2269   else
2270     extra->recv_size = recvcount;
2271   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2272
2273   retval = mpi_coll_alltoall_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
2274
2275   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2276   }
2277
2278   smpi_bench_begin();
2279   return retval;
2280 }
2281
2282 int PMPI_Alltoallv(void *sendbuf, int *sendcounts, int *senddisps,MPI_Datatype sendtype, void *recvbuf, int *recvcounts,
2283                   int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2284 {
2285   int retval = 0;
2286
2287   smpi_bench_end();
2288
2289   if (comm == MPI_COMM_NULL) {
2290     retval = MPI_ERR_COMM;
2291   } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2292     retval = MPI_ERR_TYPE;
2293   } else if (sendcounts == nullptr || senddisps == nullptr || recvcounts == nullptr || recvdisps == nullptr) {
2294     retval = MPI_ERR_ARG;
2295   } else {
2296   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2297   int i=0;
2298   int size = smpi_comm_size(comm);
2299   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2300   extra->type = TRACING_ALLTOALLV;
2301   extra->send_size = 0;
2302   extra->recv_size = 0;
2303   extra->recvcounts= xbt_new(int, size);
2304   extra->sendcounts= xbt_new(int, size);
2305   int known=0;
2306   extra->datatype1 = encode_datatype(sendtype, &known);
2307   int dt_size_send = 1;
2308   if(known==0)
2309     dt_size_send = smpi_datatype_size(sendtype);
2310   int dt_size_recv = 1;
2311   extra->datatype2 = encode_datatype(recvtype, &known);
2312   if(known==0)
2313     dt_size_recv = smpi_datatype_size(recvtype);
2314   for(i=0; i< size; i++){//copy data to avoid bad free
2315     extra->send_size += sendcounts[i]*dt_size_send;
2316     extra->recv_size += recvcounts[i]*dt_size_recv;
2317
2318     extra->sendcounts[i] = sendcounts[i]*dt_size_send;
2319     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
2320   }
2321   extra->num_processes = size;
2322   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2323
2324   retval = mpi_coll_alltoallv_fun(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype,
2325                                   comm);
2326   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2327   }
2328
2329   smpi_bench_begin();
2330   return retval;
2331 }
2332
2333
2334 int PMPI_Get_processor_name(char *name, int *resultlen)
2335 {
2336   int retval = MPI_SUCCESS;
2337
2338   strncpy(name, sg_host_get_name(SIMIX_host_self()),
2339           strlen(sg_host_get_name(SIMIX_host_self())) < MPI_MAX_PROCESSOR_NAME - 1 ?
2340           strlen(sg_host_get_name(SIMIX_host_self())) +1 : MPI_MAX_PROCESSOR_NAME - 1 );
2341   *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2342
2343   return retval;
2344 }
2345
2346 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2347 {
2348   int retval = MPI_SUCCESS;
2349   size_t size;
2350
2351   if (status == nullptr || count == nullptr) {
2352     retval = MPI_ERR_ARG;
2353   } else if (!is_datatype_valid(datatype)) {
2354     retval = MPI_ERR_TYPE;
2355   } else {
2356     size = smpi_datatype_size(datatype);
2357     if (size == 0) {
2358       *count = 0;
2359     } else if (status->count % size != 0) {
2360       retval = MPI_UNDEFINED;
2361     } else {
2362       *count = smpi_mpi_get_count(status, datatype);
2363     }
2364   }
2365   return retval;
2366 }
2367
2368 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2369   int retval = 0;
2370
2371   if (old_type == MPI_DATATYPE_NULL) {
2372     retval = MPI_ERR_TYPE;
2373   } else if (count<0){
2374     retval = MPI_ERR_COUNT;
2375   } else {
2376     retval = smpi_datatype_contiguous(count, old_type, new_type, 0);
2377   }
2378   return retval;
2379 }
2380
2381 int PMPI_Type_commit(MPI_Datatype* datatype) {
2382   int retval = 0;
2383
2384   if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2385     retval = MPI_ERR_TYPE;
2386   } else {
2387     smpi_datatype_commit(datatype);
2388     retval = MPI_SUCCESS;
2389   }
2390   return retval;
2391 }
2392
2393 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2394   int retval = 0;
2395
2396   if (old_type == MPI_DATATYPE_NULL) {
2397     retval = MPI_ERR_TYPE;
2398   } else if (count<0 || blocklen<0){
2399     retval = MPI_ERR_COUNT;
2400   } else {
2401     retval = smpi_datatype_vector(count, blocklen, stride, old_type, new_type);
2402   }
2403   return retval;
2404 }
2405
2406 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2407   int retval = 0;
2408
2409   if (old_type == MPI_DATATYPE_NULL) {
2410     retval = MPI_ERR_TYPE;
2411   } else if (count<0 || blocklen<0){
2412     retval = MPI_ERR_COUNT;
2413   } else {
2414     retval = smpi_datatype_hvector(count, blocklen, stride, old_type, new_type);
2415   }
2416   return retval;
2417 }
2418
2419 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2420   return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2421 }
2422
2423 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2424   int retval = 0;
2425
2426   if (old_type == MPI_DATATYPE_NULL) {
2427     retval = MPI_ERR_TYPE;
2428   } else if (count<0){
2429     retval = MPI_ERR_COUNT;
2430   } else {
2431     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2432   }
2433   return retval;
2434 }
2435
2436 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2437   int retval = 0;
2438
2439   if (old_type == MPI_DATATYPE_NULL) {
2440     retval = MPI_ERR_TYPE;
2441   } else if (count<0){
2442     retval = MPI_ERR_COUNT;
2443   } else {
2444     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2445   }
2446   return retval;
2447 }
2448
2449 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2450                                    MPI_Datatype* new_type) {
2451   int retval,i;
2452
2453   if (old_type == MPI_DATATYPE_NULL) {
2454     retval = MPI_ERR_TYPE;
2455   } else if (count<0){
2456     retval = MPI_ERR_COUNT;
2457   } else {
2458     int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count));
2459     for (i=0; i<count;i++)
2460       blocklens[i]=blocklength;
2461     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2462     xbt_free(blocklens);
2463   }
2464   return retval;
2465 }
2466
2467 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2468   int retval = 0;
2469
2470   if (old_type == MPI_DATATYPE_NULL) {
2471     retval = MPI_ERR_TYPE;
2472   } else if (count<0){
2473     retval = MPI_ERR_COUNT;
2474   } else {
2475     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2476   }
2477   return retval;
2478 }
2479
2480 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2481                               MPI_Datatype* new_type) {
2482   return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2483 }
2484
2485 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2486                                     MPI_Datatype* new_type) {
2487   int retval,i;
2488
2489   if (old_type == MPI_DATATYPE_NULL) {
2490     retval = MPI_ERR_TYPE;
2491   } else if (count<0){
2492     retval = MPI_ERR_COUNT;
2493   } else {
2494     int* blocklens=(int*)xbt_malloc(blocklength*count);
2495     for (i=0; i<count;i++)blocklens[i]=blocklength;
2496     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2497     xbt_free(blocklens);
2498   }
2499   return retval;
2500 }
2501
2502 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2503   int retval = 0;
2504
2505   if (count<0){
2506     retval = MPI_ERR_COUNT;
2507   } else {
2508     retval = smpi_datatype_struct(count, blocklens, indices, old_types, new_type);
2509   }
2510   return retval;
2511 }
2512
2513 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2514                             MPI_Datatype* new_type) {
2515   return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2516 }
2517
2518 int PMPI_Error_class(int errorcode, int* errorclass) {
2519   // assume smpi uses only standard mpi error codes
2520   *errorclass=errorcode;
2521   return MPI_SUCCESS;
2522 }
2523
2524 int PMPI_Initialized(int* flag) {
2525    *flag=smpi_process_initialized();
2526    return MPI_SUCCESS;
2527 }
2528
2529 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2530  * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2531  *  MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2532
2533 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2534   int retval = 0;
2535   if (comm_old == MPI_COMM_NULL){
2536     retval =  MPI_ERR_COMM;
2537   } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2538     retval = MPI_ERR_ARG;
2539   } else{
2540     retval = smpi_mpi_cart_create(comm_old, ndims, dims, periodic, reorder, comm_cart);
2541   }
2542   return retval;
2543 }
2544
2545 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2546   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2547     return MPI_ERR_TOPOLOGY;
2548   }
2549   if (coords == nullptr) {
2550     return MPI_ERR_ARG;
2551   }
2552   return smpi_mpi_cart_rank(comm, coords, rank);
2553 }
2554
2555 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2556   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2557     return MPI_ERR_TOPOLOGY;
2558   }
2559   if (source == nullptr || dest == nullptr || direction < 0 ) {
2560     return MPI_ERR_ARG;
2561   }
2562   return smpi_mpi_cart_shift(comm, direction, displ, source, dest);
2563 }
2564
2565 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2566   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2567     return MPI_ERR_TOPOLOGY;
2568   }
2569   if (rank < 0 || rank >= smpi_comm_size(comm)) {
2570     return MPI_ERR_RANK;
2571   }
2572   if (maxdims <= 0) {
2573     return MPI_ERR_ARG;
2574   }
2575   if(coords == nullptr) {
2576     return MPI_ERR_ARG;
2577   }
2578   return smpi_mpi_cart_coords(comm, rank, maxdims, coords);
2579 }
2580
2581 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2582   if(comm == nullptr || smpi_comm_topo(comm) == nullptr) {
2583     return MPI_ERR_TOPOLOGY;
2584   }
2585   if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2586     return MPI_ERR_ARG;
2587   }
2588   return smpi_mpi_cart_get(comm, maxdims, dims, periods, coords);
2589 }
2590
2591 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2592   if (comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2593     return MPI_ERR_TOPOLOGY;
2594   }
2595   if (ndims == nullptr) {
2596     return MPI_ERR_ARG;
2597   }
2598   return smpi_mpi_cartdim_get(comm, ndims);
2599 }
2600
2601 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2602   if(dims == nullptr) {
2603     return MPI_ERR_ARG;
2604   }
2605   if (ndims < 1 || nnodes < 1) {
2606     return MPI_ERR_DIMS;
2607   }
2608
2609   return smpi_mpi_dims_create(nnodes, ndims, dims);
2610 }
2611
2612 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2613   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2614     return MPI_ERR_TOPOLOGY;
2615   }
2616   if (comm_new == nullptr) {
2617     return MPI_ERR_ARG;
2618   }
2619   return smpi_mpi_cart_sub(comm, remain_dims, comm_new);
2620 }
2621
2622 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2623     if(oldtype == MPI_DATATYPE_NULL) {
2624         return MPI_ERR_TYPE;
2625     }
2626     int blocks[3] = { 1, 1, 1 };
2627     MPI_Aint disps[3] = { lb, 0, lb+extent };
2628     MPI_Datatype types[3] = { MPI_LB, oldtype, MPI_UB };
2629
2630     s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocks, disps, 3, types);
2631     smpi_datatype_create(newtype,oldtype->size, lb, lb + extent, sizeof(s_smpi_mpi_struct_t) , subtype, DT_FLAG_VECTOR);
2632
2633     (*newtype)->flags &= ~DT_FLAG_COMMITED;
2634     return MPI_SUCCESS;
2635 }
2636
2637 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2638   int retval = 0;
2639   smpi_bench_end();
2640   if (comm == MPI_COMM_NULL) {
2641     retval= MPI_ERR_COMM;
2642   }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2643     retval= MPI_ERR_OTHER;
2644   }else{
2645     *win = smpi_mpi_win_create( base, size, disp_unit, info, comm);
2646     retval = MPI_SUCCESS;
2647   }
2648   smpi_bench_begin();
2649   return retval;
2650 }
2651
2652 int PMPI_Win_free( MPI_Win* win){
2653   int retval = 0;
2654   smpi_bench_end();
2655   if (win == nullptr || *win == MPI_WIN_NULL) {
2656     retval = MPI_ERR_WIN;
2657   }else{
2658     retval=smpi_mpi_win_free(win);
2659   }
2660   smpi_bench_begin();
2661   return retval;
2662 }
2663
2664 int PMPI_Win_set_name(MPI_Win  win, char * name)
2665 {
2666   int retval = 0;
2667   if (win == MPI_WIN_NULL)  {
2668     retval = MPI_ERR_TYPE;
2669   } else if (name == nullptr)  {
2670     retval = MPI_ERR_ARG;
2671   } else {
2672     smpi_mpi_win_set_name(win, name);
2673     retval = MPI_SUCCESS;
2674   }
2675   return retval;
2676 }
2677
2678 int PMPI_Win_get_name(MPI_Win  win, char * name, int* len)
2679 {
2680   int retval = MPI_SUCCESS;
2681
2682   if (win == MPI_WIN_NULL)  {
2683     retval = MPI_ERR_WIN;
2684   } else if (name == nullptr)  {
2685     retval = MPI_ERR_ARG;
2686   } else {
2687     smpi_mpi_win_get_name(win, name, len);
2688   }
2689   return retval;
2690 }
2691
2692 int PMPI_Win_get_group(MPI_Win  win, MPI_Group * group){
2693   int retval = MPI_SUCCESS;
2694   if (win == MPI_WIN_NULL)  {
2695     retval = MPI_ERR_WIN;
2696   }else {
2697     smpi_mpi_win_get_group(win, group);
2698     smpi_group_use(*group);
2699   }
2700   return retval;
2701 }
2702
2703
2704 int PMPI_Win_fence( int assert,  MPI_Win win){
2705   int retval = 0;
2706   smpi_bench_end();
2707   if (win == MPI_WIN_NULL) {
2708     retval = MPI_ERR_WIN;
2709   } else {
2710   int rank = smpi_process_index();
2711   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2712   retval = smpi_mpi_win_fence(assert, win);
2713   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2714   }
2715   smpi_bench_begin();
2716   return retval;
2717 }
2718
2719 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2720               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2721   int retval = 0;
2722   smpi_bench_end();
2723   if (win == MPI_WIN_NULL) {
2724     retval = MPI_ERR_WIN;
2725   } else if (target_rank == MPI_PROC_NULL) {
2726     retval = MPI_SUCCESS;
2727   } else if (target_rank <0){
2728     retval = MPI_ERR_RANK;
2729   } else if (target_disp <0){
2730       retval = MPI_ERR_ARG;
2731   } else if ((origin_count < 0 || target_count < 0) ||
2732              (origin_addr==nullptr && origin_count > 0)){
2733     retval = MPI_ERR_COUNT;
2734   } else if ((!is_datatype_valid(origin_datatype)) || (!is_datatype_valid(target_datatype))) {
2735     retval = MPI_ERR_TYPE;
2736   } else {
2737     int rank = smpi_process_index();
2738     MPI_Group group;
2739     smpi_mpi_win_get_group(win, &group);
2740     int src_traced = smpi_group_index(group, target_rank);
2741     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2742
2743     retval = smpi_mpi_get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2744                            target_datatype, win);
2745
2746     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2747   }
2748   smpi_bench_begin();
2749   return retval;
2750 }
2751
2752 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2753               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2754   int retval = 0;
2755   smpi_bench_end();
2756   if (win == MPI_WIN_NULL) {
2757     retval = MPI_ERR_WIN;
2758   } else if (target_rank == MPI_PROC_NULL) {
2759     retval = MPI_SUCCESS;
2760   } else if (target_rank <0){
2761     retval = MPI_ERR_RANK;
2762   } else if (target_disp <0){
2763     retval = MPI_ERR_ARG;
2764   } else if ((origin_count < 0 || target_count < 0) ||
2765             (origin_addr==nullptr && origin_count > 0)){
2766     retval = MPI_ERR_COUNT;
2767   } else if ((!is_datatype_valid(origin_datatype)) || (!is_datatype_valid(target_datatype))) {
2768     retval = MPI_ERR_TYPE;
2769   } else {
2770     int rank = smpi_process_index();
2771     MPI_Group group;
2772     smpi_mpi_win_get_group(win, &group);
2773     int dst_traced = smpi_group_index(group, target_rank);
2774     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2775     TRACE_smpi_send(rank, rank, dst_traced, origin_count*smpi_datatype_size(origin_datatype));
2776
2777     retval = smpi_mpi_put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2778                            target_datatype, win);
2779
2780     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2781   }
2782   smpi_bench_begin();
2783   return retval;
2784 }
2785
2786 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2787               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2788   int retval = 0;
2789   smpi_bench_end();
2790   if (win == MPI_WIN_NULL) {
2791     retval = MPI_ERR_WIN;
2792   } else if (target_rank == MPI_PROC_NULL) {
2793     retval = MPI_SUCCESS;
2794   } else if (target_rank <0){
2795     retval = MPI_ERR_RANK;
2796   } else if (target_disp <0){
2797     retval = MPI_ERR_ARG;
2798   } else if ((origin_count < 0 || target_count < 0) ||
2799              (origin_addr==nullptr && origin_count > 0)){
2800     retval = MPI_ERR_COUNT;
2801   } else if ((!is_datatype_valid(origin_datatype)) ||
2802             (!is_datatype_valid(target_datatype))) {
2803     retval = MPI_ERR_TYPE;
2804   } else if (op == MPI_OP_NULL) {
2805     retval = MPI_ERR_OP;
2806   } else {
2807     int rank = smpi_process_index();
2808     MPI_Group group;
2809     smpi_mpi_win_get_group(win, &group);
2810     int src_traced = smpi_group_index(group, target_rank);
2811     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2812
2813     retval = smpi_mpi_accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2814                                   target_datatype, op, win);
2815
2816     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2817   }
2818   smpi_bench_begin();
2819   return retval;
2820 }
2821
2822 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2823   int retval = 0;
2824   smpi_bench_end();
2825   if (win == MPI_WIN_NULL) {
2826     retval = MPI_ERR_WIN;
2827   } else if (group==MPI_GROUP_NULL){
2828     retval = MPI_ERR_GROUP;
2829   }
2830   else {
2831     int rank = smpi_process_index();
2832     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2833     retval = smpi_mpi_win_post(group,assert,win);
2834     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2835   }
2836   smpi_bench_begin();
2837   return retval;
2838 }
2839
2840 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2841   int retval = 0;
2842   smpi_bench_end();
2843   if (win == MPI_WIN_NULL) {
2844     retval = MPI_ERR_WIN;
2845   } else if (group==MPI_GROUP_NULL){
2846     retval = MPI_ERR_GROUP;
2847   }
2848   else {
2849     int rank = smpi_process_index();
2850     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2851     retval = smpi_mpi_win_start(group,assert,win);
2852     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2853   }
2854   smpi_bench_begin();
2855   return retval;
2856 }
2857
2858 int PMPI_Win_complete(MPI_Win win){
2859   int retval = 0;
2860   smpi_bench_end();
2861   if (win == MPI_WIN_NULL) {
2862     retval = MPI_ERR_WIN;
2863   }
2864   else {
2865     int rank = smpi_process_index();
2866     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2867
2868     retval = smpi_mpi_win_complete(win);
2869
2870     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2871   }
2872   smpi_bench_begin();
2873   return retval;
2874 }
2875
2876 int PMPI_Win_wait(MPI_Win win){
2877   int retval = 0;
2878   smpi_bench_end();
2879   if (win == MPI_WIN_NULL) {
2880     retval = MPI_ERR_WIN;
2881   }
2882   else {
2883     int rank = smpi_process_index();
2884     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2885
2886     retval = smpi_mpi_win_wait(win);
2887
2888     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2889   }
2890   smpi_bench_begin();
2891   return retval;
2892 }
2893
2894 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
2895   void *ptr = xbt_malloc(size);
2896   if(ptr==nullptr)
2897     return MPI_ERR_NO_MEM;
2898   else {
2899     *static_cast<void**>(baseptr) = ptr;
2900     return MPI_SUCCESS;
2901   }
2902 }
2903
2904 int PMPI_Free_mem(void *baseptr){
2905   xbt_free(baseptr);
2906   return MPI_SUCCESS;
2907 }
2908
2909 int PMPI_Type_set_name(MPI_Datatype  datatype, char * name)
2910 {
2911   int retval = 0;
2912   if (datatype == MPI_DATATYPE_NULL)  {
2913     retval = MPI_ERR_TYPE;
2914   } else if (name == nullptr)  {
2915     retval = MPI_ERR_ARG;
2916   } else {
2917     smpi_datatype_set_name(datatype, name);
2918     retval = MPI_SUCCESS;
2919   }
2920   return retval;
2921 }
2922
2923 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
2924 {
2925   int retval = 0;
2926
2927   if (datatype == MPI_DATATYPE_NULL)  {
2928     retval = MPI_ERR_TYPE;
2929   } else if (name == nullptr)  {
2930     retval = MPI_ERR_ARG;
2931   } else {
2932     smpi_datatype_get_name(datatype, name, len);
2933     retval = MPI_SUCCESS;
2934   }
2935   return retval;
2936 }
2937
2938 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
2939   return smpi_type_f2c(datatype);
2940 }
2941
2942 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
2943   return smpi_type_c2f( datatype);
2944 }
2945
2946 MPI_Group PMPI_Group_f2c(MPI_Fint group){
2947   return smpi_group_f2c( group);
2948 }
2949
2950 MPI_Fint PMPI_Group_c2f(MPI_Group group){
2951   return smpi_group_c2f(group);
2952 }
2953
2954 MPI_Request PMPI_Request_f2c(MPI_Fint request){
2955   return smpi_request_f2c(request);
2956 }
2957
2958 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
2959   return smpi_request_c2f(request);
2960 }
2961
2962 MPI_Win PMPI_Win_f2c(MPI_Fint win){
2963   return smpi_win_f2c(win);
2964 }
2965
2966 MPI_Fint PMPI_Win_c2f(MPI_Win win){
2967   return smpi_win_c2f(win);
2968 }
2969
2970 MPI_Op PMPI_Op_f2c(MPI_Fint op){
2971   return smpi_op_f2c(op);
2972 }
2973
2974 MPI_Fint PMPI_Op_c2f(MPI_Op op){
2975   return smpi_op_c2f(op);
2976 }
2977
2978 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
2979   return smpi_comm_f2c(comm);
2980 }
2981
2982 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
2983   return smpi_comm_c2f(comm);
2984 }
2985
2986 MPI_Info PMPI_Info_f2c(MPI_Fint info){
2987   return smpi_info_f2c(info);
2988 }
2989
2990 MPI_Fint PMPI_Info_c2f(MPI_Info info){
2991   return smpi_info_c2f(info);
2992 }
2993
2994 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
2995   return smpi_comm_keyval_create(copy_fn, delete_fn, keyval, extra_state);
2996 }
2997
2998 int PMPI_Keyval_free(int* keyval) {
2999   return smpi_comm_keyval_free(keyval);
3000 }
3001
3002 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3003   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3004        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3005     return MPI_ERR_ARG;
3006   else if (comm==MPI_COMM_NULL)
3007     return MPI_ERR_COMM;
3008   else
3009     return smpi_comm_attr_delete(comm, keyval);
3010 }
3011
3012 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3013   static int one = 1;
3014   static int zero = 0;
3015   static int tag_ub = 1000000;
3016   static int last_used_code = MPI_ERR_LASTCODE;
3017
3018   if (comm==MPI_COMM_NULL){
3019     *flag = 0;
3020     return MPI_ERR_COMM;
3021   }
3022
3023   switch (keyval) {
3024   case MPI_HOST:
3025   case MPI_IO:
3026   case MPI_APPNUM:
3027     *flag = 1;
3028     *static_cast<int**>(attr_value) = &zero;
3029     return MPI_SUCCESS;
3030   case MPI_UNIVERSE_SIZE:
3031     *flag = 1;
3032     *static_cast<int**>(attr_value) = &smpi_universe_size;
3033     return MPI_SUCCESS;
3034   case MPI_LASTUSEDCODE:
3035     *flag = 1;
3036     *static_cast<int**>(attr_value) = &last_used_code;
3037     return MPI_SUCCESS;
3038   case MPI_TAG_UB:
3039     *flag=1;
3040     *static_cast<int**>(attr_value) = &tag_ub;
3041     return MPI_SUCCESS;
3042   case MPI_WTIME_IS_GLOBAL:
3043     *flag = 1;
3044     *static_cast<int**>(attr_value) = &one;
3045     return MPI_SUCCESS;
3046   default:
3047     return smpi_comm_attr_get(comm, keyval, attr_value, flag);
3048   }
3049 }
3050
3051 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3052   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3053        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3054     return MPI_ERR_ARG;
3055   else if (comm==MPI_COMM_NULL)
3056     return MPI_ERR_COMM;
3057   else
3058   return smpi_comm_attr_put(comm, keyval, attr_value);
3059 }
3060
3061 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3062 {
3063   return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3064 }
3065
3066 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3067 {
3068   return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3069 }
3070
3071 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3072 {
3073   return PMPI_Attr_delete(comm, comm_keyval);
3074 }
3075
3076 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3077                             void* extra_state)
3078 {
3079   return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3080 }
3081
3082 int PMPI_Comm_free_keyval(int* keyval) {
3083   return PMPI_Keyval_free(keyval);
3084 }
3085
3086 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3087 {
3088   if (type==MPI_DATATYPE_NULL)
3089     return MPI_ERR_TYPE;
3090   else
3091     return smpi_type_attr_get(type, type_keyval, attribute_val, flag);
3092 }
3093
3094 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3095 {
3096   if (type==MPI_DATATYPE_NULL)
3097     return MPI_ERR_TYPE;
3098   else
3099     return smpi_type_attr_put(type, type_keyval, attribute_val);
3100 }
3101
3102 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3103 {
3104   if (type==MPI_DATATYPE_NULL)
3105     return MPI_ERR_TYPE;
3106   else
3107     return smpi_type_attr_delete(type, type_keyval);
3108 }
3109
3110 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3111                             void* extra_state)
3112 {
3113   return smpi_type_keyval_create(copy_fn, delete_fn, keyval, extra_state);
3114 }
3115
3116 int PMPI_Type_free_keyval(int* keyval) {
3117   return smpi_type_keyval_free(keyval);
3118 }
3119
3120 int PMPI_Info_create( MPI_Info *info){
3121   if (info == nullptr)
3122     return MPI_ERR_ARG;
3123   *info = xbt_new(s_smpi_mpi_info_t, 1);
3124   (*info)->info_dict= xbt_dict_new_homogeneous(xbt_free_f);
3125   (*info)->refcount=1;
3126   return MPI_SUCCESS;
3127 }
3128
3129 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3130   if (info == nullptr || key == nullptr || value == nullptr)
3131     return MPI_ERR_ARG;
3132
3133   xbt_dict_set(info->info_dict, key, xbt_strdup(value), nullptr);
3134   return MPI_SUCCESS;
3135 }
3136
3137 int PMPI_Info_free( MPI_Info *info){
3138   if (info == nullptr || *info==nullptr)
3139     return MPI_ERR_ARG;
3140   (*info)->refcount--;
3141   if((*info)->refcount==0){
3142     xbt_dict_free(&((*info)->info_dict));
3143     xbt_free(*info);
3144   }
3145   *info=MPI_INFO_NULL;
3146   return MPI_SUCCESS;
3147 }
3148
3149 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3150   *flag=false;
3151   if (info == nullptr || key == nullptr || valuelen <0)
3152     return MPI_ERR_ARG;
3153   if (value == nullptr)
3154     return MPI_ERR_INFO_VALUE;
3155   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(info->info_dict, key));
3156   if(tmpvalue){
3157     memset(value, 0, valuelen);
3158     memcpy(value,tmpvalue, (strlen(tmpvalue) + 1 < static_cast<size_t>(valuelen)) ? strlen(tmpvalue) + 1 : valuelen);
3159     *flag=true;
3160   }
3161   return MPI_SUCCESS;
3162 }
3163
3164 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3165   if (info == nullptr || newinfo==nullptr)
3166     return MPI_ERR_ARG;
3167   *newinfo = xbt_new(s_smpi_mpi_info_t, 1);
3168   (*newinfo)->info_dict= xbt_dict_new_homogeneous(xbt_free_f);
3169   (*newinfo)->refcount=1;
3170   xbt_dict_cursor_t cursor = nullptr;
3171   int *key;
3172   void* data;
3173   xbt_dict_foreach(info->info_dict,cursor,key,data){
3174     xbt_dict_set((*newinfo)->info_dict, reinterpret_cast<char*>(key), xbt_strdup(reinterpret_cast<char*>(data)), nullptr);
3175   }
3176   return MPI_SUCCESS;
3177 }
3178
3179 int PMPI_Info_delete(MPI_Info info, char *key){
3180   if (info == nullptr || key==nullptr)
3181     return MPI_ERR_ARG;
3182   try {
3183     xbt_dict_remove(info->info_dict, key);
3184   }
3185   catch(xbt_ex& e){
3186     return MPI_ERR_INFO_NOKEY;
3187   }
3188   return MPI_SUCCESS;
3189 }
3190
3191 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3192   if (info == nullptr || nkeys==nullptr)
3193     return MPI_ERR_ARG;
3194   *nkeys=xbt_dict_size(info->info_dict);
3195   return MPI_SUCCESS;
3196 }
3197
3198 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3199   if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3200     return MPI_ERR_ARG;
3201
3202   xbt_dict_cursor_t cursor = nullptr;
3203   char *keyn;
3204   void* data;
3205   int num=0;
3206   xbt_dict_foreach(info->info_dict,cursor,keyn,data){
3207     if(num==n){
3208       strncpy(key,keyn,strlen(keyn)+1);
3209       xbt_dict_cursor_free(&cursor);
3210       return MPI_SUCCESS;
3211     }
3212     num++;
3213   }
3214   return MPI_ERR_ARG;
3215 }
3216
3217 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3218   *flag=false;
3219   if (info == nullptr || key == nullptr || valuelen==nullptr)
3220     return MPI_ERR_ARG;
3221   char* tmpvalue=(char*)xbt_dict_get_or_null(info->info_dict, key);
3222   if(tmpvalue){
3223     *valuelen=strlen(tmpvalue);
3224     *flag=true;
3225   }
3226   return MPI_SUCCESS;
3227 }
3228
3229 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3230   if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3231     return MPI_ERR_ARG;
3232   if(!is_datatype_valid(type))
3233     return MPI_ERR_TYPE;
3234   if(comm==MPI_COMM_NULL)
3235     return MPI_ERR_COMM;
3236   return smpi_mpi_unpack(inbuf, incount, position, outbuf,outcount,type, comm);
3237 }
3238
3239 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3240   if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3241     return MPI_ERR_ARG;
3242   if(!is_datatype_valid(type))
3243     return MPI_ERR_TYPE;
3244   if(comm==MPI_COMM_NULL)
3245     return MPI_ERR_COMM;
3246   return smpi_mpi_pack(inbuf, incount, type, outbuf,outcount,position, comm);
3247 }
3248
3249 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3250   if(incount<0)
3251     return MPI_ERR_ARG;
3252   if(!is_datatype_valid(datatype))
3253     return MPI_ERR_TYPE;
3254   if(comm==MPI_COMM_NULL)
3255     return MPI_ERR_COMM;
3256
3257   *size=incount*smpi_datatype_size(datatype);
3258
3259   return MPI_SUCCESS;
3260 }
3261