| 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_alltoall.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 |
int sendcount = 0; |
| 43 |
int recvcount = 0; |
| 44 |
|
| 45 |
char stackByte0, stackByte1; |
| 46 |
void *sbuf, *rbuf; |
| 47 |
if (nullBufs) { |
| 48 |
sbuf = nullptr; |
| 49 |
rbuf = nullptr; |
| 50 |
} else { |
| 51 |
sbuf = &stackByte0; |
| 52 |
rbuf = &stackByte1; |
| 53 |
} |
| 54 |
|
| 55 |
if (sameBufs) { |
| 56 |
sbuf = rbuf; |
| 57 |
} |
| 58 |
|
| 59 |
// create MPIX communicator |
| 60 |
MPIX_Comm *mpixComm = nullptr; |
| 61 |
MPIX_Comm_init(&mpixComm, comm); |
| 62 |
|
| 63 |
// reference implementation should be okay |
| 64 |
Fake_Alltoall(sbuf, sendcount, MPI_BYTE, rbuf, |
| 65 |
recvcount, MPI_BYTE, comm); |
| 66 |
|
| 67 |
// MPI advance implementation |
| 68 |
MPIX_Alltoall(sbuf, sendcount, MPI_BYTE, rbuf, |
| 69 |
recvcount, MPI_BYTE, mpixComm); |
| 70 |
|
| 71 |
MPIX_Comm_free(mpixComm); |
| 72 |
|
| 73 |
// we just require that we got this far |
| 74 |
success = true; |
| 75 |
} |
| 76 |
|
| 77 |
/*! \brief all ranks send and receive some |
| 78 |
*/ |
| 79 |
template <typename Device> |
| 80 |
void test_random(MPI_Comm comm, int seed, Teuchos::FancyOStream &out, |
| 81 |
bool &success) { |
| 82 |
static_assert(Kokkos::is_device_v<Device>, ""); |
| 83 |
|
| 84 |
int size, rank; |
| 85 |
MPI_Comm_size(comm, &size); |
| 86 |
MPI_Comm_rank(comm, &rank); |
| 87 |
|
| 88 |
int sendcount, recvcount; // alltoall |
| 89 |
|
| 90 |
// seeded rng so all ranks generate the same count |
| 91 |
std::mt19937 rng(seed); |
| 92 |
std::uniform_int_distribution<int> dist(1, 100); |
| 93 |
sendcount = dist(rng); |
| 94 |
recvcount = sendcount; |
| 95 |
|
| 96 |
// create MPIX communicator |
| 97 |
MPIX_Comm *mpixComm = nullptr; |
| 98 |
MPIX_Comm_init(&mpixComm, comm); |
| 99 |
|
| 100 |
// allocate send/recv bufs |
| 101 |
Kokkos::View<char *, typename Device::memory_space> |
| 102 |
sbuf("sbuf", sendcount * size), |
| 103 |
exp("exp", recvcount * size), |
| 104 |
act("act", recvcount * size); |
| 105 |
|
| 106 |
// fill send buf |
| 107 |
Kokkos::parallel_for(sbuf.size(), KOKKOS_LAMBDA (size_t i) {sbuf(i) = i;}); |
| 108 |
|
| 109 |
// Use reference and MPI_Advance implementation to fill buffers |
| 110 |
Fake_Alltoall(sbuf.data(), sendcount, MPI_BYTE, |
| 111 |
exp.data(), recvcount, MPI_BYTE, comm); |
| 112 |
|
| 113 |
|
| 114 |
MPIX_Alltoall(sbuf.data(), sendcount, MPI_BYTE, |
| 115 |
act.data(), recvcount, MPI_BYTE, mpixComm); |
| 116 |
|
| 117 |
MPIX_Comm_free(mpixComm); |
| 118 |
|
| 119 |
// two recv buffers should be the same |
| 120 |
auto exp_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), exp); |
| 121 |
auto act_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), act); |
| 122 |
TEST_ASSERT(exp_h.size() == act_h.size()); |
| 123 |
for (size_t i = 0; i < exp_h.size(); ++i) { |
| 124 |
TEST_ASSERT(exp_h(i) == act_h(i)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static MPI_Comm tpetra_default_comm_as_mpi_comm() { |
| 129 |
Teuchos::RCP<const Teuchos::Comm<int>> teuchosComm = Tpetra::getDefaultComm(); |
| 130 |
|
| 131 |
const Teuchos::MpiComm<int> *teuchosMpiComm = |
| 132 |
dynamic_cast<const Teuchos::MpiComm<int> *>(teuchosComm.get()); |
| 133 |
|
| 134 |
if (teuchosMpiComm) { |
| 135 |
return *(teuchosMpiComm->getRawMpiComm()); |
| 136 |
} else { |
| 137 |
throw std::runtime_error( |
| 138 |
"couldn't convert tpetra::getDefaultComm() into MPI_Comm"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
TEUCHOS_UNIT_TEST(MpiAdvance, AllToAll_nothing) { |
| 143 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 144 |
using memory_space = execution_space::memory_space; |
| 145 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 146 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 147 |
test_nothing<device_type>(comm, false, false, out, success); |
| 148 |
} |
| 149 |
|
| 150 |
TEUCHOS_UNIT_TEST(MpiAdvance, AllToAll_nothing_null) { |
| 151 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 152 |
using memory_space = execution_space::memory_space; |
| 153 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 154 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 155 |
test_nothing<device_type>(comm, true, false, out, success); |
| 156 |
} |
| 157 |
|
| 158 |
TEUCHOS_UNIT_TEST(MpiAdvance, AllToAll_nothing_same) { |
| 159 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 160 |
using memory_space = execution_space::memory_space; |
| 161 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 162 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 163 |
test_nothing<device_type>(comm, false, true, out, success); |
| 164 |
} |
| 165 |
|
| 166 |
TEUCHOS_UNIT_TEST(MpiAdvance, AllToAll_nothing_nullsame) { |
| 167 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 168 |
using memory_space = execution_space::memory_space; |
| 169 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 170 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 171 |
test_nothing<device_type>(comm, true, true, out, success); |
| 172 |
} |
| 173 |
|
| 174 |
TEUCHOS_UNIT_TEST(MpiAdvance, AllToAll_random) { |
| 175 |
using execution_space = Kokkos::DefaultExecutionSpace; |
| 176 |
using memory_space = execution_space::memory_space; |
| 177 |
using device_type = Kokkos::Device<execution_space, memory_space>; |
| 178 |
MPI_Comm comm = tpetra_default_comm_as_mpi_comm(); |
| 179 |
test_random<device_type>(comm, 42, out, success); |
| 180 |
} |
| 181 |
|
| 182 |
// Let Tpetra initialize Kokkos |
| 183 |
// We define this because we don't also include ${TEUCHOS_STD_UNIT_TEST_MAIN} |
| 184 |
// in the CMakeLists.txt |
| 185 |
int main(int argc, char* argv[]) |
| 186 |
{ |
| 187 |
Tpetra::ScopeGuard tpetraScope(&argc, &argv); |
| 188 |
const int errCode = |
| 189 |
Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv); |
| 190 |
return errCode; |
| 191 |
} |