Logo AND Algorithmique Numérique Distribuée

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