[asan] Add a new AddressDescription structure, which can describe any type of address.
[lldb.git] / parallel-libs / streamexecutor / lib / unittests / StreamTest.cpp
1 //===-- StreamTest.cpp - Tests for Stream ---------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file contains the unit tests for Stream code.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include <cstring>
16
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"
23
24 #include "gtest/gtest.h"
25
26 namespace {
27
28 namespace se = ::streamexecutor;
29
30 const auto &getDeviceValue =
31     se::test::SimpleHostPlatformDevice::getDeviceValue<int>;
32
33 /// Test fixture to hold objects used by tests.
34 class StreamTest : public ::testing::Test {
35 public:
36   StreamTest()
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));
50   }
51
52 protected:
53   int DummyPlatformStream; // Mimicking a platform where the platform stream
54                            // handle is just a stream number.
55   se::test::SimpleHostPlatformDevice PDevice;
56   se::Device Device;
57   se::Stream Stream;
58
59   // Device memory is matched by host arrays.
60   int HostA5[5];
61   int HostB5[5];
62   int HostA7[7];
63   int HostB7[7];
64
65   // Host memory to be used as actual host memory.
66   int Host5[5];
67   int Host7[7];
68
69   // Device memory.
70   se::GlobalDeviceMemory<int> DeviceA5;
71   se::GlobalDeviceMemory<int> DeviceB5;
72   se::GlobalDeviceMemory<int> DeviceA7;
73   se::GlobalDeviceMemory<int> DeviceB7;
74 };
75
76 using llvm::ArrayRef;
77 using llvm::MutableArrayRef;
78
79 // D2H tests
80
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]);
86   }
87
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]);
92   }
93
94   Stream.thenCopyD2H(DeviceA7, MutableArrayRef<int>(Host5), 7);
95   EXPECT_FALSE(Stream.isOK());
96 }
97
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]);
103   }
104
105   Stream.thenCopyD2H(DeviceA5, MutableArrayRef<int>(Host7));
106   EXPECT_FALSE(Stream.isOK());
107 }
108
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]);
114   }
115
116   Stream.thenCopyD2H(DeviceA5, Host7, 7);
117   EXPECT_FALSE(Stream.isOK());
118 }
119
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]);
126   }
127
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]);
133   }
134
135   Stream.thenCopyD2H(DeviceA5.asSlice(), MutableArrayRef<int>(Host7), 7);
136   EXPECT_FALSE(Stream.isOK());
137 }
138
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]);
145   }
146
147   Stream.thenCopyD2H(DeviceA5.asSlice(), MutableArrayRef<int>(Host7));
148   EXPECT_FALSE(Stream.isOK());
149 }
150
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]);
156   }
157
158   Stream.thenCopyD2H(DeviceA5.asSlice(), Host7, 7);
159   EXPECT_FALSE(Stream.isOK());
160 }
161
162 // H2D tests
163
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]);
169   }
170
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]);
175   }
176
177   Stream.thenCopyH2D(ArrayRef<int>(Host7), DeviceA5, 7);
178   EXPECT_FALSE(Stream.isOK());
179 }
180
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]);
186   }
187
188   Stream.thenCopyH2D(ArrayRef<int>(Host7), DeviceA5);
189   EXPECT_FALSE(Stream.isOK());
190 }
191
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]);
197   }
198
199   Stream.thenCopyH2D(Host7, DeviceA5, 7);
200   EXPECT_FALSE(Stream.isOK());
201 }
202
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]);
209   }
210
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]);
215   }
216
217   Stream.thenCopyH2D(ArrayRef<int>(Host5), DeviceA5.asSlice(), 7);
218   EXPECT_FALSE(Stream.isOK());
219 }
220
221 TEST_F(StreamTest, CopyH2DSliceToArrayRef) {
222
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]);
227   }
228
229   Stream.thenCopyH2D(ArrayRef<int>(Host7), DeviceA5.asSlice());
230   EXPECT_FALSE(Stream.isOK());
231 }
232
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]);
238   }
239
240   Stream.thenCopyH2D(Host7, DeviceA5.asSlice(), 7);
241   EXPECT_FALSE(Stream.isOK());
242 }
243
244 // D2D tests
245
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));
251   }
252
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));
257   }
258
259   Stream.thenCopyD2D(DeviceA7, DeviceB5, 7);
260   EXPECT_FALSE(Stream.isOK());
261 }
262
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));
268   }
269
270   Stream.thenCopyD2D(DeviceA7, DeviceB5);
271   EXPECT_FALSE(Stream.isOK());
272 }
273
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));
279   }
280
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));
285   }
286
287   Stream.thenCopyD2D(DeviceA5.asSlice(), DeviceB5, 7);
288   EXPECT_FALSE(Stream.isOK());
289 }
290
291 TEST_F(StreamTest, CopySliceD2D) {
292
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));
297   }
298
299   Stream.thenCopyD2D(DeviceA5.asSlice().drop_back(1), DeviceB7);
300   EXPECT_FALSE(Stream.isOK());
301 }
302
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));
308   }
309
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));
314   }
315
316   Stream.thenCopyD2D(DeviceA5, DeviceB7.asSlice(), 7);
317   EXPECT_FALSE(Stream.isOK());
318 }
319
320 TEST_F(StreamTest, CopyD2DSlice) {
321
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));
326   }
327
328   Stream.thenCopyD2D(DeviceA5, DeviceB7.asSlice());
329   EXPECT_FALSE(Stream.isOK());
330 }
331
332 TEST_F(StreamTest, CopySliceD2DSliceByCount) {
333
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));
338   }
339
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));
344   }
345
346   Stream.thenCopyD2D(DeviceA7.asSlice(), DeviceB5.asSlice(), 7);
347   EXPECT_FALSE(Stream.isOK());
348 }
349
350 TEST_F(StreamTest, CopySliceD2DSlice) {
351
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));
356   }
357
358   Stream.thenCopyD2D(DeviceA5.asSlice(), DeviceB7.asSlice());
359   EXPECT_FALSE(Stream.isOK());
360 }
361
362 } // namespace