Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
initial instrumentation points for the smpi component
[simgrid.git] / src / smpi / smpi_mpi.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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_coll_private.h"
9 #include "smpi_mpi_dt_private.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi, smpi,
12                                 "Logging specific to SMPI (mpi)");
13
14 /* MPI User level calls */
15
16 int MPI_Init(int* argc, char*** argv) {
17   smpi_process_init(argc, argv);
18   smpi_bench_begin(-1, NULL);
19 #ifdef HAVE_TRACING
20   TRACE_smpi_init(smpi_process_index());
21 #endif
22   return MPI_SUCCESS;
23 }
24
25 int MPI_Finalize(void) {
26 #ifdef HAVE_TRACING
27   TRACE_smpi_finalize(smpi_process_index());
28 #endif
29   smpi_bench_end(-1, NULL);
30   smpi_process_destroy();
31   return MPI_SUCCESS;
32 }
33
34 int MPI_Init_thread(int* argc, char*** argv, int required, int* provided) {
35   if(provided != NULL) {
36     *provided = MPI_THREAD_MULTIPLE;
37   }
38   return MPI_Init(argc, argv);
39 }
40
41 int MPI_Query_thread(int* provided) {
42   int retval;
43
44   smpi_bench_end(-1, NULL);
45   if(provided == NULL) {
46     retval = MPI_ERR_ARG;
47   } else {
48     *provided = MPI_THREAD_MULTIPLE;
49     retval = MPI_SUCCESS;
50   }
51   smpi_bench_begin(-1, NULL);
52   return retval;
53 }
54
55 int MPI_Is_thread_main(int* flag) {
56   int retval;
57
58   smpi_bench_end(-1, NULL);
59   if(flag == NULL) {
60     retval = MPI_ERR_ARG;
61   } else {
62     *flag = smpi_process_index() == 0;
63     retval = MPI_SUCCESS;
64   }
65   smpi_bench_begin(-1, NULL);
66   return retval;
67 }
68
69 int MPI_Abort(MPI_Comm comm, int errorcode) {
70   smpi_bench_end(-1, NULL);
71   smpi_process_destroy();
72   // FIXME: should kill all processes in comm instead
73   SIMIX_process_kill(SIMIX_process_self());
74   return MPI_SUCCESS;
75 }
76
77 double MPI_Wtime(void) {
78   double time;
79
80   smpi_bench_end(-1, NULL);
81   time = SIMIX_get_clock();
82   smpi_bench_begin(-1, NULL);
83   return time;
84 }
85
86 int MPI_Address(void *location, MPI_Aint *address) {
87   int retval;
88
89   smpi_bench_end(-1, NULL);
90   if(!address) {
91     retval = MPI_ERR_ARG;
92   } else {
93     *address = (MPI_Aint)location;
94   }
95   smpi_bench_begin(-1, NULL);
96   return retval;
97 }
98
99 int MPI_Type_free(MPI_Datatype* datatype) {
100   int retval;
101
102   smpi_bench_end(-1, NULL);
103   if(!datatype) {
104     retval = MPI_ERR_ARG;
105   } else {
106     // FIXME: always fail for now
107     retval = MPI_ERR_TYPE;
108   }
109   smpi_bench_begin(-1, NULL);
110   return retval;
111 }
112
113 int MPI_Type_size(MPI_Datatype datatype, size_t* size) {
114   int retval;
115
116   smpi_bench_end(-1, NULL);
117   if(datatype == MPI_DATATYPE_NULL) {
118     retval = MPI_ERR_TYPE;
119   } else if(size == NULL) {
120     retval = MPI_ERR_ARG;
121   } else {
122     *size = smpi_datatype_size(datatype);
123     retval = MPI_SUCCESS;
124   }
125   smpi_bench_begin(-1, NULL);
126   return retval;
127 }
128
129 int MPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint* lb, MPI_Aint* extent) {
130   int retval;
131
132   smpi_bench_end(-1, NULL);
133   if(datatype == MPI_DATATYPE_NULL) {
134     retval = MPI_ERR_TYPE;
135   } else if(lb == NULL || extent == NULL) {
136     retval = MPI_ERR_ARG;
137   } else {
138     retval = smpi_datatype_extent(datatype, lb, extent);
139   }
140   smpi_bench_begin(-1, NULL);
141   return retval;
142 }
143
144 int MPI_Type_extent(MPI_Datatype datatype, MPI_Aint* extent) {
145   int retval;
146   MPI_Aint dummy;
147
148   smpi_bench_end(-1, NULL);
149   if(datatype == MPI_DATATYPE_NULL) {
150     retval = MPI_ERR_TYPE;
151   } else if(extent == NULL) {
152     retval = MPI_ERR_ARG;
153   } else {
154     retval = smpi_datatype_extent(datatype, &dummy, extent);
155   }
156   smpi_bench_begin(-1, NULL);
157   return retval;
158 }
159
160 int MPI_Type_lb(MPI_Datatype datatype, MPI_Aint* disp) {
161   int retval;
162
163   smpi_bench_end(-1, NULL);
164   if(datatype == MPI_DATATYPE_NULL) {
165     retval = MPI_ERR_TYPE;
166   } else if(disp == NULL) {
167     retval = MPI_ERR_ARG;
168   } else {
169     *disp = smpi_datatype_lb(datatype);
170     retval = MPI_SUCCESS;
171   }
172   smpi_bench_begin(-1, NULL);
173   return retval;
174 }
175
176 int MPI_Type_ub(MPI_Datatype datatype, MPI_Aint* disp) {
177   int retval;
178
179   smpi_bench_end(-1, NULL);
180   if(datatype == MPI_DATATYPE_NULL) {
181     retval = MPI_ERR_TYPE;
182   } else if(disp == NULL) {
183     retval = MPI_ERR_ARG;
184   } else {
185     *disp = smpi_datatype_ub(datatype);
186     retval = MPI_SUCCESS;
187   }
188   smpi_bench_begin(-1, NULL);
189   return retval;
190 }
191
192 int MPI_Op_create(MPI_User_function* function, int commute, MPI_Op* op) {
193   int retval;
194
195   smpi_bench_end(-1, NULL);
196   if(function == NULL || op == NULL) {
197     retval = MPI_ERR_ARG;
198   } else {
199     *op = smpi_op_new(function, commute);
200     retval = MPI_SUCCESS;
201   }
202   smpi_bench_begin(-1, NULL);
203   return retval;
204 }
205
206 int MPI_Op_free(MPI_Op* op) {
207   int retval;
208
209   smpi_bench_end(-1, NULL);
210   if(op == NULL) {
211     retval = MPI_ERR_ARG;
212   } else if(*op == MPI_OP_NULL) {
213     retval = MPI_ERR_OP;
214   } else {
215     smpi_op_destroy(*op);
216     *op = MPI_OP_NULL;
217     retval = MPI_SUCCESS;
218   }
219   smpi_bench_begin(-1, NULL);
220   return retval;
221 }
222
223 int MPI_Group_free(MPI_Group *group) {
224   int retval;
225
226   smpi_bench_end(-1, NULL);
227   if(group == NULL) {
228     retval = MPI_ERR_ARG;
229   } else {
230     smpi_group_destroy(*group);
231     *group = MPI_GROUP_NULL;
232     retval = MPI_SUCCESS;
233   }
234   smpi_bench_begin(-1, NULL);
235   return retval;
236 }
237
238 int MPI_Group_size(MPI_Group group, int* size) {
239   int retval;
240
241   smpi_bench_end(-1, NULL);
242   if(group == MPI_GROUP_NULL) {
243     retval = MPI_ERR_GROUP;
244   } else if(size == NULL) {
245     retval = MPI_ERR_ARG;
246   } else {
247     *size = smpi_group_size(group);
248     retval = MPI_SUCCESS;
249   }
250   smpi_bench_begin(-1, NULL);
251   return retval;
252 }
253
254 int MPI_Group_rank(MPI_Group group, int* rank) {
255   int retval;
256
257   smpi_bench_end(-1, NULL);
258   if(group == MPI_GROUP_NULL) {
259     retval = MPI_ERR_GROUP;
260   } else if(rank == NULL) {
261     retval = MPI_ERR_ARG;
262   } else {
263     *rank = smpi_group_rank(group, smpi_process_index());
264     retval = MPI_SUCCESS;
265   }
266   smpi_bench_begin(-1, NULL);
267   return retval;
268 }
269
270 int MPI_Group_translate_ranks (MPI_Group group1, int n, int* ranks1, MPI_Group group2, int* ranks2) {
271   int retval, i, index;
272
273   smpi_bench_end(-1, NULL);
274   if(group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
275     retval = MPI_ERR_GROUP;
276   } else {
277     for(i = 0; i < n; i++) {
278       index = smpi_group_index(group1, ranks1[i]);
279       ranks2[i] = smpi_group_rank(group2, index);
280     }
281     retval = MPI_SUCCESS;
282   }
283   smpi_bench_begin(-1, NULL);
284   return retval;
285 }
286
287 int MPI_Group_compare(MPI_Group group1, MPI_Group group2, int* result) {
288   int retval;
289
290   smpi_bench_end(-1, NULL);
291   if(group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
292     retval = MPI_ERR_GROUP;
293   } else if(result == NULL) {
294     retval = MPI_ERR_ARG;
295   } else {
296     *result = smpi_group_compare(group1, group2);
297     retval = MPI_SUCCESS;
298   }
299   smpi_bench_begin(-1, NULL);
300   return retval;
301 }
302
303 int MPI_Group_union(MPI_Group group1, MPI_Group group2, MPI_Group* newgroup) {
304   int retval, i, proc1, proc2, size, size2;
305
306   smpi_bench_end(-1, NULL);
307   if(group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
308     retval = MPI_ERR_GROUP;
309   } else if(newgroup == NULL) {
310     retval = MPI_ERR_ARG;
311   } else {
312     size = smpi_group_size(group1);
313     size2 = smpi_group_size(group2);
314     for(i = 0; i < size2; i++) {
315       proc2 = smpi_group_index(group2, i);
316       proc1 = smpi_group_rank(group1, proc2);
317       if(proc1 == MPI_UNDEFINED) {
318         size++;
319       }
320     }
321     if(size == 0) {
322       *newgroup = MPI_GROUP_EMPTY;
323     } else {
324       *newgroup = smpi_group_new(size);
325       size2 = smpi_group_size(group1);
326       for(i = 0; i < size2; i++) {
327         proc1 = smpi_group_index(group1, i);
328         smpi_group_set_mapping(*newgroup, proc1, i);
329       }
330       for(i = size2; i < size; i++) {
331         proc2 = smpi_group_index(group2, i - size2);
332         smpi_group_set_mapping(*newgroup, proc2, i);
333       }
334     }
335     smpi_group_use(*newgroup);
336     retval = MPI_SUCCESS;
337   }
338   smpi_bench_begin(-1, NULL);
339   return retval;
340 }
341
342 int MPI_Group_intersection(MPI_Group group1, MPI_Group group2, MPI_Group* newgroup) {
343    int retval, i, proc1, proc2, size, size2;
344
345   smpi_bench_end(-1, NULL);
346   if(group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
347     retval = MPI_ERR_GROUP;
348   } else if(newgroup == NULL) {
349     retval = MPI_ERR_ARG;
350   } else {
351     size = smpi_group_size(group1);
352     size2 = smpi_group_size(group2);
353     for(i = 0; i < size2; i++) {
354       proc2 = smpi_group_index(group2, i);
355       proc1 = smpi_group_rank(group1, proc2);
356       if(proc1 == MPI_UNDEFINED) {
357         size--;
358       }
359     }
360     if(size == 0) {
361       *newgroup = MPI_GROUP_EMPTY;
362     } else {
363       *newgroup = smpi_group_new(size);
364       size2 = smpi_group_size(group1);
365       for(i = 0; i < size2; i++) {
366         proc1 = smpi_group_index(group1, i);
367         proc2 = smpi_group_rank(group2, proc1);
368         if(proc2 != MPI_UNDEFINED) {
369           smpi_group_set_mapping(*newgroup, proc1, i);
370         }
371       }
372     }
373     smpi_group_use(*newgroup);
374     retval = MPI_SUCCESS;
375   }
376   smpi_bench_begin(-1, NULL);
377   return retval;
378 }
379
380 int MPI_Group_difference(MPI_Group group1, MPI_Group group2, MPI_Group* newgroup) {
381   int retval, i, proc1, proc2, size, size2;
382
383   smpi_bench_end(-1, NULL);
384   if(group1 == MPI_GROUP_NULL || group2 == MPI_GROUP_NULL) {
385     retval = MPI_ERR_GROUP;
386   } else if(newgroup == NULL) {
387     retval = MPI_ERR_ARG;
388   } else {
389     size = size2 = smpi_group_size(group1);
390     for(i = 0; i < size2; i++) {
391       proc1 = smpi_group_index(group1, i);
392       proc2 = smpi_group_rank(group2, proc1);
393       if(proc2 != MPI_UNDEFINED) {
394         size--;
395       }
396     }
397     if(size == 0) {
398       *newgroup = MPI_GROUP_EMPTY;
399     } else {
400       *newgroup = smpi_group_new(size);
401       for(i = 0; i < size2; i++) {
402         proc1 = smpi_group_index(group1, i);
403         proc2 = smpi_group_rank(group2, proc1);
404         if(proc2 == MPI_UNDEFINED) {
405           smpi_group_set_mapping(*newgroup, proc1, i);
406         }
407       }
408     }
409     smpi_group_use(*newgroup);
410     retval = MPI_SUCCESS;
411   }
412   smpi_bench_begin(-1, NULL);
413   return retval;
414 }
415
416 int MPI_Group_incl(MPI_Group group, int n, int* ranks, MPI_Group* newgroup) {
417   int retval, i, index;
418
419   smpi_bench_end(-1, NULL);
420   if(group == MPI_GROUP_NULL) {
421     retval = MPI_ERR_GROUP;
422   } else if(newgroup == NULL) {
423     retval = MPI_ERR_ARG;
424   } else {
425     if(n == 0) {
426       *newgroup = MPI_GROUP_EMPTY;
427     } else if(n == smpi_group_size(group)) {
428       *newgroup = group;
429     } else {
430       *newgroup = smpi_group_new(n);
431       for(i = 0; i < n; i++) {
432         index = smpi_group_index(group, ranks[i]);
433         smpi_group_set_mapping(*newgroup, index, i);
434       }
435     }
436     smpi_group_use(*newgroup);
437     retval = MPI_SUCCESS;
438   }
439   smpi_bench_begin(-1, NULL);
440   return retval;
441 }
442
443 int MPI_Group_excl(MPI_Group group, int n, int* ranks, MPI_Group* newgroup) {
444   int retval, i, size, rank, index;
445
446   smpi_bench_end(-1, NULL);
447   if(group == MPI_GROUP_NULL) {
448     retval = MPI_ERR_GROUP;
449   } else if(newgroup == NULL) {
450     retval = MPI_ERR_ARG;
451   } else {
452     if(n == 0) {
453       *newgroup = group;
454     } else if(n == smpi_group_size(group)) {
455       *newgroup = MPI_GROUP_EMPTY;
456     } else {
457       size = smpi_group_size(group) - n;
458       *newgroup = smpi_group_new(size);
459       rank = 0;
460       while(rank < size) {
461         for(i = 0; i < n; i++) {
462           if(ranks[i] == rank) {
463             break;
464           }
465         }
466         if(i >= n) {
467           index = smpi_group_index(group, rank);
468           smpi_group_set_mapping(*newgroup, index, rank);
469           rank++;
470         }
471       }
472     }
473     smpi_group_use(*newgroup);
474     retval = MPI_SUCCESS;
475   }
476   smpi_bench_begin(-1, NULL);
477   return retval;
478 }
479
480 int MPI_Group_range_incl(MPI_Group group, int n, int ranges[][3], MPI_Group* newgroup) {
481   int retval, i, j, rank, size, index;
482
483   smpi_bench_end(-1, NULL);
484   if(group == MPI_GROUP_NULL) {
485     retval = MPI_ERR_GROUP;
486   } else if(newgroup == NULL) {
487     retval = MPI_ERR_ARG;
488   } else {
489     if(n == 0) {
490       *newgroup = MPI_GROUP_EMPTY;
491     } else {
492       size = 0;
493       for(i = 0; i < n; i++) {
494         for(rank = ranges[i][0]; /* First */
495             rank >= 0 && rank <= ranges[i][1]; /* Last */
496             rank += ranges[i][2] /* Stride */) {
497           size++;
498         }
499       }
500       if(size == smpi_group_size(group)) {
501         *newgroup = group;
502       } else {
503         *newgroup = smpi_group_new(size);
504         j = 0;
505         for(i = 0; i < n; i++) {
506           for(rank = ranges[i][0]; /* First */
507               rank >= 0 && rank <= ranges[i][1]; /* Last */
508               rank += ranges[i][2] /* Stride */) {
509             index = smpi_group_index(group, rank);
510             smpi_group_set_mapping(*newgroup, index, j);
511             j++;
512           }
513         }
514       }
515     }
516     smpi_group_use(*newgroup);
517     retval = MPI_SUCCESS;
518   }
519   smpi_bench_begin(-1, NULL);
520   return retval;
521 }
522
523 int MPI_Group_range_excl(MPI_Group group, int n, int ranges[][3], MPI_Group* newgroup) {
524   int retval, i, newrank, rank, size, index, add;
525
526   smpi_bench_end(-1, NULL);
527   if(group == MPI_GROUP_NULL) {
528     retval = MPI_ERR_GROUP;
529   } else if(newgroup == NULL) {
530     retval = MPI_ERR_ARG;
531   } else {
532     if(n == 0) {
533       *newgroup = group;
534     } else {
535       size = smpi_group_size(group);
536       for(i = 0; i < n; i++) {
537         for(rank = ranges[i][0]; /* First */
538             rank >= 0 && rank <= ranges[i][1]; /* Last */
539             rank += ranges[i][2] /* Stride */) {
540           size--;
541         }
542       }
543       if(size == 0) {
544         *newgroup = MPI_GROUP_EMPTY;
545       } else {
546         *newgroup = smpi_group_new(size);
547         newrank = 0;
548         while(newrank < size) {
549           for(i = 0; i < n; i++) {
550             add = 1;
551             for(rank = ranges[i][0]; /* First */
552                 rank >= 0 && rank <= ranges[i][1]; /* Last */
553                 rank += ranges[i][2] /* Stride */) {
554               if(rank == newrank) {
555                 add = 0;
556                 break;
557               }
558             }
559             if(add == 1) {
560               index = smpi_group_index(group, newrank);
561               smpi_group_set_mapping(*newgroup, index, newrank);
562             }
563           }
564         }
565       }
566     }
567     smpi_group_use(*newgroup);
568     retval = MPI_SUCCESS;
569   }
570   smpi_bench_begin(-1, NULL);
571   return retval;
572 }
573
574 int MPI_Comm_rank(MPI_Comm comm, int* rank) {
575   int retval;
576
577   smpi_bench_end(-1, NULL);
578   if(comm == MPI_COMM_NULL) {
579     retval = MPI_ERR_COMM;
580   } else {
581     *rank = smpi_comm_rank(comm);
582     retval = MPI_SUCCESS;
583   }
584   smpi_bench_begin(-1, NULL);
585   return retval;
586 }
587
588 int MPI_Comm_size(MPI_Comm comm, int* size) {
589   int retval;
590
591   smpi_bench_end(-1, NULL);
592   if(comm == MPI_COMM_NULL) {
593     retval = MPI_ERR_COMM;
594   } else if(size == NULL) {
595     retval = MPI_ERR_ARG;
596   } else {
597     *size = smpi_comm_size(comm);
598     retval = MPI_SUCCESS;
599   }
600   smpi_bench_begin(-1, NULL);
601   return retval;
602 }
603
604 int MPI_Comm_group(MPI_Comm comm, MPI_Group* group) {
605   int retval;
606
607   smpi_bench_end(-1, NULL);
608   if(comm == MPI_COMM_NULL) {
609     retval = MPI_ERR_COMM;
610   } else if(group == NULL) {
611     retval = MPI_ERR_ARG;
612   } else {
613     *group = smpi_comm_group(comm);
614     retval = MPI_SUCCESS;
615   }
616   smpi_bench_begin(-1, NULL);
617   return retval;
618 }
619
620 int MPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int* result) {
621   int retval;
622
623   smpi_bench_end(-1, NULL);
624   if(comm1 == MPI_COMM_NULL || comm2 == MPI_COMM_NULL) {
625     retval = MPI_ERR_COMM;
626   } else if(result == NULL) {
627     retval = MPI_ERR_ARG;
628   } else {
629     if(comm1 == comm2) { /* Same communicators means same groups */
630       *result = MPI_IDENT;
631     } else {
632       *result = smpi_group_compare(smpi_comm_group(comm1), smpi_comm_group(comm2));
633       if(*result == MPI_IDENT) {
634         *result = MPI_CONGRUENT;
635       }
636     }
637     retval = MPI_SUCCESS;
638   }
639   smpi_bench_begin(-1, NULL);
640   return retval;
641 }
642
643 int MPI_Comm_dup(MPI_Comm comm, MPI_Comm* newcomm) {
644   int retval;
645
646   smpi_bench_end(-1, NULL);
647   if(comm == MPI_COMM_NULL) {
648     retval = MPI_ERR_COMM;
649   } else if(newcomm == NULL) {
650     retval = MPI_ERR_ARG;
651   } else {
652     *newcomm = smpi_comm_new(smpi_comm_group(comm));
653     retval = MPI_SUCCESS;
654   }
655   smpi_bench_begin(-1, NULL);
656   return retval;
657 }
658
659 int MPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm* newcomm) {
660   int retval;
661
662   smpi_bench_end(-1, NULL);
663   if(comm == MPI_COMM_NULL) {
664     retval = MPI_ERR_COMM;
665   } else if(group == MPI_GROUP_NULL) {
666     retval = MPI_ERR_GROUP;
667   } else if(newcomm == NULL) {
668     retval = MPI_ERR_ARG;
669   } else {
670     *newcomm = smpi_comm_new(group);
671     retval = MPI_SUCCESS;
672   }
673   smpi_bench_begin(-1, NULL);
674   return retval;
675 }
676
677 int MPI_Comm_free(MPI_Comm* comm) {
678   int retval;
679
680   smpi_bench_end(-1, NULL);
681   if(comm == NULL) {
682     retval = MPI_ERR_ARG;
683   } else if(*comm == MPI_COMM_NULL) {
684     retval = MPI_ERR_COMM;
685   } else {
686     smpi_comm_destroy(*comm);
687     *comm = MPI_COMM_NULL;
688     retval = MPI_SUCCESS;
689   }
690   smpi_bench_begin(-1, NULL);
691   return retval;
692 }
693
694 int MPI_Send_init(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request) {
695   int retval;
696   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
697
698   smpi_bench_end(rank, "Send_init");
699   if(request == NULL) {
700     retval = MPI_ERR_ARG;
701   } else if (comm == MPI_COMM_NULL) {
702     retval = MPI_ERR_COMM;
703   } else {
704     *request = smpi_mpi_send_init(buf, count, datatype, dst, tag, comm);
705     retval = MPI_SUCCESS;
706   }
707   smpi_bench_begin(rank, "Send_init");
708   return retval;
709 }
710
711 int MPI_Recv_init(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request* request) {
712   int retval;
713   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
714
715   smpi_bench_end(rank, "Recv_init");
716   if(request == NULL) {
717     retval = MPI_ERR_ARG;
718   } else if (comm == MPI_COMM_NULL) {
719     retval = MPI_ERR_COMM;
720   } else {
721     *request = smpi_mpi_recv_init(buf, count, datatype, src, tag, comm);
722     retval = MPI_SUCCESS;
723   }
724   smpi_bench_begin(rank, "Recv_init");
725   return retval;
726 }
727
728 int MPI_Start(MPI_Request* request) {
729   int retval;
730   MPI_Comm comm = (*request)->comm;
731   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
732
733   smpi_bench_end(rank, "Start");
734   if(request == NULL) {
735     retval = MPI_ERR_ARG;
736   } else {
737     smpi_mpi_start(*request);
738     retval = MPI_SUCCESS;
739   }
740   smpi_bench_begin(rank, "Start");
741   return retval;
742 }
743
744 int MPI_Startall(int count, MPI_Request* requests) {
745   int retval;
746   MPI_Comm comm = count > 0 && requests ? requests[0]->comm : MPI_COMM_NULL;
747   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
748
749   smpi_bench_end(rank, "Startall");
750   if(requests == NULL) {
751     retval = MPI_ERR_ARG;
752   } else {
753     smpi_mpi_startall(count, requests);
754     retval = MPI_SUCCESS;
755   }
756   smpi_bench_begin(rank, "Startall");
757   return retval;
758 }
759
760 int MPI_Request_free(MPI_Request* request) {
761   int retval;
762   MPI_Comm comm = (*request)->comm;
763   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
764
765   smpi_bench_end(rank, "Request_free");
766   if(request == NULL) {
767     retval = MPI_ERR_ARG;
768   } else {
769     smpi_mpi_request_free(request);
770     retval = MPI_SUCCESS;
771   }
772   smpi_bench_begin(rank, "Request_free");
773   return retval;
774 }
775
776 int MPI_Irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request* request) {
777   int retval;
778   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
779
780   smpi_bench_end(rank, "Irecv");
781   if(request == NULL) {
782     retval = MPI_ERR_ARG;
783   } else if (comm == MPI_COMM_NULL) {
784     retval = MPI_ERR_COMM;
785   } else {
786     *request = smpi_mpi_irecv(buf, count, datatype, src, tag, comm);
787     retval = MPI_SUCCESS;
788   }
789   smpi_bench_begin(rank, "Irecv");
790   return retval;
791 }
792
793 int MPI_Isend(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request* request) {
794   int retval;
795   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
796
797   smpi_bench_end(rank, "Isend");
798   if(request == NULL) {
799     retval = MPI_ERR_ARG;
800   } else if (comm == MPI_COMM_NULL) {
801     retval = MPI_ERR_COMM;
802   } else {
803     *request = smpi_mpi_isend(buf, count, datatype, dst, tag, comm);
804     retval = MPI_SUCCESS;
805   }
806   smpi_bench_begin(rank, "Isend");
807   return retval;
808 }
809
810 int MPI_Recv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Status* status) {
811   int retval;
812   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
813
814   smpi_bench_end(rank, "Recv");
815   if (comm == MPI_COMM_NULL) {
816     retval = MPI_ERR_COMM;
817   } else {
818     smpi_mpi_recv(buf, count, datatype, src, tag, comm, status);
819     retval = MPI_SUCCESS;
820   }
821   smpi_bench_begin(rank, "Recv");
822   return retval;
823 }
824
825 int MPI_Send(void* buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm) {
826   int retval;
827   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
828
829   smpi_bench_end(rank, "Send");
830   if (comm == MPI_COMM_NULL) {
831     retval = MPI_ERR_COMM;
832   } else {
833     smpi_mpi_send(buf, count, datatype, dst, tag, comm);
834     retval = MPI_SUCCESS;
835   }
836   smpi_bench_begin(rank, "Send");
837   return retval;
838 }
839
840 int MPI_Sendrecv(void* sendbuf, int sendcount, MPI_Datatype sendtype, int dst, int sendtag, void* recvbuf, int recvcount, MPI_Datatype recvtype, int src, int recvtag, MPI_Comm comm, MPI_Status* status) {
841   int retval;
842   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
843
844   smpi_bench_end(rank, "Sendrecv");
845   if (comm == MPI_COMM_NULL) {
846     retval = MPI_ERR_COMM;
847   } else if (sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
848     retval = MPI_ERR_TYPE;
849   } else {
850     smpi_mpi_sendrecv(sendbuf, sendcount, sendtype, dst, sendtag, recvbuf, recvcount, recvtype, src, recvtag, comm, status);
851     retval = MPI_SUCCESS;
852   }
853   smpi_bench_begin(rank, "Sendrecv");
854   return retval;
855 }
856
857 int MPI_Sendrecv_replace(void* buf, int count, MPI_Datatype datatype, int dst, int sendtag, int src, int recvtag, MPI_Comm comm, MPI_Status* status) {
858   //TODO: suboptimal implementation
859   void* recvbuf;
860   int retval, size;
861
862   size = smpi_datatype_size(datatype) * count;
863   recvbuf = xbt_new(char, size);
864   retval = MPI_Sendrecv(buf, count, datatype, dst, sendtag, recvbuf, count, datatype, src, recvtag, comm, status);
865   memcpy(buf, recvbuf, size * sizeof(char));
866   xbt_free(recvbuf);
867   return retval;
868 }
869
870 int MPI_Test(MPI_Request* request, int* flag, MPI_Status* status) {
871   int retval;
872   int rank = request && (*request)->comm != MPI_COMM_NULL
873              ? smpi_comm_rank((*request)->comm)
874              : -1;
875
876   smpi_bench_end(rank, "Test");
877   if(request == NULL || flag == NULL) {
878     retval = MPI_ERR_ARG;
879   } else if(*request == MPI_REQUEST_NULL) {
880     retval = MPI_ERR_REQUEST;
881   } else {
882     *flag = smpi_mpi_test(request, status);
883     retval = MPI_SUCCESS;
884   }
885   smpi_bench_begin(rank, "Test");
886   return retval;
887 }
888
889 int MPI_Testany(int count, MPI_Request requests[], int* index, int* flag, MPI_Status* status) {
890   int retval;
891
892   smpi_bench_end(-1, NULL); //FIXME
893   if(index == NULL || flag == NULL) {
894     retval = MPI_ERR_ARG;
895   } else {
896     *flag = smpi_mpi_testany(count, requests, index, status);
897     retval = MPI_SUCCESS;
898   }
899   smpi_bench_begin(-1, NULL);
900   return retval;
901 }
902
903 int MPI_Wait(MPI_Request* request, MPI_Status* status) {
904   int retval;
905   int rank = request && (*request)->comm != MPI_COMM_NULL
906              ? smpi_comm_rank((*request)->comm)
907              : -1;
908
909   smpi_bench_end(rank, "Wait");
910   if(request == NULL) {
911     retval = MPI_ERR_ARG;
912   } else if(*request == MPI_REQUEST_NULL) {
913     retval = MPI_ERR_REQUEST;
914   } else {
915     smpi_mpi_wait(request, status);
916     retval = MPI_SUCCESS;
917   }
918   smpi_bench_begin(rank, "Wait");
919   return retval;
920 }
921
922 int MPI_Waitany(int count, MPI_Request requests[], int* index, MPI_Status* status) {
923   int retval;
924
925   smpi_bench_end(-1, NULL); //FIXME
926   if(index == NULL) {
927     retval = MPI_ERR_ARG;
928   } else {
929     *index = smpi_mpi_waitany(count, requests, status);
930     retval = MPI_SUCCESS;
931   }
932   smpi_bench_begin(-1, NULL);
933   return retval;
934 }
935
936 int MPI_Waitall(int count, MPI_Request requests[],  MPI_Status status[]) {
937   smpi_bench_end(-1, NULL); //FIXME
938   smpi_mpi_waitall(count, requests, status);
939   smpi_bench_begin(-1, NULL);
940   return MPI_SUCCESS;
941 }
942
943 int MPI_Waitsome(int incount, MPI_Request requests[], int* outcount, int* indices, MPI_Status status[]) {
944   int retval;
945
946   smpi_bench_end(-1, NULL); //FIXME
947   if(outcount == NULL || indices == NULL) {
948     retval = MPI_ERR_ARG;
949   } else {
950     *outcount = smpi_mpi_waitsome(incount, requests, indices, status);
951     retval = MPI_SUCCESS;
952   }
953   smpi_bench_begin(-1, NULL);
954   return retval;
955 }
956
957 int MPI_Bcast(void* buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) {
958   int retval;
959   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
960
961   smpi_bench_end(rank, "Bcast");
962   if(comm == MPI_COMM_NULL) {
963     retval = MPI_ERR_COMM;
964   } else {
965     smpi_mpi_bcast(buf, count, datatype, root, comm);
966     retval = MPI_SUCCESS;
967   }
968   smpi_bench_begin(rank, "Bcast");
969   return retval;
970 }
971
972 int MPI_Barrier(MPI_Comm comm) {
973   int retval;
974   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
975
976   smpi_bench_end(rank, "Barrier");
977   if(comm == MPI_COMM_NULL) {
978     retval = MPI_ERR_COMM;
979   } else {
980     smpi_mpi_barrier(comm);
981     retval = MPI_SUCCESS;
982   }
983   smpi_bench_begin(rank, "Barrier");
984   return retval;
985 }
986
987 int MPI_Gather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
988   int retval;
989   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
990
991   smpi_bench_end(rank, "Gather");
992   if(comm == MPI_COMM_NULL) {
993     retval = MPI_ERR_COMM;
994   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
995     retval = MPI_ERR_TYPE;
996   } else {
997     smpi_mpi_gather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
998     retval = MPI_SUCCESS;
999   }
1000   smpi_bench_begin(rank, "Gather");
1001   return retval;
1002 }
1003
1004 int MPI_Gatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, int root, MPI_Comm comm) {
1005   int retval;
1006   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1007
1008   smpi_bench_end(rank, "Gatherv");
1009   if(comm == MPI_COMM_NULL) {
1010     retval = MPI_ERR_COMM;
1011   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1012     retval = MPI_ERR_TYPE;
1013   } else if(recvcounts == NULL || displs == NULL) {
1014     retval = MPI_ERR_ARG;
1015   } else {
1016     smpi_mpi_gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm);
1017     retval = MPI_SUCCESS;
1018   }
1019   smpi_bench_begin(rank, "Gatherv");
1020   return retval;
1021 }
1022
1023 int MPI_Allgather(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
1024   int retval;
1025   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1026
1027   smpi_bench_end(rank, "Allgather");
1028   if(comm == MPI_COMM_NULL) {
1029     retval = MPI_ERR_COMM;
1030   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1031     retval = MPI_ERR_TYPE;
1032   } else {
1033     smpi_mpi_allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1034     retval = MPI_SUCCESS;
1035   }
1036   smpi_bench_begin(rank, "Allgather");
1037   return retval;
1038 }
1039
1040 int MPI_Allgatherv(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int* recvcounts, int* displs, MPI_Datatype recvtype, MPI_Comm comm) {
1041   int retval;
1042   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1043
1044   smpi_bench_end(rank, "Allgatherv");
1045   if(comm == MPI_COMM_NULL) {
1046     retval = MPI_ERR_COMM;
1047   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1048     retval = MPI_ERR_TYPE;
1049   } else if(recvcounts == NULL || displs == NULL) {
1050     retval = MPI_ERR_ARG;
1051   } else {
1052     smpi_mpi_allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
1053     retval = MPI_SUCCESS;
1054   }
1055   smpi_bench_begin(rank, "Allgatherv");
1056   return retval;
1057 }
1058
1059 int MPI_Scatter(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
1060   int retval;
1061   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1062
1063   smpi_bench_end(rank, "Scatter");
1064   if(comm == MPI_COMM_NULL) {
1065     retval = MPI_ERR_COMM;
1066   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1067     retval = MPI_ERR_TYPE;
1068   } else {
1069     smpi_mpi_scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
1070     retval = MPI_SUCCESS;
1071   }
1072   smpi_bench_begin(rank, "Scatter");
1073   return retval;
1074 }
1075
1076 int MPI_Scatterv(void* sendbuf, int* sendcounts, int* displs, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) {
1077   int retval;
1078   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1079
1080   smpi_bench_end(rank, "Scatterv");
1081   if(comm == MPI_COMM_NULL) {
1082     retval = MPI_ERR_COMM;
1083   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1084     retval = MPI_ERR_TYPE;
1085   } else if(sendcounts == NULL || displs == NULL) {
1086     retval = MPI_ERR_ARG;
1087   } else {
1088     smpi_mpi_scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
1089     retval = MPI_SUCCESS;
1090   }
1091   smpi_bench_begin(rank, "Scatterv");
1092   return retval;
1093 }
1094
1095 int MPI_Reduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm) {
1096   int retval;
1097   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1098
1099   smpi_bench_end(rank, "Reduce");
1100   if(comm == MPI_COMM_NULL) {
1101     retval = MPI_ERR_COMM;
1102   } else if(datatype == MPI_DATATYPE_NULL || op == MPI_OP_NULL) {
1103     retval = MPI_ERR_ARG;
1104   } else {
1105     smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
1106     retval = MPI_SUCCESS;
1107   }
1108   smpi_bench_begin(rank, "Reduce");
1109   return retval;
1110 }
1111
1112 int MPI_Allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
1113   int retval;
1114   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1115
1116   smpi_bench_end(rank, "Allreduce");
1117   if(comm == MPI_COMM_NULL) {
1118     retval = MPI_ERR_COMM;
1119   } else if(datatype == MPI_DATATYPE_NULL) {
1120     retval = MPI_ERR_TYPE;
1121   } else if(op == MPI_OP_NULL) {
1122     retval = MPI_ERR_OP;
1123   } else {
1124     smpi_mpi_allreduce(sendbuf, recvbuf, count, datatype, op, comm);
1125     retval = MPI_SUCCESS;
1126   }
1127   smpi_bench_begin(rank, "Allreduce");
1128   return retval;
1129 }
1130
1131 int MPI_Scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
1132   int retval;
1133   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1134
1135   smpi_bench_end(rank, "Scan");
1136   if(comm == MPI_COMM_NULL) {
1137     retval = MPI_ERR_COMM;
1138   } else if(datatype == MPI_DATATYPE_NULL) {
1139     retval = MPI_ERR_TYPE;
1140   } else if(op == MPI_OP_NULL) {
1141     retval = MPI_ERR_OP;
1142   } else {
1143     smpi_mpi_scan(sendbuf, recvbuf, count, datatype, op, comm);
1144     retval = MPI_SUCCESS;
1145   }
1146   smpi_bench_begin(rank, "Scan");
1147   return retval;
1148 }
1149
1150 int MPI_Reduce_scatter(void* sendbuf, void* recvbuf, int* recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm) {
1151   int retval, i, size, count;
1152   int* displs;
1153   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1154
1155   smpi_bench_end(rank, "Reduce_scatter");
1156   if(comm == MPI_COMM_NULL) {
1157     retval = MPI_ERR_COMM;
1158   } else if(datatype == MPI_DATATYPE_NULL) {
1159     retval = MPI_ERR_TYPE;
1160   } else if(op == MPI_OP_NULL) {
1161     retval = MPI_ERR_OP;
1162   } else if(recvcounts == NULL) {
1163     retval = MPI_ERR_ARG;
1164   } else {
1165     /* arbitrarily choose root as rank 0 */
1166     /* TODO: faster direct implementation ? */
1167     size = smpi_comm_size(comm);
1168     count = 0;
1169     displs = xbt_new(int, size);
1170     for(i = 0; i < size; i++) {
1171       count += recvcounts[i];
1172       displs[i] = 0;
1173     }
1174     smpi_mpi_reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
1175     smpi_mpi_scatterv(recvbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
1176     xbt_free(displs);
1177     retval = MPI_SUCCESS;
1178   }
1179   smpi_bench_begin(rank, "Reduce_scatter");
1180   return retval;
1181 }
1182
1183 int MPI_Alltoall(void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) {
1184   int retval, size, sendsize;
1185   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1186
1187   smpi_bench_end(rank, "Alltoall");
1188   if(comm == MPI_COMM_NULL) {
1189     retval = MPI_ERR_COMM;
1190   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1191     retval = MPI_ERR_TYPE;
1192   } else {
1193     size = smpi_comm_size(comm);
1194     sendsize = smpi_datatype_size(sendtype) * sendcount;
1195     if(sendsize < 200 && size > 12) {
1196       retval = smpi_coll_tuned_alltoall_bruck(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1197     } else if(sendsize < 3000) {
1198       retval = smpi_coll_tuned_alltoall_basic_linear(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1199     } else {
1200       retval = smpi_coll_tuned_alltoall_pairwise(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
1201     }
1202   }
1203   smpi_bench_begin(rank, "Alltoall");
1204   return retval;
1205 }
1206
1207 int MPI_Alltoallv(void* sendbuf, int* sendcounts, int* senddisps, MPI_Datatype sendtype, void* recvbuf, int *recvcounts, int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm) {
1208   int retval;
1209   int rank = comm != MPI_COMM_NULL ? smpi_comm_rank(comm) : -1;
1210
1211   smpi_bench_end(rank, "Alltoallv");
1212   if(comm == MPI_COMM_NULL) {
1213     retval = MPI_ERR_COMM;
1214   } else if(sendtype == MPI_DATATYPE_NULL || recvtype == MPI_DATATYPE_NULL) {
1215     retval = MPI_ERR_TYPE;
1216   } else if(sendcounts == NULL || senddisps == NULL || recvcounts == NULL || recvdisps == NULL) {
1217     retval = MPI_ERR_ARG;
1218   } else {
1219     retval = smpi_coll_basic_alltoallv(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype, comm);
1220   }
1221   smpi_bench_begin(rank, "Alltoallv");
1222   return retval;
1223 }
1224
1225
1226 int MPI_Get_processor_name( char *name, int *resultlen ) {
1227   int retval = MPI_SUCCESS;
1228   smpi_bench_end(-1, NULL);
1229   strncpy( name , SIMIX_host_get_name(SIMIX_host_self()), MPI_MAX_PROCESSOR_NAME-1);
1230   *resultlen= strlen(name) > MPI_MAX_PROCESSOR_NAME ? MPI_MAX_PROCESSOR_NAME : strlen(name);
1231
1232   smpi_bench_begin(-1, NULL);
1233   return retval;
1234 }
1235