summaryrefslogtreecommitdiff
path: root/parallel-libs/streamexecutor/lib/unittests/PackedKernelArgumentArrayTest.cpp
blob: 929af07aae32a0b0a9b3539a38aa81d0217f3d65 (plain)
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
//===-- PackedKernelArgumentArrayTest.cpp - tests for kernel arg packing --===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Unit tests for kernel argument packing.
///
//===----------------------------------------------------------------------===//

#include "SimpleHostPlatformDevice.h"
#include "streamexecutor/Device.h"
#include "streamexecutor/DeviceMemory.h"
#include "streamexecutor/PackedKernelArgumentArray.h"
#include "streamexecutor/PlatformInterfaces.h"

#include "llvm/ADT/Twine.h"

#include "gtest/gtest.h"

namespace {

namespace se = ::streamexecutor;

using Type = se::KernelArgumentType;

// Test fixture class for testing argument packing.
//
// Basically defines a bunch of types to be packed so they don't have to be
// defined separately in each test.
class DeviceMemoryPackingTest : public ::testing::Test {
public:
  DeviceMemoryPackingTest()
      : Device(&PDevice), Value(42), Handle(&Value), ByteCount(15),
        ElementCount(5),
        TypedGlobal(getOrDie(Device.allocateDeviceMemory<int>(ElementCount))),
        TypedShared(
            se::SharedDeviceMemory<int>::makeFromElementCount(ElementCount)) {}

  se::test::SimpleHostPlatformDevice PDevice;
  se::Device Device;
  int Value;
  void *Handle;
  size_t ByteCount;
  size_t ElementCount;
  se::GlobalDeviceMemory<int> TypedGlobal;
  se::SharedDeviceMemory<int> TypedShared;
};

// Utility method to check the expected address, size, and type for a packed
// argument at the given index of a PackedKernelArgumentArray.
template <typename... ParameterTs>
static void
ExpectEqual(const void *ExpectedAddress, size_t ExpectedSize, Type ExpectedType,
            const se::PackedKernelArgumentArray<ParameterTs...> &Observed,
            size_t Index) {
  SCOPED_TRACE(("Index = " + llvm::Twine(Index)).str());
  EXPECT_EQ(ExpectedAddress, Observed.getAddress(Index));
  EXPECT_EQ(ExpectedAddress, Observed.getAddresses()[Index]);
  EXPECT_EQ(ExpectedSize, Observed.getSize(Index));
  EXPECT_EQ(ExpectedSize, Observed.getSizes()[Index]);
  EXPECT_EQ(ExpectedType, Observed.getType(Index));
  EXPECT_EQ(ExpectedType, Observed.getTypes()[Index]);
}

TEST_F(DeviceMemoryPackingTest, SingleValue) {
  auto Array = se::make_kernel_argument_pack(Value);
  ExpectEqual(&Value, sizeof(Value), Type::VALUE, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(0u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, SingleTypedGlobal) {
  auto Array = se::make_kernel_argument_pack(TypedGlobal);
  ExpectEqual(TypedGlobal.getHandle(), sizeof(void *),
              Type::GLOBAL_DEVICE_MEMORY, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(0u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, SingleTypedGlobalPointer) {
  auto Array = se::make_kernel_argument_pack(&TypedGlobal);
  ExpectEqual(TypedGlobal.getHandle(), sizeof(void *),
              Type::GLOBAL_DEVICE_MEMORY, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(0u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, SingleConstTypedGlobalPointer) {
  const se::GlobalDeviceMemory<int> *ArgumentPointer = &TypedGlobal;
  auto Array = se::make_kernel_argument_pack(ArgumentPointer);
  ExpectEqual(TypedGlobal.getHandle(), sizeof(void *),
              Type::GLOBAL_DEVICE_MEMORY, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(0u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, SingleTypedShared) {
  auto Array = se::make_kernel_argument_pack(TypedShared);
  ExpectEqual(nullptr, TypedShared.getElementCount() * sizeof(int),
              Type::SHARED_DEVICE_MEMORY, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(1u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, SingleTypedSharedPointer) {
  auto Array = se::make_kernel_argument_pack(&TypedShared);
  ExpectEqual(nullptr, TypedShared.getElementCount() * sizeof(int),
              Type::SHARED_DEVICE_MEMORY, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(1u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, SingleConstTypedSharedPointer) {
  const se::SharedDeviceMemory<int> *ArgumentPointer = &TypedShared;
  auto Array = se::make_kernel_argument_pack(ArgumentPointer);
  ExpectEqual(nullptr, TypedShared.getElementCount() * sizeof(int),
              Type::SHARED_DEVICE_MEMORY, Array, 0);
  EXPECT_EQ(1u, Array.getArgumentCount());
  EXPECT_EQ(1u, Array.getSharedCount());
}

TEST_F(DeviceMemoryPackingTest, PackSeveralArguments) {
  const se::GlobalDeviceMemory<int> *TypedGlobalPointer = &TypedGlobal;
  const se::SharedDeviceMemory<int> *TypedSharedPointer = &TypedShared;
  auto Array = se::make_kernel_argument_pack(Value, TypedGlobal, &TypedGlobal,
                                             TypedGlobalPointer, TypedShared,
                                             &TypedShared, TypedSharedPointer);
  ExpectEqual(&Value, sizeof(Value), Type::VALUE, Array, 0);
  ExpectEqual(TypedGlobal.getHandle(), sizeof(void *),
              Type::GLOBAL_DEVICE_MEMORY, Array, 1);
  ExpectEqual(TypedGlobal.getHandle(), sizeof(void *),
              Type::GLOBAL_DEVICE_MEMORY, Array, 2);
  ExpectEqual(TypedGlobal.getHandle(), sizeof(void *),
              Type::GLOBAL_DEVICE_MEMORY, Array, 3);
  ExpectEqual(nullptr, TypedShared.getElementCount() * sizeof(int),
              Type::SHARED_DEVICE_MEMORY, Array, 4);
  ExpectEqual(nullptr, TypedShared.getElementCount() * sizeof(int),
              Type::SHARED_DEVICE_MEMORY, Array, 5);
  ExpectEqual(nullptr, TypedShared.getElementCount() * sizeof(int),
              Type::SHARED_DEVICE_MEMORY, Array, 6);
  EXPECT_EQ(7u, Array.getArgumentCount());
  EXPECT_EQ(3u, Array.getSharedCount());
}

} // namespace