Logo AND Algorithmique Numérique Distribuée

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