|
| 1 | +// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +#include <gtest/gtest.h> |
| 6 | + |
| 7 | +#include <ginkgo/core/distributed/dense_communicator.hpp> |
| 8 | + |
| 9 | +#include "core/test/utils/assertions.hpp" |
| 10 | + |
| 11 | +using gko::experimental::mpi::comm_index_type; |
| 12 | + |
| 13 | +class DenseCommunicator : public ::testing::Test { |
| 14 | +protected: |
| 15 | + using part_type = gko::experimental::distributed::Partition<int, long>; |
| 16 | + using map_type = gko::experimental::distributed::index_map<int, long>; |
| 17 | + |
| 18 | + void SetUp() override { ASSERT_EQ(comm.size(), 6); } |
| 19 | + |
| 20 | + std::shared_ptr<gko::Executor> ref = gko::ReferenceExecutor::create(); |
| 21 | + gko::experimental::mpi::communicator comm = MPI_COMM_WORLD; |
| 22 | + int rank = comm.rank(); |
| 23 | +}; |
| 24 | + |
| 25 | + |
| 26 | +TEST_F(DenseCommunicator, CanDefaultConstruct) |
| 27 | +{ |
| 28 | + gko::experimental::mpi::DenseCommunicator nhcomm{comm}; |
| 29 | + |
| 30 | + ASSERT_EQ(nhcomm.get_base_communicator(), comm); |
| 31 | + ASSERT_EQ(nhcomm.get_send_size(), 0); |
| 32 | + ASSERT_EQ(nhcomm.get_recv_size(), 0); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +TEST_F(DenseCommunicator, CanConstructFromIndexMap) |
| 37 | +{ |
| 38 | + auto part = gko::share(part_type::build_from_global_size_uniform( |
| 39 | + ref, comm.size(), comm.size() * 3)); |
| 40 | + gko::array<long> recv_connections[] = {{ref, {3, 5, 10, 11}}, |
| 41 | + {ref, {0, 1, 7, 12, 13}}, |
| 42 | + {ref, {3, 4, 17}}, |
| 43 | + {ref, {1, 2, 12, 14}}, |
| 44 | + {ref, {4, 5, 9, 10, 16, 15}}, |
| 45 | + {ref, {8, 12, 13, 14}}}; |
| 46 | + auto imap = map_type{ref, part, comm.rank(), recv_connections[rank]}; |
| 47 | + |
| 48 | + gko::experimental::mpi::DenseCommunicator spcomm{comm, imap}; |
| 49 | + |
| 50 | + std::array<gko::size_type, 6> send_sizes = {4, 6, 2, 4, 7, 3}; |
| 51 | + ASSERT_EQ(spcomm.get_recv_size(), recv_connections[rank].get_size()); |
| 52 | + ASSERT_EQ(spcomm.get_send_size(), send_sizes[rank]); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +TEST_F(DenseCommunicator, CanConstructFromEnvelopData) |
| 57 | +{ |
| 58 | + // clang-format off |
| 59 | + std::vector<comm_index_type> recv_sizes[] = { |
| 60 | + {0, 2, 2, |
| 61 | + 0, 0, 0}, |
| 62 | + {2, 0, 1, |
| 63 | + 2, 0, 0}, |
| 64 | + {0, 2, 0, |
| 65 | + 0, 0, 1}, |
| 66 | + {2, 0, 0, |
| 67 | + 0, 2, 0}, |
| 68 | + {0, 2, 0, |
| 69 | + 2, 0, 2}, |
| 70 | + {0, 0, 1, |
| 71 | + 0, 3, 0}}; |
| 72 | + std::vector<comm_index_type> send_sizes[] = { |
| 73 | + {0, 2, 0, |
| 74 | + 2, 0, 0}, |
| 75 | + {2, 0, 2, |
| 76 | + 0, 2, 0}, |
| 77 | + {0, 1, 0, |
| 78 | + 0, 0, 1}, |
| 79 | + {2, 0, 0, |
| 80 | + 0, 2, 0}, |
| 81 | + {0, 2, 0, |
| 82 | + 2, 0, 3}, |
| 83 | + {0, 0, 1, |
| 84 | + 0, 2, 0}}; |
| 85 | + // clang-format on |
| 86 | + std::vector<comm_index_type> recv_offsets(recv_sizes[rank].size() + 1); |
| 87 | + std::vector<comm_index_type> send_offsets(send_sizes[rank].size() + 1); |
| 88 | + std::partial_sum(recv_sizes[rank].begin(), recv_sizes[rank].end(), |
| 89 | + recv_offsets.begin() + 1); |
| 90 | + std::partial_sum(send_sizes[rank].begin(), send_sizes[rank].end(), |
| 91 | + send_offsets.begin() + 1); |
| 92 | + |
| 93 | + gko::experimental::mpi::DenseCommunicator spcomm{ |
| 94 | + comm, recv_sizes[rank], recv_offsets, send_sizes[rank], send_offsets, |
| 95 | + }; |
| 96 | + |
| 97 | + ASSERT_EQ(spcomm.get_recv_size(), recv_offsets.back()); |
| 98 | + ASSERT_EQ(spcomm.get_send_size(), send_offsets.back()); |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +TEST_F(DenseCommunicator, CanConstructFromEmptyIndexMap) |
| 103 | +{ |
| 104 | + auto imap = map_type{ref}; |
| 105 | + |
| 106 | + gko::experimental::mpi::DenseCommunicator spcomm{comm, imap}; |
| 107 | + |
| 108 | + ASSERT_EQ(spcomm.get_recv_size(), 0); |
| 109 | + ASSERT_EQ(spcomm.get_send_size(), 0); |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +TEST_F(DenseCommunicator, CanConstructFromIndexMapWithoutConnection) |
| 114 | +{ |
| 115 | + auto part = gko::share(part_type::build_from_global_size_uniform( |
| 116 | + ref, comm.size(), comm.size() * 3)); |
| 117 | + auto imap = map_type{ref, part, comm.rank(), {ref, 0}}; |
| 118 | + |
| 119 | + gko::experimental::mpi::DenseCommunicator spcomm{comm, imap}; |
| 120 | + |
| 121 | + ASSERT_EQ(spcomm.get_recv_size(), 0); |
| 122 | + ASSERT_EQ(spcomm.get_send_size(), 0); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +TEST_F(DenseCommunicator, CanConstructFromEmptyEnvelopData) |
| 127 | +{ |
| 128 | + std::vector<comm_index_type> recv_sizes; |
| 129 | + std::vector<comm_index_type> send_sizes; |
| 130 | + std::vector<comm_index_type> recv_offsets{0}; |
| 131 | + std::vector<comm_index_type> send_offsets{0}; |
| 132 | + |
| 133 | + gko::experimental::mpi::DenseCommunicator spcomm{ |
| 134 | + comm, recv_sizes, recv_offsets, send_sizes, send_offsets, |
| 135 | + }; |
| 136 | + |
| 137 | + ASSERT_EQ(spcomm.get_recv_size(), 0); |
| 138 | + ASSERT_EQ(spcomm.get_send_size(), 0); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +TEST_F(DenseCommunicator, CanCommunicateIalltoall) |
| 143 | +{ |
| 144 | + auto part = gko::share(part_type::build_from_global_size_uniform( |
| 145 | + ref, comm.size(), comm.size() * 3)); |
| 146 | + gko::array<long> recv_connections[] = {{ref, {3, 5, 10, 11}}, |
| 147 | + {ref, {0, 1, 7, 12, 13}}, |
| 148 | + {ref, {3, 4, 17}}, |
| 149 | + {ref, {1, 2, 12, 14}}, |
| 150 | + {ref, {4, 5, 9, 10, 16, 15}}, |
| 151 | + {ref, {8, 12, 13, 14}}}; |
| 152 | + auto imap = map_type{ref, part, comm.rank(), recv_connections[rank]}; |
| 153 | + gko::experimental::mpi::DenseCommunicator spcomm{comm, imap}; |
| 154 | + gko::array<long> recv_buffer{ref, recv_connections[rank].get_size()}; |
| 155 | + gko::array<long> send_buffers[] = {{ref, {0, 1, 1, 2}}, |
| 156 | + {ref, {3, 5, 3, 4, 4, 5}}, |
| 157 | + {ref, {7, 8}}, |
| 158 | + {ref, {10, 11, 9, 10}}, |
| 159 | + {ref, {12, 13, 12, 14, 12, 13, 14}}, |
| 160 | + {ref, {17, 16, 15}}}; |
| 161 | + |
| 162 | + auto req = spcomm.i_all_to_all_v(ref, send_buffers[rank].get_const_data(), |
| 163 | + recv_buffer.get_data()); |
| 164 | + req.wait(); |
| 165 | + |
| 166 | + GKO_ASSERT_ARRAY_EQ(recv_buffer, recv_connections[rank]); |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +TEST_F(DenseCommunicator, CanCommunicateIalltoallWhenEmpty) |
| 171 | +{ |
| 172 | + gko::experimental::mpi::DenseCommunicator spcomm{comm}; |
| 173 | + |
| 174 | + auto req = spcomm.i_all_to_all_v(ref, static_cast<int*>(nullptr), |
| 175 | + static_cast<int*>(nullptr)); |
| 176 | + req.wait(); |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +TEST_F(DenseCommunicator, CanCreateInverse) |
| 181 | +{ |
| 182 | + auto part = gko::share(part_type::build_from_global_size_uniform( |
| 183 | + ref, comm.size(), comm.size() * 3)); |
| 184 | + gko::array<long> recv_connections[] = {{ref, {3, 5, 10, 11}}, |
| 185 | + {ref, {0, 1, 7, 12, 13}}, |
| 186 | + {ref, {3, 4, 17}}, |
| 187 | + {ref, {1, 2, 12, 14}}, |
| 188 | + {ref, {4, 5, 9, 10, 16, 15}}, |
| 189 | + {ref, {8, 12, 13, 14}}}; |
| 190 | + auto imap = map_type{ref, part, comm.rank(), recv_connections[rank]}; |
| 191 | + gko::experimental::mpi::DenseCommunicator spcomm{comm, imap}; |
| 192 | + |
| 193 | + auto inverse = spcomm.create_inverse(); |
| 194 | + |
| 195 | + ASSERT_EQ(inverse->get_recv_size(), spcomm.get_send_size()); |
| 196 | + ASSERT_EQ(inverse->get_send_size(), spcomm.get_recv_size()); |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +TEST_F(DenseCommunicator, CanCommunicateRoundTrip) |
| 201 | +{ |
| 202 | + auto part = gko::share(part_type::build_from_global_size_uniform( |
| 203 | + ref, comm.size(), comm.size() * 3)); |
| 204 | + gko::array<long> recv_connections[] = {{ref, {3, 5, 10, 11}}, |
| 205 | + {ref, {0, 1, 7, 12, 13}}, |
| 206 | + {ref, {3, 4, 17}}, |
| 207 | + {ref, {1, 2, 12, 14}}, |
| 208 | + {ref, {4, 5, 9, 10, 16, 15}}, |
| 209 | + {ref, {8, 12, 13, 14}}}; |
| 210 | + auto imap = map_type{ref, part, comm.rank(), recv_connections[rank]}; |
| 211 | + gko::experimental::mpi::DenseCommunicator spcomm{comm, imap}; |
| 212 | + auto inverse = spcomm.create_inverse(); |
| 213 | + gko::array<long> send_buffers[] = {{ref, {1, 2, 3, 4}}, |
| 214 | + {ref, {5, 6, 7, 8, 9, 10}}, |
| 215 | + {ref, {11, 12}}, |
| 216 | + {ref, {13, 14, 15, 16}}, |
| 217 | + {ref, {17, 18, 19, 20, 21, 22, 23}}, |
| 218 | + {ref, {24, 25, 26}}}; |
| 219 | + gko::array<long> recv_buffer{ref, recv_connections[rank].get_size()}; |
| 220 | + gko::array<long> round_trip{ref, send_buffers[rank].get_size()}; |
| 221 | + |
| 222 | + spcomm |
| 223 | + .i_all_to_all_v(ref, send_buffers[rank].get_const_data(), |
| 224 | + recv_buffer.get_data()) |
| 225 | + .wait(); |
| 226 | + inverse |
| 227 | + ->i_all_to_all_v(ref, recv_buffer.get_const_data(), |
| 228 | + round_trip.get_data()) |
| 229 | + .wait(); |
| 230 | + |
| 231 | + GKO_ASSERT_ARRAY_EQ(send_buffers[rank], round_trip); |
| 232 | +} |
0 commit comments