Skip to content

Sycl interface null args #701

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 16 commits into from
Dec 1, 2021
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
16 changes: 8 additions & 8 deletions libsyclinterface/helper/source/dpctl_utils_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ backend DPCTL_DPCTLBackendTypeToSyclBackend(DPCTLSyclBackendType BeTy)
case DPCTLSyclBackendType::DPCTL_OPENCL:
return backend::opencl;
default:
throw runtime_error("Unsupported backend type", -1);
throw std::runtime_error("Unsupported backend type");
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ info::device_type DPCTL_DPCTLDeviceTypeToSyclDeviceType(DPCTLSyclDeviceType DTy)
case DPCTLSyclDeviceType::DPCTL_HOST_DEVICE:
return info::device_type::host;
default:
throw runtime_error("Unsupported device type", -1);
throw std::runtime_error("Unsupported device type");
}
}

Expand Down Expand Up @@ -223,7 +223,7 @@ std::string DPCTL_AspectToStr(aspect aspectTy)
ss << "usm_system_allocator";
break;
default:
throw runtime_error("Unsupported aspect type", -1);
throw std::runtime_error("Unsupported aspect type");
}
return ss.str();
}
Expand Down Expand Up @@ -290,7 +290,7 @@ aspect DPCTL_StrToAspectType(const std::string &aspectTyStr)
}
else {
// \todo handle the error
throw runtime_error("Unsupported aspect type", -1);
throw std::runtime_error("Unsupported aspect type");
}
return aspectTy;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ aspect DPCTL_DPCTLAspectTypeToSyclAspect(DPCTLSyclAspectType AspectTy)
case DPCTLSyclAspectType::usm_system_allocator:
return aspect::usm_system_allocator;
default:
throw runtime_error("Unsupported aspect type", -1);
throw std::runtime_error("Unsupported aspect type");
}
}

Expand Down Expand Up @@ -379,7 +379,7 @@ DPCTLSyclAspectType DPCTL_SyclAspectToDPCTLAspectType(aspect Aspect)
case aspect::usm_system_allocator:
return DPCTLSyclAspectType::usm_system_allocator;
default:
throw runtime_error("Unsupported aspect type", -1);
throw std::runtime_error("Unsupported aspect type");
}
}

Expand All @@ -402,7 +402,7 @@ info::partition_affinity_domain DPCTL_DPCTLPartitionAffinityDomainTypeToSycl(
case DPCTLPartitionAffinityDomainType::next_partitionable:
return info::partition_affinity_domain::next_partitionable;
default:
throw runtime_error("Unsupported partition_affinity_domain type", -1);
throw std::runtime_error("Unsupported partition_affinity_domain type");
}
}

Expand All @@ -425,7 +425,7 @@ DPCTLPartitionAffinityDomainType DPCTL_SyclPartitionAffinityDomainToDPCTLType(
case info::partition_affinity_domain::next_partitionable:
return DPCTLPartitionAffinityDomainType::next_partitionable;
default:
throw runtime_error("Unsupported partition_affinity_domain type", -1);
throw std::runtime_error("Unsupported partition_affinity_domain type");
}
}

Expand Down
4 changes: 4 additions & 0 deletions libsyclinterface/source/dpctl_sycl_context_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ void DPCTLContext_Delete(__dpctl_take DPCTLSyclContextRef CtxRef)
DPCTLSyclBackendType
DPCTLContext_GetBackend(__dpctl_keep const DPCTLSyclContextRef CtxRef)
{
if (!CtxRef) {
return DPCTL_UNKNOWN_BACKEND;
}

auto BE = unwrap(CtxRef)->get_platform().get_backend();

switch (BE) {
Expand Down
5 changes: 5 additions & 0 deletions libsyclinterface/source/dpctl_sycl_program_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ bool DPCTLProgram_HasKernel(__dpctl_keep DPCTLSyclProgramRef PRef,
error_handler("Input PRef is nullptr", __FILE__, __func__, __LINE__);
return false;
}
if (!KernelName) {
error_handler("Input KernelName is nullptr", __FILE__, __func__,
__LINE__);
return false;
}

auto SyclProgram = unwrap(PRef);
try {
Expand Down
3 changes: 3 additions & 0 deletions libsyclinterface/source/dpctl_sycl_queue_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ DPCTLSyclQueueRef DPCTLQueueMgr_GetCurrentQueue()
// Relies on sycl::queue class' operator= to check for equivalent of queues.
bool DPCTLQueueMgr_IsCurrentQueue(__dpctl_keep const DPCTLSyclQueueRef QRef)
{
if (!QRef) {
return false;
}
auto &qs = QueueManager::getQueueStack();
if (qs.empty()) {
error_handler("No currently active queues.", __FILE__, __func__,
Expand Down
201 changes: 201 additions & 0 deletions libsyclinterface/tests/test_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//===--- test_helper.cpp - Test cases for helper functions ===//
//
// 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
/// This file has unit test cases for functions defined in
/// helper/include/dpctl_utils_helper.h.
///
//===----------------------------------------------------------------------===//

#include "../helper/include/dpctl_utils_helper.h"
#include "Config/dpctl_config.h"
#include <CL/sycl.hpp>
#include <gtest/gtest.h>
#include <string>

struct TestHelperFns : public ::testing::Test
{
};

TEST_F(TestHelperFns, ChkDeviceTypeToStr)
{
std::string res;
EXPECT_NO_FATAL_FAILURE(
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::cpu));
ASSERT_TRUE(res == "cpu");

EXPECT_NO_FATAL_FAILURE(
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::gpu));
ASSERT_TRUE(res == "gpu");

EXPECT_NO_FATAL_FAILURE(
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::host));
ASSERT_TRUE(res == "host");

EXPECT_NO_FATAL_FAILURE(
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::custom));
ASSERT_TRUE(res == "custom");

EXPECT_NO_FATAL_FAILURE(
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::accelerator));
ASSERT_TRUE(res == "accelerator");

EXPECT_NO_FATAL_FAILURE(
res = DPCTL_DeviceTypeToStr(sycl::info::device_type::all));
ASSERT_TRUE(res == "unknown");
}

TEST_F(TestHelperFns, ChkStrToDeviceType)
{
sycl::info::device_type dev_type = sycl::info::device_type::automatic;

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("cpu"));
ASSERT_TRUE(dev_type == sycl::info::device_type::cpu);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("gpu"));
ASSERT_TRUE(dev_type == sycl::info::device_type::gpu);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("host"));
ASSERT_TRUE(dev_type == sycl::info::device_type::host);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("accelerator"));
ASSERT_TRUE(dev_type == sycl::info::device_type::accelerator);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_StrToDeviceType("custom"));
ASSERT_TRUE(dev_type == sycl::info::device_type::custom);

