Debian Codegraph @

packages/tpetra/core/test/MPIAdvance/NeighborAllToAllV.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_Dist_graph_create_adjacent(
65 comm, 0, /*indegree*/
66 nullptr, /*sources*/
67 nullptr, /*sourceweights*/
68 0, /*outdegree*/
69 nullptr /*destinations*/, nullptr /*destweights*/, MPI_INFO_NULL /*info*/,
70 0 /*reorder*/, &mpixComm);
71
72 // reference implementation should be okay
73 Fake_Alltoallv(sbuf, sendcounts.data(), senddispls.data(), MPI_BYTE, rbuf,
74 recvcounts.data(), recvdispls.data(), MPI_BYTE, comm);
75
76 // MPI advance implementation
77 MPIX_Neighbor_alltoallv(sbuf, sendcounts.data(), senddispls.data(), MPI_BYTE,
78 rbuf, recvcounts.data(), recvdispls.data(), MPI_BYTE,
79 mpixComm);
80
81 MPIX_Comm_free(mpixComm);
82
83 // we just require that we got this far
84 success = true;
85 }
86
87 /*! \brief all ranks send and receive some
88 */
89 template <typename Device>
90 void test_random(MPI_Comm comm, int seed, Teuchos::FancyOStream &out,
91 bool &success) {
92 static_assert(Kokkos::is_device_v<Device>, "");
93
94 int size, rank;
95 MPI_Comm_size(comm, &size);
96 MPI_Comm_rank(comm, &rank);
97
98 // seeded rng so all ranks generate the same communication plan
99 // plan [i * size + j] is count i sends to j
100 std::mt19937 rng(seed);
101 std::uniform_int_distribution<int> dist(1, 100);
102 std::vector<int> plan;
103 for (int i = 0; i < size * size; ++i) {
104 plan.push_back(dist(rng));
105 }
106
107 // read my part of the plan
108 std::vector<int> sendcounts, recvcounts, senddispls, recvdispls; // alltoallv
109 std::vector<int> nbrsendcounts, nbrrecvcounts, nbrsenddispls, nbrrecvdispls; // neighbor alltoallv
110 std::vector<int> sources, sourceweights, destinations, destweights; // communicator
111
112 std::uniform_int_distribution<int> soffsetdist(0, 150);
113 rng.seed(seed + rank); // different seed -> different displs per rank
114 int initsdispl = soffsetdist(rng);
115 int sdispl = initsdispl;
116 int nbrsdispl = initsdispl;
117 for (int dest = 0; dest < size; ++dest) {
118 senddispls.push_back(sdispl);
119 int count = plan[rank * size + dest];
120 sendcounts.push_back(count);
121 int offset = soffsetdist(rng);
122 sdispl += count + offset;
123
124 if (count > 0) {
125 destinations.push_back(dest);
126 destweights.push_back(count);
127 nbrsendcounts.push_back(count);
128 nbrsenddispls.push_back(nbrsdispl);
129 nbrsdispl += count + offset;
130 }
131 }
132
133 std::uniform_int_distribution<int> roffsetdist(0, 150);
134 rng.seed(seed + size + rank); // different seed -> different displs (also per rank)
135 int initrdispl = roffsetdist(rng);
136 int rdispl = initrdispl;
137 int nbrrdispl = initrdispl;
138 for (int source = 0; source < size; ++source) {
139 recvdispls.push_back(rdispl);
140 int count = plan[source * size + rank];
141 recvcounts.push_back(count);
142 int offset = roffsetdist(rng);
143 rdispl += count + offset;
144
145 if (count > 0) {
146 sources.push_back(source);
147 sourceweights.push_back(count);
148 nbrrecvcounts.push_back(count);
149 nbrrecvdispls.push_back(nbrrdispl);
150 nbrrdispl += count + offset;
151 }
152 }
153
154
155 print_vec(rank, "sources", sources);
156 print_vec(rank, "sourceweights", sourceweights);
157 print_vec(rank, "destinations", destinations);
158 print_vec(rank, "destweights", destweights);
159
160 // create MPIX communicator
161 MPIX_Comm *mpixComm = nullptr;
162 MPIX_Dist_graph_create_adjacent(
163 comm, sources.size(), /*indegree*/
164 sources.data(), /*sources*/
165 sourceweights.data(), /*sourceweights*/
166 destinations.size(), /*outdegree*/
167 destinations.data() /*destinations*/, destweights.data() /*destweights*/,
168 MPI_INFO_NULL /*info*/, 0 /*reorder*/, &mpixComm);
169
170 // allocate send/recv bufs
171 // displs are in elements, so the displs are correct since MPI_BYTE
172 // matches type in bufs, alltoallv calls as calculated above
173 Kokkos::View<char *, typename Device::memory_space>
174 sbuf("sbuf", sdispl), exp("exp", rdispl), act("act", nbrrdispl);
175
176 // fill send buf
177 Kokkos::parallel_for(sbuf.size(), KOKKOS_LAMBDA (size_t i) {sbuf(i) = i;});
178
179 // Use reference and MPI_Advance implementation to fill buffers
180 Fake_Alltoallv(sbuf.data(), sendcounts.data(), senddispls.data(), MPI_BYTE,
181 exp.data(), recvcounts.data(), recvdispls.data(), MPI_BYTE,
182 comm);
183
184 print_vec(rank, "nbrsendcounts", nbrsendcounts);
185 print_vec(rank, "nbrsenddispls", nbrsenddispls);
186 print_vec(rank, "nbrrecvcounts", nbrrecvcounts);
187 print_vec(rank, "nbrrecvdispls", nbrrecvdispls);
188
189 MPIX_Neighbor_alltoallv(sbuf.data(), nbrsendcounts.data(), nbrsenddispls.data(), MPI_BYTE,
190 act.data(), nbrrecvcounts.data(), nbrrecvdispls.data(), MPI_BYTE,
191 mpixComm);
192
193 MPIX_Comm_free(mpixComm);
194
195 // two recv buffers should be the same
196 auto exp_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), exp);
197 auto act_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), act);
198 TEST_ASSERT(exp_h.size() == act_h.size());
199 for (size_t i = 0; i < exp_h.size(); ++i) {
200 TEST_ASSERT(exp_h(i) == act_h(i));
201 }
202 }
203
204 static MPI_Comm tpetra_default_comm_as_mpi_comm() {
205 Teuchos::RCP<const Teuchos::Comm<int>> teuchosComm = Tpetra::getDefaultComm();
206
207 const Teuchos::MpiComm<int> *teuchosMpiComm =
208 dynamic_cast<const Teuchos::MpiComm<int> *>(teuchosComm.get());
209
210 if (teuchosMpiComm) {
211 return *(teuchosMpiComm->getRawMpiComm());
212 } else {
213 throw std::runtime_error(
214 "couldn't convert tpetra::getDefaultComm() into MPI_Comm");
215 }
216 }
217
218 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllV_nothing) {
219 using execution_space = Kokkos::DefaultExecutionSpace;
220 using memory_space = execution_space::memory_space;
221 using device_type = Kokkos::Device<execution_space, memory_space>;
222 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
223 test_nothing<device_type>(comm, false, false, out, success);
224 }
225
226 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllV_nothing_null) {
227 using execution_space = Kokkos::DefaultExecutionSpace;
228 using memory_space = execution_space::memory_space;
229 using device_type = Kokkos::Device<execution_space, memory_space>;
230 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
231 test_nothing<device_type>(comm, true, false, out, success);
232 }
233
234 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllV_nothing_same) {
235 using execution_space = Kokkos::DefaultExecutionSpace;
236 using memory_space = execution_space::memory_space;
237 using device_type = Kokkos::Device<execution_space, memory_space>;
238 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
239 test_nothing<device_type>(comm, false, true, out, success);
240 }
241
242 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllV_nothing_nullsame) {
243 using execution_space = Kokkos::DefaultExecutionSpace;
244 using memory_space = execution_space::memory_space;
245 using device_type = Kokkos::Device<execution_space, memory_space>;
246 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
247 test_nothing<device_type>(comm, true, true, out, success);
248 }
249
250 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllV_random) {
251 using execution_space = Kokkos::DefaultExecutionSpace;
252 using memory_space = execution_space::memory_space;
253 using device_type = Kokkos::Device<execution_space, memory_space>;
254 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
255 test_random<device_type>(comm, 42, out, success);
256 }
257
258 // Let Tpetra initialize Kokkos
259 // We define this because we don't also include ${TEUCHOS_STD_UNIT_TEST_MAIN}
260 // in the CMakeLists.txt
261 int main(int argc, char* argv[])
262 {
263 Tpetra::ScopeGuard tpetraScope(&argc, &argv);
264 const int errCode =
265 Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv);
266 return errCode;
267 }