Skip to content

Commit 6fb23a3

Browse files
Merge pull request #701 from IntelPython/sycl_interface_null_args
2 parents d22acb4 + 2870d5e commit 6fb23a3

14 files changed

+926
-8
lines changed

Diff for: libsyclinterface/helper/source/dpctl_utils_helper.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ backend DPCTL_DPCTLBackendTypeToSyclBackend(DPCTLSyclBackendType BeTy)
9797
case DPCTLSyclBackendType::DPCTL_OPENCL:
9898
return backend::opencl;
9999
default:
100-
throw runtime_error("Unsupported backend type", -1);
100+
throw std::runtime_error("Unsupported backend type");
101101
}
102102
}
103103

@@ -135,7 +135,7 @@ info::device_type DPCTL_DPCTLDeviceTypeToSyclDeviceType(DPCTLSyclDeviceType DTy)
135135
case DPCTLSyclDeviceType::DPCTL_HOST_DEVICE:
136136
return info::device_type::host;
137137
default:
138-
throw runtime_error("Unsupported device type", -1);
138+
throw std::runtime_error("Unsupported device type");
139139
}
140140
}
141141

@@ -223,7 +223,7 @@ std::string DPCTL_AspectToStr(aspect aspectTy)
223223
ss << "usm_system_allocator";
224224
break;
225225
default:
226-
throw runtime_error("Unsupported aspect type", -1);
226+
throw std::runtime_error("Unsupported aspect type");
227227
}
228228
return ss.str();
229229
}
@@ -290,7 +290,7 @@ aspect DPCTL_StrToAspectType(const std::string &aspectTyStr)
290290
}
291291
else {
292292
// \todo handle the error
293-
throw runtime_error("Unsupported aspect type", -1);
293+
throw std::runtime_error("Unsupported aspect type");
294294
}
295295
return aspectTy;
296296
}
@@ -335,7 +335,7 @@ aspect DPCTL_DPCTLAspectTypeToSyclAspect(DPCTLSyclAspectType AspectTy)
335335
case DPCTLSyclAspectType::usm_system_allocator:
336336
return aspect::usm_system_allocator;
337337
default:
338-
throw runtime_error("Unsupported aspect type", -1);
338+
throw std::runtime_error("Unsupported aspect type");
339339
}
340340
}
341341

@@ -379,7 +379,7 @@ DPCTLSyclAspectType DPCTL_SyclAspectToDPCTLAspectType(aspect Aspect)
379379
case aspect::usm_system_allocator:
380380
return DPCTLSyclAspectType::usm_system_allocator;
381381
default:
382-
throw runtime_error("Unsupported aspect type", -1);
382+
throw std::runtime_error("Unsupported aspect type");
383383
}
384384
}
385385

@@ -402,7 +402,7 @@ info::partition_affinity_domain DPCTL_DPCTLPartitionAffinityDomainTypeToSycl(
402402
case DPCTLPartitionAffinityDomainType::next_partitionable:
403403
return info::partition_affinity_domain::next_partitionable;
404404
default:
405-
throw runtime_error("Unsupported partition_affinity_domain type", -1);
405+
throw std::runtime_error("Unsupported partition_affinity_domain type");
406406
}
407407
}
408408

@@ -425,7 +425,7 @@ DPCTLPartitionAffinityDomainType DPCTL_SyclPartitionAffinityDomainToDPCTLType(
425425
case info::partition_affinity_domain::next_partitionable:
426426
return DPCTLPartitionAffinityDomainType::next_partitionable;
427427
default:
428-
throw runtime_error("Unsupported partition_affinity_domain type", -1);
428+
throw std::runtime_error("Unsupported partition_affinity_domain type");
429429
}
430430
}
431431

