|
| 1 | +//@HEADER |
| 2 | +// ************************************************************************ |
| 3 | +// |
| 4 | +// Kokkos v. 4.0 |
| 5 | +// Copyright (2025) 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 | + |
| 20 | +#include <KokkosComm/KokkosComm.hpp> |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +using namespace KokkosComm::mpi; |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +class ChannelSendRecv : public testing::Test { |
| 28 | + public: |
| 29 | + using Scalar = T; |
| 30 | +}; |
| 31 | + |
| 32 | +using ScalarTypes = ::testing::Types<int, int64_t, float, double, Kokkos::complex<float>, Kokkos::complex<double>>; |
| 33 | +TYPED_TEST_SUITE(ChannelSendRecv, ScalarTypes); |
| 34 | + |
| 35 | +template <typename Scalar> |
| 36 | +void test_channel_hostspace() { |
| 37 | + int rank, size; |
| 38 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 39 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 40 | + |
| 41 | + if (size < 2) { |
| 42 | + GTEST_SKIP() << "This test requires at least 2 MPI processes"; |
| 43 | + } |
| 44 | + |
| 45 | + const int dest_rank = (rank + 1) % size; // send to next rank |
| 46 | + const int src_rank = (rank - 1 + size) % size; // recv from prev rank |
| 47 | + const int tag = 42; |
| 48 | + |
| 49 | + KokkosComm::Channel<> channel(dest_rank, src_rank, tag, MPI_COMM_WORLD); |
| 50 | + |
| 51 | + const int N = 10; |
| 52 | + |
| 53 | + // Create host views |
| 54 | + Kokkos::View<Scalar*, Kokkos::HostSpace> send_host("send_host", N); |
| 55 | + Kokkos::View<Scalar*, Kokkos::HostSpace> recv_host("recv_host", N); |
| 56 | + |
| 57 | + for (int i = 0; i < N; i++) send_host(i) = static_cast<Scalar>(rank * N + i); |
| 58 | + |
| 59 | + channel.sendinit(send_host); |
| 60 | + channel.recvinit(recv_host); |
| 61 | + |
| 62 | + channel.start(); |
| 63 | + channel.wait(); |
| 64 | + |
| 65 | + int errs = 0; |
| 66 | + for (int i = 0; i < N; i++) { |
| 67 | + const Scalar expected = static_cast<Scalar>(src_rank * N + i); |
| 68 | + if (recv_host(i) != expected) { |
| 69 | + errs++; |
| 70 | + } |
| 71 | + } |
| 72 | + EXPECT_EQ(errs, 0); |
| 73 | +} |
| 74 | + |
| 75 | +template <typename Scalar> |
| 76 | +void test_channel_execspace() { |
| 77 | + int rank, size; |
| 78 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 79 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 80 | + |
| 81 | + if (size < 2) { |
| 82 | + GTEST_SKIP() << "This test requires at least 2 MPI processes"; |
| 83 | + } |
| 84 | + |
| 85 | + const int dest_rank = (rank + 1) % size; // send to next rank |
| 86 | + const int src_rank = (rank - 1 + size) % size; // recv from prev rank |
| 87 | + const int tag = 42; |
| 88 | + |
| 89 | + KokkosComm::Channel<> channel(dest_rank, src_rank, tag, MPI_COMM_WORLD); |
| 90 | + |
| 91 | + const int N = 10; |
| 92 | + |
| 93 | + // Create host view |
| 94 | + Kokkos::View<Scalar*, Kokkos::HostSpace> recv_host("recv_host", N); |
| 95 | + // Create device views |
| 96 | + Kokkos::View<Scalar*, Kokkos::DefaultExecutionSpace> send_dev("send_dev", N); |
| 97 | + Kokkos::View<Scalar*, Kokkos::DefaultExecutionSpace> recv_dev("recv_dev", N); |
| 98 | + |
| 99 | + GTEST_LOG_(INFO) << "Kokkos Execution Space: " << Kokkos::DefaultExecutionSpace::name(); |
| 100 | + Kokkos::parallel_for( |
| 101 | + "init_send_dev", N, KOKKOS_LAMBDA(int i) { send_dev(i) = static_cast<Scalar>(rank * N + i); }); |
| 102 | + Kokkos::fence(); |
| 103 | + |
| 104 | + channel.sendinit(send_dev); |
| 105 | + channel.recvinit(recv_dev); |
| 106 | + |
| 107 | + channel.start(); |
| 108 | + channel.wait(); |
| 109 | + |
| 110 | + Kokkos::deep_copy(recv_host, recv_dev); |
| 111 | + |
| 112 | + int errs = 0; |
| 113 | + for (int i = 0; i < N; i++) { |
| 114 | + const Scalar expected = static_cast<Scalar>(src_rank * N + i); |
| 115 | + if (recv_host(i) != expected) { |
| 116 | + errs++; |
| 117 | + } |
| 118 | + } |
| 119 | + EXPECT_EQ(errs, 0); |
| 120 | +} |
| 121 | + |
| 122 | +template <typename Scalar> |
| 123 | +void test_channel_reuse() { |
| 124 | + int rank, size; |
| 125 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 126 | + MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 127 | + |
| 128 | + if (size < 2) { |
| 129 | + GTEST_SKIP() << "This test requires at least 2 MPI processes"; |
| 130 | + } |
| 131 | + |
| 132 | + const int dest_rank = (rank + 1) % size; // send to next rank |
| 133 | + const int src_rank = (rank - 1 + size) % size; // recv from prev rank |
| 134 | + const int tag = 42; |
| 135 | + |
| 136 | + KokkosComm::Channel<> channel(dest_rank, src_rank, tag, MPI_COMM_WORLD); |
| 137 | + |
| 138 | + const int N = 10; |
| 139 | + |
| 140 | + // Create host view |
| 141 | + Kokkos::View<Scalar*, Kokkos::HostSpace> recv_host("recv_host", N); |
| 142 | + // Create device views |
| 143 | + Kokkos::View<Scalar*, Kokkos::DefaultExecutionSpace> send_dev("send_dev", N); |
| 144 | + Kokkos::View<Scalar*, Kokkos::DefaultExecutionSpace> recv_dev("recv_dev", N); |
| 145 | + |
| 146 | + GTEST_LOG_(INFO) << "Kokkos Execution Space: " << Kokkos::DefaultExecutionSpace::name(); |
| 147 | + int errs = 0; |
| 148 | + for (int i = 0; i < 3; i++) { |
| 149 | + Kokkos::parallel_for( |
| 150 | + "init_send_dev", N, KOKKOS_LAMBDA(int i) { send_dev(i) = static_cast<Scalar>(rank * N + i); }); |
| 151 | + Kokkos::fence(); |
| 152 | + |
| 153 | + channel.sendinit(send_dev); |
| 154 | + channel.recvinit(recv_dev); |
| 155 | + |
| 156 | + channel.start(); |
| 157 | + channel.wait(); |
| 158 | + |
| 159 | + Kokkos::deep_copy(recv_host, recv_dev); |
| 160 | + |
| 161 | + for (int j = 0; j < N; j++) { |
| 162 | + const Scalar expected = static_cast<Scalar>(src_rank * N + j); |
| 163 | + if (recv_host(j) != expected) { |
| 164 | + errs++; |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + EXPECT_EQ(errs, 0); |
| 169 | +} |
| 170 | + |
| 171 | +TYPED_TEST(ChannelSendRecv, 1D_contig_sendrecv_hostspace) { test_channel_hostspace<typename TestFixture::Scalar>(); } |
| 172 | +TYPED_TEST(ChannelSendRecv, 1D_contig_sendrecv_execspace) { test_channel_execspace<typename TestFixture::Scalar>(); } |
| 173 | +TYPED_TEST(ChannelSendRecv, 1D_contig_sendrecv_reuse) { test_channel_reuse<typename TestFixture::Scalar>(); } |
| 174 | + |
| 175 | +} // namespace |
0 commit comments