Logo AND Algorithmique Numérique Distribuée

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