Logo AND Algorithmique Numérique Distribuée

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