EXPECT_THROW(DPCTL_StrToDeviceType("invalid"), std::runtime_error);
}

TEST_F(TestHelperFns, ChkDPCTLBackendTypeToSyclBackend)
{
sycl::backend res = sycl::backend::level_zero;

EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
DPCTLSyclBackendType::DPCTL_CUDA));
ASSERT_TRUE(res == sycl::backend::cuda);

EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
DPCTLSyclBackendType::DPCTL_HOST));
ASSERT_TRUE(res == sycl::backend::host);

EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
DPCTLSyclBackendType::DPCTL_OPENCL));
ASSERT_TRUE(res == sycl::backend::opencl);

EXPECT_NO_FATAL_FAILURE(res = DPCTL_DPCTLBackendTypeToSyclBackend(
DPCTLSyclBackendType::DPCTL_LEVEL_ZERO));
ASSERT_TRUE(res == sycl::backend::level_zero);

EXPECT_THROW(DPCTL_DPCTLBackendTypeToSyclBackend(
DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND),
std::runtime_error);
}

TEST_F(TestHelperFns, ChkSyclBackendToDPCTLBackendType)
{
DPCTLSyclBackendType DTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND;

EXPECT_NO_FATAL_FAILURE(
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::level_zero));
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_LEVEL_ZERO);

EXPECT_NO_FATAL_FAILURE(
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::opencl));
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_OPENCL);

EXPECT_NO_FATAL_FAILURE(
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::host));
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_HOST);

EXPECT_NO_FATAL_FAILURE(
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::cuda));
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_CUDA);

EXPECT_NO_FATAL_FAILURE(
DTy = DPCTL_SyclBackendToDPCTLBackendType(sycl::backend::all));
ASSERT_TRUE(DTy == DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND);
}

TEST_F(TestHelperFns, ChkDPCTLDeviceTypeToSyclDeviceType)
{
sycl::info::device_type dev_type = sycl::info::device_type::automatic;

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_CPU));
ASSERT_TRUE(dev_type == sycl::info::device_type::cpu);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_GPU));
ASSERT_TRUE(dev_type == sycl::info::device_type::gpu);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_ACCELERATOR));
ASSERT_TRUE(dev_type == sycl::info::device_type::accelerator);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_CUSTOM));
ASSERT_TRUE(dev_type == sycl::info::device_type::custom);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_HOST_DEVICE));
ASSERT_TRUE(dev_type == sycl::info::device_type::host);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_AUTOMATIC));
ASSERT_TRUE(dev_type == sycl::info::device_type::automatic);

EXPECT_NO_FATAL_FAILURE(dev_type = DPCTL_DPCTLDeviceTypeToSyclDeviceType(
DPCTLSyclDeviceType::DPCTL_ALL));
ASSERT_TRUE(dev_type == sycl::info::device_type::all);
}

TEST_F(TestHelperFns, SyclDeviceTypeToDPCTLDeviceType)
{
DPCTLSyclDeviceType DTy = DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE;

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::cpu));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_CPU);

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::gpu));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_GPU);

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::host));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_HOST_DEVICE);

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::accelerator));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_ACCELERATOR);

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::automatic));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_AUTOMATIC);

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::all));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_ALL);

EXPECT_NO_FATAL_FAILURE(DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(
sycl::info::device_type::custom));
ASSERT_TRUE(DTy == DPCTLSyclDeviceType::DPCTL_CUSTOM);
}
79 changes: 79 additions & 0 deletions libsyclinterface/tests/test_sycl_context_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,82 @@ INSTANTIATE_TEST_SUITE_P(DPCTLContextTests,
"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, ChkIsHost)
{
bool is_host = true;
EXPECT_NO_FATAL_FAILURE(is_host = DPCTLContext_IsHost(Null_CRef));
ASSERT_FALSE(is_host);
}

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));
}
Loading