Debian Codegraph @

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