Logo AND Algorithmique Numérique Distribuée

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