Debian Codegraph @

offload/unittests/OffloadAPI/memory/olMemFill.cpp

raw
1 //===------- Offload API tests - olMemFill --------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "../common/Fixtures.hpp"
10 #include <OffloadAPI.h>
11 #include <gtest/gtest.h>
12
13 struct olMemFillTest : OffloadQueueTest {
14 template <typename PatternTy, PatternTy PatternVal, size_t Size,
15 bool Block = false>
16 void test_body() {
17 ManuallyTriggeredTask Manual;
18
19 // Block/enqueue tests ensure that the test has been enqueued to a queue
20 // (rather than being done synchronously if the queue happens to be empty)
21 if constexpr (Block) {
22 ASSERT_SUCCESS(Manual.enqueue(Queue));
23 }
24
25 void *Alloc;
26 ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, Size, &Alloc));
27
28 PatternTy Pattern = PatternVal;
29 ASSERT_SUCCESS(olMemFill(Queue, Alloc, sizeof(Pattern), &Pattern, Size));
30
31 if constexpr (Block) {
32 ASSERT_SUCCESS(Manual.trigger());
33 }
34 olSyncQueue(Queue);
35
36 size_t N = Size / sizeof(Pattern);
37 for (size_t i = 0; i < N; i++) {
38 PatternTy *AllocPtr = reinterpret_cast<PatternTy *>(Alloc);
39 ASSERT_EQ(AllocPtr[i], Pattern);
40 }
41
42 olMemFree(Alloc);
43 }
44 };
45 OFFLOAD_TESTS_INSTANTIATE_DEVICE_FIXTURE(olMemFillTest);
46
47 TEST_P(olMemFillTest, Success8) { test_body<uint8_t, 0x42, 1024>(); }
48 TEST_P(olMemFillTest, Success8NotMultiple4) {
49 test_body<uint8_t, 0x42, 1023>();
50 }
51 TEST_P(olMemFillTest, Success8Enqueue) {
52 test_body<uint8_t, 0x42, 1024, true>();
53 }
54 TEST_P(olMemFillTest, Success8NotMultiple4Enqueue) {
55 test_body<uint8_t, 0x42, 1023, true>();
56 }
57
58 TEST_P(olMemFillTest, Success16) { test_body<uint8_t, 0x42, 1024>(); }
59 TEST_P(olMemFillTest, Success16NotMultiple4) {
60 test_body<uint16_t, 0x4243, 1022>();
61 }
62 TEST_P(olMemFillTest, Success16Enqueue) {
63 test_body<uint8_t, 0x42, 1024, true>();
64 }
65 TEST_P(olMemFillTest, Success16NotMultiple4Enqueue) {
66 test_body<uint16_t, 0x4243, 1022, true>();
67 }
68
69 TEST_P(olMemFillTest, Success32) { test_body<uint32_t, 0xDEADBEEF, 1024>(); }
70 TEST_P(olMemFillTest, Success32Enqueue) {
71 test_body<uint32_t, 0xDEADBEEF, 1024, true>();
72 }
73
74 TEST_P(olMemFillTest, SuccessLarge) {
75 constexpr size_t Size = 1024;
76 void *Alloc;
77 ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, Size, &Alloc));
78
79 struct PatternT {
80 uint64_t A;
81 uint64_t B;
82 } Pattern{UINT64_MAX, UINT64_MAX};
83
84 ASSERT_SUCCESS(olMemFill(Queue, Alloc, sizeof(Pattern), &Pattern, Size));
85
86 olSyncQueue(Queue);
87
88 size_t N = Size / sizeof(Pattern);
89 for (size_t i = 0; i < N; i++) {
90 PatternT *AllocPtr = reinterpret_cast<PatternT *>(Alloc);
91 ASSERT_EQ(AllocPtr[i].A, UINT64_MAX);
92 ASSERT_EQ(AllocPtr[i].B, UINT64_MAX);
93 }
94
95 olMemFree(Alloc);
96 }
97
98 TEST_P(olMemFillTest, SuccessLargeEnqueue) {
99 constexpr size_t Size = 1024;
100 void *Alloc;
101 ManuallyTriggeredTask Manual;
102 ASSERT_SUCCESS(Manual.enqueue(Queue));
103
104 ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, Size, &Alloc));
105
106 struct PatternT {
107 uint64_t A;
108 uint64_t B;
109 } Pattern{UINT64_MAX, UINT64_MAX};
110
111 ASSERT_SUCCESS(olMemFill(Queue, Alloc, sizeof(Pattern), &Pattern, Size));
112
113 Manual.trigger();
114 olSyncQueue(Queue);
115
116 size_t N = Size / sizeof(Pattern);
117 for (size_t i = 0; i < N; i++) {
118 PatternT *AllocPtr = reinterpret_cast<PatternT *>(Alloc);
119 ASSERT_EQ(AllocPtr[i].A, UINT64_MAX);
120 ASSERT_EQ(AllocPtr[i].B, UINT64_MAX);
121 }
122
123 olMemFree(Alloc);
124 }
125
126 TEST_P(olMemFillTest, SuccessLargeByteAligned) {
127 constexpr size_t Size = 17 * 64;
128 void *Alloc;
129 ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, Size, &Alloc));
130
131 struct __attribute__((packed)) PatternT {
132 uint64_t A;
133 uint64_t B;
134 uint8_t C;
135 } Pattern{UINT64_MAX, UINT64_MAX, 255};
136
137 ASSERT_SUCCESS(olMemFill(Queue, Alloc, sizeof(Pattern), &Pattern, Size));
138
139 olSyncQueue(Queue);
140
141 size_t N = Size / sizeof(Pattern);
142 for (size_t i = 0; i < N; i++) {
143 PatternT *AllocPtr = reinterpret_cast<PatternT *>(Alloc);
144 ASSERT_EQ(AllocPtr[i].A, UINT64_MAX);
145 ASSERT_EQ(AllocPtr[i].B, UINT64_MAX);
146 ASSERT_EQ(AllocPtr[i].C, 255);
147 }
148
149 olMemFree(Alloc);
150 }
151
152 TEST_P(olMemFillTest, SuccessLargeByteAlignedEnqueue) {
153 constexpr size_t Size = 17 * 64;
154 void *Alloc;
155 ManuallyTriggeredTask Manual;
156 ASSERT_SUCCESS(Manual.enqueue(Queue));
157
158 ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, Size, &Alloc));
159
160 struct __attribute__((packed)) PatternT {
161 uint64_t A;
162 uint64_t B;
163 uint8_t C;
164 } Pattern{UINT64_MAX, UINT64_MAX, 255};
165
166 ASSERT_SUCCESS(olMemFill(Queue, Alloc, sizeof(Pattern), &Pattern, Size));
167
168 Manual.trigger();
169 olSyncQueue(Queue);
170
171 size_t N = Size / sizeof(Pattern);
172 for (size_t i = 0; i < N; i++) {
173 PatternT *AllocPtr = reinterpret_cast<PatternT *>(Alloc);
174 ASSERT_EQ(AllocPtr[i].A, UINT64_MAX);
175 ASSERT_EQ(AllocPtr[i].B, UINT64_MAX);
176 ASSERT_EQ(AllocPtr[i].C, 255);
177 }
178
179 olMemFree(Alloc);
180 }
181
182 TEST_P(olMemFillTest, InvalidPatternSize) {
183 constexpr size_t Size = 1025;
184 void *Alloc;
185 ASSERT_SUCCESS(olMemAlloc(Device, OL_ALLOC_TYPE_MANAGED, Size, &Alloc));
186
187 uint16_t Pattern = 0x4242;
188 ASSERT_ERROR(OL_ERRC_INVALID_SIZE,
189 olMemFill(Queue, Alloc, sizeof(Pattern), &Pattern, Size));
190
191 olSyncQueue(Queue);
192 olMemFree(Alloc);
193 }