Diff for: libsyclinterface/source/dpctl_sycl_context_interface.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ void DPCTLContext_Delete(__dpctl_take DPCTLSyclContextRef CtxRef)
183183
DPCTLSyclBackendType
184184
DPCTLContext_GetBackend(__dpctl_keep const DPCTLSyclContextRef CtxRef)
185185
{
186+
if (!CtxRef) {
187+
return DPCTL_UNKNOWN_BACKEND;
188+
}
189+
186190
auto BE = unwrap(CtxRef)->get_platform().get_backend();
187191

188192
switch (BE) {

Diff for: libsyclinterface/source/dpctl_sycl_program_interface.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ bool DPCTLProgram_HasKernel(__dpctl_keep DPCTLSyclProgramRef PRef,
317317
error_handler("Input PRef is nullptr", __FILE__, __func__, __LINE__);
318318
return false;
319319
}
320+
if (!KernelName) {
321+
error_handler("Input KernelName is nullptr", __FILE__, __func__,
322+
__LINE__);
323+
return false;
324+
}
320325

321326
auto SyclProgram = unwrap(PRef);
322327
try {

Diff for: libsyclinterface/source/dpctl_sycl_queue_manager.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ DPCTLSyclQueueRef DPCTLQueueMgr_GetCurrentQueue()
115115
// Relies on sycl::queue class' operator= to check for equivalent of queues.
116116
bool DPCTLQueueMgr_IsCurrentQueue(__dpctl_keep const DPCTLSyclQueueRef QRef)
117117
{
118+
if (!QRef) {
119+
return false;
120+
}
118121
auto &qs = QueueManager::getQueueStack();
119122
if (qs.empty()) {
120123
error_handler("No currently active queues.", __FILE__, __func__,

Diff for: libsyclinterface/tests/test_helper.cpp

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
//===--- test_helper.cpp - Test cases for helper functions ===//
2+
//
3+
// Data Parallel Control (dpctl)
4+
//
5+
// Copyright 2020-2021 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file has unit test cases for functions defined in
23+
/// helper/include/dpctl_utils_helper.h.
24+
///
25+
//===----------------------------------------------------------------------===//
26+
27+
#include "../helper/include/dpctl_utils_helper.h"
28+
#include "Config/dpctl_config.h"
29+
#include <CL/sycl.hpp>
30+
#include <gtest/gtest.h>
31+
#include <string>
32+
33+
struct TestHelperFns : public ::testing::Test
34+
{
35+
};
36+
37+
TEST_F(TestHelperFns, ChkDeviceTypeToStr)
38+
{
39+
std::string res;
40+
EXPECT_NO_FATAL_FAILURE(
41+
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::cpu));
42+
ASSERT_TRUE(res == "cpu");
43+
44+
EXPECT_NO_FATAL_FAILURE(
45+
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::gpu));
46+
ASSERT_TRUE(res == "gpu");
47+
48+
EXPECT_NO_FATAL_FAILURE(
49+
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::host));
50+
ASSERT_TRUE(res == "host");
51+
52+
EXPECT_NO_FATAL_FAILURE(
53+
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::custom));
54+
ASSERT_TRUE(res == "custom");
55+
56+
EXPECT_NO_FATAL_FAILURE(
57+
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::accelerator));
58+
ASSERT_TRUE(res == "accelerator");
59+
60+
EXPECT_NO_FATAL_FAILURE(
61+
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::all));
62+
ASSERT_TRUE(res == "unknown");
63+
}
64+
65+
TEST_F(TestHelperFns, ChkStrToDeviceType)
66+
{
67+
sycl::info::device_type dev_type = sycl::info::device_type::automatic;
68+
69+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("cpu"));
70+
ASSERT_TRUE(dev_type == sycl::info::device_type::cpu);
71+
72+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("gpu"));
73+
ASSERT_TRUE(dev_type == sycl::info::device_type::gpu);
74+
75+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("host"));
76+
ASSERT_TRUE(dev_type == sycl::info::device_type::host);
77+
78+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("accelerator"));
79+
ASSERT_TRUE(dev_type == sycl::info::device_type::accelerator);
80+
81+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("custom"));
82+
ASSERT_TRUE(dev_type == sycl::info::device_type::custom);
83+
84+
EXPECT_THROW(DPCTL_StrToDeviceType("invalid"), std::runtime_error);
85+
}
86+
87+
TEST_F(TestHelperFns, ChkDPCTLBackendTypeToSyclBackend)
88+
{
89+
sycl::backend res = sycl::backend::level_zero;
90+
91+
EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
92+
DPCTLSyclBackendType::DPCTL_CUDA));
93+
ASSERT_TRUE(res == sycl::backend::cuda);
94+
95+
EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
96+
DPCTLSyclBackendType::DPCTL_HOST));
97+
ASSERT_TRUE(res == sycl::backend::host);
98+
99+
EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
100+
DPCTLSyclBackendType::DPCTL_OPENCL));
101+
ASSERT_TRUE(res == sycl::backend::opencl);
102+
103+
EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
104+
DPCTLSyclBackendType::DPCTL_LEVEL_ZERO));
105+
ASSERT_TRUE(res == sycl::backend::level_zero);
106+
107+
EXPECT_THROW(DPCTL_DPCTLBackendTypeToSyclBackend(
108+
DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND),
109+
std::runtime_error);
110+
}
111+
112+
TEST_F(TestHelperFns, ChkSyclBackendToDPCTLBackendType)
113+
{
114+
DPCTLSyclBackendType DTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
115+
116+
EXPECT_NO_FATAL_FAILURE(
117+
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::level_zero));
118+
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_LEVEL_ZERO);
119+
120+
EXPECT_NO_FATAL_FAILURE(
121+
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::opencl));
122+
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_OPENCL);
123+
124+
EXPECT_NO_FATAL_FAILURE(
125+
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::host));
126+
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_HOST);
127+
128+
EXPECT_NO_FATAL_FAILURE(
129+
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::cuda));
130+
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_CUDA);
131+
132+
EXPECT_NO_FATAL_FAILURE(
133+
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::all));
134+
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND);
135+
}
136+
137+
TEST_F(TestHelperFns, ChkDPCTLDeviceTypeToSyclDeviceType)
138+
{
139+
sycl::info::device_type dev_type = sycl::info::device_type::automatic;
140+
141+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
142+
DPCTLSyclDeviceType::DPCTL_CPU));
143+
ASSERT_TRUE(dev_type == sycl::info::device_type::cpu);
144+
145+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
146+
DPCTLSyclDeviceType::DPCTL_GPU));
147+
ASSERT_TRUE(dev_type == sycl::info::device_type::gpu);
148+
149+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
150+
DPCTLSyclDeviceType::DPCTL_ACCELERATOR));
151+
ASSERT_TRUE(dev_type == sycl::info::device_type::accelerator);
152+
153+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
154+
DPCTLSyclDeviceType::DPCTL_CUSTOM));
155+
ASSERT_TRUE(dev_type == sycl::info::device_type::custom);
156+
157+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
158+
DPCTLSyclDeviceType::DPCTL_HOST_DEVICE));
159+
ASSERT_TRUE(dev_type == sycl::info::device_type::host);
160+
161+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
162+
DPCTLSyclDeviceType::DPCTL_AUTOMATIC));
163+
ASSERT_TRUE(dev_type == sycl::info::device_type::automatic);
164+
165+
EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
166+
DPCTLSyclDeviceType::DPCTL_ALL));
167+
ASSERT_TRUE(dev_type == sycl::info::device_type::all);
168+
}
169+
170+
TEST_F(TestHelperFns, SyclDeviceTypeToDPCTLDeviceType)
171+
{
172+
DPCTLSyclDeviceType DTy = DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE;
173+
174+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
175+
sycl::info::device_type::cpu));
176+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_CPU);
177+
178+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
179+
sycl::info::device_type::gpu));
180+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_GPU);
181+
182+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
183+
sycl::info::device_type::host));
184+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_HOST_DEVICE);
185+
186+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
187+
sycl::info::device_type::accelerator));
188+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_ACCELERATOR);
189+
190+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
191+
sycl::info::device_type::automatic));
192+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_AUTOMATIC);
193+
194+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
195+
sycl::info::device_type::all));
196+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_ALL);
197+
198+
EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
199+
sycl::info::device_type::custom));
200+
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_CUSTOM);
201+
}

