Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
check illegal in_place for gather/scatter
[simgrid.git] / src / smpi / bindings / smpi_pmpi_coll.cpp
1 /* Copyright (c) 2007-2021. 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 "private.hpp"
7 #include "smpi_coll.hpp"
8 #include "smpi_comm.hpp"
9 #include "smpi_request.hpp"
10 #include "smpi_datatype_derived.hpp"
11 #include "smpi_op.hpp"
12 #include "src/smpi/include/smpi_actor.hpp"
13
14 #include <vector>
15
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
17
18 static const void* smpi_get_in_place_buf(const void* inplacebuf, const void* otherbuf,
19                                          std::vector<unsigned char>& tmp_sendbuf, int count, MPI_Datatype datatype)
20 {
21   if (inplacebuf == MPI_IN_PLACE) {
22     tmp_sendbuf.resize(count * datatype->get_extent());
23     simgrid::smpi::Datatype::copy(otherbuf, count, datatype, tmp_sendbuf.data(), count, datatype);
24     return tmp_sendbuf.data();
25   }else{
26     return inplacebuf;
27   }
28 }
29 /* PMPI User level calls */
30
31 int PMPI_Barrier(MPI_Comm comm)
32 {
33   return PMPI_Ibarrier(comm, MPI_REQUEST_IGNORED);
34 }
35
36 int PMPI_Ibarrier(MPI_Comm comm, MPI_Request *request)
37 {
38   CHECK_COMM(1)
39   CHECK_REQUEST(2)
40
41   smpi_bench_end();
42   int rank = simgrid::s4u::this_actor::get_pid();
43   TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Barrier" : "PMPI_Ibarrier",
44                      new simgrid::instr::NoOpTIData(request == MPI_REQUEST_IGNORED ? "barrier" : "ibarrier"));
45   if (request == MPI_REQUEST_IGNORED) {
46     simgrid::smpi::colls::barrier(comm);
47     // Barrier can be used to synchronize RMA calls. Finish all requests from comm before.
48     comm->finish_rma_calls();
49   } else
50     simgrid::smpi::colls::ibarrier(comm, request);
51
52   TRACE_smpi_comm_out(rank);
53   smpi_bench_begin();
54   return MPI_SUCCESS;
55 }
56
57 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
58 {
59   return PMPI_Ibcast(buf, count, datatype, root, comm, MPI_REQUEST_IGNORED);
60 }
61
62 int PMPI_Ibcast(void *buf, int count, MPI_Datatype datatype, 
63                    int root, MPI_Comm comm, MPI_Request* request)
64 {
65   CHECK_COMM(5)
66   CHECK_BUFFER(1, buf, count)
67   CHECK_COUNT(2, count)
68   CHECK_TYPE(3, datatype)
69   CHECK_ROOT(4)
70   CHECK_REQUEST(6)
71
72   smpi_bench_end();
73   int pid = simgrid::s4u::this_actor::get_pid();
74   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Bcast" : "PMPI_Ibcast",
75                      new simgrid::instr::CollTIData(request == MPI_REQUEST_IGNORED ? "bcast" : "ibcast", root, -1.0,
76                                                     datatype->is_replayable() ? count : count * datatype->size(), -1,
77                                                     simgrid::smpi::Datatype::encode(datatype), ""));
78   if (comm->size() > 1) {
79     if (request == MPI_REQUEST_IGNORED)
80       simgrid::smpi::colls::bcast(buf, count, datatype, root, comm);
81     else
82       simgrid::smpi::colls::ibcast(buf, count, datatype, root, comm, request);
83   } else {
84     if (request != MPI_REQUEST_IGNORED)
85       *request = MPI_REQUEST_NULL;
86   }
87
88   TRACE_smpi_comm_out(pid);
89   smpi_bench_begin();
90   return MPI_SUCCESS;
91 }
92
93 int PMPI_Gather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
94                 int root, MPI_Comm comm){
95   return PMPI_Igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, MPI_REQUEST_IGNORED);
96 }
97
98 int PMPI_Igather(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
99                  MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
100 {
101   CHECK_COMM(8)
102   int rank = comm->rank();
103   if(sendbuf != MPI_IN_PLACE){
104     CHECK_BUFFER(1,sendbuf, sendcount)
105     CHECK_COUNT(2, sendcount)
106     CHECK_TYPE(3, sendtype)
107   }
108   if(rank == root){
109     CHECK_NOT_IN_PLACE_ROOT(4, recvbuf)
110     CHECK_TYPE(6, recvtype)
111     CHECK_COUNT(5, recvcount)
112     CHECK_BUFFER(4, recvbuf, recvcount)
113   } else {
114     CHECK_NOT_IN_PLACE_ROOT(1, sendbuf)
115   }
116   CHECK_ROOT(7)
117   CHECK_REQUEST(9)
118
119   const void* real_sendbuf   = sendbuf;
120   int real_sendcount         = sendcount;
121   MPI_Datatype real_sendtype = sendtype;
122   if (rank == root){
123     if (sendbuf == MPI_IN_PLACE) {
124       real_sendcount = 0;
125       real_sendtype  = recvtype;
126     } else if(recvtype->size() * recvcount !=  sendtype->size() * sendcount){
127       XBT_WARN("MPI_(I)Gather : received size at root differs from sent size : %zu vs %zu", recvtype->size() * recvcount , sendtype->size() * sendcount);
128       return MPI_ERR_TRUNCATE;
129     }
130   }
131
132   smpi_bench_end();
133
134   int pid = simgrid::s4u::this_actor::get_pid();
135
136   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Gather" : "PMPI_Igather",
137                      new simgrid::instr::CollTIData(
138                          request == MPI_REQUEST_IGNORED ? "gather" : "igather", root, -1.0,
139                          real_sendtype->is_replayable() ? real_sendcount : real_sendcount * real_sendtype->size(),
140                          (comm->rank() != root || recvtype->is_replayable()) ? recvcount : recvcount * recvtype->size(),
141                          simgrid::smpi::Datatype::encode(real_sendtype), simgrid::smpi::Datatype::encode(recvtype)));
142   if (request == MPI_REQUEST_IGNORED)
143     simgrid::smpi::colls::gather(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype, root, comm);
144   else
145     simgrid::smpi::colls::igather(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype, root, comm,
146                                   request);
147
148   TRACE_smpi_comm_out(pid);
149   smpi_bench_begin();
150   return MPI_SUCCESS;
151 }
152
153 int PMPI_Gatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int *recvcounts, const int *displs,
154                 MPI_Datatype recvtype, int root, MPI_Comm comm){
155   return PMPI_Igatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm, MPI_REQUEST_IGNORED);
156 }
157
158 int PMPI_Igatherv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, const int* recvcounts, const int* displs,
159                   MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
160 {
161   CHECK_COMM(9)
162   int rank = comm->rank();
163   CHECK_BUFFER(1, sendbuf, sendcount)
164   if(sendbuf != MPI_IN_PLACE){
165     CHECK_TYPE(3, sendtype)
166     CHECK_COUNT(2, sendcount)
167   }
168   if(rank == root){
169     CHECK_NOT_IN_PLACE_ROOT(4, recvbuf)
170     CHECK_TYPE(6, recvtype)
171     CHECK_NULL(5, MPI_ERR_COUNT, recvcounts)
172     CHECK_NULL(6, MPI_ERR_ARG, displs)
173   } else {
174     CHECK_NOT_IN_PLACE_ROOT(1, sendbuf)
175   }
176   CHECK_ROOT(8)
177   CHECK_REQUEST(10)
178
179   if (rank == root){
180     for (int i = 0; i < comm->size(); i++) {
181       CHECK_COUNT(5, recvcounts[i])
182       CHECK_BUFFER(4,recvbuf,recvcounts[i])
183     }
184   }
185
186   smpi_bench_end();
187   const void* real_sendbuf   = sendbuf;
188   int real_sendcount         = sendcount;
189   MPI_Datatype real_sendtype = sendtype;
190   if ((rank == root) && (sendbuf == MPI_IN_PLACE)) {
191     real_sendcount = 0;
192     real_sendtype  = recvtype;
193   }
194
195   int pid         = simgrid::s4u::this_actor::get_pid();
196   int dt_size_recv = recvtype->is_replayable() ? 1 : recvtype->size();
197
198   auto trace_recvcounts = std::make_shared<std::vector<int>>();
199   if (rank == root) {
200     for (int i = 0; i < comm->size(); i++) // copy data to avoid bad free
201       trace_recvcounts->push_back(recvcounts[i] * dt_size_recv);
202   }
203
204   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Gatherv" : "PMPI_Igatherv",
205                      new simgrid::instr::VarCollTIData(
206                          request == MPI_REQUEST_IGNORED ? "gatherv" : "igatherv", root,
207                          real_sendtype->is_replayable() ? real_sendcount : real_sendcount * real_sendtype->size(),
208                          nullptr, dt_size_recv, trace_recvcounts, simgrid::smpi::Datatype::encode(real_sendtype),
209                          simgrid::smpi::Datatype::encode(recvtype)));
210   if (request == MPI_REQUEST_IGNORED)
211     simgrid::smpi::colls::gatherv(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcounts, displs, recvtype,
212                                   root, comm);
213   else
214     simgrid::smpi::colls::igatherv(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcounts, displs, recvtype,
215                                    root, comm, request);
216
217   TRACE_smpi_comm_out(pid);
218   smpi_bench_begin();
219   return MPI_SUCCESS;
220 }
221
222 int PMPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
223                    void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm){
224   return PMPI_Iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, MPI_REQUEST_IGNORED);
225 }
226
227 int PMPI_Iallgather(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
228                     MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
229 {
230   CHECK_COMM(7)
231   int rank = comm->rank();
232   CHECK_BUFFER(1, sendbuf, sendcount)
233   CHECK_BUFFER(4, recvbuf, recvcount)
234   CHECK_NOT_IN_PLACE(4, recvbuf)
235   if(sendbuf != MPI_IN_PLACE){
236     CHECK_COUNT(2, sendcount)
237     CHECK_TYPE(3, sendtype)
238   }
239   CHECK_TYPE(6, recvtype)
240   CHECK_COUNT(5, recvcount)
241   CHECK_REQUEST(8)
242
243   if (sendbuf == MPI_IN_PLACE) {
244     sendbuf   = static_cast<char*>(recvbuf) + recvtype->get_extent() * recvcount * comm->rank();
245     sendcount = recvcount;
246     sendtype  = recvtype;
247   }
248
249   if(recvtype->size() * recvcount !=  sendtype->size() * sendcount){
250     XBT_WARN("MPI_(I)Allgather : received size from each process differs from sent size : %zu vs %zu", recvtype->size() * recvcount, sendtype->size() * sendcount);
251     return MPI_ERR_TRUNCATE;
252   }
253
254   smpi_bench_end();
255
256   int pid = simgrid::s4u::this_actor::get_pid();
257
258   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Allgather" : "PMPI_Iallggather",
259                      new simgrid::instr::CollTIData(
260                          request == MPI_REQUEST_IGNORED ? "allgather" : "iallgather", -1, -1.0,
261                          sendtype->is_replayable() ? sendcount : sendcount * sendtype->size(),
262                          recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
263                          simgrid::smpi::Datatype::encode(sendtype), simgrid::smpi::Datatype::encode(recvtype)));
264   if (request == MPI_REQUEST_IGNORED)
265     simgrid::smpi::colls::allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
266   else
267     simgrid::smpi::colls::iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request);
268
269   TRACE_smpi_comm_out(pid);
270   smpi_bench_begin();
271   return MPI_SUCCESS;
272 }
273
274 int PMPI_Allgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
275                    void *recvbuf, const int *recvcounts, const int *displs, MPI_Datatype recvtype, MPI_Comm comm){
276   return PMPI_Iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm, MPI_REQUEST_IGNORED);
277 }
278
279 int PMPI_Iallgatherv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, const int* recvcounts, const int* displs,
280                      MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
281 {
282   CHECK_COMM(8)
283   int rank = comm->rank();
284   CHECK_BUFFER(1, sendbuf, sendcount)
285   if(sendbuf != MPI_IN_PLACE)
286     CHECK_TYPE(3, sendtype)
287   CHECK_TYPE(6, recvtype)
288   CHECK_NULL(5, MPI_ERR_COUNT, recvcounts)
289   CHECK_NULL(6, MPI_ERR_ARG, displs)
290   if(sendbuf != MPI_IN_PLACE)
291     CHECK_COUNT(2, sendcount)
292   CHECK_REQUEST(9)
293   CHECK_NOT_IN_PLACE(4, recvbuf)
294   for (int i = 0; i < comm->size(); i++) {
295     CHECK_COUNT(5, recvcounts[i])
296     CHECK_BUFFER(4, recvbuf, recvcounts[i])
297   }
298
299   smpi_bench_end();
300   if (sendbuf == MPI_IN_PLACE) {
301     sendbuf   = static_cast<char*>(recvbuf) + recvtype->get_extent() * displs[comm->rank()];
302     sendcount = recvcounts[comm->rank()];
303     sendtype  = recvtype;
304   }
305   int pid         = simgrid::s4u::this_actor::get_pid();
306   int dt_size_recv = recvtype->is_replayable() ? 1 : recvtype->size();
307
308   auto trace_recvcounts = std::make_shared<std::vector<int>>();
309   for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
310     trace_recvcounts->push_back(recvcounts[i] * dt_size_recv);
311   }
312
313   TRACE_smpi_comm_in(
314       pid, request == MPI_REQUEST_IGNORED ? "PMPI_Allgatherv" : "PMPI_Iallgatherv",
315       new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "allgatherv" : "iallgatherv", -1,
316                                         sendtype->is_replayable() ? sendcount : sendcount * sendtype->size(), nullptr,
317                                         dt_size_recv, trace_recvcounts, simgrid::smpi::Datatype::encode(sendtype),
318                                         simgrid::smpi::Datatype::encode(recvtype)));
319   if (request == MPI_REQUEST_IGNORED)
320     simgrid::smpi::colls::allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
321   else
322     simgrid::smpi::colls::iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm,
323                                       request);
324
325   TRACE_smpi_comm_out(pid);
326   smpi_bench_begin();
327   return MPI_SUCCESS;
328 }
329
330 int PMPI_Scatter(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
331                 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm){
332   return PMPI_Iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, MPI_REQUEST_IGNORED);
333 }
334
335 int PMPI_Iscatter(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
336                   MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
337 {
338   CHECK_COMM(8)
339   int rank = comm->rank();
340   if(rank == root){
341     CHECK_NOT_IN_PLACE_ROOT(1, sendbuf)
342     CHECK_BUFFER(1, sendbuf, sendcount)
343     CHECK_COUNT(2, sendcount)
344     CHECK_TYPE(3, sendtype)
345   } else {
346     CHECK_NOT_IN_PLACE_ROOT(4, recvbuf)
347   }
348   if(recvbuf != MPI_IN_PLACE){
349     CHECK_BUFFER(4, recvbuf, recvcount)
350     CHECK_COUNT(5, recvcount)
351     CHECK_TYPE(6, recvtype)
352   }
353   CHECK_ROOT(8)
354   CHECK_REQUEST(9)
355
356   if (recvbuf == MPI_IN_PLACE) {
357     recvtype  = sendtype;
358     recvcount = sendcount;
359   }
360
361   if((rank == root) && (recvtype->size() * recvcount !=  sendtype->size() * sendcount)){
362     XBT_WARN("MPI_(I)Scatter : sent size to each process differs from receive size");
363     return MPI_ERR_TRUNCATE;
364   }
365
366   smpi_bench_end();
367
368   int pid = simgrid::s4u::this_actor::get_pid();
369
370   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Scatter" : "PMPI_Iscatter",
371                      new simgrid::instr::CollTIData(
372                          request == MPI_REQUEST_IGNORED ? "scatter" : "iscatter", root, -1.0,
373                          (rank != root || sendtype->is_replayable()) ? sendcount : sendcount * sendtype->size(),
374                          recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
375                          simgrid::smpi::Datatype::encode(sendtype), simgrid::smpi::Datatype::encode(recvtype)));
376   if (request == MPI_REQUEST_IGNORED)
377     simgrid::smpi::colls::scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
378   else
379     simgrid::smpi::colls::iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request);
380
381   TRACE_smpi_comm_out(pid);
382   smpi_bench_begin();
383   return MPI_SUCCESS;
384 }
385
386 int PMPI_Scatterv(const void *sendbuf, const int *sendcounts, const int *displs,
387                  MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm){
388   return PMPI_Iscatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm, MPI_REQUEST_IGNORED);
389 }
390
391 int PMPI_Iscatterv(const void* sendbuf, const int* sendcounts, const int* displs, MPI_Datatype sendtype, void* recvbuf, int recvcount,
392                    MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
393 {
394   CHECK_COMM(9)
395   int rank = comm->rank();
396   if(recvbuf != MPI_IN_PLACE){
397     CHECK_NOT_IN_PLACE_ROOT(1, sendbuf)
398     CHECK_BUFFER(4, recvbuf, recvcount)
399     CHECK_COUNT(5, recvcount)
400     CHECK_TYPE(7, recvtype)
401   }
402   CHECK_ROOT(9)
403   CHECK_REQUEST(10)
404   if (rank == root) {
405     CHECK_NULL(2, MPI_ERR_COUNT, sendcounts)
406     CHECK_NULL(3, MPI_ERR_ARG, displs)
407     CHECK_TYPE(4, sendtype)
408     for (int i = 0; i < comm->size(); i++){
409       CHECK_BUFFER(1, sendbuf, sendcounts[i])
410       CHECK_COUNT(2, sendcounts[i])
411     }
412     if (recvbuf == MPI_IN_PLACE) {
413       recvtype  = sendtype;
414       recvcount = sendcounts[rank];
415     }
416   } else {
417     CHECK_NOT_IN_PLACE_ROOT(4, recvbuf)
418   }
419
420   smpi_bench_end();
421
422   int pid         = simgrid::s4u::this_actor::get_pid();
423   int dt_size_send = sendtype->is_replayable() ? 1 : sendtype->size();
424
425   auto trace_sendcounts = std::make_shared<std::vector<int>>();
426   if (rank == root) {
427     for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
428       trace_sendcounts->push_back(sendcounts[i] * dt_size_send);
429     }
430   }
431
432   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Scatterv" : "PMPI_Iscatterv",
433                      new simgrid::instr::VarCollTIData(
434                          request == MPI_REQUEST_IGNORED ? "scatterv" : "iscatterv", root, dt_size_send,
435                          trace_sendcounts, recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
436                          nullptr, simgrid::smpi::Datatype::encode(sendtype),
437                          simgrid::smpi::Datatype::encode(recvtype)));
438   if (request == MPI_REQUEST_IGNORED)
439     simgrid::smpi::colls::scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
440   else
441     simgrid::smpi::colls::iscatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm,
442                                     request);
443
444   TRACE_smpi_comm_out(pid);
445   smpi_bench_begin();
446   return MPI_SUCCESS;
447 }
448
449 int PMPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
450 {
451   return PMPI_Ireduce(sendbuf, recvbuf, count, datatype, op, root, comm, MPI_REQUEST_IGNORED);
452 }
453
454 int PMPI_Ireduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm, MPI_Request* request)
455 {
456   CHECK_COMM(7)
457   int rank = comm->rank();
458   CHECK_BUFFER(1, sendbuf, count)
459   if(rank == root){
460     CHECK_NOT_IN_PLACE(2, recvbuf)
461     CHECK_BUFFER(5, recvbuf, count)
462   }
463   CHECK_TYPE(4, datatype)
464   CHECK_COUNT(3, count)
465   CHECK_OP(5, op, datatype)
466   CHECK_ROOT(7)
467   CHECK_REQUEST(8)
468
469   smpi_bench_end();
470   int pid = simgrid::s4u::this_actor::get_pid();
471
472   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Reduce" : "PMPI_Ireduce",
473                       new simgrid::instr::CollTIData(request == MPI_REQUEST_IGNORED ? "reduce" : "ireduce", root, 0,
474                                                     datatype->is_replayable() ? count : count * datatype->size(), -1,
475                                                     simgrid::smpi::Datatype::encode(datatype), ""));
476   if (request == MPI_REQUEST_IGNORED)
477     simgrid::smpi::colls::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
478   else
479     simgrid::smpi::colls::ireduce(sendbuf, recvbuf, count, datatype, op, root, comm, request);
480
481   TRACE_smpi_comm_out(pid);
482   smpi_bench_begin();
483   return MPI_SUCCESS;
484 }
485
486 int PMPI_Reduce_local(const void* inbuf, void* inoutbuf, int count, MPI_Datatype datatype, MPI_Op op)
487 {
488   CHECK_BUFFER(1, inbuf, count)
489   CHECK_BUFFER(2, inoutbuf, count)
490   CHECK_TYPE(4, datatype)
491   CHECK_COUNT(3, count)
492   CHECK_OP(5, op, datatype)
493
494   smpi_bench_end();
495   op->apply(inbuf, inoutbuf, &count, datatype);
496   smpi_bench_begin();
497   return MPI_SUCCESS;
498 }
499
500 int PMPI_Allreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
501 {
502   return PMPI_Iallreduce(sendbuf, recvbuf, count, datatype, op, comm, MPI_REQUEST_IGNORED);
503 }
504
505 int PMPI_Iallreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request *request)
506 {
507   CHECK_COMM(6)
508   int rank = comm->rank();
509   CHECK_BUFFER(1, sendbuf, count)
510   CHECK_BUFFER(2, recvbuf, count)
511   CHECK_NOT_IN_PLACE(2, recvbuf)
512   CHECK_TYPE(4, datatype)
513   CHECK_COUNT(3, count)
514   CHECK_REQUEST(7)
515   CHECK_OP(5, op, datatype)
516
517   smpi_bench_end();
518   std::vector<unsigned char> tmp_sendbuf;
519   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, count, datatype);
520
521   int pid = simgrid::s4u::this_actor::get_pid();
522
523   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Allreduce" : "PMPI_Iallreduce",
524                      new simgrid::instr::CollTIData(request == MPI_REQUEST_IGNORED ? "allreduce" : "iallreduce", -1, 0,
525                                                     datatype->is_replayable() ? count : count * datatype->size(), -1,
526                                                     simgrid::smpi::Datatype::encode(datatype), ""));
527
528   if (request == MPI_REQUEST_IGNORED)
529     simgrid::smpi::colls::allreduce(real_sendbuf, recvbuf, count, datatype, op, comm);
530   else
531     simgrid::smpi::colls::iallreduce(real_sendbuf, recvbuf, count, datatype, op, comm, request);
532
533   TRACE_smpi_comm_out(pid);
534   smpi_bench_begin();
535   return MPI_SUCCESS;
536 }
537
538 int PMPI_Scan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
539 {
540   return PMPI_Iscan(sendbuf, recvbuf, count, datatype, op, comm, MPI_REQUEST_IGNORED);
541 }
542
543 int PMPI_Iscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request)
544 {
545   CHECK_COMM(6)
546   CHECK_BUFFER(1,sendbuf,count)
547   CHECK_BUFFER(2,recvbuf,count)
548   CHECK_TYPE(4, datatype)
549   CHECK_COUNT(3, count)
550   CHECK_REQUEST(7)
551   CHECK_OP(5, op, datatype)
552
553   smpi_bench_end();
554   int pid         = simgrid::s4u::this_actor::get_pid();
555   std::vector<unsigned char> tmp_sendbuf;
556   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, count, datatype);
557
558   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Scan" : "PMPI_Iscan",
559                      new simgrid::instr::Pt2PtTIData(request == MPI_REQUEST_IGNORED ? "scan" : "iscan", -1,
560                                                      datatype->is_replayable() ? count : count * datatype->size(),
561                                                      simgrid::smpi::Datatype::encode(datatype)));
562
563   int retval;
564   if (request == MPI_REQUEST_IGNORED)
565     retval = simgrid::smpi::colls::scan(real_sendbuf, recvbuf, count, datatype, op, comm);
566   else
567     retval = simgrid::smpi::colls::iscan(real_sendbuf, recvbuf, count, datatype, op, comm, request);
568
569   TRACE_smpi_comm_out(pid);
570   smpi_bench_begin();
571   return retval;
572 }
573
574 int PMPI_Exscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
575 {
576   return PMPI_Iexscan(sendbuf, recvbuf, count, datatype, op, comm, MPI_REQUEST_IGNORED);
577 }
578
579 int PMPI_Iexscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request){
580   CHECK_COMM(6)
581   CHECK_BUFFER(1, sendbuf, count)
582   CHECK_BUFFER(2, recvbuf, count)
583   CHECK_TYPE(4, datatype)
584   CHECK_COUNT(3, count)
585   CHECK_REQUEST(7)
586   CHECK_OP(5, op, datatype)
587
588   smpi_bench_end();
589   int pid         = simgrid::s4u::this_actor::get_pid();
590   std::vector<unsigned char> tmp_sendbuf;
591   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, count, datatype);
592
593   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Exscan" : "PMPI_Iexscan",
594                      new simgrid::instr::Pt2PtTIData(request == MPI_REQUEST_IGNORED ? "exscan" : "iexscan", -1,
595                                                      datatype->is_replayable() ? count : count * datatype->size(),
596                                                      simgrid::smpi::Datatype::encode(datatype)));
597
598   int retval;
599   if (request == MPI_REQUEST_IGNORED)
600     retval = simgrid::smpi::colls::exscan(real_sendbuf, recvbuf, count, datatype, op, comm);
601   else
602     retval = simgrid::smpi::colls::iexscan(real_sendbuf, recvbuf, count, datatype, op, comm, request);
603
604   TRACE_smpi_comm_out(pid);
605   smpi_bench_begin();
606   return retval;
607 }
608
609 int PMPI_Reduce_scatter(const void *sendbuf, void *recvbuf, const int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
610 {
611   return PMPI_Ireduce_scatter(sendbuf, recvbuf, recvcounts, datatype, op, comm, MPI_REQUEST_IGNORED);
612 }
613
614 int PMPI_Ireduce_scatter(const void *sendbuf, void *recvbuf, const int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request *request)
615 {
616   CHECK_COMM(6)
617   int rank = comm->rank();
618   CHECK_NOT_IN_PLACE(2, recvbuf)
619   CHECK_TYPE(4, datatype)
620   CHECK_NULL(3, MPI_ERR_COUNT, recvcounts)
621   CHECK_REQUEST(7)
622   CHECK_OP(5, op, datatype)
623   for (int i = 0; i < comm->size(); i++) {
624     CHECK_COUNT(3, recvcounts[i])
625     CHECK_BUFFER(1, sendbuf, recvcounts[i])
626     CHECK_BUFFER(2, recvbuf, recvcounts[i])
627   }
628
629   smpi_bench_end();
630   int pid                           = simgrid::s4u::this_actor::get_pid();
631   auto trace_recvcounts              = std::make_shared<std::vector<int>>();
632   int dt_send_size                   = datatype->is_replayable() ? 1 : datatype->size();
633   int totalcount                     = 0;
634
635   for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
636     trace_recvcounts->push_back(recvcounts[i] * dt_send_size);
637     totalcount += recvcounts[i];
638   }
639   std::vector<unsigned char> tmp_sendbuf;
640   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, totalcount, datatype);
641
642   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Reduce_scatter" : "PMPI_Ireduce_scatter",
643                      new simgrid::instr::VarCollTIData(
644                          request == MPI_REQUEST_IGNORED ? "reducescatter" : "ireducescatter", -1, dt_send_size, nullptr,
645                          -1, trace_recvcounts, simgrid::smpi::Datatype::encode(datatype), ""));
646
647   if (request == MPI_REQUEST_IGNORED)
648     simgrid::smpi::colls::reduce_scatter(real_sendbuf, recvbuf, recvcounts, datatype, op, comm);
649   else
650     simgrid::smpi::colls::ireduce_scatter(real_sendbuf, recvbuf, recvcounts, datatype, op, comm, request);
651
652   TRACE_smpi_comm_out(pid);
653   smpi_bench_begin();
654   return MPI_SUCCESS;
655 }
656
657 int PMPI_Reduce_scatter_block(const void *sendbuf, void *recvbuf, int recvcount,
658                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
659 {
660   return PMPI_Ireduce_scatter_block(sendbuf, recvbuf, recvcount, datatype, op, comm, MPI_REQUEST_IGNORED);
661 }
662
663 int PMPI_Ireduce_scatter_block(const void* sendbuf, void* recvbuf, int recvcount, MPI_Datatype datatype, MPI_Op op,
664                                MPI_Comm comm, MPI_Request* request)
665 {
666   CHECK_COMM(6)
667   CHECK_BUFFER(1, sendbuf, recvcount)
668   CHECK_BUFFER(2, recvbuf, recvcount)
669   CHECK_TYPE(4, datatype)
670   CHECK_COUNT(3, recvcount)
671   CHECK_REQUEST(7)
672   CHECK_OP(5, op, datatype)
673
674   smpi_bench_end();
675   int count = comm->size();
676
677   int pid                           = simgrid::s4u::this_actor::get_pid();
678   int dt_send_size                   = datatype->is_replayable() ? 1 : datatype->size();
679   auto trace_recvcounts = std::make_shared<std::vector<int>>(recvcount * dt_send_size); // copy data to avoid bad free
680   std::vector<unsigned char> tmp_sendbuf;
681   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, recvcount * count, datatype);
682
683   TRACE_smpi_comm_in(
684       pid, request == MPI_REQUEST_IGNORED ? "PMPI_Reduce_scatter_block" : "PMPI_Ireduce_scatter_block",
685       new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "reducescatter" : "ireducescatter", -1, 0,
686                                         nullptr, -1, trace_recvcounts, simgrid::smpi::Datatype::encode(datatype), ""));
687
688   std::vector<int> recvcounts(count);
689   for (int i      = 0; i < count; i++)
690     recvcounts[i] = recvcount;
691   if (request == MPI_REQUEST_IGNORED)
692     simgrid::smpi::colls::reduce_scatter(real_sendbuf, recvbuf, recvcounts.data(), datatype, op, comm);
693   else
694     simgrid::smpi::colls::ireduce_scatter(real_sendbuf, recvbuf, recvcounts.data(), datatype, op, comm, request);
695
696   TRACE_smpi_comm_out(pid);
697   smpi_bench_begin();
698   return MPI_SUCCESS;
699 }
700
701 int PMPI_Alltoall(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
702                   MPI_Datatype recvtype, MPI_Comm comm){
703   return PMPI_Ialltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, MPI_REQUEST_IGNORED);
704 }
705
706 int PMPI_Ialltoall(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
707                    MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
708 {
709   CHECK_COMM(7)
710   CHECK_BUFFER(1, sendbuf, sendcount)
711   CHECK_BUFFER(4, recvbuf, recvcount)
712   if(sendbuf != MPI_IN_PLACE)
713     CHECK_TYPE(3, sendtype)
714   CHECK_TYPE(6, recvtype)
715   CHECK_COUNT(5, recvcount)
716   if(sendbuf != MPI_IN_PLACE)
717     CHECK_COUNT(2, sendcount)
718   CHECK_COUNT(5, recvcount)
719   CHECK_REQUEST(8)
720
721   int pid                 = simgrid::s4u::this_actor::get_pid();
722   int real_sendcount         = sendcount;
723   MPI_Datatype real_sendtype = sendtype;
724
725   std::vector<unsigned char> tmp_sendbuf;
726   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, recvcount * comm->size(), recvtype);
727
728   if (sendbuf == MPI_IN_PLACE) {
729     real_sendcount = recvcount;
730     real_sendtype  = recvtype;
731   }
732
733   if(recvtype->size() * recvcount !=  real_sendtype->size() * real_sendcount){
734     XBT_WARN("MPI_(I)Alltoall : receive size from each process differs from sent size : %zu vs %zu", recvtype->size() * recvcount,  real_sendtype->size() * real_sendcount);
735     return MPI_ERR_TRUNCATE;
736   }
737
738   smpi_bench_end();
739
740   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Alltoall" : "PMPI_Ialltoall",
741                      new simgrid::instr::CollTIData(
742                          request == MPI_REQUEST_IGNORED ? "alltoall" : "ialltoall", -1, -1.0,
743                          real_sendtype->is_replayable() ? real_sendcount : real_sendcount * real_sendtype->size(),
744                          recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
745                          simgrid::smpi::Datatype::encode(real_sendtype), simgrid::smpi::Datatype::encode(recvtype)));
746   int retval;
747   if (request == MPI_REQUEST_IGNORED)
748     retval =
749         simgrid::smpi::colls::alltoall(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype, comm);
750   else
751     retval = simgrid::smpi::colls::ialltoall(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype,
752                                              comm, request);
753
754   TRACE_smpi_comm_out(pid);
755   smpi_bench_begin();
756   return retval;
757 }
758
759 int PMPI_Alltoallv(const void* sendbuf, const int* sendcounts, const int* senddispls, MPI_Datatype sendtype, void* recvbuf,
760                    const int* recvcounts, const int* recvdispls, MPI_Datatype recvtype, MPI_Comm comm)
761 {
762   return PMPI_Ialltoallv(sendbuf, sendcounts, senddispls, sendtype, recvbuf, recvcounts, recvdispls, recvtype, comm, MPI_REQUEST_IGNORED);
763 }
764
765 int PMPI_Ialltoallv(const void* sendbuf, const int* sendcounts, const int* senddispls, MPI_Datatype sendtype, void* recvbuf,
766                     const int* recvcounts, const int* recvdispls, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
767 {
768   CHECK_COMM(9)
769   if(sendbuf != MPI_IN_PLACE){
770     CHECK_NULL(2, MPI_ERR_COUNT, sendcounts)
771     CHECK_NULL(3, MPI_ERR_ARG, senddispls)
772     CHECK_TYPE(4, sendtype)
773   }
774   CHECK_TYPE(8, recvtype)
775   CHECK_NULL(6, MPI_ERR_COUNT, recvcounts)
776   CHECK_NULL(7, MPI_ERR_ARG, recvdispls)
777   CHECK_REQUEST(10)
778
779   int pid = simgrid::s4u::this_actor::get_pid();
780   int size = comm->size();
781   for (int i = 0; i < size; i++) {
782     if(sendbuf != MPI_IN_PLACE){
783       CHECK_BUFFER(1, sendbuf, sendcounts[i])
784       CHECK_COUNT(2, sendcounts[i])
785     }
786     CHECK_BUFFER(5, recvbuf, recvcounts[i])
787     CHECK_COUNT(6, recvcounts[i])
788   }
789
790   smpi_bench_end();
791   int send_size                      = 0;
792   int recv_size                      = 0;
793   auto trace_sendcounts              = std::make_shared<std::vector<int>>();
794   auto trace_recvcounts              = std::make_shared<std::vector<int>>();
795   int dt_size_recv                   = recvtype->size();
796
797   const int* real_sendcounts = sendcounts;
798   const int* real_senddispls  = senddispls;
799   MPI_Datatype real_sendtype = sendtype;
800   int maxsize              = 0;
801   for (int i = 0; i < size; i++) { // copy data to avoid bad free
802     recv_size += recvcounts[i] * dt_size_recv;
803     trace_recvcounts->push_back(recvcounts[i] * dt_size_recv);
804     if (((recvdispls[i] + recvcounts[i]) * dt_size_recv) > maxsize)
805       maxsize = (recvdispls[i] + recvcounts[i]) * dt_size_recv;
806   }
807
808   std::vector<unsigned char> tmp_sendbuf;
809   std::vector<int> tmp_sendcounts;
810   std::vector<int> tmp_senddispls;
811   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, maxsize, MPI_CHAR);
812   if (sendbuf == MPI_IN_PLACE) {
813     tmp_sendcounts.assign(recvcounts, recvcounts + size);
814     real_sendcounts = tmp_sendcounts.data();
815     tmp_senddispls.assign(recvdispls, recvdispls + size);
816     real_senddispls = tmp_senddispls.data();
817     real_sendtype  = recvtype;
818   }
819
820   if(recvtype->size() * recvcounts[comm->rank()] !=  real_sendtype->size() * real_sendcounts[comm->rank()]){
821     XBT_WARN("MPI_(I)Alltoallv : receive size from me differs from sent size to me : %zu vs %zu", recvtype->size() * recvcounts[comm->rank()], real_sendtype->size() * real_sendcounts[comm->rank()]);
822     smpi_bench_begin();
823     return MPI_ERR_TRUNCATE;
824   }
825
826   int dt_size_send = real_sendtype->size();
827
828   for (int i = 0; i < size; i++) { // copy data to avoid bad free
829     send_size += real_sendcounts[i] * dt_size_send;
830     trace_sendcounts->push_back(real_sendcounts[i] * dt_size_send);
831   }
832
833   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Alltoallv" : "PMPI_Ialltoallv",
834                      new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "alltoallv" : "ialltoallv", -1,
835                                                        send_size, trace_sendcounts, recv_size, trace_recvcounts,
836                                                        simgrid::smpi::Datatype::encode(real_sendtype),
837                                                        simgrid::smpi::Datatype::encode(recvtype)));
838
839   int retval;
840   if (request == MPI_REQUEST_IGNORED)
841     retval = simgrid::smpi::colls::alltoallv(real_sendbuf, real_sendcounts, real_senddispls, real_sendtype, recvbuf,
842                                              recvcounts, recvdispls, recvtype, comm);
843   else
844     retval = simgrid::smpi::colls::ialltoallv(real_sendbuf, real_sendcounts, real_senddispls, real_sendtype, recvbuf,
845                                               recvcounts, recvdispls, recvtype, comm, request);
846
847   TRACE_smpi_comm_out(pid);
848   smpi_bench_begin();
849   return retval;
850 }
851
852 int PMPI_Alltoallw(const void* sendbuf, const int* sendcounts, const int* senddispls, const MPI_Datatype* sendtypes, void* recvbuf,
853                    const int* recvcounts, const int* recvdispls, const MPI_Datatype* recvtypes, MPI_Comm comm)
854 {
855   return PMPI_Ialltoallw(sendbuf, sendcounts, senddispls, sendtypes, recvbuf, recvcounts, recvdispls, recvtypes, comm, MPI_REQUEST_IGNORED);
856 }
857
858 int PMPI_Ialltoallw(const void* sendbuf, const int* sendcounts, const int* senddispls, const MPI_Datatype* sendtypes, void* recvbuf,
859                     const int* recvcounts, const int* recvdispls, const MPI_Datatype* recvtypes, MPI_Comm comm, MPI_Request* request)
860 {
861   CHECK_COMM(9)
862   if(sendbuf != MPI_IN_PLACE){
863     CHECK_NULL(2, MPI_ERR_COUNT, sendcounts)
864     CHECK_NULL(3, MPI_ERR_ARG, senddispls)
865     CHECK_NULL(4, MPI_ERR_TYPE, sendtypes)
866   }
867   CHECK_NULL(6, MPI_ERR_COUNT, recvcounts)
868   CHECK_NULL(7, MPI_ERR_ARG, recvdispls)
869   CHECK_NULL(8, MPI_ERR_TYPE, recvtypes)
870   CHECK_REQUEST(10)
871   int pid = simgrid::s4u::this_actor::get_pid();
872   int size = comm->size();
873   for (int i = 0; i < size; i++) {
874     if(sendbuf != MPI_IN_PLACE){
875       CHECK_BUFFER(1, sendbuf, sendcounts[i])
876       CHECK_COUNT(2, sendcounts[i])
877       CHECK_TYPE(4, sendtypes[i])
878     }
879     CHECK_BUFFER(5, recvbuf, recvcounts[i])
880     CHECK_COUNT(6, recvcounts[i])
881     CHECK_TYPE(8, recvtypes[i])
882   }
883
884   smpi_bench_end();
885
886   int send_size                      = 0;
887   int recv_size                      = 0;
888   auto trace_sendcounts              = std::make_shared<std::vector<int>>();
889   auto trace_recvcounts              = std::make_shared<std::vector<int>>();
890
891   const int* real_sendcounts         = sendcounts;
892   const int* real_senddispls          = senddispls;
893   const MPI_Datatype* real_sendtypes = sendtypes;
894   unsigned long maxsize      = 0;
895   for (int i = 0; i < size; i++) { // copy data to avoid bad free
896     if (recvtypes[i] == MPI_DATATYPE_NULL)
897       return MPI_ERR_TYPE;
898     recv_size += recvcounts[i] * recvtypes[i]->size();
899     trace_recvcounts->push_back(recvcounts[i] * recvtypes[i]->size());
900     if ((recvdispls[i] + (recvcounts[i] * recvtypes[i]->size())) > maxsize)
901       maxsize = recvdispls[i] + (recvcounts[i] * recvtypes[i]->size());
902   }
903
904   std::vector<unsigned char> tmp_sendbuf;
905   std::vector<int> tmp_sendcounts;
906   std::vector<int> tmp_senddispls;
907   std::vector<MPI_Datatype> tmp_sendtypes;
908   const void* real_sendbuf = smpi_get_in_place_buf(sendbuf, recvbuf, tmp_sendbuf, maxsize, MPI_CHAR);
909   if (sendbuf == MPI_IN_PLACE) {
910     tmp_sendcounts.assign(recvcounts, recvcounts + size);
911     real_sendcounts = tmp_sendcounts.data();
912     tmp_senddispls.assign(recvdispls, recvdispls + size);
913     real_senddispls = tmp_senddispls.data();
914     tmp_sendtypes.assign(recvtypes, recvtypes + size);
915     real_sendtypes = tmp_sendtypes.data();
916   }
917
918
919   if(recvtypes[comm->rank()]->size() * recvcounts[comm->rank()] !=  real_sendtypes[comm->rank()]->size() * real_sendcounts[comm->rank()]){
920     XBT_WARN("MPI_(I)Alltoallw : receive size from me differs from sent size to me : %zu vs %zu", recvtypes[comm->rank()]->size() * recvcounts[comm->rank()],  real_sendtypes[comm->rank()]->size() * real_sendcounts[comm->rank()]);
921     smpi_bench_begin();
922     return MPI_ERR_TRUNCATE;
923   }
924
925   for (int i = 0; i < size; i++) { // copy data to avoid bad free
926     send_size += real_sendcounts[i] * real_sendtypes[i]->size();
927     trace_sendcounts->push_back(real_sendcounts[i] * real_sendtypes[i]->size());
928   }
929
930   TRACE_smpi_comm_in(pid, request == MPI_REQUEST_IGNORED ? "PMPI_Alltoallw" : "PMPI_Ialltoallw",
931                      new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "alltoallv" : "ialltoallv", -1,
932                                                        send_size, trace_sendcounts, recv_size, trace_recvcounts,
933                                                        simgrid::smpi::Datatype::encode(real_sendtypes[0]),
934                                                        simgrid::smpi::Datatype::encode(recvtypes[0])));
935
936   int retval;
937   if (request == MPI_REQUEST_IGNORED)
938     retval = simgrid::smpi::colls::alltoallw(real_sendbuf, real_sendcounts, real_senddispls, real_sendtypes, recvbuf,
939                                              recvcounts, recvdispls, recvtypes, comm);
940   else
941     retval = simgrid::smpi::colls::ialltoallw(real_sendbuf, real_sendcounts, real_senddispls, real_sendtypes, recvbuf,
942                                               recvcounts, recvdispls, recvtypes, comm, request);
943
944   TRACE_smpi_comm_out(pid);
945   smpi_bench_begin();
946   return retval;
947 }