Skip to content
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

Fix sub devices #479

Merged
merged 4 commits into from
May 27, 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
38 changes: 28 additions & 10 deletions dpctl-capi/source/dpctl_sycl_device_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "Support/CBindingWrapping.h"
#include "dpctl_sycl_device_manager.h"
#include <CL/sycl.hpp> /* SYCL headers */
#include <algorithm>
#include <cstring>

using namespace cl::sycl;
Expand Down Expand Up @@ -577,8 +578,13 @@ DPCTLDevice_CreateSubDevicesEqually(__dpctl_keep const DPCTLSyclDeviceRef DRef,
size_t count)
{
vector_class<DPCTLSyclDeviceRef> *Devices = nullptr;
auto D = unwrap(DRef);
if (D) {
if (DRef) {
if (count == 0) {
std::cerr << "Can not create sub-devices with zero compute units"
<< '\n';
return nullptr;
}
auto D = unwrap(DRef);
try {
auto subDevices = D->create_sub_devices<
info::partition_property::partition_equally>(count);
Expand Down Expand Up @@ -610,13 +616,29 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef,
size_t ncounts)
{
vector_class<DPCTLSyclDeviceRef> *Devices = nullptr;
std::vector<size_t> vcounts;
std::vector<size_t> vcounts(ncounts);
vcounts.assign(counts, counts + ncounts);
oleksandr-pavlyk marked this conversation as resolved.
Show resolved Hide resolved
auto D = unwrap(DRef);
if (D) {
size_t min_elem = *std::min_element(vcounts.begin(), vcounts.end());
if (min_elem == 0) {
std::cerr << "Can not create sub-devices with zero compute units"
<< '\n';
return nullptr;
}
if (DRef) {
auto D = unwrap(DRef);
vector_class<std::remove_pointer<decltype(D)>::type> subDevices;
try {
auto subDevices = D->create_sub_devices<
subDevices = D->create_sub_devices<
info::partition_property::partition_by_counts>(vcounts);
} catch (feature_not_supported const &fnse) {
std::cerr << fnse.what() << '\n';
return nullptr;
} catch (runtime_error const &re) {
// \todo log error
std::cerr << re.what() << '\n';
return nullptr;
}
try {
Devices = new vector_class<DPCTLSyclDeviceRef>();
for (const auto &sd : subDevices) {
Devices->emplace_back(wrap(new device(sd)));
Expand All @@ -625,10 +647,6 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef,
delete Devices;
std::cerr << ba.what() << '\n';
return nullptr;
} catch (feature_not_supported const &fnse) {
delete Devices;
std::cerr << fnse.what() << '\n';
return nullptr;
} catch (runtime_error const &re) {
delete Devices;
// \todo log error
Expand Down
8 changes: 8 additions & 0 deletions dpctl-capi/tests/test_sycl_device_subdevices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesEqually)
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(pDRef));
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
}
EXPECT_NO_FATAL_FAILURE(
DVRef = DPCTLDevice_CreateSubDevicesEqually(DRef, 0));
EXPECT_TRUE(DVRef == nullptr);
}
}

Expand All @@ -114,7 +117,12 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByCounts)
if (DVRef) {
EXPECT_TRUE(DPCTLDeviceVector_Size(DVRef) > 0);
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
DVRef = nullptr;
}
counts[n - 1] = 0;
EXPECT_NO_FATAL_FAILURE(
DVRef = DPCTLDevice_CreateSubDevicesByCounts(DRef, counts, n));
EXPECT_TRUE(DVRef == nullptr);
}
}

Expand Down
21 changes: 17 additions & 4 deletions dpctl/_sycl_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,12 @@ cdef class SyclDevice(_SyclDevice):
the sub-devices.
"""
cdef DPCTLDeviceVectorRef DVRef = NULL
DVRef = DPCTLDevice_CreateSubDevicesEqually(self._device_ref, count)
if count > 0:
DVRef = DPCTLDevice_CreateSubDevicesEqually(self._device_ref, count)
else:
raise ValueError(
"Creating sub-devices with zero compute units is requested"
oleksandr-pavlyk marked this conversation as resolved.
Show resolved Hide resolved
)
if DVRef is NULL:
raise SubDeviceCreationError("Sub-devices were not created.")
return _get_devices(DVRef)
Expand All @@ -720,6 +725,7 @@ cdef class SyclDevice(_SyclDevice):
"""
cdef int ncounts = len(counts)
cdef size_t *counts_buff = NULL
cdef size_t min_count = 1
cdef DPCTLDeviceVectorRef DVRef = NULL
cdef int i

Expand All @@ -734,10 +740,17 @@ cdef class SyclDevice(_SyclDevice):
)
for i in range(ncounts):
counts_buff[i] = counts[i]
DVRef = DPCTLDevice_CreateSubDevicesByCounts(
self._device_ref, counts_buff, ncounts
)
if counts_buff[i] == 0:
oleksandr-pavlyk marked this conversation as resolved.
Show resolved Hide resolved
min_count = 0
if min_count:
DVRef = DPCTLDevice_CreateSubDevicesByCounts(
self._device_ref, counts_buff, ncounts
)
free(counts_buff)
if min_count == 0:
raise ValueError(
"Targeted sub-device execution units must positive"
)
if DVRef is NULL:
raise SubDeviceCreationError("Sub-devices were not created.")
return _get_devices(DVRef)
Expand Down
14 changes: 14 additions & 0 deletions dpctl/tests/test_sycl_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ def check_create_sub_devices_equally(device):
pytest.fail("create_sub_devices failed")


def check_create_sub_devices_equally_zeros(device):
try:
device.create_sub_devices(partition=0)
except TypeError:
pass


def check_create_sub_devices_by_counts(device):
try:
n = device.max_compute_units / 2
Expand All @@ -372,6 +379,13 @@ def check_create_sub_devices_by_counts(device):
pytest.fail("create_sub_devices failed")


def check_create_sub_devices_by_counts_zeros(device):
try:
device.create_sub_devices(partition=(0, 1))
except TypeError:
pass


def check_create_sub_devices_by_affinity_not_applicable(device):
try:
device.create_sub_devices(partition="not_applicable")
Expand Down