Skip to content

Feature/get devices ordering #451

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 4 commits into from
May 19, 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
24 changes: 12 additions & 12 deletions dpctl-capi/include/dpctl_sycl_enum_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ DPCTL_C_EXTERN_C_BEGIN
enum DPCTLSyclBackendType
{
// clang-format off
DPCTL_CUDA = 1 << 13,
DPCTL_HOST = 1 << 14,
DPCTL_LEVEL_ZERO = 1 << 15,
DPCTL_OPENCL = 1 << 16,
DPCTL_CUDA = 1 << 16,
DPCTL_HOST = 1 << 17,
DPCTL_LEVEL_ZERO = 1 << 18,
DPCTL_OPENCL = 1 << 19,
DPCTL_UNKNOWN_BACKEND = 0,
DPCTL_ALL_BACKENDS = ((1<<10)-1) << 7
DPCTL_ALL_BACKENDS = ((1<<5)-1) << 16
// clang-format on
};

Expand All @@ -57,13 +57,13 @@ enum DPCTLSyclDeviceType
// The values should not overlap.

// clang-format off
DPCTL_ACCELERATOR = 1 << 1,
DPCTL_AUTOMATIC = 1 << 2,
DPCTL_CPU = 1 << 3,
DPCTL_CUSTOM = 1 << 4,
DPCTL_GPU = 1 << 5,
DPCTL_HOST_DEVICE = 1 << 6,
DPCTL_ALL = (1 << 7) -1 ,
DPCTL_ACCELERATOR = 1 << 0,
DPCTL_AUTOMATIC = 1 << 1,
DPCTL_CPU = 1 << 2,
DPCTL_CUSTOM = 1 << 3,
DPCTL_GPU = 1 << 4,
DPCTL_HOST_DEVICE = 1 << 5,
DPCTL_ALL = (1 << 6) - 1,
DPCTL_UNKNOWN_DEVICE = 0
// clang-format on
};
Expand Down
13 changes: 8 additions & 5 deletions dpctl-capi/source/dpctl_sycl_device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,18 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
} catch (std::bad_alloc const &ba) {
return nullptr;
}
auto &cache = DeviceCacheBuilder::getDeviceCache();
const auto &root_devices = device::get_devices();
default_selector mRanker;

for (const auto &entry : cache) {
for (const auto &root_device : root_devices) {
if (mRanker(root_device) < 0)
continue;
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
entry.first.get_platform().get_backend()));
root_device.get_platform().get_backend()));
auto Dty(DPCTL_SyclDeviceTypeToDPCTLDeviceType(
entry.first.get_info<info::device::device_type>()));
root_device.get_info<info::device::device_type>()));
if ((device_identifier & Bty) && (device_identifier & Dty)) {
Devices->emplace_back(wrap(new device(entry.first)));
Devices->emplace_back(wrap(new device(root_device)));
}
}
// the wrap function is defined inside dpctl_vector_templ.cpp
Expand Down
78 changes: 72 additions & 6 deletions dpctl-capi/tests/test_sycl_device_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
///
//===----------------------------------------------------------------------===//

#include "../helper/include/dpctl_utils_helper.h"
#include "dpctl_sycl_device_interface.h"
#include "dpctl_sycl_device_manager.h"
#include "dpctl_sycl_device_selector_interface.h"
#include <gtest/gtest.h>
#include <string>

struct TestDPCTLDeviceManager : public ::testing::TestWithParam<const char *>
{
Expand Down Expand Up @@ -81,12 +83,12 @@ INSTANTIATE_TEST_SUITE_P(DeviceMgrFunctions,
"opencl:cpu:0",
"level_zero:gpu:0"));

struct TestDPCTLDeviceVector : public ::testing::TestWithParam<int>
struct TestDPCTLGetDevices : public ::testing::TestWithParam<int>
{
DPCTLDeviceVectorRef DV = nullptr;
size_t nDevices = 0;

TestDPCTLDeviceVector()
TestDPCTLGetDevices()
{
EXPECT_NO_FATAL_FAILURE(DV = DPCTLDeviceMgr_GetDevices(GetParam()));
EXPECT_TRUE(DV != nullptr);
Expand All @@ -100,14 +102,14 @@ struct TestDPCTLDeviceVector : public ::testing::TestWithParam<int>
}
}

~TestDPCTLDeviceVector()
~TestDPCTLGetDevices()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Clear(DV));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DV));
}
};

