Skip to content

Commit 76680ff

Browse files
Fix sub devices (#479)
* In CreateSubDevicesEqually and ByCounts check that counts by positive Also change error handling in CreateSubDevicesByCounts. * SyclDevice.create_sub_devices checks partition argument better Use of zero EU counts now raises and error. * Added tests that handling of partitions with zero EU counts works as designed * improved error handling of zero count
1 parent 7ea23e5 commit 76680ff

File tree

4 files changed

+67
-14
lines changed

4 files changed

+67
-14
lines changed

dpctl-capi/source/dpctl_sycl_device_interface.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Support/CBindingWrapping.h"
3030
#include "dpctl_sycl_device_manager.h"
3131
#include <CL/sycl.hpp> /* SYCL headers */
32+
#include <algorithm>
3233
#include <cstring>
3334

3435
using namespace cl::sycl;
@@ -577,8 +578,13 @@ DPCTLDevice_CreateSubDevicesEqually(__dpctl_keep const DPCTLSyclDeviceRef DRef,
577578
size_t count)
578579
{
579580
vector_class<DPCTLSyclDeviceRef> *Devices = nullptr;
580-
auto D = unwrap(DRef);
581-
if (D) {
581+
if (DRef) {
582+
if (count == 0) {
583+
std::cerr << "Can not create sub-devices with zero compute units"
584+
<< '\n';
585+
return nullptr;
586+
}
587+
auto D = unwrap(DRef);
582588
try {
583589
auto subDevices = D->create_sub_devices<
584590
info::partition_property::partition_equally>(count);
@@ -610,13 +616,29 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef,
610616
size_t ncounts)
611617
{
612618
vector_class<DPCTLSyclDeviceRef> *Devices = nullptr;
613-
std::vector<size_t> vcounts;
619+
std::vector<size_t> vcounts(ncounts);
614620
vcounts.assign(counts, counts + ncounts);
615-
auto D = unwrap(DRef);
616-
if (D) {
621+
size_t min_elem = *std::min_element(vcounts.begin(), vcounts.end());
622+
if (min_elem == 0) {
623+
std::cerr << "Can not create sub-devices with zero compute units"
624+
<< '\n';
625+
return nullptr;
626+
}
627+
if (DRef) {
628+
auto D = unwrap(DRef);
629+
vector_class<std::remove_pointer<decltype(D)>::type> subDevices;
617630
try {
618-
auto subDevices = D->create_sub_devices<
631+
subDevices = D->create_sub_devices<
619632
info::partition_property::partition_by_counts>(vcounts);
633+
} catch (feature_not_supported const &fnse) {
634+
std::cerr << fnse.what() << '\n';
635+
return nullptr;
636+
} catch (runtime_error const &re) {
637+
// \todo log error
638+
std::cerr << re.what() << '\n';
639+
return nullptr;
640+
}
641+
try {
620642
Devices = new vector_class<DPCTLSyclDeviceRef>();
621643
for (const auto &sd : subDevices) {
622644
Devices->emplace_back(wrap(new device(sd)));
@@ -625,10 +647,6 @@ DPCTLDevice_CreateSubDevicesByCounts(__dpctl_keep const DPCTLSyclDeviceRef DRef,
625647
delete Devices;
626648
std::cerr << ba.what() << '\n';
627649
return nullptr;
628-
} catch (feature_not_supported const &fnse) {
629-
delete Devices;
630-
std::cerr << fnse.what() << '\n';
631-
return nullptr;
632650
} catch (runtime_error const &re) {
633651
delete Devices;
634652
// \todo log error

dpctl-capi/tests/test_sycl_device_subdevices.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesEqually)
9292
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(pDRef));
9393
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
9494
}
95+
EXPECT_NO_FATAL_FAILURE(
96+
DVRef = DPCTLDevice_CreateSubDevicesEqually(DRef, 0));
97+
EXPECT_TRUE(DVRef == nullptr);
9598
}
9699
}
97100

@@ -114,7 +117,12 @@ TEST_P(TestDPCTLSyclDeviceInterface, ChkCreateSubDevicesByCounts)
114117
if (DVRef) {
115118
EXPECT_TRUE(DPCTLDeviceVector_Size(DVRef) > 0);
116119
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
120+
DVRef = nullptr;
117121
}
122+
counts[n - 1] = 0;
123+
EXPECT_NO_FATAL_FAILURE(
124+
DVRef = DPCTLDevice_CreateSubDevicesByCounts(DRef, counts, n));
125+
EXPECT_TRUE(DVRef == nullptr);
118126
}
119127
}
120128

dpctl/_sycl_device.pyx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,12 @@ cdef class SyclDevice(_SyclDevice):
720720
the sub-devices.
721721
"""
722722
cdef DPCTLDeviceVectorRef DVRef = NULL
723-
DVRef = DPCTLDevice_CreateSubDevicesEqually(self._device_ref, count)
723+
if count > 0:
724+
DVRef = DPCTLDevice_CreateSubDevicesEqually(self._device_ref, count)
725+
else:
726+
raise ValueError(
727+
"Creating sub-devices with zero compute units is requested"
728+
)
724729
if DVRef is NULL:
725730
raise SubDeviceCreationError("Sub-devices were not created.")
726731
return _get_devices(DVRef)
@@ -734,6 +739,7 @@ cdef class SyclDevice(_SyclDevice):
734739
"""
735740
cdef int ncounts = len(counts)
736741
cdef size_t *counts_buff = NULL
742+
cdef size_t min_count = 1
737743
cdef DPCTLDeviceVectorRef DVRef = NULL
738744
cdef int i
739745

@@ -748,10 +754,17 @@ cdef class SyclDevice(_SyclDevice):
748754
)
749755
for i in range(ncounts):
750756
counts_buff[i] = counts[i]
751-
DVRef = DPCTLDevice_CreateSubDevicesByCounts(
752-
self._device_ref, counts_buff, ncounts
753-
)
757+
if counts_buff[i] == 0:
758+
min_count = 0
759+
if min_count:
760+
DVRef = DPCTLDevice_CreateSubDevicesByCounts(
761+
self._device_ref, counts_buff, ncounts
762+
)
754763
free(counts_buff)
764+
if min_count == 0:
765+
raise ValueError(
766+
"Targeted sub-device execution units must positive"
767+
)
755768
if DVRef is NULL:
756769
raise SubDeviceCreationError("Sub-devices were not created.")
757770
return _get_devices(DVRef)

dpctl/tests/test_sycl_device.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ def check_create_sub_devices_equally(device):
360360
pytest.fail("create_sub_devices failed")
361361

362362

363+
def check_create_sub_devices_equally_zeros(device):
364+
try:
365+
device.create_sub_devices(partition=0)
366+
except TypeError:
367+
pass
368+
369+
363370
def check_create_sub_devices_by_counts(device):
364371
try:
365372
n = device.max_compute_units / 2
@@ -372,6 +379,13 @@ def check_create_sub_devices_by_counts(device):
372379
pytest.fail("create_sub_devices failed")
373380

374381

382+
def check_create_sub_devices_by_counts_zeros(device):
383+
try:
384+
device.create_sub_devices(partition=(0, 1))
385+
except TypeError:
386+
pass
387+
388+
375389
def check_create_sub_devices_by_affinity_not_applicable(device):
376390
try:
377391
device.create_sub_devices(partition="not_applicable")

0 commit comments

Comments
 (0)