Logo AND Algorithmique Numérique Distribuée

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