1 //===-- StreamTest.cpp - Tests for Stream ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file contains the unit tests for Stream code.
13 //===----------------------------------------------------------------------===//
17 #include "SimpleHostPlatformDevice.h"
18 #include "streamexecutor/Device.h"
19 #include "streamexecutor/Kernel.h"
20 #include "streamexecutor/KernelSpec.h"
21 #include "streamexecutor/PlatformDevice.h"
22 #include "streamexecutor/Stream.h"
24 #include "gtest/gtest.h"
28 namespace se = ::streamexecutor;
30 const auto &getDeviceValue =
31 se::test::SimpleHostPlatformDevice::getDeviceValue<int>;
33 /// Test fixture to hold objects used by tests.
34 class StreamTest : public ::testing::Test {
37 : DummyPlatformStream(1), Device(&PDevice),
38 Stream(&PDevice, &DummyPlatformStream), HostA5{0, 1, 2, 3, 4},
39 HostB5{5, 6, 7, 8, 9}, HostA7{10, 11, 12, 13, 14, 15, 16},
40 HostB7{17, 18, 19, 20, 21, 22, 23}, Host5{24, 25, 26, 27, 28},
41 Host7{29, 30, 31, 32, 33, 34, 35},
42 DeviceA5(getOrDie(Device.allocateDeviceMemory<int>(5))),
43 DeviceB5(getOrDie(Device.allocateDeviceMemory<int>(5))),
44 DeviceA7(getOrDie(Device.allocateDeviceMemory<int>(7))),
45 DeviceB7(getOrDie(Device.allocateDeviceMemory<int>(7))) {
46 se::dieIfError(Device.synchronousCopyH2D<int>(HostA5, DeviceA5));
47 se::dieIfError(Device.synchronousCopyH2D<int>(HostB5, DeviceB5));
48 se::dieIfError(Device.synchronousCopyH2D<int>(HostA7, DeviceA7));
49 se::dieIfError(Device.synchronousCopyH2D<int>(HostB7, DeviceB7));
53 int DummyPlatformStream; // Mimicking a platform where the platform stream
54 // handle is just a stream number.
55 se::test::SimpleHostPlatformDevice PDevice;
59 // Device memory is matched by host arrays.
65 // Host memory to be used as actual host memory.
70 se::GlobalDeviceMemory<int> DeviceA5;
71 se::GlobalDeviceMemory<int> DeviceB5;
72 se::GlobalDeviceMemory<int> DeviceA7;
73 se::GlobalDeviceMemory<int> DeviceB7;
77 using llvm::MutableArrayRef;
81 TEST_F(StreamTest, CopyD2HToMutableArrayRefByCount) {
82 Stream.thenCopyD2H(DeviceA5, MutableArrayRef<int>(Host5), 5);
83 EXPECT_TRUE(Stream.isOK());
84 for (int I = 0; I < 5; ++I) {
85 EXPECT_EQ(HostA5[I], Host5[I]);
88 Stream.thenCopyD2H(DeviceB5, MutableArrayRef<int>(Host5), 2);
89 EXPECT_TRUE(Stream.isOK());
90 for (int I = 0; I < 2; ++I) {
91 EXPECT_EQ(HostB5[I], Host5[I]);
94 Stream.thenCopyD2H(DeviceA7, MutableArrayRef<int>(Host5), 7);
95 EXPECT_FALSE(Stream.isOK());
98 TEST_F(StreamTest, CopyD2HToMutableArrayRef) {
99 Stream.thenCopyD2H(DeviceA5, MutableArrayRef<int>(Host5));
100 EXPECT_TRUE(Stream.isOK());
101 for (int I = 0; I < 5; ++I) {
102 EXPECT_EQ(HostA5[I], Host5[I]);
105 Stream.thenCopyD2H(DeviceA5, MutableArrayRef<int>(Host7));
106 EXPECT_FALSE(Stream.isOK());
109 TEST_F(StreamTest, CopyD2HToPointer) {
110 Stream.thenCopyD2H(DeviceA5, Host5, 5);
111 EXPECT_TRUE(Stream.isOK());
112 for (int I = 0; I < 5; ++I) {
113 EXPECT_EQ(HostA5[I], Host5[I]);
116 Stream.thenCopyD2H(DeviceA5, Host7, 7);
117 EXPECT_FALSE(Stream.isOK());
120 TEST_F(StreamTest, CopyD2HSliceToMutableArrayRefByCount) {
121 Stream.thenCopyD2H(DeviceA5.asSlice().drop_front(1),
122 MutableArrayRef<int>(Host5 + 1, 4), 4);
123 EXPECT_TRUE(Stream.isOK());
124 for (int I = 1; I < 5; ++I) {
125 EXPECT_EQ(HostA5[I], Host5[I]);
128 Stream.thenCopyD2H(DeviceB5.asSlice().drop_back(1),
129 MutableArrayRef<int>(Host5), 2);
130 EXPECT_TRUE(Stream.isOK());
131 for (int I = 0; I < 2; ++I) {
132 EXPECT_EQ(HostB5[I], Host5[I]);
135 Stream.thenCopyD2H(DeviceA5.asSlice(), MutableArrayRef<int>(Host7), 7);
136 EXPECT_FALSE(Stream.isOK());
139 TEST_F(StreamTest, CopyD2HSliceToMutableArrayRef) {
140 Stream.thenCopyD2H(DeviceA7.asSlice().slice(1, 5),
141 MutableArrayRef<int>(Host5));
142 EXPECT_TRUE(Stream.isOK());
143 for (int I = 0; I < 5; ++I) {
144 EXPECT_EQ(HostA7[I + 1], Host5[I]);
147 Stream.thenCopyD2H(DeviceA5.asSlice(), MutableArrayRef<int>(Host7));
148 EXPECT_FALSE(Stream.isOK());
151 TEST_F(StreamTest, CopyD2HSliceToPointer) {
152 Stream.thenCopyD2H(DeviceA5.asSlice().drop_front(1), Host5 + 1, 4);
153 EXPECT_TRUE(Stream.isOK());
154 for (int I = 1; I < 5; ++I) {
155 EXPECT_EQ(HostA5[I], Host5[I]);
158 Stream.thenCopyD2H(DeviceA5.asSlice(), Host7, 7);
159 EXPECT_FALSE(Stream.isOK());
164 TEST_F(StreamTest, CopyH2DToArrayRefByCount) {
165 Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceA5, 5);
166 EXPECT_TRUE(Stream.isOK());
167 for (int I = 0; I < 5; ++I) {
168 EXPECT_EQ(getDeviceValue(DeviceA5, I), Host5[I]);
171 Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceB5, 2);
172 EXPECT_TRUE(Stream.isOK());
173 for (int I = 0; I < 2; ++I) {
174 EXPECT_EQ(getDeviceValue(DeviceB5, I), Host5[I]);
177 Stream.thenCopyH2D(ArrayRef<int>(Host7), DeviceA5, 7);
178 EXPECT_FALSE(Stream.isOK());
181 TEST_F(StreamTest, CopyH2DToArrayRef) {
182 Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceA5);
183 EXPECT_TRUE(Stream.isOK());
184 for (int I = 0; I < 5; ++I) {
185 EXPECT_EQ(getDeviceValue(DeviceA5, I), Host5[I]);
188 Stream.thenCopyH2D(ArrayRef<int>(Host7), DeviceA5);
189 EXPECT_FALSE(Stream.isOK());
192 TEST_F(StreamTest, CopyH2DToPointer) {
193 Stream.thenCopyH2D(Host5, DeviceA5, 5);
194 EXPECT_TRUE(Stream.isOK());
195 for (int I = 0; I < 5; ++I) {
196 EXPECT_EQ(getDeviceValue(DeviceA5, I), Host5[I]);
199 Stream.thenCopyH2D(Host7, DeviceA5, 7);
200 EXPECT_FALSE(Stream.isOK());
203 TEST_F(StreamTest, CopyH2DSliceToArrayRefByCount) {
204 Stream.thenCopyH2D(ArrayRef<int>(Host5 + 1, 4),
205 DeviceA5.asSlice().drop_front(1), 4);
206 EXPECT_TRUE(Stream.isOK());
207 for (int I = 1; I < 5; ++I) {
208 EXPECT_EQ(getDeviceValue(DeviceA5, I), Host5[I]);
211 Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceB5.asSlice().drop_back(1), 2);
212 EXPECT_TRUE(Stream.isOK());
213 for (int I = 0; I < 2; ++I) {
214 EXPECT_EQ(getDeviceValue(DeviceB5, I), Host5[I]);
217 Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceA5.asSlice(), 7);
218 EXPECT_FALSE(Stream.isOK());
221 TEST_F(StreamTest, CopyH2DSliceToArrayRef) {
223 Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceA5.asSlice());
224 EXPECT_TRUE(Stream.isOK());
225 for (int I = 0; I < 5; ++I) {
226 EXPECT_EQ(getDeviceValue(DeviceA5, I), Host5[I]);
229 Stream.thenCopyH2D(ArrayRef<int>(Host7), DeviceA5.asSlice());
230 EXPECT_FALSE(Stream.isOK());
233 TEST_F(StreamTest, CopyH2DSliceToPointer) {
234 Stream.thenCopyH2D(Host5, DeviceA5.asSlice(), 5);
235 EXPECT_TRUE(Stream.isOK());
236 for (int I = 0; I < 5; ++I) {
237 EXPECT_EQ(getDeviceValue(DeviceA5, I), Host5[I]);
240 Stream.thenCopyH2D(Host7, DeviceA5.asSlice(), 7);
241 EXPECT_FALSE(Stream.isOK());
246 TEST_F(StreamTest, CopyD2DByCount) {
247 Stream.thenCopyD2D(DeviceA5, DeviceB5, 5);
248 EXPECT_TRUE(Stream.isOK());
249 for (int I = 0; I < 5; ++I) {
250 EXPECT_EQ(getDeviceValue(DeviceA5, I), getDeviceValue(DeviceB5, I));
253 Stream.thenCopyD2D(DeviceA7, DeviceB7, 2);
254 EXPECT_TRUE(Stream.isOK());
255 for (int I = 0; I < 2; ++I) {
256 EXPECT_EQ(getDeviceValue(DeviceA7, I), getDeviceValue(DeviceB7, I));
259 Stream.thenCopyD2D(DeviceA7, DeviceB5, 7);
260 EXPECT_FALSE(Stream.isOK());
263 TEST_F(StreamTest, CopyD2D) {
264 Stream.thenCopyD2D(DeviceA5, DeviceB5);
265 EXPECT_TRUE(Stream.isOK());
266 for (int I = 0; I < 5; ++I) {
267 EXPECT_EQ(getDeviceValue(DeviceA5, I), getDeviceValue(DeviceB5, I));
270 Stream.thenCopyD2D(DeviceA7, DeviceB5);
271 EXPECT_FALSE(Stream.isOK());
274 TEST_F(StreamTest, CopySliceD2DByCount) {
275 Stream.thenCopyD2D(DeviceA5.asSlice().drop_front(1), DeviceB5, 4);
276 EXPECT_TRUE(Stream.isOK());
277 for (int I = 0; I < 4; ++I) {
278 EXPECT_EQ(getDeviceValue(DeviceA5, I + 1), getDeviceValue(DeviceB5, I));
281 Stream.thenCopyD2D(DeviceA7.asSlice().drop_back(1), DeviceB7, 2);
282 EXPECT_TRUE(Stream.isOK());
283 for (int I = 0; I < 2; ++I) {
284 EXPECT_EQ(getDeviceValue(DeviceA7, I), getDeviceValue(DeviceB7, I));
287 Stream.thenCopyD2D(DeviceA5.asSlice(), DeviceB5, 7);
288 EXPECT_FALSE(Stream.isOK());
291 TEST_F(StreamTest, CopySliceD2D) {
293 Stream.thenCopyD2D(DeviceA7.asSlice().drop_back(2), DeviceB5);
294 EXPECT_TRUE(Stream.isOK());
295 for (int I = 0; I < 5; ++I) {
296 EXPECT_EQ(getDeviceValue(DeviceA7, I), getDeviceValue(DeviceB5, I));
299 Stream.thenCopyD2D(DeviceA5.asSlice().drop_back(1), DeviceB7);
300 EXPECT_FALSE(Stream.isOK());
303 TEST_F(StreamTest, CopyD2DSliceByCount) {
304 Stream.thenCopyD2D(DeviceA5, DeviceB7.asSlice().drop_front(2), 5);
305 EXPECT_TRUE(Stream.isOK());
306 for (int I = 0; I < 5; ++I) {
307 EXPECT_EQ(getDeviceValue(DeviceA5, I), getDeviceValue(DeviceB7, I + 2));
310 Stream.thenCopyD2D(DeviceA7, DeviceB7.asSlice().drop_back(3), 2);
311 EXPECT_TRUE(Stream.isOK());
312 for (int I = 0; I < 2; ++I) {
313 EXPECT_EQ(getDeviceValue(DeviceA7, I), getDeviceValue(DeviceB7, I));
316 Stream.thenCopyD2D(DeviceA5, DeviceB7.asSlice(), 7);
317 EXPECT_FALSE(Stream.isOK());
320 TEST_F(StreamTest, CopyD2DSlice) {
322 Stream.thenCopyD2D(DeviceA5, DeviceB7.asSlice().drop_back(2));
323 EXPECT_TRUE(Stream.isOK());
324 for (int I = 0; I < 5; ++I) {
325 EXPECT_EQ(getDeviceValue(DeviceA5, I), getDeviceValue(DeviceB7, I));
328 Stream.thenCopyD2D(DeviceA5, DeviceB7.asSlice());
329 EXPECT_FALSE(Stream.isOK());
332 TEST_F(StreamTest, CopySliceD2DSliceByCount) {
334 Stream.thenCopyD2D(DeviceA5.asSlice(), DeviceB5.asSlice(), 5);
335 EXPECT_TRUE(Stream.isOK());
336 for (int I = 0; I < 5; ++I) {
337 EXPECT_EQ(getDeviceValue(DeviceA5, I), getDeviceValue(DeviceB5, I));
340 Stream.thenCopyD2D(DeviceA7.asSlice(), DeviceB7.asSlice(), 2);
341 EXPECT_TRUE(Stream.isOK());
342 for (int I = 0; I < 2; ++I) {
343 EXPECT_EQ(getDeviceValue(DeviceA7, I), getDeviceValue(DeviceB7, I));
346 Stream.thenCopyD2D(DeviceA7.asSlice(), DeviceB5.asSlice(), 7);
347 EXPECT_FALSE(Stream.isOK());
350 TEST_F(StreamTest, CopySliceD2DSlice) {
352 Stream.thenCopyD2D(DeviceA5.asSlice(), DeviceB5.asSlice());
353 EXPECT_TRUE(Stream.isOK());
354 for (int I = 0; I < 5; ++I) {
355 EXPECT_EQ(getDeviceValue(DeviceA5, I), getDeviceValue(DeviceB5, I));
358 Stream.thenCopyD2D(DeviceA5.asSlice(), DeviceB7.asSlice());
359 EXPECT_FALSE(Stream.isOK());