Diff for: libsyclinterface/tests/test_sycl_context_interface.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,82 @@ INSTANTIATE_TEST_SUITE_P(DPCTLContextTests,
211211
"gpu:0",
212212
"gpu:1",
213213
"1"));
214+
215+
struct TestDPCTLContextNullArgs : public ::testing::Test
216+
{
217+
DPCTLSyclContextRef Null_CRef = nullptr;
218+
DPCTLSyclDeviceRef Null_DRef = nullptr;
219+
DPCTLDeviceVectorRef Null_DVRef = nullptr;
220+
TestDPCTLContextNullArgs() = default;
221+
~TestDPCTLContextNullArgs() = default;
222+
};
223+
224+
TEST_F(TestDPCTLContextNullArgs, ChkCreate)
225+
{
226+
DPCTLSyclContextRef CRef = nullptr;
227+
EXPECT_NO_FATAL_FAILURE(CRef = DPCTLContext_Create(Null_DRef, nullptr, 0));
228+
ASSERT_FALSE(bool(CRef));
229+
}
230+
231+
TEST_F(TestDPCTLContextNullArgs, ChkCreateFromDevices)
232+
{
233+
DPCTLSyclContextRef CRef = nullptr;
234+
EXPECT_NO_FATAL_FAILURE(
235+
CRef = DPCTLContext_CreateFromDevices(Null_DVRef, nullptr, 0));
236+
ASSERT_FALSE(bool(CRef));
237+
}
238+
239+
TEST_F(TestDPCTLContextNullArgs, ChkAreEq)
240+
{
241+
DPCTLSyclContextRef Null_C2Ref = nullptr;
242+
bool are_eq = true;
243+
EXPECT_NO_FATAL_FAILURE(are_eq = DPCTLContext_AreEq(Null_CRef, Null_C2Ref));
244+
ASSERT_FALSE(are_eq);
245+
}
246+
247+
TEST_F(TestDPCTLContextNullArgs, ChkCopy)
248+
{
249+
DPCTLSyclContextRef Copied_CRef = nullptr;
250+
EXPECT_NO_FATAL_FAILURE(Copied_CRef = DPCTLContext_Copy(Null_CRef));
251+
ASSERT_FALSE(bool(Copied_CRef));
252+
}
253+
254+
TEST_F(TestDPCTLContextNullArgs, ChkGetDevices)
255+
{
256+
DPCTLDeviceVectorRef DVRef = nullptr;
257+
EXPECT_NO_FATAL_FAILURE(DVRef = DPCTLContext_GetDevices(Null_CRef));
258+
ASSERT_FALSE(bool(DVRef));
259+
}
260+
261+
TEST_F(TestDPCTLContextNullArgs, ChkDeviceCount)
262+
{
263+
size_t count = -1;
264+
EXPECT_NO_FATAL_FAILURE(count = DPCTLContext_DeviceCount(Null_CRef));
265+
ASSERT_TRUE(count == 0);
266+
}
267+
268+
TEST_F(TestDPCTLContextNullArgs, ChkIsHost)
269+
{
270+
bool is_host = true;
271+
EXPECT_NO_FATAL_FAILURE(is_host = DPCTLContext_IsHost(Null_CRef));
272+
ASSERT_FALSE(is_host);
273+
}
274+
275+
TEST_F(TestDPCTLContextNullArgs, ChkHash)
276+
{
277+
size_t hash = 0;
278+
EXPECT_NO_FATAL_FAILURE(hash = DPCTLContext_Hash(Null_CRef));
279+
ASSERT_TRUE(hash == 0);
280+
}
281+
282+
TEST_F(TestDPCTLContextNullArgs, ChkGetBackend)
283+
{
284+
DPCTLSyclBackendType BTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;
285+
EXPECT_NO_FATAL_FAILURE(BTy = DPCTLContext_GetBackend(Null_CRef));
286+
ASSERT_TRUE(BTy == DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND);
287+
}
288+
289+
TEST_F(TestDPCTLContextNullArgs, ChkDelete)
290+
{
291+
EXPECT_NO_FATAL_FAILURE(DPCTLContext_Delete(Null_CRef));
292+
}

0 commit comments

Comments
 (0)