Logo AND Algorithmique Numérique Distribuée

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