|
| 1 | +//@HEADER |
| 2 | +// ************************************************************************ |
| 3 | +// |
| 4 | +// Kokkos v. 4.0 |
| 5 | +// Copyright (2022) National Technology & Engineering |
| 6 | +// Solutions of Sandia, LLC (NTESS). |
| 7 | +// |
| 8 | +// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 | +// the U.S. Government retains certain rights in this software. |
| 10 | +// |
| 11 | +// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 | +// See https://kokkos.org/LICENSE for license information. |
| 13 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 | +// |
| 15 | +//@HEADER |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include <type_traits> |
| 19 | +#include <algorithm> // iota |
| 20 | +#include <random> |
| 21 | + |
| 22 | +#include "KokkosComm/KokkosComm.hpp" |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +using namespace KokkosComm::mpi; |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +class MpiWaitAny : public testing::Test { |
| 30 | + public: |
| 31 | + using Scalar = T; |
| 32 | +}; |
| 33 | + |
| 34 | +using ScalarTypes = ::testing::Types<int, double, Kokkos::complex<float>>; |
| 35 | +TYPED_TEST_SUITE(MpiWaitAny, ScalarTypes); |
| 36 | + |
| 37 | +template <KokkosComm::KokkosExecutionSpace ExecSpace, typename Scalar> |
| 38 | +void wait_any() { |
| 39 | + using TestView = Kokkos::View<Scalar *>; |
| 40 | + |
| 41 | + int rank, size; |
| 42 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 43 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 44 | + if (size < 2) { |
| 45 | + GTEST_SKIP() << "Requires >= 2 ranks (" << size << " provided)"; |
| 46 | + } |
| 47 | + |
| 48 | + constexpr size_t numMsg = 128; |
| 49 | + ExecSpace space; |
| 50 | + std::vector<KokkosComm::Req<>> reqs; |
| 51 | + std::vector<TestView> views; |
| 52 | + |
| 53 | + for (size_t i = 0; i < numMsg; ++i) { |
| 54 | + views.push_back(TestView(std::to_string(i), i)); |
| 55 | + } |
| 56 | + |
| 57 | + constexpr unsigned int SEED = 31337; |
| 58 | + std::random_device rd; |
| 59 | + std::mt19937 g(SEED); |
| 60 | + |
| 61 | + // random send/recv order |
| 62 | + std::vector<size_t> order(numMsg); |
| 63 | + std::iota(order.begin(), order.end(), size_t(0)); |
| 64 | + std::shuffle(order.begin(), order.end(), g); |
| 65 | + |
| 66 | + KokkosComm::Handle<ExecSpace, KokkosComm::Mpi> h(space, MPI_COMM_WORLD); |
| 67 | + |
| 68 | + if (0 == rank) { |
| 69 | + constexpr int dst = 1; |
| 70 | + |
| 71 | + for (size_t i : order) { |
| 72 | + reqs.push_back(KokkosComm::send(h, views[i], dst)); |
| 73 | + } |
| 74 | + |
| 75 | + for (size_t i = 0; i < numMsg; ++i) { |
| 76 | + reqs.erase(reqs.begin() + KokkosComm::wait_any(reqs)); |
| 77 | + } |
| 78 | + } else if (1 == rank) { |
| 79 | + constexpr int src = 0; |
| 80 | + |
| 81 | + for (size_t i : order) { |
| 82 | + reqs.push_back(KokkosComm::recv(h, views[i], src)); |
| 83 | + } |
| 84 | + |
| 85 | + for (size_t i = 0; i < numMsg; ++i) { |
| 86 | + reqs.erase(reqs.begin() + KokkosComm::wait_any(reqs)); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// TODO: test call on no requests |
| 92 | + |
| 93 | +TYPED_TEST(MpiWaitAny, default_execution_space) { |
| 94 | + wait_any<Kokkos::DefaultExecutionSpace, typename TestFixture::Scalar>(); |
| 95 | +} |
| 96 | + |
| 97 | +TYPED_TEST(MpiWaitAny, default_host_execution_space) { |
| 98 | + if constexpr (std::is_same_v<Kokkos::DefaultHostExecutionSpace, Kokkos::DefaultExecutionSpace>) { |
| 99 | + GTEST_SKIP() << "Skipping test: DefaultHostExecSpace = DefaultExecSpace"; |
| 100 | + } else { |
| 101 | + wait_any<Kokkos::DefaultHostExecutionSpace, typename TestFixture::Scalar>(); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace |
0 commit comments