Debian Codegraph @

packages/tpetra/core/test/MPIAdvance/fake_alltoall.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #pragma once
11
12 #include <mpi.h>
13
14 /*! reference alltoall impl
15 */
16 inline void Fake_Alltoall(const void *sendbuf, int sendcount,
17 MPI_Datatype sendtype, void *recvbuf,
18 int recvcount, MPI_Datatype recvtype,
19 MPI_Comm comm) {
20
21 constexpr int ARBITRARY_TAG = 0;
22
23 // communicator properties
24 int size, rank;
25 MPI_Comm_size(comm, &size);
26 MPI_Comm_rank(comm, &rank);
27
28 // requests for sends and receives
29 std::vector<MPI_Request> sreqs(size, MPI_REQUEST_NULL);
30 std::vector<MPI_Request> rreqs(size, MPI_REQUEST_NULL);
31
32 auto rb = reinterpret_cast<char *>(recvbuf);
33 auto sb = reinterpret_cast<const char *>(sendbuf);
34
35 // get sizes for indexing into sb / rb
36 int sendsize, recvsize;
37 MPI_Type_size(sendtype, &sendsize);
38 MPI_Type_size(recvtype, &recvsize);
39
40 // issue sends & recvs
41 for (int source = 0; source < size; ++source) {
42 MPI_Irecv(&rb[source * recvcount * recvsize], recvcount, recvtype, source,
43 ARBITRARY_TAG, comm, &rreqs[source]);
44 }
45 for (int dest = 0; dest < size; ++dest) {
46 MPI_Isend(&sb[dest * sendcount * sendsize], sendcount, sendtype, dest,
47 ARBITRARY_TAG, comm, &sreqs[dest]);
48 }
49
50 // wait for communication to finish
51 MPI_Waitall(size, sreqs.data(), MPI_STATUSES_IGNORE);
52 MPI_Waitall(size, rreqs.data(), MPI_STATUSES_IGNORE);
53 }