Debian Codegraph @

packages/tpetra/core/test/MPIAdvance/AllToAllV.cpp

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 #include <iostream>
11 #include <random>
12 #include <vector>
13
14 #include <mpi.h>
15
16 #include "Teuchos_Comm.hpp"
17 #include "Teuchos_DefaultMpiComm.hpp"
18 #include "Teuchos_UnitTestHarness.hpp"
19
20 #include "Kokkos_Core.hpp"
21
22 #include "Tpetra_Core.hpp"
23
24 #include <mpi_advance.h>
25
26 #include "print_vec.hpp"
27 #include "fake_alltoallv.hpp"
28
29 /*! \brief all ranks send and receive nothing
30
31 Passes if it does not crash or error out
32 */
33 template <typename Device>
34 void test_nothing(MPI_Comm comm, bool nullBufs, bool sameBufs,
35 Teuchos::FancyOStream &out, bool &success) {
36 static_assert(Kokkos::is_device_v<Device>, "");
37
38 int size, rank;
39 MPI_Comm_size(comm, &size);
40 MPI_Comm_rank(comm, &rank);
41
42 std::vector<int> sendcounts(size, 0);
43 std::vector<int> recvcounts(size, 0);
44
45 std::vector<int> senddispls(size, 0);
46 std::vector<int> recvdispls(size, 0);
47
48 char stackByte0, stackByte1;
49 void *sbuf, *rbuf;
50 if (nullBufs) {
51 sbuf = nullptr;
52 rbuf = nullptr;
53 } else {
54 sbuf = &stackByte0;
55 rbuf = &stackByte1;
56 }
57
58 if (sameBufs) {
59 sbuf = rbuf;
60 }
61
62 // create MPIX communicator
63 MPIX_Comm *mpixComm = nullptr;
64 MPIX_Comm_init(&mpixComm, comm);
65
66 // reference implementation should be okay
67 Fake_Alltoallv(sbuf, sendcounts.data(), senddispls.data(), MPI_BYTE, rbuf,
68 recvcounts.data(), recvdispls.data(), MPI_BYTE, comm);
69
70 // MPI advance implementation
71 MPIX_Alltoallv(sbuf, sendcounts.data(), senddispls.data(), MPI_BYTE, rbuf,
72 recvcounts.data(), recvdispls.data(), MPI_BYTE, mpixComm);
73
74 MPIX_Comm_free(mpixComm);
75
76 // we just require that we got this far
77 success = true;
78 }
79
80 /*! \brief all ranks send and receive some
81 */
82 template <typename Device>
83 void test_random(MPI_Comm comm, int seed, Teuchos::FancyOStream &out,
84 bool &success) {
85 static_assert(Kokkos::is_device_v<Device>, "");
86
87 int size, rank;
88 MPI_Comm_size(comm, &size);
89 MPI_Comm_rank(comm, &rank);
90
91 // seeded rng so all ranks generate the same communication plan
92 // plan [i * size + j] is count i sends to j
93 std::mt19937 rng(seed);
94 std::uniform_int_distribution<int> dist(1, 100);
95 std::vector<int> plan;
96 for (int i = 0; i < size * size; ++i) {
97 plan.push_back(dist(rng));
98 }
99
100 // read my part of the plan
101 std::vector<int> sendcounts, recvcounts, senddispls, recvdispls; // alltoallv
102
103 std::uniform_int_distribution<int> soffsetdist(0, 150);
104 rng.seed(seed + rank); // different seed -> different displs per rank
105 int initsdispl = soffsetdist(rng);
106 int sdispl = initsdispl;
107 for (int dest = 0; dest < size; ++dest) {
108 senddispls.push_back(sdispl);
109 int count = plan[rank * size + dest];
110 sendcounts.push_back(count);
111 sdispl += count + soffsetdist(rng);
112 }
113
114 std::uniform_int_distribution<int> roffsetdist(0, 150);
115 rng.seed(seed + size + rank); // different seed -> different displs (also per rank)
116 int initrdispl = roffsetdist(rng);
117 int rdispl = initrdispl;
118 for (int source = 0; source < size; ++source) {
119 recvdispls.push_back(rdispl);
120 int count = plan[source * size + rank];
121 recvcounts.push_back(count);
122 rdispl += count + roffsetdist(rng);
123 }
124
125 // create MPIX communicator
126 MPIX_Comm *mpixComm = nullptr;
127 MPIX_Comm_init(&mpixComm, comm);
128
129 // allocate send/recv bufs
130 // displs are in elements, so the displs are correct since MPI_BYTE
131 // matches type in bufs, alltoallv calls as calculated above
132 Kokkos::View<char *, typename Device::memory_space>
133 sbuf("sbuf", sdispl), exp("exp", rdispl), act("act", rdispl);
134
135 // fill send buf
136 Kokkos::parallel_for(sbuf.size(), KOKKOS_LAMBDA (size_t i) {sbuf(i) = i;});
137
138 // Use reference and MPI_Advance implementation to fill buffers
139 Fake_Alltoallv(sbuf.data(), sendcounts.data(), senddispls.data(), MPI_BYTE,
140 exp.data(), recvcounts.data(), recvdispls.data(), MPI_BYTE,
141 comm);
142
143
144 MPIX_Alltoallv(sbuf.data(), sendcounts.data(), senddispls.data(), MPI_BYTE,
145 act.data(), recvcounts.data(), recvdispls.data(), MPI_BYTE,
146 mpixComm);
147
148 MPIX_Comm_free(mpixComm);
149
150 // two recv buffers should be the same
151 auto exp_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), exp);
152 auto act_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), act);
153 TEST_ASSERT(exp_h.size() == act_h.size());
154 for (size_t i = 0; i < exp_h.size(); ++i) {
155 TEST_ASSERT(exp_h(i) == act_h(i));
156 }
157 }
158
159 static MPI_Comm tpetra_default_comm_as_mpi_comm() {
160 Teuchos::RCP<const Teuchos::Comm<int>> teuchosComm = Tpetra::getDefaultComm();
161
162 const Teuchos::MpiComm<int> *teuchosMpiComm =
163 dynamic_cast<const Teuchos::MpiComm<int> *>(teuchosComm.get());
164
165 if (teuchosMpiComm) {
166 return *(teuchosMpiComm->getRawMpiComm());
167 } else {
168 throw std::runtime_error(
169 "couldn't convert tpetra::getDefaultComm() into MPI_Comm");
170 }
171 }
172
173 TEUCHOS_UNIT_TEST(MpiAdvance, AllToAllV_nothing) {
174 using execution_space = Kokkos::DefaultExecutionSpace;
175 using memory_space = execution_space::memory_space;
176 using device_type = Kokkos::Device<execution_space, memory_space>;
177 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
178 test_nothing<device_type>(comm, false, false, out, success);
179 }
180
181 TEUCHOS_UNIT_TEST(MpiAdvance, AllToAllV_nothing_null) {
182 using execution_space = Kokkos::DefaultExecutionSpace;
183 using memory_space = execution_space::memory_space;
184 using device_type = Kokkos::Device<execution_space, memory_space>;
185 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
186 test_nothing<device_type>(comm, true, false, out, success);
187 }
188
189 TEUCHOS_UNIT_TEST(MpiAdvance, AllToAllV_nothing_same) {
190 using execution_space = Kokkos::DefaultExecutionSpace;
191 using memory_space = execution_space::memory_space;
192 using device_type = Kokkos::Device<execution_space, memory_space>;
193 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
194 test_nothing<device_type>(comm, false, true, out, success);
195 }
196
197 TEUCHOS_UNIT_TEST(MpiAdvance, AllToAllV_nothing_nullsame) {
198 using execution_space = Kokkos::DefaultExecutionSpace;
199 using memory_space = execution_space::memory_space;
200 using device_type = Kokkos::Device<execution_space, memory_space>;
201 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
202 test_nothing<device_type>(comm, true, true, out, success);
203 }
204
205 TEUCHOS_UNIT_TEST(MpiAdvance, AllToAllV_random) {
206 using execution_space = Kokkos::DefaultExecutionSpace;
207 using memory_space = execution_space::memory_space;
208 using device_type = Kokkos::Device<execution_space, memory_space>;
209 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
210 test_random<device_type>(comm, 42, out, success);
211 }
212
213 // Let Tpetra initialize Kokkos
214 // We define this because we don't also include ${TEUCHOS_STD_UNIT_TEST_MAIN}
215 // in the CMakeLists.txt
216 int main(int argc, char* argv[])
217 {
218 Tpetra::ScopeGuard tpetraScope(&argc, &argv);
219 const int errCode =
220 Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv);
221 return errCode;
222 }