Skip to content

Commit 9314b3a

Browse files
authored
[SYCL] Balance device reference counter (#6708)
While it's not strictly required to release(decrement reference counters) of handles of root devices it might be convenient for plugins to have the same behavior as for other handles.
1 parent 15edb1b commit 9314b3a

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

sycl/source/detail/platform_impl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ platform_impl::get_devices(info::device_type DeviceType) const {
505505
pi::cast<RT::PiDeviceType>(DeviceType), // CP info::device_type::all
506506
NumDevices, PiDevices.data(), nullptr);
507507

508+
// Some elements of PiDevices vector might be filtered out, so make a copy of
509+
// handles to do a cleanup later
510+
std::vector<RT::PiDevice> PiDevicesToCleanUp = PiDevices;
511+
508512
// Filter out devices that are not present in the SYCL_DEVICE_ALLOWLIST
509513
if (SYCLConfig<SYCL_DEVICE_ALLOWLIST>::get())
510514
applyAllowList(PiDevices, MPlatform, Plugin);
@@ -537,6 +541,11 @@ platform_impl::get_devices(info::device_type DeviceType) const {
537541
PlatformImpl->getOrMakeDeviceImpl(PiDevice, PlatformImpl));
538542
});
539543

544+
// The reference counter for handles, that we used to create sycl objects, is
545+
// incremented, so we need to call release here.
546+
for (RT::PiDevice &PiDev : PiDevicesToCleanUp)
547+
Plugin.call<PiApiKind::piDeviceRelease>(PiDev);
548+
540549
// If we aren't using ONEAPI_DEVICE_SELECTOR, then we are done.
541550
// and if there are no devices so far, there won't be any need to replace them
542551
// with subdevices.

sycl/unittests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ add_subdirectory(Extensions)
4646
add_subdirectory(windows)
4747
add_subdirectory(event)
4848
add_subdirectory(buffer)
49-
add_subdirectory(context)
49+
add_subdirectory(context_device)
5050
add_subdirectory(accessor)
5151
add_subdirectory(handler)

sycl/unittests/context/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_sycl_unittest(ContextDeviceTests OBJECT
2+
Context.cpp
3+
DeviceRefCounter.cpp
4+
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//==----- DeviceRefCounter - Kernel build options processing unit test -----==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#define SYCL2020_DISABLE_DEPRECATION_WARNINGS
10+
11+
#include <gtest/gtest.h>
12+
#include <helpers/PiMock.hpp>
13+
#include <sycl/sycl.hpp>
14+
15+
int DevRefCounter = 0;
16+
17+
static pi_result redefinedDevicesGetAfter(pi_platform platform,
18+
pi_device_type device_type,
19+
pi_uint32 num_entries,
20+
pi_device *devices,
21+
pi_uint32 *num_devices) {
22+
if (devices)
23+
DevRefCounter += num_entries;
24+
return PI_SUCCESS;
25+
}
26+
27+
static pi_result redefinedDeviceRetainAfter(pi_device device) {
28+
DevRefCounter++;
29+
return PI_SUCCESS;
30+
}
31+
32+
static pi_result redefinedDeviceReleaseAfter(pi_device device) {
33+
DevRefCounter--;
34+
return PI_SUCCESS;
35+
}
36+
37+
TEST(DevRefCounter, DevRefCounter) {
38+
{
39+
sycl::unittest::PiMock Mock;
40+
sycl::platform Plt = Mock.getPlatform();
41+
42+
Mock.redefineAfter<sycl::detail::PiApiKind::piDevicesGet>(
43+
redefinedDevicesGetAfter);
44+
Mock.redefineAfter<sycl::detail::PiApiKind::piDeviceRetain>(
45+
redefinedDeviceRetainAfter);
46+
Mock.redefineAfter<sycl::detail::PiApiKind::piDeviceRelease>(
47+
redefinedDeviceReleaseAfter);
48+
49+
Plt.get_devices();
50+
}
51+
EXPECT_EQ(DevRefCounter, 0);
52+
}

0 commit comments

Comments
 (0)