Logo AND Algorithmique Numérique Distribuée

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