1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#include <cstdio>
#include <gtest/gtest.h>
#include <Kokkos_Core.hpp>
namespace Test {
namespace {
template <class ViewType>
struct CheckResult {
using value_type = typename ViewType::non_const_value_type;
ViewType v;
value_type value;
CheckResult(ViewType v_, value_type value_) : v(v_), value(value_) {}
KOKKOS_FUNCTION
void operator()(const int i, int& lsum) const {
for (int j = 0; j < static_cast<int>(v.extent(1)); j++) {
if (v.access(i, j) != value) lsum++;
}
}
};
template <class ViewType>
bool run_check(ViewType v, typename ViewType::value_type value) {
using exec_space = typename ViewType::memory_space::execution_space;
int errors = 0;
Kokkos::fence();
Kokkos::parallel_reduce(Kokkos::RangePolicy<exec_space>(0, v.extent(0)),
CheckResult<ViewType>(v, value), errors);
return errors == 0;
}
} // namespace
TEST(TEST_CATEGORY, view_copy_tests) {
int N = 10000;
int M = 10;
Kokkos::View<int**, Kokkos::LayoutRight, TEST_EXECSPACE> defaulted;
Kokkos::View<int**, Kokkos::LayoutRight, TEST_EXECSPACE> a("A", N, M);
Kokkos::View<int**, Kokkos::LayoutRight, TEST_EXECSPACE> b("B", N, M);
auto h_a = Kokkos::create_mirror(a);
auto h_b = Kokkos::create_mirror(b);
auto m_a = Kokkos::create_mirror_view(a);
auto s_a = Kokkos::subview(a, Kokkos::ALL, 1);
auto s_b = Kokkos::subview(b, Kokkos::ALL, 1);
auto hs_a = Kokkos::subview(h_a, Kokkos::ALL, 1);
auto hs_b = Kokkos::subview(h_b, Kokkos::ALL, 1);
auto dev = typename TEST_EXECSPACE::execution_space();
auto host = Kokkos::DefaultHostExecutionSpace();
constexpr bool DevExecCanAccessHost =
Kokkos::SpaceAccessibility<typename TEST_EXECSPACE::execution_space,
Kokkos::HostSpace>::accessible;
constexpr bool HostExecCanAccessDev = Kokkos::SpaceAccessibility<
typename Kokkos::HostSpace::execution_space,
typename TEST_EXECSPACE::memory_space>::accessible;
// Contiguous copies
{ Kokkos::deep_copy(defaulted, defaulted); }
{
Kokkos::deep_copy(a, 0);
ASSERT_TRUE(run_check(a, 0));
}
{
Kokkos::deep_copy(a, 1);
ASSERT_TRUE(run_check(a, 1));
}
{
Kokkos::deep_copy(a, a);
ASSERT_TRUE(run_check(a, 1));
}
{
Kokkos::deep_copy(m_a, a);
ASSERT_TRUE(run_check(m_a, 1));
}
{
Kokkos::deep_copy(m_a, 2);
ASSERT_TRUE(run_check(m_a, 2));
}
{
Kokkos::deep_copy(a, m_a);
ASSERT_TRUE(run_check(a, 2));
}
{
Kokkos::deep_copy(b, 3);
ASSERT_TRUE(run_check(b, 3));
}
{
Kokkos::deep_copy(h_a, 4);
ASSERT_TRUE(run_check(h_a, 4));
}
{
Kokkos::deep_copy(a, b);
ASSERT_TRUE(run_check(a, 3));
}
{
Kokkos::deep_copy(h_b, h_a);
ASSERT_TRUE(run_check(h_b, 4));
}
{
Kokkos::deep_copy(h_a, a);
ASSERT_TRUE(run_check(h_a, 3));
}
{
Kokkos::deep_copy(b, h_b);
ASSERT_TRUE(run_check(b, 4));
}
// Non contiguous copies
{
Kokkos::deep_copy(s_a, 5);
ASSERT_TRUE(run_check(s_a, 5));
}
{
Kokkos::deep_copy(hs_a, 6);
ASSERT_TRUE(run_check(hs_a, 6));
}
{
Kokkos::deep_copy(s_b, s_a);
ASSERT_TRUE(run_check(s_b, 5));
}
{
Kokkos::deep_copy(hs_b, hs_a);
ASSERT_TRUE(run_check(hs_b, 6));
}
if (DevExecCanAccessHost || HostExecCanAccessDev) {
{
Kokkos::deep_copy(hs_b, s_b);
ASSERT_TRUE(run_check(hs_b, 5));
}
{
Kokkos::deep_copy(s_a, hs_a);
ASSERT_TRUE(run_check(s_a, 6));
}
} else {
// These copies won't succeed, but they should each throw
// an exception whose message contains the view labels,
// and the names of the views' memory spaces.
//
// Note: original a,b both have the same device type,
// and their mirrors have the same device type.
using memory_space = typename decltype(a)::memory_space;
using mirror_memory_space = typename decltype(h_a)::memory_space;
bool threw = false;
std::string msg;
try {
Kokkos::deep_copy(hs_b, s_b);
} catch (std::exception& e) {
threw = true;
msg = e.what();
}
ASSERT_TRUE(threw);
ASSERT_NE(msg.find(hs_b.label()), std::string::npos);
ASSERT_NE(msg.find(s_b.label()), std::string::npos);
ASSERT_NE(msg.find(memory_space().name()), std::string::npos);
ASSERT_NE(msg.find(mirror_memory_space().name()), std::string::npos);
threw = false;
try {
Kokkos::deep_copy(s_a, hs_a);
} catch (std::exception& e) {
threw = true;
msg = e.what();
}
ASSERT_TRUE(threw);
ASSERT_NE(msg.find(s_a.label()), std::string::npos);
ASSERT_NE(msg.find(hs_a.label()), std::string::npos);
ASSERT_NE(msg.find(memory_space().name()), std::string::npos);
ASSERT_NE(msg.find(mirror_memory_space().name()), std::string::npos);
}
// Contiguous copies
{ Kokkos::deep_copy(dev, defaulted, defaulted); }
{
Kokkos::deep_copy(dev, a, 0);
ASSERT_TRUE(run_check(a, 0));
}
{
Kokkos::deep_copy(dev, a, 1);
ASSERT_TRUE(run_check(a, 1));
}
{
Kokkos::deep_copy(dev, a, a);
ASSERT_TRUE(run_check(a, 1));
}
{
Kokkos::deep_copy(dev, m_a, a);
ASSERT_TRUE(run_check(m_a, 1));
}
{
Kokkos::deep_copy(dev, m_a, 2);
ASSERT_TRUE(run_check(m_a, 2));
}
{
Kokkos::deep_copy(dev, a, m_a);
ASSERT_TRUE(run_check(a, 2));
}
{
Kokkos::deep_copy(dev, b, 3);
ASSERT_TRUE(run_check(b, 3));
}
{
Kokkos::deep_copy(dev, h_a, 4);
ASSERT_TRUE(run_check(h_a, 4));
}
{
Kokkos::deep_copy(dev, a, b);
ASSERT_TRUE(run_check(a, 3));
}
{
Kokkos::deep_copy(dev, h_b, h_a);
ASSERT_TRUE(run_check(h_b, 4));
}
{
Kokkos::deep_copy(dev, h_a, a);
ASSERT_TRUE(run_check(h_a, 3));
}
{
Kokkos::deep_copy(dev, b, h_b);
ASSERT_TRUE(run_check(b, 4));
}
// Non contiguous copies
{
Kokkos::deep_copy(dev, s_a, 5);
ASSERT_TRUE(run_check(s_a, 5));
}
{
Kokkos::deep_copy(dev, hs_a, 6);
ASSERT_TRUE(run_check(hs_a, 6));
}
{
Kokkos::deep_copy(dev, s_b, s_a);
ASSERT_TRUE(run_check(s_b, 5));
}
{
Kokkos::deep_copy(dev, hs_b, hs_a);
ASSERT_TRUE(run_check(hs_b, 6));
}
if (DevExecCanAccessHost || HostExecCanAccessDev) {
{
Kokkos::deep_copy(dev, hs_b, s_b);
ASSERT_TRUE(run_check(hs_b, 5));
}
{
Kokkos::deep_copy(dev, s_a, hs_a);
ASSERT_TRUE(run_check(s_a, 6));
}
}
// Contiguous copies
{ Kokkos::deep_copy(host, defaulted, defaulted); }
{
Kokkos::deep_copy(host, a, 0);
ASSERT_TRUE(run_check(a, 0));
}
{
Kokkos::deep_copy(host, a, 1);
ASSERT_TRUE(run_check(a, 1));
}
{
Kokkos::deep_copy(host, a, a);
ASSERT_TRUE(run_check(a, 1));
}
{
Kokkos::deep_copy(host, m_a, a);
ASSERT_TRUE(run_check(m_a, 1));
}
{
Kokkos::deep_copy(host, m_a, 2);
ASSERT_TRUE(run_check(m_a, 2));
}
{
Kokkos::deep_copy(host, a, m_a);
ASSERT_TRUE(run_check(a, 2));
}
{
Kokkos::deep_copy(host, b, 3);
ASSERT_TRUE(run_check(b, 3));
}
{
Kokkos::deep_copy(host, h_a, 4);
ASSERT_TRUE(run_check(h_a, 4));
}
{
Kokkos::deep_copy(host, a, b);
ASSERT_TRUE(run_check(a, 3));
}
{
Kokkos::deep_copy(host, h_b, h_a);
ASSERT_TRUE(run_check(h_b, 4));
}
{
Kokkos::deep_copy(host, h_a, a);
ASSERT_TRUE(run_check(h_a, 3));
}
{
Kokkos::deep_copy(host, b, h_b);
ASSERT_TRUE(run_check(b, 4));
}
// Non contiguous copies
{
Kokkos::deep_copy(host, s_a, 5);
ASSERT_TRUE(run_check(s_a, 5));
}
{
Kokkos::deep_copy(host, hs_a, 6);
ASSERT_TRUE(run_check(hs_a, 6));
}
{
Kokkos::deep_copy(host, s_b, s_a);
ASSERT_TRUE(run_check(s_b, 5));
}
{
Kokkos::deep_copy(host, hs_b, hs_a);
ASSERT_TRUE(run_check(hs_b, 6));
}
if (DevExecCanAccessHost || HostExecCanAccessDev) {
{
Kokkos::deep_copy(host, hs_b, s_b);
ASSERT_TRUE(run_check(hs_b, 5));
}
{
Kokkos::deep_copy(host, s_a, hs_a);
ASSERT_TRUE(run_check(s_a, 6));
}
}
}
} // namespace Test
|