Debian Codegraph @

packages/tpetra/core/test/MPIAdvance/NeighborAllToAllW.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_alltoallw.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 bool emptyTypes, 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 // neighbor call uses different type
49 std::vector<MPI_Aint> nbrsenddispls(size, 0);
50 std::vector<MPI_Aint> nbrrecvdispls(size, 0);
51
52 MPI_Datatype type;
53 if (emptyTypes) {
54 MPI_Type_contiguous(0, MPI_BYTE, &type);
55 MPI_Type_commit(&type);
56 } else {
57 type = MPI_BYTE;
58 }
59 std::vector<MPI_Datatype> sendtypes(size, type);
60 std::vector<MPI_Datatype> recvtypes(size, type);
61
62 char stackByte0, stackByte1;
63 void *sbuf, *rbuf;
64 if (nullBufs) {
65 sbuf = nullptr;
66 rbuf = nullptr;
67 } else {
68 sbuf = &stackByte0;
69 rbuf = &stackByte1;
70 }
71
72 if (sameBufs) {
73 sbuf = rbuf;
74 }
75
76 // create MPIX communicator
77 MPIX_Comm *mpixComm = nullptr;
78 MPIX_Dist_graph_create_adjacent(
79 comm, 0, /*indegree*/
80 nullptr, /*sources*/
81 nullptr, /*sourceweights*/
82 0, /*outdegree*/
83 nullptr /*destinations*/, nullptr /*destweights*/, MPI_INFO_NULL /*info*/,
84 0 /*reorder*/, &mpixComm);
85
86 // reference implementation should be okay
87 Fake_Alltoallw(sbuf, sendcounts.data(), senddispls.data(), sendtypes.data(), rbuf,
88 recvcounts.data(), recvdispls.data(), recvtypes.data(), comm);
89
90 // MPI advance implementation
91 MPIX_Neighbor_alltoallw(sbuf, sendcounts.data(), nbrsenddispls.data(), sendtypes.data(),
92 rbuf, recvcounts.data(), nbrrecvdispls.data(), recvtypes.data(),
93 mpixComm);
94
95 MPIX_Comm_free(mpixComm);
96
97 // we just require that we got this far
98 success = true;
99
100 if (emptyTypes) {
101 MPI_Type_free(&type);
102 }
103 }
104
105 /*! \brief all ranks send and receive some
106 */
107 template <typename Device>
108 void test_random(MPI_Comm comm, int seed, Teuchos::FancyOStream &out,
109 bool &success) {
110 static_assert(Kokkos::is_device_v<Device>, "");
111
112 int size, rank;
113 MPI_Comm_size(comm, &size);
114 MPI_Comm_rank(comm, &rank);
115
116 // seeded rng so all ranks generate the same communication plan
117 // plan [i * size + j] is count i sends to j
118 std::mt19937 rng(seed);
119 std::uniform_int_distribution<int> dist(1, 100);
120 std::vector<int> plan;
121 std::vector<int> typeindplan;
122
123 // types to choose from
124 constexpr int numTypeOps = 4;
125 MPI_Datatype typeoptions[numTypeOps] = {MPI_BYTE, MPI_SHORT, MPI_INT, MPI_LONG};
126 int typesizes[numTypeOps];
127 MPI_Type_size(MPI_BYTE, &(typesizes[0]));
128 MPI_Type_size(MPI_SHORT, &(typesizes[1]));
129 MPI_Type_size(MPI_INT, &(typesizes[2]));
130 MPI_Type_size(MPI_LONG, &(typesizes[3]));
131 std::uniform_int_distribution<int> typedist(0, numTypeOps - 1);
132
133 for (int i = 0; i < size * size; ++i) {
134 plan.push_back(dist(rng));
135 typeindplan.push_back(typedist(rng));
136 }
137
138 // read my part of the plan
139 std::vector<int> sendcounts, recvcounts, senddispls, recvdispls; // alltoallw
140 std::vector<MPI_Datatype> sendtypes, recvtypes;
141 std::vector<int> nbrsendcounts, nbrrecvcounts; // neighbor alltoallw
142 std::vector<MPI_Aint> nbrsenddispls, nbrrecvdispls; // different types in neighbor call
143 std::vector<MPI_Datatype> nbrsendtypes, nbrrecvtypes;
144 std::vector<int> sources, sourceweights, destinations, destweights; // communicator
145
146 // displs are in bytes
147 std::uniform_int_distribution<int> soffsetdist(0, 150);
148 rng.seed(seed + rank); // different seed -> different displs per rank
149 int initsdispl = soffsetdist(rng);
150 int sdispl = initsdispl;
151 int nbrsdispl = initsdispl;
152 for (int dest = 0; dest < size; ++dest) {
153 // pick type
154 int ind = typeindplan[rank * size + dest];
155 MPI_Datatype mytype = typeoptions[ind];
156 int mytypesize = typesizes[ind];
157
158 senddispls.push_back(sdispl);
159 int count = plan[rank * size + dest];
160 sendcounts.push_back(count);
161 sendtypes.push_back(mytype);
162
163 // pick random displacement
164 int offset = soffsetdist(rng);
165 if (offset < mytypesize * count) { // prevent overwrites
166 offset += mytypesize * count;
167 }
168 sdispl += offset;
169
170 if (count > 0) {
171 destinations.push_back(dest);
172 destweights.push_back(count);
173 nbrsendcounts.push_back(count);
174 nbrsenddispls.push_back((MPI_Aint)nbrsdispl);
175 nbrsendtypes.push_back(mytype);
176
177 nbrsdispl += offset;
178 }
179 }
180
181 // displs are in bytes
182 std::uniform_int_distribution<int> roffsetdist(0, 150);
183 rng.seed(seed + size + rank); // different seed -> different displs (also per rank)
184 int initrdispl = roffsetdist(rng);
185 int rdispl = initrdispl;
186 int nbrrdispl = initrdispl;
187 for (int source = 0; source < size; ++source) {
188 // pick type
189 int ind = typeindplan[source * size + rank];
190 MPI_Datatype mytype = typeoptions[ind];
191 int mytypesize = typesizes[ind];
192
193 recvdispls.push_back(rdispl);
194 int count = plan[source * size + rank];
195 recvcounts.push_back(count);
196 recvtypes.push_back(mytype);
197
198 // pick random displacement
199 int offset = roffsetdist(rng);
200 if (offset < mytypesize * count) { // prevent overwrites
201 offset += mytypesize * count;
202 }
203 rdispl += offset;
204
205 if (count > 0) {
206 sources.push_back(source);
207 sourceweights.push_back(count);
208 nbrrecvcounts.push_back(count);
209 nbrrecvdispls.push_back((MPI_Aint)nbrrdispl);
210 nbrrecvtypes.push_back(mytype);
211
212 nbrrdispl += offset;
213 }
214 }
215
216
217 print_vec(rank, "sources", sources);
218 print_vec(rank, "sourceweights", sourceweights);
219 print_vec(rank, "destinations", destinations);
220 print_vec(rank, "destweights", destweights);
221
222 // create MPIX communicator
223 MPIX_Comm *mpixComm = nullptr;
224 MPIX_Dist_graph_create_adjacent(
225 comm, sources.size(), /*indegree*/
226 sources.data(), /*sources*/
227 sourceweights.data(), /*sourceweights*/
228 destinations.size(), /*outdegree*/
229 destinations.data() /*destinations*/, destweights.data() /*destweights*/,
230 MPI_INFO_NULL /*info*/, 0 /*reorder*/, &mpixComm);
231
232 // allocate send/recv bufs
233 // displs are in bytes, so the displs are correct no matter types below.
234 // (guarranteed by loops above about types)
235 Kokkos::View<char *, typename Device::memory_space>
236 sbuf("sbuf", sdispl), exp("exp", rdispl), act("act", nbrrdispl);
237
238 // fill send buf
239 Kokkos::parallel_for(sbuf.size(), KOKKOS_LAMBDA (size_t i) {sbuf(i) = i;});
240
241 // Use reference and MPI_Advance implementation to fill buffers
242 Fake_Alltoallw(sbuf.data(), sendcounts.data(), senddispls.data(), sendtypes.data(),
243 exp.data(), recvcounts.data(), recvdispls.data(), recvtypes.data(),
244 comm);
245
246 print_vec(rank, "nbrsendcounts", nbrsendcounts);
247 print_vec(rank, "nbrsenddispls", nbrsenddispls);
248 // print_vec(rank, "nbrsendtypes", nbrsendtypes);
249 print_vec(rank, "nbrrecvcounts", nbrrecvcounts);
250 print_vec(rank, "nbrrecvdispls", nbrrecvdispls);
251 // print_vec(rank, "nbrrecvtypes", nbrrecvtypes);
252
253 MPIX_Neighbor_alltoallw(sbuf.data(), nbrsendcounts.data(), nbrsenddispls.data(), nbrsendtypes.data(),
254 act.data(), nbrrecvcounts.data(), nbrrecvdispls.data(), nbrrecvtypes.data(),
255 mpixComm);
256
257 MPIX_Comm_free(mpixComm);
258
259 // two recv buffers should be the same
260 auto exp_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), exp);
261 auto act_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), act);
262 TEST_ASSERT(exp_h.size() == act_h.size());
263 for (size_t i = 0; i < exp_h.size(); ++i) {
264 TEST_ASSERT(exp_h(i) == act_h(i));
265 }
266 }
267
268 static MPI_Comm tpetra_default_comm_as_mpi_comm() {
269 Teuchos::RCP<const Teuchos::Comm<int>> teuchosComm = Tpetra::getDefaultComm();
270
271 const Teuchos::MpiComm<int> *teuchosMpiComm =
272 dynamic_cast<const Teuchos::MpiComm<int> *>(teuchosComm.get());
273
274 if (teuchosMpiComm) {
275 return *(teuchosMpiComm->getRawMpiComm());
276 } else {
277 throw std::runtime_error(
278 "couldn't convert tpetra::getDefaultComm() into MPI_Comm");
279 }
280 }
281
282 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing) {
283 using execution_space = Kokkos::DefaultExecutionSpace;
284 using memory_space = execution_space::memory_space;
285 using device_type = Kokkos::Device<execution_space, memory_space>;
286 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
287 test_nothing<device_type>(comm, false, false, false, out, success);
288 }
289
290 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_emptytypes) {
291 using execution_space = Kokkos::DefaultExecutionSpace;
292 using memory_space = execution_space::memory_space;
293 using device_type = Kokkos::Device<execution_space, memory_space>;
294 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
295 test_nothing<device_type>(comm, false, false, true, out, success);
296 }
297
298 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_null) {
299 using execution_space = Kokkos::DefaultExecutionSpace;
300 using memory_space = execution_space::memory_space;
301 using device_type = Kokkos::Device<execution_space, memory_space>;
302 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
303 test_nothing<device_type>(comm, true, false, false, out, success);
304 }
305
306 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_nullemptytypes) {
307 using execution_space = Kokkos::DefaultExecutionSpace;
308 using memory_space = execution_space::memory_space;
309 using device_type = Kokkos::Device<execution_space, memory_space>;
310 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
311 test_nothing<device_type>(comm, true, false, true, out, success);
312 }
313
314 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_same) {
315 using execution_space = Kokkos::DefaultExecutionSpace;
316 using memory_space = execution_space::memory_space;
317 using device_type = Kokkos::Device<execution_space, memory_space>;
318 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
319 test_nothing<device_type>(comm, false, true, false, out, success);
320 }
321
322 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_sameemptytypes) {
323 using execution_space = Kokkos::DefaultExecutionSpace;
324 using memory_space = execution_space::memory_space;
325 using device_type = Kokkos::Device<execution_space, memory_space>;
326 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
327 test_nothing<device_type>(comm, false, true, true, out, success);
328 }
329
330 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_nullsame) {
331 using execution_space = Kokkos::DefaultExecutionSpace;
332 using memory_space = execution_space::memory_space;
333 using device_type = Kokkos::Device<execution_space, memory_space>;
334 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
335 test_nothing<device_type>(comm, true, true, false, out, success);
336 }
337
338 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_nothing_nullsameemptytypes) {
339 using execution_space = Kokkos::DefaultExecutionSpace;
340 using memory_space = execution_space::memory_space;
341 using device_type = Kokkos::Device<execution_space, memory_space>;
342 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
343 test_nothing<device_type>(comm, true, true, true, out, success);
344 }
345
346 TEUCHOS_UNIT_TEST(MpiAdvance, NeighborAllToAllW_random) {
347 using execution_space = Kokkos::DefaultExecutionSpace;
348 using memory_space = execution_space::memory_space;
349 using device_type = Kokkos::Device<execution_space, memory_space>;
350 MPI_Comm comm = tpetra_default_comm_as_mpi_comm();
351 test_random<device_type>(comm, 42, out, success);
352 }
353
354 // Let Tpetra initialize Kokkos
355 // We define this because we don't also include ${TEUCHOS_STD_UNIT_TEST_MAIN}
356 // in the CMakeLists.txt
357 int main(int argc, char* argv[])
358 {
359 Tpetra::ScopeGuard tpetraScope(&argc, &argv);
360 const int errCode =
361 Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv);
362 return errCode;
363 }