From 88fd5fc75b5682b0c6edd10f53e9523a0fed7df9 Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Sat, 27 Feb 2021 17:06:40 -0600 Subject: [PATCH] Add new getter functions to device interface. - add get_backend, get_platform, get_device_type to SyclDevice - parameterize the Gtests for dpctl_sycl_devie_interface --- .../include/dpctl_sycl_device_interface.h | 42 +- .../source/dpctl_sycl_device_interface.cpp | 45 ++ .../tests/test_sycl_device_interface.cpp | 488 +++++++----------- dpctl/_backend.pxd | 47 +- dpctl/_sycl_device.pxd | 4 +- dpctl/_sycl_device.pyx | 75 ++- dpctl/tests/test_sycl_device.py | 2 +- 7 files changed, 378 insertions(+), 325 deletions(-) diff --git a/dpctl-capi/include/dpctl_sycl_device_interface.h b/dpctl-capi/include/dpctl_sycl_device_interface.h index 79d380d9eb..f5a55507aa 100644 --- a/dpctl-capi/include/dpctl_sycl_device_interface.h +++ b/dpctl-capi/include/dpctl_sycl_device_interface.h @@ -124,6 +124,28 @@ bool DPCTLDevice_IsGPU(__dpctl_keep const DPCTLSyclDeviceRef DRef); DPCTL_API bool DPCTLDevice_IsHost(__dpctl_keep const DPCTLSyclDeviceRef DRef); +/*! + * @brief Returns the backend for the device. + * + * @param DRef Opaque pointer to a sycl::device + * @return A DPCTLSyclBackendType enum value representing the sycl::backend + * for the device. + */ +DPCTL_API +DPCTLSyclBackendType +DPCTLDevice_GetBackend(__dpctl_keep const DPCTLSyclDeviceRef DRef); + +/*! + * @brief Returns the DPCTLSyclDeviceType enum value for the DPCTLSyclDeviceRef + * argument. + * + * @param DRef Opaque pointer to a sycl::device + * @return The DPCTLSyclDeviceType value corresponding to the device. + */ +DPCTL_API +DPCTLSyclDeviceType +DPCTLDevice_GetDeviceType(__dpctl_keep const DPCTLSyclDeviceRef DRef); + /*! * @brief Returns the OpenCL software driver version as a C string. * @@ -185,6 +207,17 @@ DPCTL_API uint32_t DPCTLDevice_GetMaxNumSubGroups(__dpctl_keep const DPCTLSyclDeviceRef DRef); +/*! + * @brief Returns the sycl::platform for the device as DPCTLSyclPlatformRef + * opaque pointer. + * + * @param DRef Opaque pointer to a sycl::device + * @return An opaque pointer to the sycl::platform for the device. + */ +DPCTL_API +__dpctl_give DPCTLSyclPlatformRef +DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef); + /*! * @brief Wrapper over * device.get_info. @@ -244,11 +277,12 @@ bool DPCTLDevice_IsHostUnifiedMemory( * @brief Checks if two DPCTLSyclDeviceRef objects point to the same * sycl::device. * - * @param DevRef1 First opaque pointer to the sycl device. - * @param DevRef2 Second opaque pointer to the sycl device. + * @param DRef1 First opaque pointer to a sycl device. + * @param DRef2 Second opaque pointer to a sycl device. * @return True if the underlying sycl::device are same, false otherwise. */ DPCTL_API -bool DPCTLDevice_AreEq(__dpctl_keep const DPCTLSyclDeviceRef DevRef1, - __dpctl_keep const DPCTLSyclDeviceRef DevRef2); +bool DPCTLDevice_AreEq(__dpctl_keep const DPCTLSyclDeviceRef DRef1, + __dpctl_keep const DPCTLSyclDeviceRef DRef2); + DPCTL_C_EXTERN_C_END diff --git a/dpctl-capi/source/dpctl_sycl_device_interface.cpp b/dpctl-capi/source/dpctl_sycl_device_interface.cpp index d534dd8d7a..28b033114c 100644 --- a/dpctl-capi/source/dpctl_sycl_device_interface.cpp +++ b/dpctl-capi/source/dpctl_sycl_device_interface.cpp @@ -39,6 +39,7 @@ namespace // Create wrappers for C Binding types (see CBindingWrapping.h). DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPCTLSyclDeviceRef) DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device_selector, DPCTLSyclDeviceSelectorRef) +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(platform, DPCTLSyclPlatformRef) /*! * @brief Helper function to print the metadata for a sycl::device. @@ -139,6 +140,23 @@ void DPCTLDevice_Delete(__dpctl_take DPCTLSyclDeviceRef DRef) delete unwrap(DRef); } +DPCTLSyclDeviceType +DPCTLDevice_GetDeviceType(__dpctl_keep const DPCTLSyclDeviceRef DRef) +{ + DPCTLSyclDeviceType DTy = DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE; + auto D = unwrap(DRef); + if (D) { + try { + auto SyclDTy = D->get_info(); + DTy = DPCTL_SyclDeviceTypeToDPCTLDeviceType(SyclDTy); + } catch (runtime_error const &re) { + // \todo log error + std::cerr << re.what() << '\n'; + } + } + return DTy; +} + bool DPCTLDevice_IsAccelerator(__dpctl_keep const DPCTLSyclDeviceRef DRef) { auto D = unwrap(DRef); @@ -175,6 +193,18 @@ bool DPCTLDevice_IsHost(__dpctl_keep const DPCTLSyclDeviceRef DRef) return false; } +DPCTLSyclBackendType +DPCTLDevice_GetBackend(__dpctl_keep const DPCTLSyclDeviceRef DRef) +{ + DPCTLSyclBackendType BTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND; + auto D = unwrap(DRef); + if (D) { + BTy = DPCTL_SyclBackendToDPCTLBackendType( + D->get_platform().get_backend()); + } + return BTy; +} + uint32_t DPCTLDevice_GetMaxComputeUnits(__dpctl_keep const DPCTLSyclDeviceRef DRef) { @@ -263,6 +293,21 @@ DPCTLDevice_GetMaxNumSubGroups(__dpctl_keep const DPCTLSyclDeviceRef DRef) return max_nsubgroups; } +__dpctl_give DPCTLSyclPlatformRef +DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef) +{ + DPCTLSyclPlatformRef PRef = nullptr; + auto D = unwrap(DRef); + if (D) { + try { + PRef = wrap(new platform(D->get_platform())); + } catch (std::bad_alloc const &ba) { + std::cerr << ba.what() << '\n'; + } + } + return PRef; +} + bool DPCTLDevice_HasInt64BaseAtomics(__dpctl_keep const DPCTLSyclDeviceRef DRef) { bool hasBaseAtomics = false; diff --git a/dpctl-capi/tests/test_sycl_device_interface.cpp b/dpctl-capi/tests/test_sycl_device_interface.cpp index 65032446ba..b913317d84 100644 --- a/dpctl-capi/tests/test_sycl_device_interface.cpp +++ b/dpctl-capi/tests/test_sycl_device_interface.cpp @@ -25,6 +25,8 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_device_interface.h" +#include "dpctl_sycl_device_selector_interface.h" +#include "dpctl_sycl_platform_interface.h" #include "dpctl_sycl_queue_interface.h" #include "dpctl_sycl_queue_manager.h" #include "dpctl_utils.h" @@ -33,361 +35,271 @@ using namespace cl::sycl; -struct TestDPCTLSyclDeviceInterface : public ::testing::Test +struct TestDPCTLSyclDeviceInterface + : public ::testing::TestWithParam { - DPCTLSyclDeviceRef OpenCL_cpu = nullptr; - DPCTLSyclDeviceRef OpenCL_gpu = nullptr; - DPCTLSyclDeviceRef OpenCL_Level0_gpu = nullptr; + DPCTLSyclDeviceSelectorRef DSRef = nullptr; TestDPCTLSyclDeviceInterface() { - if (DPCTLQueueMgr_GetNumQueues(DPCTL_OPENCL, DPCTL_CPU)) { - auto Q = DPCTLQueueMgr_GetQueue(DPCTL_OPENCL, DPCTL_CPU, 0); - OpenCL_cpu = DPCTLQueue_GetDevice(Q); - DPCTLQueue_Delete(Q); - } - - if (DPCTLQueueMgr_GetNumQueues(DPCTL_OPENCL, DPCTL_GPU)) { - auto Q = DPCTLQueueMgr_GetQueue(DPCTL_OPENCL, DPCTL_GPU, 0); - OpenCL_gpu = DPCTLQueue_GetDevice(Q); - DPCTLQueue_Delete(Q); - } + EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(GetParam())); + } - if (DPCTLQueueMgr_GetNumQueues(DPCTL_LEVEL_ZERO, DPCTL_GPU)) { - auto Q = DPCTLQueueMgr_GetQueue(DPCTL_LEVEL_ZERO, DPCTL_GPU, 0); - OpenCL_Level0_gpu = DPCTLQueue_GetDevice(Q); - DPCTLQueue_Delete(Q); + void SetUp() + { + if (!DSRef) { + auto message = "Skipping as no device of type " + + std::string(GetParam()) + "."; + GTEST_SKIP_(message.c_str()); } } ~TestDPCTLSyclDeviceInterface() { - DPCTLDevice_Delete(OpenCL_cpu); - DPCTLDevice_Delete(OpenCL_gpu); - DPCTLDevice_Delete(OpenCL_Level0_gpu); + EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef)); } }; -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetDriverInfo) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto DriverInfo = DPCTLDevice_GetDriverInfo(OpenCL_cpu); - EXPECT_TRUE(DriverInfo != nullptr); - DPCTLCString_Delete(DriverInfo); +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetBackend) +{ + DPCTLSyclDeviceRef DRef = nullptr; + DPCTLSyclBackendType BTy = DPCTLSyclBackendType::DPCTL_UNKNOWN_BACKEND; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(BTy = DPCTLDevice_GetBackend(DRef)); + EXPECT_TRUE([BTy] { + switch (BTy) { + case DPCTLSyclBackendType::DPCTL_CUDA: + return true; + case DPCTLSyclBackendType::DPCTL_HOST: + return true; + case DPCTLSyclBackendType::DPCTL_LEVEL_ZERO: + return true; + case DPCTLSyclBackendType::DPCTL_OPENCL: + return true; + default: + return false; + } + }()); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetDriverInfo) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetDeviceType) { - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto DriverInfo = DPCTLDevice_GetDriverInfo(OpenCL_gpu); - EXPECT_TRUE(DriverInfo != nullptr); - DPCTLCString_Delete(DriverInfo); + DPCTLSyclDeviceRef DRef = nullptr; + DPCTLSyclDeviceType DTy = DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(DTy = DPCTLDevice_GetDeviceType(DRef)); + EXPECT_TRUE(DTy != DPCTLSyclDeviceType::DPCTL_UNKNOWN_DEVICE); + EXPECT_TRUE(DTy != DPCTLSyclDeviceType::DPCTL_ALL); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetDriverInfo) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetDriverInfo) { - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto DriverInfo = DPCTLDevice_GetDriverInfo(OpenCL_Level0_gpu); + DPCTLSyclDeviceRef DRef = nullptr; + const char *DriverInfo = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(DriverInfo = DPCTLDevice_GetDriverInfo(DRef)); EXPECT_TRUE(DriverInfo != nullptr); - DPCTLCString_Delete(DriverInfo); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetMaxComputeUnits) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto n = DPCTLDevice_GetMaxComputeUnits(OpenCL_cpu); - EXPECT_TRUE(n > 0); + EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(DriverInfo)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetMaxComputeUnits) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetName) { - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto n = DPCTLDevice_GetMaxComputeUnits(OpenCL_gpu); - EXPECT_TRUE(n > 0); + DPCTLSyclDeviceRef DRef = nullptr; + const char *Name = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(Name = DPCTLDevice_GetName(DRef)); + EXPECT_TRUE(Name != nullptr); + EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(Name)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetMaxComputeUnits) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetVendorName) { - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto n = DPCTLDevice_GetMaxComputeUnits(OpenCL_Level0_gpu); - EXPECT_TRUE(n > 0); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetMaxWorkItemDims) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto n = DPCTLDevice_GetMaxWorkItemDims(OpenCL_cpu); - EXPECT_TRUE(n > 0); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetMaxWorkItemDims) -{ - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto n = DPCTLDevice_GetMaxWorkItemDims(OpenCL_gpu); - EXPECT_TRUE(n > 0); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetMaxWorkItemDims) -{ - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto n = DPCTLDevice_GetMaxWorkItemDims(OpenCL_Level0_gpu); - EXPECT_TRUE(n > 0); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetMaxWorkItemSizes) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto item_sizes = DPCTLDevice_GetMaxWorkItemSizes(OpenCL_cpu); - EXPECT_TRUE(item_sizes != nullptr); - DPCTLSize_t_Array_Delete(item_sizes); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetMaxWorkItemSizes) -{ - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto item_sizes = DPCTLDevice_GetMaxWorkItemSizes(OpenCL_gpu); - EXPECT_TRUE(item_sizes != nullptr); - DPCTLSize_t_Array_Delete(item_sizes); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetMaxWorkItemSizes) -{ - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto item_sizes = DPCTLDevice_GetMaxWorkItemSizes(OpenCL_Level0_gpu); - EXPECT_TRUE(item_sizes != nullptr); - DPCTLSize_t_Array_Delete(item_sizes); + DPCTLSyclDeviceRef DRef = nullptr; + const char *VendorName = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(VendorName = DPCTLDevice_GetVendorName(DRef)); + EXPECT_TRUE(VendorName != nullptr); + EXPECT_NO_FATAL_FAILURE(DPCTLCString_Delete(VendorName)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetMaxWorkGroupSize) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetMaxComputeUnits) { - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto n = DPCTLDevice_GetMaxWorkGroupSize(OpenCL_cpu); + DPCTLSyclDeviceRef DRef = nullptr; + size_t n = 0; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(n = DPCTLDevice_GetMaxComputeUnits(DRef)); EXPECT_TRUE(n > 0); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetMaxWorkGroupSize) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetMaxWorkItemDims) { - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto n = DPCTLDevice_GetMaxWorkGroupSize(OpenCL_gpu); + DPCTLSyclDeviceRef DRef = nullptr; + size_t n = 0; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(n = DPCTLDevice_GetMaxWorkItemDims(DRef)); EXPECT_TRUE(n > 0); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetMaxWorkGroupSize) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetMaxWorkItemSizes) { - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto n = DPCTLDevice_GetMaxWorkGroupSize(OpenCL_Level0_gpu); - EXPECT_TRUE(n > 0); + DPCTLSyclDeviceRef DRef = nullptr; + size_t *sizes = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(sizes = DPCTLDevice_GetMaxWorkItemSizes(DRef)); + EXPECT_TRUE(sizes != nullptr); + EXPECT_NO_FATAL_FAILURE(DPCTLSize_t_Array_Delete(sizes)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetMaxNumSubGroups) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetMaxWorkGroupSize) { - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto n = DPCTLDevice_GetMaxNumSubGroups(OpenCL_cpu); - EXPECT_TRUE(n > 0); + DPCTLSyclDeviceRef DRef = nullptr; + size_t n = 0; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(n = DPCTLDevice_GetMaxWorkGroupSize(DRef)); + if (DPCTLDevice_IsAccelerator(DRef)) + EXPECT_TRUE(n >= 0); + else + EXPECT_TRUE(n > 0); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetMaxNumSubGroups) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetMaxNumSubGroups) { - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto n = DPCTLDevice_GetMaxNumSubGroups(OpenCL_gpu); - EXPECT_TRUE(n > 0); + DPCTLSyclDeviceRef DRef = nullptr; + size_t n = 0; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(n = DPCTLDevice_GetMaxNumSubGroups(DRef)); + if (DPCTLDevice_IsAccelerator(DRef)) + EXPECT_TRUE(n >= 0); + else + EXPECT_TRUE(n > 0); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetMaxNumSubGroups) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_GetPlatform) { - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto n = DPCTLDevice_GetMaxNumSubGroups(OpenCL_Level0_gpu); - EXPECT_TRUE(n > 0); + DPCTLSyclDeviceRef DRef = nullptr; + DPCTLSyclPlatformRef PRef = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(PRef = DPCTLDevice_GetPlatform(DRef)); + ASSERT_TRUE(PRef); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); + EXPECT_NO_FATAL_FAILURE(DPCTLPlatform_Delete(PRef)); } // TODO: Update when DPC++ properly supports aspects -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_HasInt64BaseAtomics) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto atomics = DPCTLDevice_HasInt64BaseAtomics(OpenCL_cpu); - auto D = reinterpret_cast(OpenCL_cpu); +TEST_P(TestDPCTLSyclDeviceInterface, Chk_HasInt64BaseAtomics) +{ + DPCTLSyclDeviceRef DRef = nullptr; + bool atomics = 0; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(atomics = DPCTLDevice_HasInt64BaseAtomics(DRef)); + auto D = reinterpret_cast(DRef); auto has_atomics = D->has(aspect::int64_base_atomics); EXPECT_TRUE(has_atomics == atomics); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } // TODO: Update when DPC++ properly supports aspects -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_HasInt64BaseAtomics) -{ - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto atomics = DPCTLDevice_HasInt64BaseAtomics(OpenCL_gpu); - auto D = reinterpret_cast(OpenCL_gpu); - auto has_atomics = D->has(aspect::int64_base_atomics); - EXPECT_TRUE(has_atomics == atomics); -} - -// TODO: Update when DPC++ properly supports aspects -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_HasInt64BaseAtomics) -{ - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto atomics = DPCTLDevice_HasInt64BaseAtomics(OpenCL_Level0_gpu); - auto D = reinterpret_cast(OpenCL_Level0_gpu); - auto has_atomics = D->has(aspect::int64_base_atomics); - EXPECT_TRUE(has_atomics == atomics); -} - -// TODO: Update when DPC++ properly supports aspects -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_HasInt64ExtendedAtomics) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto atomics = DPCTLDevice_HasInt64ExtendedAtomics(OpenCL_cpu); - auto D = reinterpret_cast(OpenCL_cpu); - auto has_atomics = D->has(aspect::int64_extended_atomics); - EXPECT_TRUE(has_atomics == atomics); -} - -// TODO: Update when DPC++ properly supports aspects -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_HasInt64ExtendedAtomics) -{ - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL GPU device found."); - - auto atomics = DPCTLDevice_HasInt64ExtendedAtomics(OpenCL_gpu); - auto D = reinterpret_cast(OpenCL_gpu); - auto has_atomics = D->has(aspect::int64_extended_atomics); - EXPECT_TRUE(has_atomics == atomics); -} - -// TODO: Update when DPC++ properly supports aspects -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_HasInt64ExtendedAtomics) -{ - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto atomics = DPCTLDevice_HasInt64ExtendedAtomics(OpenCL_Level0_gpu); - auto D = reinterpret_cast(OpenCL_Level0_gpu); +TEST_P(TestDPCTLSyclDeviceInterface, Chk_HasInt64ExtendedAtomics) +{ + DPCTLSyclDeviceRef DRef = nullptr; + bool atomics = 0; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(atomics = + DPCTLDevice_HasInt64ExtendedAtomics(DRef)); + auto D = reinterpret_cast(DRef); auto has_atomics = D->has(aspect::int64_extended_atomics); EXPECT_TRUE(has_atomics == atomics); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetName) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_IsAccelerator) { - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto DevName = DPCTLDevice_GetName(OpenCL_cpu); - EXPECT_TRUE(DevName != nullptr); - DPCTLCString_Delete(DevName); + DPCTLSyclDeviceRef DRef = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_IsAccelerator(DRef)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetName) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_IsCPU) { - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto DevName = DPCTLDevice_GetName(OpenCL_gpu); - EXPECT_TRUE(DevName != nullptr); - DPCTLCString_Delete(DevName); + DPCTLSyclDeviceRef DRef = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_IsCPU(DRef)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetName) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_IsGPU) { - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto DevName = DPCTLDevice_GetName(OpenCL_Level0_gpu); - EXPECT_TRUE(DevName != nullptr); - DPCTLCString_Delete(DevName); + DPCTLSyclDeviceRef DRef = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_IsGPU(DRef)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_GetVendorName) +TEST_P(TestDPCTLSyclDeviceInterface, Chk_IsHost) { - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto VendorName = DPCTLDevice_GetVendorName(OpenCL_cpu); - EXPECT_TRUE(VendorName != nullptr); - DPCTLCString_Delete(VendorName); + DPCTLSyclDeviceRef DRef = nullptr; + EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDevice_CreateFromSelector(DSRef)); + if (!DRef) + GTEST_SKIP_("Device not found"); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_IsHost(DRef)); + EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef)); } -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_GetVendorName) -{ - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - auto VendorName = DPCTLDevice_GetVendorName(OpenCL_gpu); - EXPECT_TRUE(VendorName != nullptr); - DPCTLCString_Delete(VendorName); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_GetVendorName) -{ - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - auto VendorName = DPCTLDevice_GetVendorName(OpenCL_Level0_gpu); - EXPECT_TRUE(VendorName != nullptr); - DPCTLCString_Delete(VendorName); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLCPU_IsCPU) -{ - if (!OpenCL_cpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - EXPECT_TRUE(DPCTLDevice_IsCPU(OpenCL_cpu)); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckOCLGPU_IsGPU) -{ - if (!OpenCL_gpu) - GTEST_SKIP_("Skipping as no OpenCL CPU device found."); - - EXPECT_TRUE(DPCTLDevice_IsGPU(OpenCL_gpu)); -} - -TEST_F(TestDPCTLSyclDeviceInterface, CheckLevel0GPU_IsGPU) -{ - if (!OpenCL_Level0_gpu) - GTEST_SKIP_("Skipping as no Level0 GPU device found."); - - EXPECT_TRUE(DPCTLDevice_IsGPU(OpenCL_Level0_gpu)); -} +INSTANTIATE_TEST_SUITE_P(DPCTLDevice_Fns, + TestDPCTLSyclDeviceInterface, + ::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")); diff --git a/dpctl/_backend.pxd b/dpctl/_backend.pxd index a9c95dec53..4e28a9968b 100644 --- a/dpctl/_backend.pxd +++ b/dpctl/_backend.pxd @@ -32,19 +32,24 @@ cdef extern from "dpctl_utils.h": cdef extern from "dpctl_sycl_enum_types.h": cdef enum _backend_type 'DPCTLSyclBackendType': - _OPENCL 'DPCTL_OPENCL' + _ALL_BACKENDS 'DPCTL_ALL_BACKENDS' + _CUDA 'DPCTL_CUDA' _HOST 'DPCTL_HOST' _LEVEL_ZERO 'DPCTL_LEVEL_ZERO' - _CUDA 'DPCTL_CUDA' + _OPENCL 'DPCTL_OPENCL' _UNKNOWN_BACKEND 'DPCTL_UNKNOWN_BACKEND' ctypedef _backend_type DPCTLSyclBackendType cdef enum _device_type 'DPCTLSyclDeviceType': - _GPU 'DPCTL_GPU' - _CPU 'DPCTL_CPU' - _ACCELERATOR 'DPCTL_ACCELERATOR' - _HOST_DEVICE 'DPCTL_HOST_DEVICE' + _ACCELERATOR 'DPCTL_ACCELERATOR' + _ALL_DEVICES 'DPCTL_ALL' + _AUTOMATIC 'DPCTL_AUTOMATIC' + _CPU 'DPCTL_CPU' + _CUSTOM 'DPCTL_CUSTOM' + _GPU 'DPCTL_GPU' + _HOST_DEVICE 'DPCTL_HOST_DEVICE' + _UNKNOWN_DEVICE 'DPCTL_UNKNOWN_DEVICE' ctypedef _device_type DPCTLSyclDeviceType @@ -92,29 +97,35 @@ cdef extern from "dpctl_sycl_types.h": cdef extern from "dpctl_sycl_device_interface.h": + cdef bool DPCTLDevice_AreEq(const DPCTLSyclDeviceRef DRef1, + const DPCTLSyclDeviceRef DRef2) cdef DPCTLSyclDeviceRef DPCTLDevice_Copy(const DPCTLSyclDeviceRef DRef) cdef DPCTLSyclDeviceRef DPCTLDevice_Create() cdef DPCTLSyclDeviceRef DPCTLDevice_CreateFromSelector( const DPCTLSyclDeviceSelectorRef DSRef) cdef void DPCTLDevice_DumpInfo(const DPCTLSyclDeviceRef DRef) cdef void DPCTLDevice_Delete(DPCTLSyclDeviceRef DRef) - cdef void DPCTLDevice_DumpInfo(const DPCTLSyclDeviceRef DRef) + cdef DPCTLSyclBackendType DPCTLDevice_GetBackend( + const DPCTLSyclDeviceRef DRef) + cdef DPCTLSyclDeviceType DPCTLDevice_GetDeviceType( + const DPCTLSyclDeviceRef DRef) + cdef const char *DPCTLDevice_GetDriverInfo(const DPCTLSyclDeviceRef DRef) + cdef uint32_t DPCTLDevice_GetMaxComputeUnits(const DPCTLSyclDeviceRef DRef) + cdef uint32_t DPCTLDevice_GetMaxNumSubGroups(const DPCTLSyclDeviceRef DRef) + cdef size_t DPCTLDevice_GetMaxWorkGroupSize(const DPCTLSyclDeviceRef DRef) + cdef uint32_t DPCTLDevice_GetMaxWorkItemDims(const DPCTLSyclDeviceRef DRef) + cdef size_t *DPCTLDevice_GetMaxWorkItemSizes(const DPCTLSyclDeviceRef DRef) + cdef const char *DPCTLDevice_GetName(const DPCTLSyclDeviceRef DRef) + cdef DPCTLSyclPlatformRef DPCTLDevice_GetPlatform( + const DPCTLSyclDeviceRef DRef) + cdef const char *DPCTLDevice_GetVendorName(const DPCTLSyclDeviceRef DRef) + cdef bool DPCTLDevice_HasInt64BaseAtomics(const DPCTLSyclDeviceRef DRef) + cdef bool DPCTLDevice_HasInt64ExtendedAtomics(const DPCTLSyclDeviceRef DRef) cdef bool DPCTLDevice_IsAccelerator(const DPCTLSyclDeviceRef DRef) cdef bool DPCTLDevice_IsCPU(const DPCTLSyclDeviceRef DRef) cdef bool DPCTLDevice_IsGPU(const DPCTLSyclDeviceRef DRef) cdef bool DPCTLDevice_IsHost(const DPCTLSyclDeviceRef DRef) - cpdef const char *DPCTLDevice_GetDriverInfo(const DPCTLSyclDeviceRef DRef) - cpdef const char *DPCTLDevice_GetName(const DPCTLSyclDeviceRef DRef) - cpdef const char *DPCTLDevice_GetVendorName(const DPCTLSyclDeviceRef DRef) cdef bool DPCTLDevice_IsHostUnifiedMemory(const DPCTLSyclDeviceRef DRef) - cpdef uint32_t DPCTLDevice_GetMaxComputeUnits(const DPCTLSyclDeviceRef DRef) - cpdef uint32_t DPCTLDevice_GetMaxWorkItemDims(const DPCTLSyclDeviceRef DRef) - cpdef size_t *DPCTLDevice_GetMaxWorkItemSizes(const DPCTLSyclDeviceRef DRef) - cpdef size_t DPCTLDevice_GetMaxWorkGroupSize(const DPCTLSyclDeviceRef DRef) - cpdef uint32_t DPCTLDevice_GetMaxNumSubGroups(const DPCTLSyclDeviceRef DRef) - cpdef bool DPCTLDevice_HasInt64BaseAtomics(const DPCTLSyclDeviceRef DRef) - cpdef bool DPCTLDevice_HasInt64ExtendedAtomics( - const DPCTLSyclDeviceRef DRef) cdef extern from "dpctl_sycl_device_selector_interface.h": diff --git a/dpctl/_sycl_device.pxd b/dpctl/_sycl_device.pxd index 207ba0a8d4..2e0c76a296 100644 --- a/dpctl/_sycl_device.pxd +++ b/dpctl/_sycl_device.pxd @@ -46,9 +46,8 @@ cdef class _SyclDevice: cdef uint32_t _max_num_sub_groups cdef bool _int64_base_atomics cdef bool _int64_extended_atomics - - cdef DPCTLSyclDeviceRef get_device_ref(self) + cpdef get_backend(self) cpdef get_device_name(self) cpdef get_device_type(self) cpdef get_vendor_name(self) @@ -74,6 +73,7 @@ cdef class SyclDevice(_SyclDevice): cdef void _init_from__SyclDevice(self, _SyclDevice other) cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef) + cpdef select_accelerator_device() cpdef select_cpu_device() cpdef select_default_device() diff --git a/dpctl/_sycl_device.pyx b/dpctl/_sycl_device.pyx index 1aed50e76a..e8ea50bfc5 100644 --- a/dpctl/_sycl_device.pyx +++ b/dpctl/_sycl_device.pyx @@ -21,37 +21,44 @@ """ from ._backend cimport ( + _backend_type, + _device_type, DPCTLAcceleratorSelector_Create, DPCTLCPUSelector_Create, DPCTLDefaultSelector_Create, - DPCTLGPUSelector_Create, - DPCTLHostSelector_Create, DPCTLCString_Delete, DPCTLDevice_Copy, DPCTLDevice_CreateFromSelector, DPCTLDevice_Delete, DPCTLDevice_DumpInfo, - DPCTLDevice_GetVendorName, - DPCTLDevice_GetName, + DPCTLDevice_GetBackend, + DPCTLDevice_GetDeviceType, DPCTLDevice_GetDriverInfo, DPCTLDevice_GetMaxComputeUnits, + DPCTLDevice_GetMaxNumSubGroups, + DPCTLDevice_GetMaxWorkGroupSize, DPCTLDevice_GetMaxWorkItemDims, DPCTLDevice_GetMaxWorkItemSizes, - DPCTLDevice_GetMaxWorkGroupSize, - DPCTLDevice_GetMaxNumSubGroups, + DPCTLDevice_GetVendorName, + DPCTLDevice_GetName, DPCTLDevice_HasInt64BaseAtomics, DPCTLDevice_HasInt64ExtendedAtomics, DPCTLDevice_IsAccelerator, DPCTLDevice_IsCPU, DPCTLDevice_IsGPU, DPCTLDevice_IsHost, - DPCTLFilterSelector_Create, DPCTLDeviceSelector_Delete, + DPCTLFilterSelector_Create, + DPCTLGPUSelector_Create, + DPCTLHostSelector_Create, DPCTLSize_t_Array_Delete, + DPCTLSyclBackendType, DPCTLSyclDeviceRef, DPCTLSyclDeviceSelectorRef, + DPCTLSyclDeviceType, ) -from . import device_type +from . import backend_type, device_type +import warnings __all__ = [ "SyclDevice", @@ -77,20 +84,64 @@ cdef class _SyclDevice: def dump_device_info(self): """ Print information about the SYCL device. """ + warnings.warn( + "WARNING: dump_device_info is depracated and will be removed in " + "a future release of dpctl. Use print_device_info instead." + ) DPCTLDevice_DumpInfo(self._device_ref) + + def print_device_info(self): + """ Print information about the SYCL device. + """ + DPCTLDevice_DumpInfo(self._device_ref) + + cpdef get_backend(self): + """Returns the backend_type enum value for this device + + Returns: + backend_type: The backend for the device. + """ + cdef DPCTLSyclBackendType BTy = ( + DPCTLDevice_GetBackend(self._device_ref) + ) + if BTy == _backend_type._CUDA: + return backend_type.cuda + elif BTy == _backend_type._HOST: + return backend_type.host + elif BTy == _backend_type._LEVEL_ZERO: + return backend_type.level_zero + elif BTy == _backend_type._OPENCL: + return backend_type.opencl + else: + raise ValueError("Unknown backend type.") + cpdef get_device_name(self): """ Returns the name of the device as a string """ return self._device_name.decode() cpdef get_device_type(self): - """ Returns the type of the device as a `device_type` enum + """ Returns the type of the device as a `device_type` enum. + + Returns: + device_type: The type of device encoded as a device_type enum. + Raises: + A ValueError is raised if the device type is not recognized. """ - if DPCTLDevice_IsGPU(self._device_ref): - return device_type.gpu - elif DPCTLDevice_IsCPU(self._device_ref): + cdef DPCTLSyclDeviceType DTy = ( + DPCTLDevice_GetDeviceType(self._device_ref) + ) + if DTy == _device_type._ACCELERATOR: + return device_type.accelerator + elif DTy == _device_type._AUTOMATIC: + return device_type.automatic + elif DTy == _device_type._CPU: return device_type.cpu + elif DTy == _device_type._GPU: + return device_type.gpu + elif DTy == _device_type._HOST_DEVICE: + return device_type.host_device else: raise ValueError("Unknown device type.") diff --git a/dpctl/tests/test_sycl_device.py b/dpctl/tests/test_sycl_device.py index 23f79d222b..7df90c6df3 100644 --- a/dpctl/tests/test_sycl_device.py +++ b/dpctl/tests/test_sycl_device.py @@ -80,7 +80,7 @@ def check_get_max_work_group_size(device): def check_get_max_num_sub_groups(device): max_num_sub_groups = device.get_max_num_sub_groups() # Special case for FPGA simulator - if device.is_accelerator(): + if device.is_accelerator() or device.is_host(): assert max_num_sub_groups >= 0 else: assert max_num_sub_groups > 0