Skip to content

Fix/crashes reported in 345 344 #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dpctl-capi/source/dpctl_sycl_platform_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ __dpctl_give DPCTLSyclPlatformRef DPCTLPlatform_CreateFromSelector(
PRef = wrap(P);
} catch (std::bad_alloc const &ba) {
std::cerr << ba.what() << '\n';
} catch (runtime_error const &re) {
std::cerr << re.what() << '\n';
return nullptr;
Comment on lines +87 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do catch(...) finally. Any uncatched exception will break C API interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us do that as a follow up along with the overall work needed to improve error handling (#35).

}
}
else {
Expand Down
2 changes: 2 additions & 0 deletions dpctl-capi/source/dpctl_sycl_queue_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ struct QueueManager
delete unwrap(CRef);
} catch (std::bad_alloc const &ba) {
std::cerr << ba.what() << '\n';
} catch (runtime_error const &re) {
std::cerr << re.what() << '\n';
}

return qs;
Expand Down
67 changes: 67 additions & 0 deletions dpctl-capi/tests/test_sycl_device_invalid_filters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//===--- test_sycl_device_invalid_filters.cpp - -ve tests for device iface ===//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2021 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
/// Negative test cases for SYCL device creation with invalid filter selectors.
///
//===----------------------------------------------------------------------===//

#include "dpctl_sycl_device_interface.h"
#include "dpctl_sycl_device_selector_interface.h"
#include <CL/sycl.hpp>
#include <gtest/gtest.h>

using namespace cl::sycl;
struct TestUnsupportedFilters : public ::testing::TestWithParam<const char *>
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;

TestUnsupportedFilters()
{
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(GetParam()));
}

void SetUp()
{
if (!DSRef) {
auto message = "Skipping as no device of type " +
std::string(GetParam()) + ".";
GTEST_SKIP_(message.c_str());
}
}

~TestUnsupportedFilters()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
}
};

TEST_P(TestUnsupportedFilters, Chk_DPCTLDevice_CreateFromSelector)
{
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef == nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

INSTANTIATE_TEST_SUITE_P(
NegativeDeviceCreationTests,
TestUnsupportedFilters,
::testing::Values("abc", "-1", "invalid_filter", "cuda:cpu:0"));
136 changes: 80 additions & 56 deletions dpctl-capi/tests/test_sycl_device_selector_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ struct TestDeviceSelectorInterface : public ::testing::Test
struct TestFilterSelector : public ::testing::TestWithParam<const char *>
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

TestFilterSelector()
{
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(GetParam()));
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
}

void SetUp()
{
if (!DSRef) {
if (!DRef) {
auto message = "Skipping as no device of type " +
std::string(GetParam()) + ".";
GTEST_SKIP_(message.c_str());
Expand All @@ -64,6 +66,7 @@ struct TestFilterSelector : public ::testing::TestWithParam<const char *>
~TestFilterSelector()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}
};

Expand All @@ -76,15 +79,6 @@ struct TestUnsupportedFilters : public ::testing::TestWithParam<const char *>
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(GetParam()));
}

void SetUp()
{
if (!DSRef) {
auto message = "Skipping as no device of type " +
std::string(GetParam()) + ".";
GTEST_SKIP_(message.c_str());
}
}

~TestUnsupportedFilters()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
Expand All @@ -94,84 +88,107 @@ struct TestUnsupportedFilters : public ::testing::TestWithParam<const char *>
TEST_F(TestDeviceSelectorInterface, Chk_DPCTLAcceleratorSelector_Create)
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLAcceleratorSelector_Create());
if (DSRef) {
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_TRUE(DPCTLDevice_IsAccelerator(DRef));
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));

if (!DRef) {
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
GTEST_SKIP_("Device not found. Skip tests.");
}

EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_TRUE(DPCTLDevice_IsAccelerator(DRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

TEST_F(TestDeviceSelectorInterface, Chk_DPCTLDefaultSelector_Create)
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLDefaultSelector_Create());
if (DSRef) {
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));

if (!DRef) {
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
GTEST_SKIP_("Device not found. Skip tests.");
}

ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

TEST_F(TestDeviceSelectorInterface, Chk_DPCTLCPUSelector_Create)
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLCPUSelector_Create());
if (DSRef) {
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_TRUE(DPCTLDevice_IsCPU(DRef));
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));

if (!DRef) {
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
GTEST_SKIP_("Device not found. Skip tests.");
}

EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_TRUE(DPCTLDevice_IsCPU(DRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

TEST_F(TestDeviceSelectorInterface, Chk_DPCTLGPUSelector_Create)
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLGPUSelector_Create());
if (DSRef) {
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_TRUE(DPCTLDevice_IsGPU(DRef));
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));

if (!DRef) {
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
GTEST_SKIP_("Device not found. Skip tests.");
}

EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
EXPECT_TRUE(DPCTLDevice_IsGPU(DRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

TEST_F(TestDeviceSelectorInterface, Chk_DPCTLHostSelector_Create)
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLHostSelector_Create());
if (DSRef) {
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
// FIXME: DPCPP's host_selector returns a CPU device for some reason.
// EXPECT_TRUE(DPCTLDevice_IsHost(DRef));
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));

if (!DRef) {
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
GTEST_SKIP_("Device not found. Skip tests.");
}

EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));
// FIXME: DPCPP's host_selector returns a CPU device for some reason.
// EXPECT_TRUE(DPCTLDevice_IsHost(DRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

TEST_P(TestFilterSelector, Chk_DPCTLFilterSelector_Create)
{
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef));
ASSERT_TRUE(DRef != nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

TEST_P(TestUnsupportedFilters, Chk_DPCTLFilterSelector_Create)
Expand All @@ -186,20 +203,26 @@ TEST_F(TestDeviceSelectorInterface, Chk_DPCTLGPUSelector_Score)
{
DPCTLSyclDeviceSelectorRef DSRef_GPU = nullptr;
DPCTLSyclDeviceSelectorRef DSRef_CPU = nullptr;
DPCTLSyclDeviceRef DRef = nullptr;

EXPECT_NO_FATAL_FAILURE(DSRef_GPU = DPCTLGPUSelector_Create());
EXPECT_NO_FATAL_FAILURE(DSRef_CPU = DPCTLCPUSelector_Create());
if (DSRef_CPU && DSRef_GPU) {
DPCTLSyclDeviceRef DRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef =
DPCTLDevice_CreateFromSelector(DSRef_CPU));
ASSERT_TRUE(DRef != nullptr);
EXPECT_TRUE(DPCTLDevice_IsCPU(DRef));
EXPECT_TRUE(DPCTLDeviceSelector_Score(DSRef_GPU, DRef) < 0);
EXPECT_TRUE(DPCTLDeviceSelector_Score(DSRef_CPU, DRef) > 0);
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef_CPU));

if (!DRef) {
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef_GPU));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef_CPU));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
GTEST_SKIP_("Device not found. Skip tests.");
}

ASSERT_TRUE(DRef != nullptr);
EXPECT_TRUE(DPCTLDevice_IsCPU(DRef));
EXPECT_TRUE(DPCTLDeviceSelector_Score(DSRef_GPU, DRef) < 0);
EXPECT_TRUE(DPCTLDeviceSelector_Score(DSRef_CPU, DRef) > 0);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef_GPU));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef_CPU));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}

INSTANTIATE_TEST_SUITE_P(FilterSelectorCreation,
Expand All @@ -216,9 +239,10 @@ INSTANTIATE_TEST_SUITE_P(FilterSelectorCreation,
"level_zero:gpu:0",
"gpu:0",
"gpu:1",
"1"));
"1",
"0",
"host"));

INSTANTIATE_TEST_SUITE_P(
NegativeFilterSelectorCreation,
TestUnsupportedFilters,
::testing::Values("abc", "-1", "opencl:gpu:1", "level_zero:cpu:0"));
INSTANTIATE_TEST_SUITE_P(NegativeFilterSelectorCreation,
TestUnsupportedFilters,
::testing::Values("abc", "-1", "cuda:cpu:0"));
68 changes: 68 additions & 0 deletions dpctl-capi/tests/test_sycl_platform_invalid_filters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//=== test_sycl_platform_invalid_filters.cpp - -ve tests for platform iface ==//
//
// Data Parallel Control (dpctl)
//
// Copyright 2020-2021 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
/// Negative test cases for SYCL platform creation with invalid filter
/// selectors.
///
//===----------------------------------------------------------------------===//

#include "dpctl_sycl_device_selector_interface.h"
#include "dpctl_sycl_platform_interface.h"
#include <CL/sycl.hpp>
#include <gtest/gtest.h>

using namespace cl::sycl;
struct TestUnsupportedFilters : public ::testing::TestWithParam<const char *>
{
DPCTLSyclDeviceSelectorRef DSRef = nullptr;

TestUnsupportedFilters()
{
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(GetParam()));
}

void SetUp()
{
if (!DSRef) {
auto message = "Skipping as no device of type " +
std::string(GetParam()) + ".";
GTEST_SKIP_(message.c_str());
}
}

~TestUnsupportedFilters()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
}
};

TEST_P(TestUnsupportedFilters, Chk_DPCTLPlatform_CreateFromSelector)
{
DPCTLSyclPlatformRef PRef = nullptr;
EXPECT_NO_FATAL_FAILURE(PRef = DPCTLPlatform_CreateFromSelector(DSRef));
ASSERT_TRUE(PRef == nullptr);
EXPECT_NO_FATAL_FAILURE(DPCTLPlatform_Delete(PRef));
}

INSTANTIATE_TEST_SUITE_P(
NegativeDeviceCreationTests,
TestUnsupportedFilters,
::testing::Values("abc", "-1", "invalid_filter", "cuda:cpu:0"));
Loading