Logo AND Algorithmique Numérique Distribuée

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