Logo AND Algorithmique Numérique Distribuée

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