Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MPI_Comm_group_create ... which is basically MPI_Comm_create
[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   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2167
2168   smpi_mpi_exscan(sendbuf, recvbuf, count, datatype, op, comm);
2169     retval = MPI_SUCCESS;
2170   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2171   }
2172
2173   smpi_bench_begin();
2174   return retval;
2175 }
2176
2177 int PMPI_Reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2178 {
2179   int retval = 0;
2180   smpi_bench_end();
2181
2182   if (comm == MPI_COMM_NULL) {
2183     retval = MPI_ERR_COMM;
2184   } else if (!is_datatype_valid(datatype)) {
2185     retval = MPI_ERR_TYPE;
2186   } else if (op == MPI_OP_NULL) {
2187     retval = MPI_ERR_OP;
2188   } else if (recvcounts == nullptr) {
2189     retval = MPI_ERR_ARG;
2190   } else {
2191   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2192   int i=0;
2193   int size = smpi_comm_size(comm);
2194   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2195   extra->type = TRACING_REDUCE_SCATTER;
2196   extra->num_processes = size;
2197   int known=0;
2198   extra->datatype1 = encode_datatype(datatype, &known);
2199   int dt_size_send = 1;
2200   if(known==0)
2201     dt_size_send = smpi_datatype_size(datatype);
2202   extra->send_size = 0;
2203   extra->recvcounts= xbt_new(int, size);
2204   for(i=0; i< size; i++)//copy data to avoid bad free
2205     extra->recvcounts[i] = recvcounts[i]*dt_size_send;
2206   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2207
2208   void* sendtmpbuf=sendbuf;
2209     if(sendbuf==MPI_IN_PLACE)
2210       sendtmpbuf=recvbuf;
2211
2212     mpi_coll_reduce_scatter_fun(sendtmpbuf, recvbuf, recvcounts, datatype,  op, comm);
2213     retval = MPI_SUCCESS;
2214   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2215   }
2216
2217   smpi_bench_begin();
2218   return retval;
2219 }
2220
2221 int PMPI_Reduce_scatter_block(void *sendbuf, void *recvbuf, int recvcount,
2222                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
2223 {
2224   int retval,i;
2225   smpi_bench_end();
2226
2227   if (comm == MPI_COMM_NULL) {
2228     retval = MPI_ERR_COMM;
2229   } else if (!is_datatype_valid(datatype)) {
2230     retval = MPI_ERR_TYPE;
2231   } else if (op == MPI_OP_NULL) {
2232     retval = MPI_ERR_OP;
2233   } else if (recvcount < 0) {
2234     retval = MPI_ERR_ARG;
2235   } else {
2236     int count=smpi_comm_size(comm);
2237
2238   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2239   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2240   extra->type = TRACING_REDUCE_SCATTER;
2241   extra->num_processes = count;
2242   int known=0;
2243   extra->datatype1 = encode_datatype(datatype, &known);
2244   int dt_size_send = 1;
2245   if(known==0)
2246     dt_size_send = smpi_datatype_size(datatype);
2247   extra->send_size = 0;
2248   extra->recvcounts= xbt_new(int, count);
2249   for(i=0; i< count; i++)//copy data to avoid bad free
2250     extra->recvcounts[i] = recvcount*dt_size_send;
2251
2252   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2253
2254   int* recvcounts=static_cast<int*>(xbt_malloc(count*sizeof(int)));
2255     for (i=0; i<count;i++)
2256       recvcounts[i]=recvcount;
2257     mpi_coll_reduce_scatter_fun(sendbuf, recvbuf, recvcounts, datatype,  op, comm);
2258     xbt_free(recvcounts);
2259     retval = MPI_SUCCESS;
2260
2261     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2262   }
2263
2264   smpi_bench_begin();
2265   return retval;
2266 }
2267
2268 int PMPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype,
2269                  void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
2270 {
2271   int retval = 0;
2272
2273   smpi_bench_end();
2274
2275   if (comm == MPI_COMM_NULL) {
2276     retval = MPI_ERR_COMM;
2277   } else if (sendtype == MPI_DATATYPE_NULL
2278              || recvtype == MPI_DATATYPE_NULL) {
2279     retval = MPI_ERR_TYPE;
2280   } else {
2281   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2282   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2283   extra->type = TRACING_ALLTOALL;
2284   int known=0;
2285   extra->datatype1 = encode_datatype(sendtype, &known);
2286   if(known==0)
2287     extra->send_size = sendcount*smpi_datatype_size(sendtype);
2288   else
2289     extra->send_size = sendcount;
2290   extra->datatype2 = encode_datatype(recvtype, &known);
2291   if(known==0)
2292     extra->recv_size = recvcount*smpi_datatype_size(recvtype);
2293   else
2294     extra->recv_size = recvcount;
2295   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2296
2297   retval = mpi_coll_alltoall_fun(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
2298
2299   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2300   }
2301
2302   smpi_bench_begin();
2303   return retval;
2304 }
2305
2306 int PMPI_Alltoallv(void *sendbuf, int *sendcounts, int *senddisps,MPI_Datatype sendtype, void *recvbuf, int *recvcounts,
2307                   int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
2308 {
2309   int retval = 0;
2310
2311   smpi_bench_end();
2312
2313   if (comm == MPI_COMM_NULL) {
2314     retval = MPI_ERR_COMM;
2315   } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
2316     retval = MPI_ERR_TYPE;
2317   } else if (sendcounts == nullptr || senddisps == nullptr || recvcounts == nullptr || recvdisps == nullptr) {
2318     retval = MPI_ERR_ARG;
2319   } else {
2320   int rank = comm != MPI_COMM_NULL ? smpi_process_index() : -1;
2321   int i=0;
2322   int size = smpi_comm_size(comm);
2323   instr_extra_data extra = xbt_new0(s_instr_extra_data_t,1);
2324   extra->type = TRACING_ALLTOALLV;
2325   extra->send_size = 0;
2326   extra->recv_size = 0;
2327   extra->recvcounts= xbt_new(int, size);
2328   extra->sendcounts= xbt_new(int, size);
2329   int known=0;
2330   extra->datatype1 = encode_datatype(sendtype, &known);
2331   int dt_size_send = 1;
2332   if(known==0)
2333     dt_size_send = smpi_datatype_size(sendtype);
2334   int dt_size_recv = 1;
2335   extra->datatype2 = encode_datatype(recvtype, &known);
2336   if(known==0)
2337     dt_size_recv = smpi_datatype_size(recvtype);
2338   for(i=0; i< size; i++){//copy data to avoid bad free
2339     extra->send_size += sendcounts[i]*dt_size_send;
2340     extra->recv_size += recvcounts[i]*dt_size_recv;
2341
2342     extra->sendcounts[i] = sendcounts[i]*dt_size_send;
2343     extra->recvcounts[i] = recvcounts[i]*dt_size_recv;
2344   }
2345   extra->num_processes = size;
2346   TRACE_smpi_collective_in(rank, -1, __FUNCTION__,extra);
2347
2348   retval = mpi_coll_alltoallv_fun(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype,
2349                                   comm);
2350   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2351   }
2352
2353   smpi_bench_begin();
2354   return retval;
2355 }
2356
2357
2358 int PMPI_Get_processor_name(char *name, int *resultlen)
2359 {
2360   int retval = MPI_SUCCESS;
2361
2362   strncpy(name, SIMIX_host_self()->cname(), strlen(SIMIX_host_self()->cname()) < MPI_MAX_PROCESSOR_NAME - 1
2363                                                 ? strlen(SIMIX_host_self()->cname()) + 1
2364                                                 : MPI_MAX_PROCESSOR_NAME - 1);
2365   *resultlen = strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
2366
2367   return retval;
2368 }
2369
2370 int PMPI_Get_count(MPI_Status * status, MPI_Datatype datatype, int *count)
2371 {
2372   int retval = MPI_SUCCESS;
2373   size_t size;
2374
2375   if (status == nullptr || count == nullptr) {
2376     retval = MPI_ERR_ARG;
2377   } else if (!is_datatype_valid(datatype)) {
2378     retval = MPI_ERR_TYPE;
2379   } else {
2380     size = smpi_datatype_size(datatype);
2381     if (size == 0) {
2382       *count = 0;
2383     } else if (status->count % size != 0) {
2384       retval = MPI_UNDEFINED;
2385     } else {
2386       *count = smpi_mpi_get_count(status, datatype);
2387     }
2388   }
2389   return retval;
2390 }
2391
2392 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
2393   int retval = 0;
2394
2395   if (old_type == MPI_DATATYPE_NULL) {
2396     retval = MPI_ERR_TYPE;
2397   } else if (count<0){
2398     retval = MPI_ERR_COUNT;
2399   } else {
2400     retval = smpi_datatype_contiguous(count, old_type, new_type, 0);
2401   }
2402   return retval;
2403 }
2404
2405 int PMPI_Type_commit(MPI_Datatype* datatype) {
2406   int retval = 0;
2407
2408   if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
2409     retval = MPI_ERR_TYPE;
2410   } else {
2411     smpi_datatype_commit(datatype);
2412     retval = MPI_SUCCESS;
2413   }
2414   return retval;
2415 }
2416
2417 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2418   int retval = 0;
2419
2420   if (old_type == MPI_DATATYPE_NULL) {
2421     retval = MPI_ERR_TYPE;
2422   } else if (count<0 || blocklen<0){
2423     retval = MPI_ERR_COUNT;
2424   } else {
2425     retval = smpi_datatype_vector(count, blocklen, stride, old_type, new_type);
2426   }
2427   return retval;
2428 }
2429
2430 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2431   int retval = 0;
2432
2433   if (old_type == MPI_DATATYPE_NULL) {
2434     retval = MPI_ERR_TYPE;
2435   } else if (count<0 || blocklen<0){
2436     retval = MPI_ERR_COUNT;
2437   } else {
2438     retval = smpi_datatype_hvector(count, blocklen, stride, old_type, new_type);
2439   }
2440   return retval;
2441 }
2442
2443 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
2444   return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
2445 }
2446
2447 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2448   int retval = 0;
2449
2450   if (old_type == MPI_DATATYPE_NULL) {
2451     retval = MPI_ERR_TYPE;
2452   } else if (count<0){
2453     retval = MPI_ERR_COUNT;
2454   } else {
2455     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2456   }
2457   return retval;
2458 }
2459
2460 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2461   int retval = 0;
2462
2463   if (old_type == MPI_DATATYPE_NULL) {
2464     retval = MPI_ERR_TYPE;
2465   } else if (count<0){
2466     retval = MPI_ERR_COUNT;
2467   } else {
2468     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2469   }
2470   return retval;
2471 }
2472
2473 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
2474                                    MPI_Datatype* new_type) {
2475   int retval,i;
2476
2477   if (old_type == MPI_DATATYPE_NULL) {
2478     retval = MPI_ERR_TYPE;
2479   } else if (count<0){
2480     retval = MPI_ERR_COUNT;
2481   } else {
2482     int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
2483     for (i=0; i<count;i++)
2484       blocklens[i]=blocklength;
2485     retval = smpi_datatype_indexed(count, blocklens, indices, old_type, new_type);
2486     xbt_free(blocklens);
2487   }
2488   return retval;
2489 }
2490
2491 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
2492   int retval = 0;
2493
2494   if (old_type == MPI_DATATYPE_NULL) {
2495     retval = MPI_ERR_TYPE;
2496   } else if (count<0){
2497     retval = MPI_ERR_COUNT;
2498   } else {
2499     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2500   }
2501   return retval;
2502 }
2503
2504 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
2505                               MPI_Datatype* new_type) {
2506   return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
2507 }
2508
2509 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
2510                                     MPI_Datatype* new_type) {
2511   int retval,i;
2512
2513   if (old_type == MPI_DATATYPE_NULL) {
2514     retval = MPI_ERR_TYPE;
2515   } else if (count<0){
2516     retval = MPI_ERR_COUNT;
2517   } else {
2518     int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
2519     for (i=0; i<count;i++)blocklens[i]=blocklength;
2520     retval = smpi_datatype_hindexed(count, blocklens, indices, old_type, new_type);
2521     xbt_free(blocklens);
2522   }
2523   return retval;
2524 }
2525
2526 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
2527   int retval = 0;
2528
2529   if (count<0){
2530     retval = MPI_ERR_COUNT;
2531   } else {
2532     retval = smpi_datatype_struct(count, blocklens, indices, old_types, new_type);
2533   }
2534   return retval;
2535 }
2536
2537 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
2538                             MPI_Datatype* new_type) {
2539   return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
2540 }
2541
2542 int PMPI_Error_class(int errorcode, int* errorclass) {
2543   // assume smpi uses only standard mpi error codes
2544   *errorclass=errorcode;
2545   return MPI_SUCCESS;
2546 }
2547
2548 int PMPI_Initialized(int* flag) {
2549    *flag=smpi_process_initialized();
2550    return MPI_SUCCESS;
2551 }
2552
2553 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
2554  * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
2555  *  MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
2556
2557 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
2558   int retval = 0;
2559   if (comm_old == MPI_COMM_NULL){
2560     retval =  MPI_ERR_COMM;
2561   } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
2562     retval = MPI_ERR_ARG;
2563   } else{
2564     retval = smpi_mpi_cart_create(comm_old, ndims, dims, periodic, reorder, comm_cart);
2565   }
2566   return retval;
2567 }
2568
2569 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
2570   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2571     return MPI_ERR_TOPOLOGY;
2572   }
2573   if (coords == nullptr) {
2574     return MPI_ERR_ARG;
2575   }
2576   return smpi_mpi_cart_rank(comm, coords, rank);
2577 }
2578
2579 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
2580   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2581     return MPI_ERR_TOPOLOGY;
2582   }
2583   if (source == nullptr || dest == nullptr || direction < 0 ) {
2584     return MPI_ERR_ARG;
2585   }
2586   return smpi_mpi_cart_shift(comm, direction, displ, source, dest);
2587 }
2588
2589 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
2590   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2591     return MPI_ERR_TOPOLOGY;
2592   }
2593   if (rank < 0 || rank >= smpi_comm_size(comm)) {
2594     return MPI_ERR_RANK;
2595   }
2596   if (maxdims <= 0) {
2597     return MPI_ERR_ARG;
2598   }
2599   if(coords == nullptr) {
2600     return MPI_ERR_ARG;
2601   }
2602   return smpi_mpi_cart_coords(comm, rank, maxdims, coords);
2603 }
2604
2605 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
2606   if(comm == nullptr || smpi_comm_topo(comm) == nullptr) {
2607     return MPI_ERR_TOPOLOGY;
2608   }
2609   if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
2610     return MPI_ERR_ARG;
2611   }
2612   return smpi_mpi_cart_get(comm, maxdims, dims, periods, coords);
2613 }
2614
2615 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
2616   if (comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2617     return MPI_ERR_TOPOLOGY;
2618   }
2619   if (ndims == nullptr) {
2620     return MPI_ERR_ARG;
2621   }
2622   return smpi_mpi_cartdim_get(comm, ndims);
2623 }
2624
2625 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
2626   if(dims == nullptr) {
2627     return MPI_ERR_ARG;
2628   }
2629   if (ndims < 1 || nnodes < 1) {
2630     return MPI_ERR_DIMS;
2631   }
2632
2633   return smpi_mpi_dims_create(nnodes, ndims, dims);
2634 }
2635
2636 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
2637   if(comm == MPI_COMM_NULL || smpi_comm_topo(comm) == nullptr) {
2638     return MPI_ERR_TOPOLOGY;
2639   }
2640   if (comm_new == nullptr) {
2641     return MPI_ERR_ARG;
2642   }
2643   return smpi_mpi_cart_sub(comm, remain_dims, comm_new);
2644 }
2645
2646 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
2647     if(oldtype == MPI_DATATYPE_NULL) {
2648         return MPI_ERR_TYPE;
2649     }
2650     int blocks[3] = { 1, 1, 1 };
2651     MPI_Aint disps[3] = { lb, 0, lb+extent };
2652     MPI_Datatype types[3] = { MPI_LB, oldtype, MPI_UB };
2653
2654     s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocks, disps, 3, types);
2655     smpi_datatype_create(newtype,oldtype->size, lb, lb + extent, sizeof(s_smpi_mpi_struct_t) , subtype, DT_FLAG_VECTOR);
2656
2657     (*newtype)->flags &= ~DT_FLAG_COMMITED;
2658     return MPI_SUCCESS;
2659 }
2660
2661 int PMPI_Win_create( void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm, MPI_Win *win){
2662   int retval = 0;
2663   smpi_bench_end();
2664   if (comm == MPI_COMM_NULL) {
2665     retval= MPI_ERR_COMM;
2666   }else if ((base == nullptr && size != 0) || disp_unit <= 0 || size < 0 ){
2667     retval= MPI_ERR_OTHER;
2668   }else{
2669     *win = smpi_mpi_win_create( base, size, disp_unit, info, comm);
2670     retval = MPI_SUCCESS;
2671   }
2672   smpi_bench_begin();
2673   return retval;
2674 }
2675
2676 int PMPI_Win_free( MPI_Win* win){
2677   int retval = 0;
2678   smpi_bench_end();
2679   if (win == nullptr || *win == MPI_WIN_NULL) {
2680     retval = MPI_ERR_WIN;
2681   }else{
2682     retval=smpi_mpi_win_free(win);
2683   }
2684   smpi_bench_begin();
2685   return retval;
2686 }
2687
2688 int PMPI_Win_set_name(MPI_Win  win, char * name)
2689 {
2690   int retval = 0;
2691   if (win == MPI_WIN_NULL)  {
2692     retval = MPI_ERR_TYPE;
2693   } else if (name == nullptr)  {
2694     retval = MPI_ERR_ARG;
2695   } else {
2696     smpi_mpi_win_set_name(win, name);
2697     retval = MPI_SUCCESS;
2698   }
2699   return retval;
2700 }
2701
2702 int PMPI_Win_get_name(MPI_Win  win, char * name, int* len)
2703 {
2704   int retval = MPI_SUCCESS;
2705
2706   if (win == MPI_WIN_NULL)  {
2707     retval = MPI_ERR_WIN;
2708   } else if (name == nullptr)  {
2709     retval = MPI_ERR_ARG;
2710   } else {
2711     smpi_mpi_win_get_name(win, name, len);
2712   }
2713   return retval;
2714 }
2715
2716 int PMPI_Win_get_group(MPI_Win  win, MPI_Group * group){
2717   int retval = MPI_SUCCESS;
2718   if (win == MPI_WIN_NULL)  {
2719     retval = MPI_ERR_WIN;
2720   }else {
2721     smpi_mpi_win_get_group(win, group);
2722     smpi_group_use(*group);
2723   }
2724   return retval;
2725 }
2726
2727
2728 int PMPI_Win_fence( int assert,  MPI_Win win){
2729   int retval = 0;
2730   smpi_bench_end();
2731   if (win == MPI_WIN_NULL) {
2732     retval = MPI_ERR_WIN;
2733   } else {
2734   int rank = smpi_process_index();
2735   TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2736   retval = smpi_mpi_win_fence(assert, win);
2737   TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2738   }
2739   smpi_bench_begin();
2740   return retval;
2741 }
2742
2743 int PMPI_Get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2744               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2745   int retval = 0;
2746   smpi_bench_end();
2747   if (win == MPI_WIN_NULL) {
2748     retval = MPI_ERR_WIN;
2749   } else if (target_rank == MPI_PROC_NULL) {
2750     retval = MPI_SUCCESS;
2751   } else if (target_rank <0){
2752     retval = MPI_ERR_RANK;
2753   } else if (target_disp <0){
2754       retval = MPI_ERR_ARG;
2755   } else if ((origin_count < 0 || target_count < 0) ||
2756              (origin_addr==nullptr && origin_count > 0)){
2757     retval = MPI_ERR_COUNT;
2758   } else if ((!is_datatype_valid(origin_datatype)) || (!is_datatype_valid(target_datatype))) {
2759     retval = MPI_ERR_TYPE;
2760   } else {
2761     int rank = smpi_process_index();
2762     MPI_Group group;
2763     smpi_mpi_win_get_group(win, &group);
2764     int src_traced = smpi_group_index(group, target_rank);
2765     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2766
2767     retval = smpi_mpi_get( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2768                            target_datatype, win);
2769
2770     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2771   }
2772   smpi_bench_begin();
2773   return retval;
2774 }
2775
2776 int PMPI_Put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2777               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Win win){
2778   int retval = 0;
2779   smpi_bench_end();
2780   if (win == MPI_WIN_NULL) {
2781     retval = MPI_ERR_WIN;
2782   } else if (target_rank == MPI_PROC_NULL) {
2783     retval = MPI_SUCCESS;
2784   } else if (target_rank <0){
2785     retval = MPI_ERR_RANK;
2786   } else if (target_disp <0){
2787     retval = MPI_ERR_ARG;
2788   } else if ((origin_count < 0 || target_count < 0) ||
2789             (origin_addr==nullptr && origin_count > 0)){
2790     retval = MPI_ERR_COUNT;
2791   } else if ((!is_datatype_valid(origin_datatype)) || (!is_datatype_valid(target_datatype))) {
2792     retval = MPI_ERR_TYPE;
2793   } else {
2794     int rank = smpi_process_index();
2795     MPI_Group group;
2796     smpi_mpi_win_get_group(win, &group);
2797     int dst_traced = smpi_group_index(group, target_rank);
2798     TRACE_smpi_ptp_in(rank, rank, dst_traced, __FUNCTION__, nullptr);
2799     TRACE_smpi_send(rank, rank, dst_traced, SMPI_RMA_TAG, origin_count*smpi_datatype_size(origin_datatype));
2800
2801     retval = smpi_mpi_put( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2802                            target_datatype, win);
2803
2804     TRACE_smpi_ptp_out(rank, rank, dst_traced, __FUNCTION__);
2805   }
2806   smpi_bench_begin();
2807   return retval;
2808 }
2809
2810 int PMPI_Accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
2811               MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Win win){
2812   int retval = 0;
2813   smpi_bench_end();
2814   if (win == MPI_WIN_NULL) {
2815     retval = MPI_ERR_WIN;
2816   } else if (target_rank == MPI_PROC_NULL) {
2817     retval = MPI_SUCCESS;
2818   } else if (target_rank <0){
2819     retval = MPI_ERR_RANK;
2820   } else if (target_disp <0){
2821     retval = MPI_ERR_ARG;
2822   } else if ((origin_count < 0 || target_count < 0) ||
2823              (origin_addr==nullptr && origin_count > 0)){
2824     retval = MPI_ERR_COUNT;
2825   } else if ((!is_datatype_valid(origin_datatype)) ||
2826             (!is_datatype_valid(target_datatype))) {
2827     retval = MPI_ERR_TYPE;
2828   } else if (op == MPI_OP_NULL) {
2829     retval = MPI_ERR_OP;
2830   } else {
2831     int rank = smpi_process_index();
2832     MPI_Group group;
2833     smpi_mpi_win_get_group(win, &group);
2834     int src_traced = smpi_group_index(group, target_rank);
2835     TRACE_smpi_ptp_in(rank, src_traced, rank, __FUNCTION__, nullptr);
2836
2837     retval = smpi_mpi_accumulate( origin_addr, origin_count, origin_datatype, target_rank, target_disp, target_count,
2838                                   target_datatype, op, win);
2839
2840     TRACE_smpi_ptp_out(rank, src_traced, rank, __FUNCTION__);
2841   }
2842   smpi_bench_begin();
2843   return retval;
2844 }
2845
2846 int PMPI_Win_post(MPI_Group group, int assert, MPI_Win win){
2847   int retval = 0;
2848   smpi_bench_end();
2849   if (win == MPI_WIN_NULL) {
2850     retval = MPI_ERR_WIN;
2851   } else if (group==MPI_GROUP_NULL){
2852     retval = MPI_ERR_GROUP;
2853   }
2854   else {
2855     int rank = smpi_process_index();
2856     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2857     retval = smpi_mpi_win_post(group,assert,win);
2858     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2859   }
2860   smpi_bench_begin();
2861   return retval;
2862 }
2863
2864 int PMPI_Win_start(MPI_Group group, int assert, MPI_Win win){
2865   int retval = 0;
2866   smpi_bench_end();
2867   if (win == MPI_WIN_NULL) {
2868     retval = MPI_ERR_WIN;
2869   } else if (group==MPI_GROUP_NULL){
2870     retval = MPI_ERR_GROUP;
2871   }
2872   else {
2873     int rank = smpi_process_index();
2874     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2875     retval = smpi_mpi_win_start(group,assert,win);
2876     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2877   }
2878   smpi_bench_begin();
2879   return retval;
2880 }
2881
2882 int PMPI_Win_complete(MPI_Win win){
2883   int retval = 0;
2884   smpi_bench_end();
2885   if (win == MPI_WIN_NULL) {
2886     retval = MPI_ERR_WIN;
2887   }
2888   else {
2889     int rank = smpi_process_index();
2890     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2891
2892     retval = smpi_mpi_win_complete(win);
2893
2894     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2895   }
2896   smpi_bench_begin();
2897   return retval;
2898 }
2899
2900 int PMPI_Win_wait(MPI_Win win){
2901   int retval = 0;
2902   smpi_bench_end();
2903   if (win == MPI_WIN_NULL) {
2904     retval = MPI_ERR_WIN;
2905   }
2906   else {
2907     int rank = smpi_process_index();
2908     TRACE_smpi_collective_in(rank, -1, __FUNCTION__, nullptr);
2909
2910     retval = smpi_mpi_win_wait(win);
2911
2912     TRACE_smpi_collective_out(rank, -1, __FUNCTION__);
2913   }
2914   smpi_bench_begin();
2915   return retval;
2916 }
2917
2918 int PMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr){
2919   void *ptr = xbt_malloc(size);
2920   if(ptr==nullptr)
2921     return MPI_ERR_NO_MEM;
2922   else {
2923     *static_cast<void**>(baseptr) = ptr;
2924     return MPI_SUCCESS;
2925   }
2926 }
2927
2928 int PMPI_Free_mem(void *baseptr){
2929   xbt_free(baseptr);
2930   return MPI_SUCCESS;
2931 }
2932
2933 int PMPI_Type_set_name(MPI_Datatype  datatype, char * name)
2934 {
2935   int retval = 0;
2936   if (datatype == MPI_DATATYPE_NULL)  {
2937     retval = MPI_ERR_TYPE;
2938   } else if (name == nullptr)  {
2939     retval = MPI_ERR_ARG;
2940   } else {
2941     smpi_datatype_set_name(datatype, name);
2942     retval = MPI_SUCCESS;
2943   }
2944   return retval;
2945 }
2946
2947 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
2948 {
2949   int retval = 0;
2950
2951   if (datatype == MPI_DATATYPE_NULL)  {
2952     retval = MPI_ERR_TYPE;
2953   } else if (name == nullptr)  {
2954     retval = MPI_ERR_ARG;
2955   } else {
2956     smpi_datatype_get_name(datatype, name, len);
2957     retval = MPI_SUCCESS;
2958   }
2959   return retval;
2960 }
2961
2962 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
2963   return smpi_type_f2c(datatype);
2964 }
2965
2966 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
2967   return smpi_type_c2f( datatype);
2968 }
2969
2970 MPI_Group PMPI_Group_f2c(MPI_Fint group){
2971   return smpi_group_f2c( group);
2972 }
2973
2974 MPI_Fint PMPI_Group_c2f(MPI_Group group){
2975   return smpi_group_c2f(group);
2976 }
2977
2978 MPI_Request PMPI_Request_f2c(MPI_Fint request){
2979   return smpi_request_f2c(request);
2980 }
2981
2982 MPI_Fint PMPI_Request_c2f(MPI_Request request) {
2983   return smpi_request_c2f(request);
2984 }
2985
2986 MPI_Win PMPI_Win_f2c(MPI_Fint win){
2987   return smpi_win_f2c(win);
2988 }
2989
2990 MPI_Fint PMPI_Win_c2f(MPI_Win win){
2991   return smpi_win_c2f(win);
2992 }
2993
2994 MPI_Op PMPI_Op_f2c(MPI_Fint op){
2995   return smpi_op_f2c(op);
2996 }
2997
2998 MPI_Fint PMPI_Op_c2f(MPI_Op op){
2999   return smpi_op_c2f(op);
3000 }
3001
3002 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
3003   return smpi_comm_f2c(comm);
3004 }
3005
3006 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
3007   return smpi_comm_c2f(comm);
3008 }
3009
3010 MPI_Info PMPI_Info_f2c(MPI_Fint info){
3011   return smpi_info_f2c(info);
3012 }
3013
3014 MPI_Fint PMPI_Info_c2f(MPI_Info info){
3015   return smpi_info_c2f(info);
3016 }
3017
3018 int PMPI_Keyval_create(MPI_Copy_function* copy_fn, MPI_Delete_function* delete_fn, int* keyval, void* extra_state) {
3019   return smpi_comm_keyval_create(copy_fn, delete_fn, keyval, extra_state);
3020 }
3021
3022 int PMPI_Keyval_free(int* keyval) {
3023   return smpi_comm_keyval_free(keyval);
3024 }
3025
3026 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
3027   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3028        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3029     return MPI_ERR_ARG;
3030   else if (comm==MPI_COMM_NULL)
3031     return MPI_ERR_COMM;
3032   else
3033     return smpi_comm_attr_delete(comm, keyval);
3034 }
3035
3036 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
3037   static int one = 1;
3038   static int zero = 0;
3039   static int tag_ub = 1000000;
3040   static int last_used_code = MPI_ERR_LASTCODE;
3041
3042   if (comm==MPI_COMM_NULL){
3043     *flag = 0;
3044     return MPI_ERR_COMM;
3045   }
3046
3047   switch (keyval) {
3048   case MPI_HOST:
3049   case MPI_IO:
3050   case MPI_APPNUM:
3051     *flag = 1;
3052     *static_cast<int**>(attr_value) = &zero;
3053     return MPI_SUCCESS;
3054   case MPI_UNIVERSE_SIZE:
3055     *flag = 1;
3056     *static_cast<int**>(attr_value) = &smpi_universe_size;
3057     return MPI_SUCCESS;
3058   case MPI_LASTUSEDCODE:
3059     *flag = 1;
3060     *static_cast<int**>(attr_value) = &last_used_code;
3061     return MPI_SUCCESS;
3062   case MPI_TAG_UB:
3063     *flag=1;
3064     *static_cast<int**>(attr_value) = &tag_ub;
3065     return MPI_SUCCESS;
3066   case MPI_WTIME_IS_GLOBAL:
3067     *flag = 1;
3068     *static_cast<int**>(attr_value) = &one;
3069     return MPI_SUCCESS;
3070   default:
3071     return smpi_comm_attr_get(comm, keyval, attr_value, flag);
3072   }
3073 }
3074
3075 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
3076   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
3077        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
3078     return MPI_ERR_ARG;
3079   else if (comm==MPI_COMM_NULL)
3080     return MPI_ERR_COMM;
3081   else
3082   return smpi_comm_attr_put(comm, keyval, attr_value);
3083 }
3084
3085 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
3086 {
3087   return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
3088 }
3089
3090 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
3091 {
3092   return PMPI_Attr_put(comm, comm_keyval, attribute_val);
3093 }
3094
3095 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
3096 {
3097   return PMPI_Attr_delete(comm, comm_keyval);
3098 }
3099
3100 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
3101                             void* extra_state)
3102 {
3103   return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
3104 }
3105
3106 int PMPI_Comm_free_keyval(int* keyval) {
3107   return PMPI_Keyval_free(keyval);
3108 }
3109
3110 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
3111 {
3112   if (type==MPI_DATATYPE_NULL)
3113     return MPI_ERR_TYPE;
3114   else
3115     return smpi_type_attr_get(type, type_keyval, attribute_val, flag);
3116 }
3117
3118 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
3119 {
3120   if (type==MPI_DATATYPE_NULL)
3121     return MPI_ERR_TYPE;
3122   else
3123     return smpi_type_attr_put(type, type_keyval, attribute_val);
3124 }
3125
3126 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
3127 {
3128   if (type==MPI_DATATYPE_NULL)
3129     return MPI_ERR_TYPE;
3130   else
3131     return smpi_type_attr_delete(type, type_keyval);
3132 }
3133
3134 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
3135                             void* extra_state)
3136 {
3137   return smpi_type_keyval_create(copy_fn, delete_fn, keyval, extra_state);
3138 }
3139
3140 int PMPI_Type_free_keyval(int* keyval) {
3141   return smpi_type_keyval_free(keyval);
3142 }
3143
3144 int PMPI_Info_create( MPI_Info *info){
3145   if (info == nullptr)
3146     return MPI_ERR_ARG;
3147   *info = xbt_new(s_smpi_mpi_info_t, 1);
3148   (*info)->info_dict= xbt_dict_new_homogeneous(xbt_free_f);
3149   (*info)->refcount=1;
3150   return MPI_SUCCESS;
3151 }
3152
3153 int PMPI_Info_set( MPI_Info info, char *key, char *value){
3154   if (info == nullptr || key == nullptr || value == nullptr)
3155     return MPI_ERR_ARG;
3156
3157   xbt_dict_set(info->info_dict, key, xbt_strdup(value), nullptr);
3158   return MPI_SUCCESS;
3159 }
3160
3161 int PMPI_Info_free( MPI_Info *info){
3162   if (info == nullptr || *info==nullptr)
3163     return MPI_ERR_ARG;
3164   (*info)->refcount--;
3165   if((*info)->refcount==0){
3166     xbt_dict_free(&((*info)->info_dict));
3167     xbt_free(*info);
3168   }
3169   *info=MPI_INFO_NULL;
3170   return MPI_SUCCESS;
3171 }
3172
3173 int PMPI_Info_get(MPI_Info info,char *key,int valuelen, char *value, int *flag){
3174   *flag=false;
3175   if (info == nullptr || key == nullptr || valuelen <0)
3176     return MPI_ERR_ARG;
3177   if (value == nullptr)
3178     return MPI_ERR_INFO_VALUE;
3179   char* tmpvalue=static_cast<char*>(xbt_dict_get_or_null(info->info_dict, key));
3180   if(tmpvalue){
3181     memset(value, 0, valuelen);
3182     memcpy(value,tmpvalue, (strlen(tmpvalue) + 1 < static_cast<size_t>(valuelen)) ? strlen(tmpvalue) + 1 : valuelen);
3183     *flag=true;
3184   }
3185   return MPI_SUCCESS;
3186 }
3187
3188 int PMPI_Info_dup(MPI_Info info, MPI_Info *newinfo){
3189   if (info == nullptr || newinfo==nullptr)
3190     return MPI_ERR_ARG;
3191   *newinfo = xbt_new(s_smpi_mpi_info_t, 1);
3192   (*newinfo)->info_dict= xbt_dict_new_homogeneous(xbt_free_f);
3193   (*newinfo)->refcount=1;
3194   xbt_dict_cursor_t cursor = nullptr;
3195   int *key;
3196   void* data;
3197   xbt_dict_foreach(info->info_dict,cursor,key,data){
3198     xbt_dict_set((*newinfo)->info_dict, reinterpret_cast<char*>(key), xbt_strdup(reinterpret_cast<char*>(data)), nullptr);
3199   }
3200   return MPI_SUCCESS;
3201 }
3202
3203 int PMPI_Info_delete(MPI_Info info, char *key){
3204   if (info == nullptr || key==nullptr)
3205     return MPI_ERR_ARG;
3206   try {
3207     xbt_dict_remove(info->info_dict, key);
3208   }
3209   catch(xbt_ex& e){
3210     return MPI_ERR_INFO_NOKEY;
3211   }
3212   return MPI_SUCCESS;
3213 }
3214
3215 int PMPI_Info_get_nkeys( MPI_Info info, int *nkeys){
3216   if (info == nullptr || nkeys==nullptr)
3217     return MPI_ERR_ARG;
3218   *nkeys=xbt_dict_size(info->info_dict);
3219   return MPI_SUCCESS;
3220 }
3221
3222 int PMPI_Info_get_nthkey( MPI_Info info, int n, char *key){
3223   if (info == nullptr || key==nullptr || n<0 || n> MPI_MAX_INFO_KEY)
3224     return MPI_ERR_ARG;
3225
3226   xbt_dict_cursor_t cursor = nullptr;
3227   char *keyn;
3228   void* data;
3229   int num=0;
3230   xbt_dict_foreach(info->info_dict,cursor,keyn,data){
3231     if(num==n){
3232       strncpy(key,keyn,strlen(keyn)+1);
3233       xbt_dict_cursor_free(&cursor);
3234       return MPI_SUCCESS;
3235     }
3236     num++;
3237   }
3238   return MPI_ERR_ARG;
3239 }
3240
3241 int PMPI_Info_get_valuelen( MPI_Info info, char *key, int *valuelen, int *flag){
3242   *flag=false;
3243   if (info == nullptr || key == nullptr || valuelen==nullptr)
3244     return MPI_ERR_ARG;
3245   char* tmpvalue=(char*)xbt_dict_get_or_null(info->info_dict, key);
3246   if(tmpvalue){
3247     *valuelen=strlen(tmpvalue);
3248     *flag=true;
3249   }
3250   return MPI_SUCCESS;
3251 }
3252
3253 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
3254   if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
3255     return MPI_ERR_ARG;
3256   if(!is_datatype_valid(type))
3257     return MPI_ERR_TYPE;
3258   if(comm==MPI_COMM_NULL)
3259     return MPI_ERR_COMM;
3260   return smpi_mpi_unpack(inbuf, incount, position, outbuf,outcount,type, comm);
3261 }
3262
3263 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
3264   if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
3265     return MPI_ERR_ARG;
3266   if(!is_datatype_valid(type))
3267     return MPI_ERR_TYPE;
3268   if(comm==MPI_COMM_NULL)
3269     return MPI_ERR_COMM;
3270   return smpi_mpi_pack(inbuf, incount, type, outbuf,outcount,position, comm);
3271 }
3272
3273 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
3274   if(incount<0)
3275     return MPI_ERR_ARG;
3276   if(!is_datatype_valid(datatype))
3277     return MPI_ERR_TYPE;
3278   if(comm==MPI_COMM_NULL)
3279     return MPI_ERR_COMM;
3280
3281   *size=incount*smpi_datatype_size(datatype);
3282
3283   return MPI_SUCCESS;
3284 }
3285