Logo AND Algorithmique Numérique Distribuée

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