1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
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. */
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"
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
16 #define CHECK_ARGS(test, errcode, ...) \
18 XBT_WARN(__VA_ARGS__); \
22 /* PMPI User level calls */
24 int PMPI_Barrier(MPI_Comm comm)
26 return PMPI_Ibarrier(comm, MPI_REQUEST_IGNORED);
29 int PMPI_Ibarrier(MPI_Comm comm, MPI_Request *request)
31 if (comm == MPI_COMM_NULL)
33 if (request == nullptr)
37 int rank = simgrid::s4u::this_actor::get_pid();
38 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Barrier" : "PMPI_Ibarrier",
39 new simgrid::instr::NoOpTIData(request == MPI_REQUEST_IGNORED ? "barrier" : "ibarrier"));
40 if (request == MPI_REQUEST_IGNORED) {
41 simgrid::smpi::Colls::barrier(comm);
42 // Barrier can be used to synchronize RMA calls. Finish all requests from comm before.
43 comm->finish_rma_calls();
45 simgrid::smpi::Colls::ibarrier(comm, request);
47 TRACE_smpi_comm_out(rank);
52 int PMPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
54 return PMPI_Ibcast(buf, count, datatype, root, comm, MPI_REQUEST_IGNORED);
57 int PMPI_Ibcast(void *buf, int count, MPI_Datatype datatype,
58 int root, MPI_Comm comm, MPI_Request* request)
60 if (comm == MPI_COMM_NULL)
62 if (buf == nullptr && count > 0)
63 return MPI_ERR_BUFFER;
64 if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid())
68 if (root < 0 || root >= comm->size())
70 if (request == nullptr)
74 int rank = simgrid::s4u::this_actor::get_pid();
75 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Bcast" : "PMPI_Ibcast",
76 new simgrid::instr::CollTIData(request == MPI_REQUEST_IGNORED ? "bcast" : "ibcast", root, -1.0,
77 datatype->is_replayable() ? count : count * datatype->size(), -1,
78 simgrid::smpi::Datatype::encode(datatype), ""));
79 if (comm->size() > 1) {
80 if (request == MPI_REQUEST_IGNORED)
81 simgrid::smpi::Colls::bcast(buf, count, datatype, root, comm);
83 simgrid::smpi::Colls::ibcast(buf, count, datatype, root, comm, request);
85 if (request != MPI_REQUEST_IGNORED)
86 *request = MPI_REQUEST_NULL;
89 TRACE_smpi_comm_out(rank);
94 int PMPI_Gather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,void *recvbuf, int recvcount, MPI_Datatype recvtype,
95 int root, MPI_Comm comm){
96 return PMPI_Igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, MPI_REQUEST_IGNORED);
99 int PMPI_Igather(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
100 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
102 if (comm == MPI_COMM_NULL)
104 if ((sendbuf == nullptr) || ((comm->rank() == root) && recvbuf == nullptr))
105 return MPI_ERR_BUFFER;
106 if (((sendbuf != MPI_IN_PLACE && sendcount > 0) && (sendtype == MPI_DATATYPE_NULL)) ||
107 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL)))
109 if (((sendbuf != MPI_IN_PLACE) && (sendcount < 0)) || ((comm->rank() == root) && (recvcount < 0)))
110 return MPI_ERR_COUNT;
111 if (root < 0 || root >= comm->size())
113 if (request == nullptr)
117 const void* real_sendbuf = sendbuf;
118 int real_sendcount = sendcount;
119 MPI_Datatype real_sendtype = sendtype;
120 if ((comm->rank() == root) && (sendbuf == MPI_IN_PLACE)) {
122 real_sendtype = recvtype;
124 int rank = simgrid::s4u::this_actor::get_pid();
126 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Gather" : "PMPI_Igather",
127 new simgrid::instr::CollTIData(
128 request == MPI_REQUEST_IGNORED ? "gather" : "igather", root, -1.0,
129 real_sendtype->is_replayable() ? real_sendcount : real_sendcount * real_sendtype->size(),
130 (comm->rank() != root || recvtype->is_replayable()) ? recvcount : recvcount * recvtype->size(),
131 simgrid::smpi::Datatype::encode(real_sendtype), simgrid::smpi::Datatype::encode(recvtype)));
132 if (request == MPI_REQUEST_IGNORED)
133 simgrid::smpi::Colls::gather(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype, root, comm);
135 simgrid::smpi::Colls::igather(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype, root, comm,
138 TRACE_smpi_comm_out(rank);
143 int PMPI_Gatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int *recvcounts, const int *displs,
144 MPI_Datatype recvtype, int root, MPI_Comm comm){
145 return PMPI_Igatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm, MPI_REQUEST_IGNORED);
148 int PMPI_Igatherv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, const int* recvcounts, const int* displs,
149 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
151 if (comm == MPI_COMM_NULL)
153 if ((sendbuf == nullptr && sendcount > 0) || ((comm->rank() == root) && recvbuf == nullptr))
154 return MPI_ERR_BUFFER;
155 if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) ||
156 ((comm->rank() == root) && (recvtype == MPI_DATATYPE_NULL)))
158 if ((sendbuf != MPI_IN_PLACE) && (sendcount < 0))
159 return MPI_ERR_COUNT;
160 if ((comm->rank() == root) && (recvcounts == nullptr || displs == nullptr))
162 if (root < 0 || root >= comm->size())
164 if (request == nullptr)
167 for (int i = 0; i < comm->size(); i++) {
168 if ((comm->rank() == root) && (recvcounts[i] < 0))
169 return MPI_ERR_COUNT;
173 const void* real_sendbuf = sendbuf;
174 int real_sendcount = sendcount;
175 MPI_Datatype real_sendtype = sendtype;
176 if ((comm->rank() == root) && (sendbuf == MPI_IN_PLACE)) {
178 real_sendtype = recvtype;
181 int rank = simgrid::s4u::this_actor::get_pid();
182 int dt_size_recv = recvtype->is_replayable() ? 1 : recvtype->size();
184 std::vector<int>* trace_recvcounts = new std::vector<int>;
185 if (comm->rank() == root) {
186 for (int i = 0; i < comm->size(); i++) // copy data to avoid bad free
187 trace_recvcounts->push_back(recvcounts[i] * dt_size_recv);
190 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Gatherv" : "PMPI_Igatherv",
191 new simgrid::instr::VarCollTIData(
192 request == MPI_REQUEST_IGNORED ? "gatherv" : "igatherv", root,
193 real_sendtype->is_replayable() ? real_sendcount : real_sendcount * real_sendtype->size(),
194 nullptr, dt_size_recv, trace_recvcounts, simgrid::smpi::Datatype::encode(real_sendtype),
195 simgrid::smpi::Datatype::encode(recvtype)));
196 if (request == MPI_REQUEST_IGNORED)
197 simgrid::smpi::Colls::gatherv(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcounts, displs, recvtype,
200 simgrid::smpi::Colls::igatherv(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcounts, displs, recvtype,
201 root, comm, request);
203 TRACE_smpi_comm_out(rank);
208 int PMPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
209 void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm){
210 return PMPI_Iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, MPI_REQUEST_IGNORED);
213 int PMPI_Iallgather(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
214 MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
216 if (comm == MPI_COMM_NULL)
218 if ((sendbuf == nullptr && sendcount > 0) || (recvbuf == nullptr))
219 return MPI_ERR_BUFFER;
220 if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) || (recvtype == MPI_DATATYPE_NULL))
222 if (((sendbuf != MPI_IN_PLACE) && (sendcount < 0)) || (recvcount < 0))
223 return MPI_ERR_COUNT;
224 if (request == nullptr)
228 if (sendbuf == MPI_IN_PLACE) {
229 sendbuf = static_cast<char*>(recvbuf) + recvtype->get_extent() * recvcount * comm->rank();
230 sendcount = recvcount;
233 int rank = simgrid::s4u::this_actor::get_pid();
235 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Allgather" : "PMPI_Iallggather",
236 new simgrid::instr::CollTIData(
237 request == MPI_REQUEST_IGNORED ? "allgather" : "iallgather", -1, -1.0,
238 sendtype->is_replayable() ? sendcount : sendcount * sendtype->size(),
239 recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
240 simgrid::smpi::Datatype::encode(sendtype), simgrid::smpi::Datatype::encode(recvtype)));
241 if (request == MPI_REQUEST_IGNORED)
242 simgrid::smpi::Colls::allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm);
244 simgrid::smpi::Colls::iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request);
246 TRACE_smpi_comm_out(rank);
251 int PMPI_Allgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
252 void *recvbuf, const int *recvcounts, const int *displs, MPI_Datatype recvtype, MPI_Comm comm){
253 return PMPI_Iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm, MPI_REQUEST_IGNORED);
256 int PMPI_Iallgatherv(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, const int* recvcounts, const int* displs,
257 MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
259 if (comm == MPI_COMM_NULL)
261 if ((sendbuf == nullptr && sendcount > 0) || (recvbuf == nullptr))
262 return MPI_ERR_BUFFER;
263 if (((sendbuf != MPI_IN_PLACE) && (sendtype == MPI_DATATYPE_NULL)) || (recvtype == MPI_DATATYPE_NULL))
265 if ((sendbuf != MPI_IN_PLACE) && (sendcount < 0))
266 return MPI_ERR_COUNT;
267 if (recvcounts == nullptr || displs == nullptr)
269 if (request == nullptr)
272 for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
273 if (recvcounts[i] < 0)
274 return MPI_ERR_COUNT;
278 if (sendbuf == MPI_IN_PLACE) {
279 sendbuf = static_cast<char*>(recvbuf) + recvtype->get_extent() * displs[comm->rank()];
280 sendcount = recvcounts[comm->rank()];
283 int rank = simgrid::s4u::this_actor::get_pid();
284 int dt_size_recv = recvtype->is_replayable() ? 1 : recvtype->size();
286 std::vector<int>* trace_recvcounts = new std::vector<int>;
287 for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
288 trace_recvcounts->push_back(recvcounts[i] * dt_size_recv);
292 rank, request == MPI_REQUEST_IGNORED ? "PMPI_Allgatherv" : "PMPI_Iallgatherv",
293 new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "allgatherv" : "iallgatherv", -1,
294 sendtype->is_replayable() ? sendcount : sendcount * sendtype->size(), nullptr,
295 dt_size_recv, trace_recvcounts, simgrid::smpi::Datatype::encode(sendtype),
296 simgrid::smpi::Datatype::encode(recvtype)));
297 if (request == MPI_REQUEST_IGNORED)
298 simgrid::smpi::Colls::allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
300 simgrid::smpi::Colls::iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm,
303 TRACE_smpi_comm_out(rank);
308 int PMPI_Scatter(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
309 void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm){
310 return PMPI_Iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, MPI_REQUEST_IGNORED);
313 int PMPI_Iscatter(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
314 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
316 if (comm == MPI_COMM_NULL)
318 if (((comm->rank() == root) && (sendtype == MPI_DATATYPE_NULL || not sendtype->is_valid())) ||
319 ((recvbuf != MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL || not recvtype->is_valid())))
321 if (((comm->rank() == root) && (sendcount < 0)) || ((recvbuf != MPI_IN_PLACE) && (recvcount < 0)))
322 return MPI_ERR_COUNT;
323 if ((sendbuf == recvbuf) || ((comm->rank() == root) && sendcount > 0 && (sendbuf == nullptr)) ||
324 (recvcount > 0 && recvbuf == nullptr))
325 return MPI_ERR_BUFFER;
326 if (root < 0 || root >= comm->size())
328 if (request == nullptr)
332 if (recvbuf == MPI_IN_PLACE) {
334 recvcount = sendcount;
336 int rank = simgrid::s4u::this_actor::get_pid();
338 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Scatter" : "PMPI_Iscatter",
339 new simgrid::instr::CollTIData(
340 request == MPI_REQUEST_IGNORED ? "scatter" : "iscatter", root, -1.0,
341 (comm->rank() != root || sendtype->is_replayable()) ? sendcount : sendcount * sendtype->size(),
342 recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
343 simgrid::smpi::Datatype::encode(sendtype), simgrid::smpi::Datatype::encode(recvtype)));
344 if (request == MPI_REQUEST_IGNORED)
345 simgrid::smpi::Colls::scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm);
347 simgrid::smpi::Colls::iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request);
349 TRACE_smpi_comm_out(rank);
354 int PMPI_Scatterv(const void *sendbuf, const int *sendcounts, const int *displs,
355 MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm){
356 return PMPI_Iscatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm, MPI_REQUEST_IGNORED);
359 int PMPI_Iscatterv(const void* sendbuf, const int* sendcounts, const int* displs, MPI_Datatype sendtype, void* recvbuf, int recvcount,
360 MPI_Datatype recvtype, int root, MPI_Comm comm, MPI_Request* request)
362 CHECK_ARGS(comm == MPI_COMM_NULL, MPI_ERR_COMM, "Iscatterv: the communicator cannot be MPI_COMM_NULL");
363 CHECK_ARGS(sendcounts == nullptr, MPI_ERR_ARG, "Iscatterv: param 2 sendcounts cannot be NULL");
364 CHECK_ARGS(displs == nullptr, MPI_ERR_ARG, "Iscatterv: param 3 displs cannot be NULL");
365 CHECK_ARGS((comm->rank() == root) && (sendtype == MPI_DATATYPE_NULL), MPI_ERR_TYPE,
366 "Iscatterv: The sendtype cannot be NULL on the root rank");
367 CHECK_ARGS((recvbuf != MPI_IN_PLACE) && (recvtype == MPI_DATATYPE_NULL), MPI_ERR_TYPE,
368 "Iscatterv: the recvtype cannot be NULL when not receiving in place");
369 CHECK_ARGS(request == nullptr, MPI_ERR_ARG, "Iscatterv: param 10 request cannot be NULL");
370 CHECK_ARGS(recvbuf != MPI_IN_PLACE && recvcount < 0, MPI_ERR_COUNT,
371 "Iscatterv: When not receiving in place, the recvcound cannot be negative");
372 CHECK_ARGS(root < 0, MPI_ERR_ROOT, "Iscatterv: root cannot be negative");
373 CHECK_ARGS(root >= comm->size(), MPI_ERR_ROOT, "Iscatterv: root (=%d) is larger than communicator size (=%d)", root,
376 if (comm->rank() == root) {
377 if (recvbuf == MPI_IN_PLACE) {
379 recvcount = sendcounts[comm->rank()];
381 for (int i = 0; i < comm->size(); i++)
382 CHECK_ARGS(sendcounts[i] < 0, MPI_ERR_COUNT, "Iscatterv: sendcounts[%d]=%d but this cannot be negative", i,
388 int rank = simgrid::s4u::this_actor::get_pid();
389 int dt_size_send = sendtype->is_replayable() ? 1 : sendtype->size();
391 std::vector<int>* trace_sendcounts = new std::vector<int>;
392 if (comm->rank() == root) {
393 for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
394 trace_sendcounts->push_back(sendcounts[i] * dt_size_send);
398 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Scatterv" : "PMPI_Iscatterv",
399 new simgrid::instr::VarCollTIData(
400 request == MPI_REQUEST_IGNORED ? "scatterv" : "iscatterv", root, dt_size_send,
401 trace_sendcounts, recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
402 nullptr, simgrid::smpi::Datatype::encode(sendtype),
403 simgrid::smpi::Datatype::encode(recvtype)));
404 if (request == MPI_REQUEST_IGNORED)
405 simgrid::smpi::Colls::scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm);
407 simgrid::smpi::Colls::iscatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm,
410 TRACE_smpi_comm_out(rank);
415 int PMPI_Reduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)
417 return PMPI_Ireduce(sendbuf, recvbuf, count, datatype, op, root, comm, MPI_REQUEST_IGNORED);
420 int PMPI_Ireduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm, MPI_Request* request)
422 if (comm == MPI_COMM_NULL)
424 if ((sendbuf == nullptr && count > 0) || ((comm->rank() == root) && recvbuf == nullptr))
425 return MPI_ERR_BUFFER;
426 if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid())
428 if (op == MPI_OP_NULL)
430 if (request == nullptr)
432 if (root < 0 || root >= comm->size())
435 return MPI_ERR_COUNT;
438 int rank = simgrid::s4u::this_actor::get_pid();
440 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Reduce" : "PMPI_Ireduce",
441 new simgrid::instr::CollTIData(request == MPI_REQUEST_IGNORED ? "reduce" : "ireduce", root, 0,
442 datatype->is_replayable() ? count : count * datatype->size(), -1,
443 simgrid::smpi::Datatype::encode(datatype), ""));
444 if (request == MPI_REQUEST_IGNORED)
445 simgrid::smpi::Colls::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
447 simgrid::smpi::Colls::ireduce(sendbuf, recvbuf, count, datatype, op, root, comm, request);
449 TRACE_smpi_comm_out(rank);
454 int PMPI_Reduce_local(const void* inbuf, void* inoutbuf, int count, MPI_Datatype datatype, MPI_Op op)
456 if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid())
458 if (op == MPI_OP_NULL)
461 return MPI_ERR_COUNT;
464 op->apply(inbuf, inoutbuf, &count, datatype);
469 int PMPI_Allreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
471 return PMPI_Iallreduce(sendbuf, recvbuf, count, datatype, op, comm, MPI_REQUEST_IGNORED);
474 int PMPI_Iallreduce(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request *request)
476 if (comm == MPI_COMM_NULL)
478 if ((sendbuf == nullptr && count > 0) || (recvbuf == nullptr))
479 return MPI_ERR_BUFFER;
480 if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid())
483 return MPI_ERR_COUNT;
484 if (op == MPI_OP_NULL)
486 if (request == nullptr)
490 const void* real_sendbuf = sendbuf;
491 std::unique_ptr<unsigned char[]> tmp_sendbuf;
492 if (sendbuf == MPI_IN_PLACE) {
493 tmp_sendbuf.reset(new unsigned char[count * datatype->get_extent()]);
494 simgrid::smpi::Datatype::copy(recvbuf, count, datatype, tmp_sendbuf.get(), count, datatype);
495 real_sendbuf = tmp_sendbuf.get();
497 int rank = simgrid::s4u::this_actor::get_pid();
499 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Allreduce" : "PMPI_Iallreduce",
500 new simgrid::instr::CollTIData(request == MPI_REQUEST_IGNORED ? "allreduce" : "iallreduce", -1, 0,
501 datatype->is_replayable() ? count : count * datatype->size(), -1,
502 simgrid::smpi::Datatype::encode(datatype), ""));
504 if (request == MPI_REQUEST_IGNORED)
505 simgrid::smpi::Colls::allreduce(real_sendbuf, recvbuf, count, datatype, op, comm);
507 simgrid::smpi::Colls::iallreduce(real_sendbuf, recvbuf, count, datatype, op, comm, request);
509 TRACE_smpi_comm_out(rank);
514 int PMPI_Scan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
516 return PMPI_Iscan(sendbuf, recvbuf, count, datatype, op, comm, MPI_REQUEST_IGNORED);
519 int PMPI_Iscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request)
521 if (comm == MPI_COMM_NULL)
523 if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid())
525 if (op == MPI_OP_NULL)
527 if (request == nullptr)
530 return MPI_ERR_COUNT;
531 if (sendbuf == nullptr || recvbuf == nullptr)
532 return MPI_ERR_BUFFER;
535 int rank = simgrid::s4u::this_actor::get_pid();
536 const void* real_sendbuf = sendbuf;
537 std::unique_ptr<unsigned char[]> tmp_sendbuf;
538 if (sendbuf == MPI_IN_PLACE) {
539 tmp_sendbuf.reset(new unsigned char[count * datatype->size()]);
540 real_sendbuf = memcpy(tmp_sendbuf.get(), recvbuf, count * datatype->size());
542 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Scan" : "PMPI_Iscan",
543 new simgrid::instr::Pt2PtTIData(request == MPI_REQUEST_IGNORED ? "scan" : "iscan", -1,
544 datatype->is_replayable() ? count : count * datatype->size(),
545 simgrid::smpi::Datatype::encode(datatype)));
548 if (request == MPI_REQUEST_IGNORED)
549 retval = simgrid::smpi::Colls::scan(real_sendbuf, recvbuf, count, datatype, op, comm);
551 retval = simgrid::smpi::Colls::iscan(real_sendbuf, recvbuf, count, datatype, op, comm, request);
553 TRACE_smpi_comm_out(rank);
558 int PMPI_Exscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
560 return PMPI_Iexscan(sendbuf, recvbuf, count, datatype, op, comm, MPI_REQUEST_IGNORED);
563 int PMPI_Iexscan(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request* request){
564 if (comm == MPI_COMM_NULL)
566 if (not datatype->is_valid())
568 if (op == MPI_OP_NULL)
570 if (request == nullptr)
573 return MPI_ERR_COUNT;
574 if (sendbuf == nullptr || recvbuf == nullptr)
575 return MPI_ERR_BUFFER;
578 int rank = simgrid::s4u::this_actor::get_pid();
579 const void* real_sendbuf = sendbuf;
580 std::unique_ptr<unsigned char[]> tmp_sendbuf;
581 if (sendbuf == MPI_IN_PLACE) {
582 tmp_sendbuf.reset(new unsigned char[count * datatype->size()]);
583 real_sendbuf = memcpy(tmp_sendbuf.get(), recvbuf, count * datatype->size());
586 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Exscan" : "PMPI_Iexscan",
587 new simgrid::instr::Pt2PtTIData(request == MPI_REQUEST_IGNORED ? "exscan" : "iexscan", -1,
588 datatype->is_replayable() ? count : count * datatype->size(),
589 simgrid::smpi::Datatype::encode(datatype)));
592 if (request == MPI_REQUEST_IGNORED)
593 retval = simgrid::smpi::Colls::exscan(real_sendbuf, recvbuf, count, datatype, op, comm);
595 retval = simgrid::smpi::Colls::iexscan(real_sendbuf, recvbuf, count, datatype, op, comm, request);
597 TRACE_smpi_comm_out(rank);
602 int PMPI_Reduce_scatter(const void *sendbuf, void *recvbuf, const int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
604 return PMPI_Ireduce_scatter(sendbuf, recvbuf, recvcounts, datatype, op, comm, MPI_REQUEST_IGNORED);
607 int PMPI_Ireduce_scatter(const void *sendbuf, void *recvbuf, const int *recvcounts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm, MPI_Request *request)
609 if (comm == MPI_COMM_NULL)
611 if ((sendbuf == nullptr) || (recvbuf == nullptr))
612 return MPI_ERR_BUFFER;
613 if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid())
615 if (op == MPI_OP_NULL)
617 if (recvcounts == nullptr)
619 if (request == nullptr)
622 for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
623 if (recvcounts[i] < 0)
624 return MPI_ERR_COUNT;
628 int rank = simgrid::s4u::this_actor::get_pid();
629 std::vector<int>* trace_recvcounts = new std::vector<int>;
630 int dt_send_size = datatype->is_replayable() ? 1 : datatype->size();
633 for (int i = 0; i < comm->size(); i++) { // copy data to avoid bad free
634 trace_recvcounts->push_back(recvcounts[i] * dt_send_size);
635 totalcount += recvcounts[i];
638 const void* real_sendbuf = sendbuf;
639 std::unique_ptr<unsigned char[]> tmp_sendbuf;
640 if (sendbuf == MPI_IN_PLACE) {
641 tmp_sendbuf.reset(new unsigned char[totalcount * datatype->size()]);
642 real_sendbuf = memcpy(tmp_sendbuf.get(), recvbuf, totalcount * datatype->size());
645 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Reduce_scatter" : "PMPI_Ireduce_scatter",
646 new simgrid::instr::VarCollTIData(
647 request == MPI_REQUEST_IGNORED ? "reducescatter" : "ireducescatter", -1, dt_send_size, nullptr,
648 -1, trace_recvcounts, simgrid::smpi::Datatype::encode(datatype), ""));
650 if (request == MPI_REQUEST_IGNORED)
651 simgrid::smpi::Colls::reduce_scatter(real_sendbuf, recvbuf, recvcounts, datatype, op, comm);
653 simgrid::smpi::Colls::ireduce_scatter(real_sendbuf, recvbuf, recvcounts, datatype, op, comm, request);
655 TRACE_smpi_comm_out(rank);
660 int PMPI_Reduce_scatter_block(const void *sendbuf, void *recvbuf, int recvcount,
661 MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
663 return PMPI_Ireduce_scatter_block(sendbuf, recvbuf, recvcount, datatype, op, comm, MPI_REQUEST_IGNORED);
666 int PMPI_Ireduce_scatter_block(const void* sendbuf, void* recvbuf, int recvcount, MPI_Datatype datatype, MPI_Op op,
667 MPI_Comm comm, MPI_Request* request)
669 if (comm == MPI_COMM_NULL)
671 if (not datatype->is_valid())
673 if (op == MPI_OP_NULL)
677 if (request == nullptr)
681 int count = comm->size();
683 int rank = simgrid::s4u::this_actor::get_pid();
684 int dt_send_size = datatype->is_replayable() ? 1 : datatype->size();
685 std::vector<int>* trace_recvcounts = new std::vector<int>(recvcount * dt_send_size); // copy data to avoid bad free
687 const void* real_sendbuf = sendbuf;
688 std::unique_ptr<unsigned char[]> tmp_sendbuf;
689 if (sendbuf == MPI_IN_PLACE) {
690 tmp_sendbuf.reset(new unsigned char[recvcount * count * datatype->size()]);
691 real_sendbuf = memcpy(tmp_sendbuf.get(), recvbuf, recvcount * count * datatype->size());
695 rank, request == MPI_REQUEST_IGNORED ? "PMPI_Reduce_scatter_block" : "PMPI_Ireduce_scatter_block",
696 new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "reducescatter" : "ireducescatter", -1, 0,
697 nullptr, -1, trace_recvcounts, simgrid::smpi::Datatype::encode(datatype), ""));
699 int* recvcounts = new int[count];
700 for (int i = 0; i < count; i++)
701 recvcounts[i] = recvcount;
702 if (request == MPI_REQUEST_IGNORED)
703 simgrid::smpi::Colls::reduce_scatter(real_sendbuf, recvbuf, recvcounts, datatype, op, comm);
705 simgrid::smpi::Colls::ireduce_scatter(real_sendbuf, recvbuf, recvcounts, datatype, op, comm, request);
708 TRACE_smpi_comm_out(rank);
713 int PMPI_Alltoall(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
714 MPI_Datatype recvtype, MPI_Comm comm){
715 return PMPI_Ialltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, MPI_REQUEST_IGNORED);
718 int PMPI_Ialltoall(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
719 MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
721 if (comm == MPI_COMM_NULL)
723 if ((sendbuf == nullptr && sendcount > 0) || (recvbuf == nullptr && recvcount > 0))
724 return MPI_ERR_BUFFER;
725 if ((sendbuf != MPI_IN_PLACE && sendtype == MPI_DATATYPE_NULL) || recvtype == MPI_DATATYPE_NULL)
727 if ((sendbuf != MPI_IN_PLACE && sendcount < 0) || recvcount < 0)
728 return MPI_ERR_COUNT;
729 if (request == nullptr)
733 int rank = simgrid::s4u::this_actor::get_pid();
734 const void* real_sendbuf = sendbuf;
735 int real_sendcount = sendcount;
736 MPI_Datatype real_sendtype = sendtype;
737 std::unique_ptr<unsigned char[]> tmp_sendbuf;
738 if (sendbuf == MPI_IN_PLACE) {
739 tmp_sendbuf.reset(new unsigned char[recvcount * comm->size() * recvtype->size()]);
740 // memcpy(??,nullptr,0) is actually undefined behavor, even if harmless.
741 if (recvbuf != nullptr)
742 memcpy(tmp_sendbuf.get(), recvbuf, recvcount * comm->size() * recvtype->size());
743 real_sendbuf = tmp_sendbuf.get();
744 real_sendcount = recvcount;
745 real_sendtype = recvtype;
748 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Alltoall" : "PMPI_Ialltoall",
749 new simgrid::instr::CollTIData(
750 request == MPI_REQUEST_IGNORED ? "alltoall" : "ialltoall", -1, -1.0,
751 real_sendtype->is_replayable() ? real_sendcount : real_sendcount * real_sendtype->size(),
752 recvtype->is_replayable() ? recvcount : recvcount * recvtype->size(),
753 simgrid::smpi::Datatype::encode(real_sendtype), simgrid::smpi::Datatype::encode(recvtype)));
755 if (request == MPI_REQUEST_IGNORED)
757 simgrid::smpi::Colls::alltoall(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype, comm);
759 retval = simgrid::smpi::Colls::ialltoall(real_sendbuf, real_sendcount, real_sendtype, recvbuf, recvcount, recvtype,
762 TRACE_smpi_comm_out(rank);
767 int PMPI_Alltoallv(const void* sendbuf, const int* sendcounts, const int* senddisps, MPI_Datatype sendtype, void* recvbuf,
768 const int* recvcounts, const int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
770 return PMPI_Ialltoallv(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype, comm, MPI_REQUEST_IGNORED);
773 int PMPI_Ialltoallv(const void* sendbuf, const int* sendcounts, const int* senddisps, MPI_Datatype sendtype, void* recvbuf,
774 const int* recvcounts, const int* recvdisps, MPI_Datatype recvtype, MPI_Comm comm, MPI_Request* request)
776 if (comm == MPI_COMM_NULL)
778 if (sendbuf == nullptr || recvbuf == nullptr)
779 return MPI_ERR_BUFFER;
780 if ((sendbuf != MPI_IN_PLACE && sendtype == MPI_DATATYPE_NULL) || recvtype == MPI_DATATYPE_NULL)
782 if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
783 recvdisps == nullptr)
785 if (request == nullptr)
788 int rank = simgrid::s4u::this_actor::get_pid();
789 int size = comm->size();
790 for (int i = 0; i < size; i++) {
791 if (recvcounts[i] < 0 || (sendbuf != MPI_IN_PLACE && sendcounts[i] < 0))
792 return MPI_ERR_COUNT;
798 std::vector<int>* trace_sendcounts = new std::vector<int>;
799 std::vector<int>* trace_recvcounts = new std::vector<int>;
800 int dt_size_recv = recvtype->size();
802 const void* real_sendbuf = sendbuf;
803 const int* real_sendcounts = sendcounts;
804 const int* real_senddisps = senddisps;
805 MPI_Datatype real_sendtype = sendtype;
807 for (int i = 0; i < size; i++) { // copy data to avoid bad free
808 recv_size += recvcounts[i] * dt_size_recv;
809 trace_recvcounts->push_back(recvcounts[i] * dt_size_recv);
810 if (((recvdisps[i] + recvcounts[i]) * dt_size_recv) > maxsize)
811 maxsize = (recvdisps[i] + recvcounts[i]) * dt_size_recv;
814 std::unique_ptr<unsigned char[]> tmp_sendbuf;
815 std::unique_ptr<int[]> tmp_sendcounts;
816 std::unique_ptr<int[]> tmp_senddisps;
817 if (sendbuf == MPI_IN_PLACE) {
818 tmp_sendbuf.reset(new unsigned char[maxsize]);
819 real_sendbuf = memcpy(tmp_sendbuf.get(), recvbuf, maxsize);
820 tmp_sendcounts.reset(new int[size]);
821 std::copy(recvcounts, recvcounts + size, tmp_sendcounts.get());
822 real_sendcounts = tmp_sendcounts.get();
823 tmp_senddisps.reset(new int[size]);
824 std::copy(recvdisps, recvdisps + size, tmp_senddisps.get());
825 real_senddisps = tmp_senddisps.get();
826 real_sendtype = recvtype;
829 int dt_size_send = real_sendtype->size();
831 for (int i = 0; i < size; i++) { // copy data to avoid bad free
832 send_size += real_sendcounts[i] * dt_size_send;
833 trace_sendcounts->push_back(real_sendcounts[i] * dt_size_send);
836 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Alltoallv" : "PMPI_Ialltoallv",
837 new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "alltoallv" : "ialltoallv", -1,
838 send_size, trace_sendcounts, recv_size, trace_recvcounts,
839 simgrid::smpi::Datatype::encode(real_sendtype),
840 simgrid::smpi::Datatype::encode(recvtype)));
843 if (request == MPI_REQUEST_IGNORED)
844 retval = simgrid::smpi::Colls::alltoallv(real_sendbuf, real_sendcounts, real_senddisps, real_sendtype, recvbuf,
845 recvcounts, recvdisps, recvtype, comm);
847 retval = simgrid::smpi::Colls::ialltoallv(real_sendbuf, real_sendcounts, real_senddisps, real_sendtype, recvbuf,
848 recvcounts, recvdisps, recvtype, comm, request);
850 TRACE_smpi_comm_out(rank);
855 int PMPI_Alltoallw(const void* sendbuf, const int* sendcounts, const int* senddisps, const MPI_Datatype* sendtypes, void* recvbuf,
856 const int* recvcounts, const int* recvdisps, const MPI_Datatype* recvtypes, MPI_Comm comm)
858 return PMPI_Ialltoallw(sendbuf, sendcounts, senddisps, sendtypes, recvbuf, recvcounts, recvdisps, recvtypes, comm, MPI_REQUEST_IGNORED);
861 int PMPI_Ialltoallw(const void* sendbuf, const int* sendcounts, const int* senddisps, const MPI_Datatype* sendtypes, void* recvbuf,
862 const int* recvcounts, const int* recvdisps, const MPI_Datatype* recvtypes, MPI_Comm comm, MPI_Request* request)
864 if (comm == MPI_COMM_NULL)
866 if (sendbuf == nullptr || recvbuf == nullptr)
867 return MPI_ERR_BUFFER;
868 if ((sendbuf != MPI_IN_PLACE && sendtypes == nullptr) || recvtypes == nullptr)
870 if ((sendbuf != MPI_IN_PLACE && (sendcounts == nullptr || senddisps == nullptr)) || recvcounts == nullptr ||
871 recvdisps == nullptr)
873 if (request == nullptr)
877 int rank = simgrid::s4u::this_actor::get_pid();
878 int size = comm->size();
879 for (int i = 0; i < size; i++) { // copy data to avoid bad free
880 if (recvcounts[i] < 0 || (sendbuf != MPI_IN_PLACE && sendcounts[i] < 0))
881 return MPI_ERR_COUNT;
885 std::vector<int>* trace_sendcounts = new std::vector<int>;
886 std::vector<int>* trace_recvcounts = new std::vector<int>;
888 const void* real_sendbuf = sendbuf;
889 const int* real_sendcounts = sendcounts;
890 const int* real_senddisps = senddisps;
891 const MPI_Datatype* real_sendtypes = sendtypes;
892 unsigned long maxsize = 0;
893 for (int i = 0; i < size; i++) { // copy data to avoid bad free
894 if (recvtypes[i] == MPI_DATATYPE_NULL) {
895 delete trace_recvcounts;
896 delete trace_sendcounts;
899 recv_size += recvcounts[i] * recvtypes[i]->size();
900 trace_recvcounts->push_back(recvcounts[i] * recvtypes[i]->size());
901 if ((recvdisps[i] + (recvcounts[i] * recvtypes[i]->size())) > maxsize)
902 maxsize = recvdisps[i] + (recvcounts[i] * recvtypes[i]->size());
905 std::unique_ptr<unsigned char[]> tmp_sendbuf;
906 std::unique_ptr<int[]> tmp_sendcounts;
907 std::unique_ptr<int[]> tmp_senddisps;
908 std::unique_ptr<MPI_Datatype[]> tmp_sendtypes;
909 if (sendbuf == MPI_IN_PLACE) {
910 tmp_sendbuf.reset(new unsigned char[maxsize]);
911 real_sendbuf = memcpy(tmp_sendbuf.get(), recvbuf, maxsize);
912 tmp_sendcounts.reset(new int[size]);
913 std::copy(recvcounts, recvcounts + size, tmp_sendcounts.get());
914 real_sendcounts = tmp_sendcounts.get();
915 tmp_senddisps.reset(new int[size]);
916 std::copy(recvdisps, recvdisps + size, tmp_senddisps.get());
917 real_senddisps = tmp_senddisps.get();
918 tmp_sendtypes.reset(new MPI_Datatype[size]);
919 std::copy(recvtypes, recvtypes + size, tmp_sendtypes.get());
920 real_sendtypes = tmp_sendtypes.get();
923 for (int i = 0; i < size; i++) { // copy data to avoid bad free
924 send_size += real_sendcounts[i] * real_sendtypes[i]->size();
925 trace_sendcounts->push_back(real_sendcounts[i] * real_sendtypes[i]->size());
928 TRACE_smpi_comm_in(rank, request == MPI_REQUEST_IGNORED ? "PMPI_Alltoallw" : "PMPI_Ialltoallw",
929 new simgrid::instr::VarCollTIData(request == MPI_REQUEST_IGNORED ? "alltoallv" : "ialltoallv", -1,
930 send_size, trace_sendcounts, recv_size, trace_recvcounts,
931 simgrid::smpi::Datatype::encode(real_sendtypes[0]),
932 simgrid::smpi::Datatype::encode(recvtypes[0])));
935 if (request == MPI_REQUEST_IGNORED)
936 retval = simgrid::smpi::Colls::alltoallw(real_sendbuf, real_sendcounts, real_senddisps, real_sendtypes, recvbuf,
937 recvcounts, recvdisps, recvtypes, comm);
939 retval = simgrid::smpi::Colls::ialltoallw(real_sendbuf, real_sendcounts, real_senddisps, real_sendtypes, recvbuf,
940 recvcounts, recvdisps, recvtypes, comm, request);
942 TRACE_smpi_comm_out(rank);