| 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_part_locality_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 part locality 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 |
|
| 190 |
MPIX_Neighbor_part_locality_alltoallv(sbuf.data(), nbrsendcounts.data(), nbrsenddispls.data(), MPI_BYTE, |
| 191 |
act.data(), nbrrecvcounts.data(), nbrrecvdispls.data(), MPI_BYTE, |
| 192 |
mpixComm); |
| 193 |
|
| 194 |
MPIX_Comm_free(mpixComm); |
| 195 |
|
| 196 |
// two recv buffers should be the same |
| 197 |
auto exp_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), exp); |
| 198 |
auto act_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), act); |
| 199 |
TEST_ASSERT(exp_h.size() == act_h.size()); |
| 200 |
for (size_t i = 0; i < exp_h.size(); ++i) { |
| 201 |
TEST_ASSERT(exp_h(i) == act_h(i)); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static MPI_Comm tpetra_default_comm_as_mpi_comm() { |
| 206 |
Teuchos::RCP<const Teuchos::Comm<int>> teuchosComm = Tpetra::getDefaultComm(); |
| 207 |
|
| 208 |
const Teuchos::MpiComm<int> *teuchosMpiComm = |
| 209 |
dynamic_cast<const Teuchos::MpiComm<int> *>(teuchosComm.get()); |
| 210 |
|
| 211 |
if (teuchosMpiComm) { |
| 212 |
return *(teuchosMpiComm->getRawMpiComm()); |
| 213 |
} else { |
| 214 |
throw std::runtime_error( |
| 215 |
"couldn't convert tpetra::getDefaultComm() into MPI_Comm"); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
TEUCHOS_UNIT_TEST(MpiAdvance, NeighborPartLocalityAllToAllV_nothing) { |
| 220 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 221 |
using memory_space = execution_space::memory_space; |
| 222 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 223 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 224 |
test_nothing<device_type>(comm, false, false, out, success); |
| 225 |
} |
| 226 |
|
| 227 |
TEUCHOS_UNIT_TEST(MpiAdvance, NeighborPartLocalityAllToAllV_nothing_null) { |
| 228 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 229 |
using memory_space = execution_space::memory_space; |
| 230 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 231 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 232 |
test_nothing<device_type>(comm, true, false, out, success); |
| 233 |
} |
| 234 |
|
| 235 |
TEUCHOS_UNIT_TEST(MpiAdvance, NeighborPartLocalityAllToAllV_nothing_same) { |
| 236 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 237 |
using memory_space = execution_space::memory_space; |
| 238 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 239 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 240 |
test_nothing<device_type>(comm, false, true, out, success); |
| 241 |
} |
| 242 |
|
| 243 |
TEUCHOS_UNIT_TEST(MpiAdvance, NeighborPartLocalityAllToAllV_nothing_nullsame) { |
| 244 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 245 |
using memory_space = execution_space::memory_space; |
| 246 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 247 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 248 |
test_nothing<device_type>(comm, true, true, out, success); |
| 249 |
} |
| 250 |
|
| 251 |
TEUCHOS_UNIT_TEST(MpiAdvance, NeighborPartLocalityAllToAllV_random) { |
| 252 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 253 |
using memory_space = execution_space::memory_space; |
| 254 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 255 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 256 |
test_random<device_type>(comm, 42, out, success); |
| 257 |
} |
| 258 |
|
| 259 |
// Let Tpetra initialize Kokkos |
| 260 |
// We define this because we don't also include ${TEUCHOS_STD_UNIT_TEST_MAIN} |
| 261 |
// in the CMakeLists.txt |
| 262 |
int main(int argc, char* argv[]) |
| 263 |
{ |
| 264 |
Tpetra::ScopeGuard tpetraScope(&argc, &argv); |
| 265 |
const int errCode = |
| 266 |
Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv); |
| 267 |
return errCode; |
| 268 |
} |