TEST_P(TestDPCTLDeviceVector, ChkGetAt)
TEST_P(TestDPCTLGetDevices, ChkGetAt)
{
for (auto i = 0ul; i < nDevices; ++i) {
DPCTLSyclDeviceRef DRef = nullptr;
Expand All @@ -118,14 +120,18 @@ TEST_P(TestDPCTLDeviceVector, ChkGetAt)

INSTANTIATE_TEST_SUITE_P(
GetDevices,
TestDPCTLDeviceVector,
TestDPCTLGetDevices,
::testing::Values(DPCTLSyclBackendType::DPCTL_HOST,
DPCTLSyclBackendType::DPCTL_LEVEL_ZERO,
DPCTLSyclBackendType::DPCTL_OPENCL,
DPCTLSyclBackendType::DPCTL_OPENCL |
DPCTLSyclDeviceType::DPCTL_GPU));

TEST(TestDPCTLDeviceVector, ChkDPCTLDeviceVectorCreate)
struct TestDPCTLDeviceVector : public ::testing::Test
{
};

TEST_F(TestDPCTLDeviceVector, ChkDPCTLDeviceVectorCreate)
{
DPCTLDeviceVectorRef DVRef = nullptr;
size_t nDevices = 0;
Expand All @@ -135,3 +141,63 @@ TEST(TestDPCTLDeviceVector, ChkDPCTLDeviceVectorCreate)
EXPECT_TRUE(nDevices == 0);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
}

struct TestDPCTLGetDevicesOrdering : public ::testing::TestWithParam<int>
{
DPCTLDeviceVectorRef DV = nullptr;
size_t nDevices = 0;

TestDPCTLGetDevicesOrdering()
{
const int device_type_mask =
(GetParam() & DPCTLSyclDeviceType::DPCTL_ALL) |
DPCTLSyclBackendType::DPCTL_ALL_BACKENDS;
EXPECT_NO_FATAL_FAILURE(
DV = DPCTLDeviceMgr_GetDevices(device_type_mask));
EXPECT_TRUE(DV != nullptr);
EXPECT_NO_FATAL_FAILURE(nDevices = DPCTLDeviceVector_Size(DV));
}

void SetUp()
{
if (!nDevices) {
GTEST_SKIP_("Skipping as no devices returned for identifier");
}
}

~TestDPCTLGetDevicesOrdering()
{
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Clear(DV));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DV));
}
};

TEST_P(TestDPCTLGetDevicesOrdering, ChkConsistencyWithFilterSelector)
{
for (auto i = 0ul; i < nDevices; ++i) {
DPCTLSyclDeviceType Dty;
std::string fs_device_type, fs;
DPCTLSyclDeviceRef DRef = nullptr, D0Ref = nullptr;
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDeviceVector_GetAt(DV, i));
EXPECT_NO_FATAL_FAILURE(Dty = DPCTLDevice_GetDeviceType(DRef));
EXPECT_NO_FATAL_FAILURE(
fs_device_type = DPCTL_DeviceTypeToStr(
DPCTL_DPCTLDeviceTypeToSyclDeviceType(Dty)));
EXPECT_NO_FATAL_FAILURE(fs = fs_device_type + ":" + std::to_string(i));
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(fs.c_str()));
EXPECT_NO_FATAL_FAILURE(D0Ref = DPCTLDevice_CreateFromSelector(DSRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
EXPECT_TRUE(DPCTLDevice_AreEq(DRef, D0Ref));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(D0Ref));
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
}
}

INSTANTIATE_TEST_SUITE_P(
GetDevices,
TestDPCTLGetDevicesOrdering,
::testing::Values(DPCTLSyclDeviceType::DPCTL_HOST_DEVICE,
DPCTLSyclDeviceType::DPCTL_ACCELERATOR,
DPCTLSyclDeviceType::DPCTL_GPU,
DPCTLSyclDeviceType::DPCTL_CPU));
15 changes: 11 additions & 4 deletions dpctl/tests/test_sycl_device_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,17 @@ def test_get_devices_with_device_type_enum(device_type):


def test_get_devices_with_device_type_str(device_type_str):
devices = dpctl.get_devices(device_type=device_type_str)
if len(devices):
d = string_to_device_type(device_type_str)
check_if_device_type_matches(devices, d)
num_devices = dpctl.get_num_devices(device_type=device_type_str)
if num_devices > 0:
devices = dpctl.get_devices(device_type=device_type_str)
assert len(devices) == num_devices
dty = string_to_device_type(device_type_str)
check_if_device_type_matches(devices, dty)
check_if_device_type_is_valid(devices)
# check for consistency of ordering between filter selector
# where backend is omitted, but device type and id is specified
for i in range(num_devices):
dev = dpctl.SyclDevice(":".join((device_type_str, str(i))))
assert dev == devices[i]
else:
pytest.skip()