-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_sycl_context_interface.cpp
More file actions
270 lines (236 loc) · 9.04 KB
/
test_sycl_context_interface.cpp
File metadata and controls
270 lines (236 loc) · 9.04 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//===----------------- test_sycl_context_interface.cpp --------------------===//
//===---------------- Tests for sycl context interface -------------------===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2024 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file has unit test cases for functions defined in
/// dpctl_sycl_context_interface.h.
///
//===----------------------------------------------------------------------===//
#include "dpctl_sycl_context_interface.h"
#include "dpctl_sycl_device_interface.h"
#include "dpctl_sycl_device_selector_interface.h"
#include "dpctl_sycl_types.h"
#include <stddef.h>
#include <gtest/gtest.h>
#include <sycl/sycl.hpp>
#include <vector>
using namespace sycl;
namespace
{
} // namespace
struct TestDPCTLContextInterface : public ::testing::TestWithParam<const char *>
{
DPCTLSyclDeviceRef DRef = nullptr;
TestDPCTLContextInterface()
{
auto DS = DPCTLFilterSelector_Create(GetParam());
if (DS) {
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DS));
}
DPCTLDeviceSelector_Delete(DS);
}
void SetUp()
{
if (!DRef) {
auto message = "Skipping as no device of type " +
std::string(GetParam()) + ".";
GTEST_SKIP_(message.c_str());
}
}
~TestDPCTLContextInterface()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}
};
TEST_P(TestDPCTLContextInterface, ChkCreate)
{
DPCTLSyclContextRef CRef = nullptr;
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(DRef, nullptr, 0));
ASSERT_TRUE(CRef);
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
}
TEST_P(TestDPCTLContextInterface, ChkCreateWithDevices)
{
size_t nCUs = 0;
DPCTLSyclContextRef CRef = nullptr;
DPCTLDeviceVectorRef DVRef = nullptr;
EXPECT_NO_FATAL_FAILURE(nCUs = DPCTLDevice_GetMaxComputeUnits(DRef));
if (nCUs > 1) {
EXPECT_NO_FATAL_FAILURE(
DVRef = DPCTLDevice_CreateSubDevicesEqually(DRef, nCUs / 2));
if (!DVRef) {
GTEST_SKIP_("Skipping creating context for sub-devices");
}
EXPECT_NO_FATAL_FAILURE(
CRef = DPCTLContext_CreateFromDevices(DVRef, nullptr, 0));
ASSERT_TRUE(CRef);
}
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
}
TEST_P(TestDPCTLContextInterface, ChkCreateWithDevicesGetDevices)
{
size_t nCUs = 0;
DPCTLSyclContextRef CRef = nullptr;
DPCTLDeviceVectorRef DVRef = nullptr;
DPCTLDeviceVectorRef Res_DVRef = nullptr;
EXPECT_NO_FATAL_FAILURE(nCUs = DPCTLDevice_GetMaxComputeUnits(DRef));
if (nCUs > 1) {
EXPECT_NO_FATAL_FAILURE(
DVRef = DPCTLDevice_CreateSubDevicesEqually(DRef, nCUs / 2));
if (!DVRef) {
GTEST_SKIP_("Skipping creating context for sub-devices");
}
EXPECT_NO_FATAL_FAILURE(
CRef = DPCTLContext_CreateFromDevices(DVRef, nullptr, 0));
ASSERT_TRUE(CRef);
const size_t len = DPCTLDeviceVector_Size(DVRef);
ASSERT_TRUE(DPCTLContext_DeviceCount(CRef) == len);
EXPECT_NO_FATAL_FAILURE(Res_DVRef = DPCTLContext_GetDevices(CRef));
ASSERT_TRUE(DPCTLDeviceVector_Size(Res_DVRef) == len);
}
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(Res_DVRef));
}
TEST_P(TestDPCTLContextInterface, ChkGetDevices)
{
DPCTLSyclContextRef CRef = nullptr;
DPCTLDeviceVectorRef DVRef = nullptr;
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(DRef, nullptr, 0));
ASSERT_TRUE(CRef);
EXPECT_NO_FATAL_FAILURE(DVRef = DPCTLContext_GetDevices(CRef));
ASSERT_TRUE(DVRef);
EXPECT_TRUE(DPCTLDeviceVector_Size(DVRef) == 1);
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
}
TEST_P(TestDPCTLContextInterface, ChkAreEq)
{
DPCTLSyclContextRef CRef1 = nullptr, CRef2 = nullptr, CRef3 = nullptr;
bool are_eq = true, are_not_eq = false;
EXPECT_NO_FATAL_FAILURE(CRef1 = DPCTLContext_Create(DRef, nullptr, 0));
EXPECT_NO_FATAL_FAILURE(CRef2 = DPCTLContext_Copy(CRef1));
// TODO: This work till DPC++ does not have a default context per device,
// after that we need to change the test case some how.
EXPECT_NO_FATAL_FAILURE(CRef3 = DPCTLContext_Create(DRef, nullptr, 0));
ASSERT_TRUE(CRef3);
ASSERT_TRUE(CRef2);
ASSERT_TRUE(CRef1);
EXPECT_NO_FATAL_FAILURE(are_eq = DPCTLContext_AreEq(CRef1, CRef2));
EXPECT_NO_FATAL_FAILURE(are_not_eq = DPCTLContext_AreEq(CRef1, CRef3));
EXPECT_TRUE(are_eq);
EXPECT_FALSE(are_not_eq);
EXPECT_TRUE(DPCTLContext_Hash(CRef1) == DPCTLContext_Hash(CRef2));
EXPECT_FALSE(DPCTLContext_Hash(CRef1) == DPCTLContext_Hash(CRef3));
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef1));
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef2));
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef3));
}
TEST_P(TestDPCTLContextInterface, ChkGetBackend)
{
DPCTLSyclContextRef CRef = nullptr;
DPCTLSyclBackendType context_backend = DPCTL_UNKNOWN_BACKEND,
device_backend = DPCTL_UNKNOWN_BACKEND;
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(DRef, nullptr, 0));
ASSERT_TRUE(CRef);
EXPECT_NO_FATAL_FAILURE(device_backend = DPCTLDevice_GetBackend(DRef));
EXPECT_NO_FATAL_FAILURE(context_backend = DPCTLContext_GetBackend(CRef));
EXPECT_TRUE(device_backend == context_backend);
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(CRef));
}
INSTANTIATE_TEST_SUITE_P(DPCTLContextTests,
TestDPCTLContextInterface,
::testing::Values("opencl",
"opencl:gpu",
"opencl:cpu",
"opencl:gpu:0",
"gpu",
"cpu",
"level_zero",
"level_zero:gpu",
"opencl:cpu:0",
"level_zero:gpu:0",
"gpu:0",
"gpu:1",
"1"));
struct TestDPCTLContextNullArgs : public ::testing::Test
{
DPCTLSyclContextRef Null_CRef = nullptr;
DPCTLSyclDeviceRef Null_DRef = nullptr;
DPCTLDeviceVectorRef Null_DVRef = nullptr;
TestDPCTLContextNullArgs() = default;
~TestDPCTLContextNullArgs() = default;
};
TEST_F(TestDPCTLContextNullArgs, ChkCreate)
{
DPCTLSyclContextRef CRef = nullptr;
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(Null_DRef, nullptr, 0));
ASSERT_FALSE(bool(CRef));
}
TEST_F(TestDPCTLContextNullArgs, ChkCreateFromDevices)
{
DPCTLSyclContextRef CRef = nullptr;
EXPECT_NO_FATAL_FAILURE(
CRef = DPCTLContext_CreateFromDevices(Null_DVRef, nullptr, 0));
ASSERT_FALSE(bool(CRef));
}
TEST_F(TestDPCTLContextNullArgs, ChkAreEq)
{
DPCTLSyclContextRef Null_C2Ref = nullptr;
bool are_eq = true;
EXPECT_NO_FATAL_FAILURE(are_eq = DPCTLContext_AreEq(Null_CRef, Null_C2Ref));
ASSERT_FALSE(are_eq);
}
TEST_F(TestDPCTLContextNullArgs, ChkCopy)
{
DPCTLSyclContextRef Copied_CRef = nullptr;
EXPECT_NO_FATAL_FAILURE(Copied_CRef = DPCTLContext_Copy(Null_CRef));
ASSERT_FALSE(bool(Copied_CRef));
}
TEST_F(TestDPCTLContextNullArgs, ChkGetDevices)
{
DPCTLDeviceVectorRef DVRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DVRef = DPCTLContext_GetDevices(Null_CRef));
ASSERT_FALSE(bool(DVRef));
}
TEST_F(TestDPCTLContextNullArgs, ChkDeviceCount)
{
size_t count = -1;
EXPECT_NO_FATAL_FAILURE(count = DPCTLContext_DeviceCount(Null_CRef));
ASSERT_TRUE(count == 0);
}
TEST_F(TestDPCTLContextNullArgs, ChkHash)
{
size_t hash = 0;
EXPECT_NO_FATAL_FAILURE(hash = DPCTLContext_Hash(Null_CRef));
ASSERT_TRUE(hash == 0);
}
TEST_F(TestDPCTLContextNullArgs, ChkGetBackend)
{
DPCTLSyclBackendType BTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
EXPECT_NO_FATAL_FAILURE(BTy = DPCTLContext_GetBackend(Null_CRef));
ASSERT_TRUE(BTy == DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND);
}
TEST_F(TestDPCTLContextNullArgs, ChkDelete)
{
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(Null_CRef));
}