-
Notifications
You must be signed in to change notification settings - Fork 31
/
DeviceAllocation.test.cc
93 lines (78 loc) · 2.71 KB
/
DeviceAllocation.test.cc
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/data/DeviceAllocation.test.cc
//---------------------------------------------------------------------------//
#include "corecel/data/DeviceAllocation.hh"
#include <algorithm>
#include "corecel/cont/Span.hh"
#include "celeritas_test.hh"
namespace celeritas
{
namespace test
{
//---------------------------------------------------------------------------//
// NOTE: don't have 'device' in the name here
TEST(ConstructionTest, should_work_always)
{
DeviceAllocation alloc;
EXPECT_EQ(0, alloc.size());
EXPECT_TRUE(alloc.empty());
}
TEST(ConstructionTest, nocuda)
{
#if !CELER_USE_DEVICE
// Can't allocate
EXPECT_THROW(DeviceAllocation(1234), DebugError);
#else
GTEST_SKIP() << "CUDA is enabled";
#endif
}
TEST(DeviceAllocationTest, TEST_IF_CELER_DEVICE(device))
{
DeviceAllocation alloc(1024);
EXPECT_EQ(1024, alloc.size());
EXPECT_FALSE(alloc.empty());
{
DeviceAllocation other(128);
std::byte* orig_alloc = alloc.device_ref().data();
std::byte* orig_other = other.device_ref().data();
swap(alloc, other);
EXPECT_EQ(1024, other.size());
EXPECT_EQ(orig_other, alloc.device_ref().data());
EXPECT_EQ(orig_alloc, other.device_ref().data());
}
std::vector<std::byte> data(alloc.size());
ASSERT_EQ(128, data.size());
data.front() = std::byte(1);
data.back() = std::byte(127);
alloc.copy_to_device(make_span(data));
std::vector<std::byte> newdata(alloc.size());
ASSERT_EQ(128, newdata.size());
alloc.copy_to_host(make_span(newdata));
EXPECT_EQ(std::byte(1), newdata.front());
EXPECT_EQ(std::byte(127), newdata.back());
// Test move construction/assignment
{
std::byte* orig_ptr = alloc.device_ref().data();
DeviceAllocation other(std::move(alloc));
EXPECT_EQ(128, other.size());
EXPECT_EQ(0, alloc.size());
EXPECT_EQ(orig_ptr, other.device_ref().data());
}
}
TEST(DeviceAllocationTest, TEST_IF_CELER_DEVICE(empty))
{
DeviceAllocation alloc{0};
EXPECT_TRUE(alloc.empty());
EXPECT_EQ(0, alloc.size());
EXPECT_EQ(nullptr, alloc.device_ref().data());
std::vector<std::byte> newdata(alloc.size());
alloc.copy_to_device(make_span(newdata));
alloc.copy_to_host(make_span(newdata));
}
//---------------------------------------------------------------------------//
} // namespace test
} // namespace celeritas