Skip to content

Commit 4a2b9c0

Browse files
authored
[UR][OpenCL] Refactor UR OpenCL reference counting (#19176)
For #18644 Most UR adapters had their own reference counting of some sort. This adds a new `RefCount` class and refactors adapter code so all adapters can share the same code for reference counting. This PR handles OpenCL and I will open more PRs for each adapter in turn.
1 parent 2d2cc85 commit 4a2b9c0

21 files changed

+57
-106
lines changed

unified-runtime/source/adapters/opencl/adapter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
7878
}
7979

8080
auto &adapter = *phAdapters;
81-
adapter->RefCount++;
81+
adapter->RefCount.retain();
8282
}
8383

8484
if (pNumAdapters) {
@@ -90,13 +90,13 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
9090

9191
UR_APIEXPORT ur_result_t UR_APICALL
9292
urAdapterRetain(ur_adapter_handle_t hAdapter) {
93-
++hAdapter->RefCount;
93+
hAdapter->RefCount.retain();
9494
return UR_RESULT_SUCCESS;
9595
}
9696

9797
UR_APIEXPORT ur_result_t UR_APICALL
9898
urAdapterRelease(ur_adapter_handle_t hAdapter) {
99-
if (--hAdapter->RefCount == 0) {
99+
if (hAdapter->RefCount.release()) {
100100
delete hAdapter;
101101
}
102102
return UR_RESULT_SUCCESS;
@@ -119,7 +119,7 @@ urAdapterGetInfo(ur_adapter_handle_t hAdapter, ur_adapter_info_t propName,
119119
case UR_ADAPTER_INFO_BACKEND:
120120
return ReturnValue(UR_BACKEND_OPENCL);
121121
case UR_ADAPTER_INFO_REFERENCE_COUNT:
122-
return ReturnValue(hAdapter->RefCount.load());
122+
return ReturnValue(hAdapter->RefCount.getCount());
123123
case UR_ADAPTER_INFO_VERSION:
124124
return ReturnValue(uint32_t{1});
125125
default:

unified-runtime/source/adapters/opencl/adapter.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "CL/cl.h"
1717
#include "common.hpp"
18+
#include "common/ur_ref_count.hpp"
1819
#include "logger/ur_logger.hpp"
1920

2021
struct ur_adapter_handle_t_ : ur::opencl::handle_base {
@@ -24,13 +25,14 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base {
2425
ur_adapter_handle_t_(ur_adapter_handle_t_ &) = delete;
2526
ur_adapter_handle_t_ &operator=(const ur_adapter_handle_t_ &) = delete;
2627

27-
std::atomic<uint32_t> RefCount = 0;
2828
logger::Logger &log = logger::get_logger("opencl");
2929
cl_ext::ExtFuncPtrCacheT fnCache{};
3030

3131
std::vector<std::unique_ptr<ur_platform_handle_t_>> URPlatforms;
3232
uint32_t NumPlatforms = 0;
3333

34+
ur::RefCount RefCount;
35+
3436
// Function pointers to core OpenCL entry points which may not exist in older
3537
// versions of the OpenCL-ICD-Loader are tracked here and initialized by
3638
// dynamically loading the symbol by name.

unified-runtime/source/adapters/opencl/command_buffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp(
108108

109109
UR_APIEXPORT ur_result_t UR_APICALL
110110
urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
111-
hCommandBuffer->incrementReferenceCount();
111+
hCommandBuffer->RefCount.retain();
112112
return UR_RESULT_SUCCESS;
113113
}
114114

115115
UR_APIEXPORT ur_result_t UR_APICALL
116116
urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) {
117-
if (hCommandBuffer->decrementReferenceCount() == 0) {
117+
if (hCommandBuffer->RefCount.release()) {
118118
delete hCommandBuffer;
119119
}
120120

@@ -783,7 +783,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp(
783783

784784
switch (propName) {
785785
case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT:
786-
return ReturnValue(hCommandBuffer->getReferenceCount());
786+
return ReturnValue(hCommandBuffer->RefCount.getCount());
787787
case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: {
788788
ur_exp_command_buffer_desc_t Descriptor{};
789789
Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC;

unified-runtime/source/adapters/opencl/command_buffer.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//===----------------------------------------------------------------------===//
1010

1111
#include "common.hpp"
12+
#include "common/ur_ref_count.hpp"
1213
#include <ur/ur.hpp>
1314

1415
/// Handle to a kernel command.
@@ -53,11 +54,11 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base {
5354
/// List of commands in the command-buffer.
5455
std::vector<std::unique_ptr<ur_exp_command_buffer_command_handle_t_>>
5556
CommandHandles;
56-
/// Object reference count
57-
std::atomic_uint32_t RefCount;
5857
/// Track last submission of the command-buffer
5958
cl_event LastSubmission;
6059

60+
ur::RefCount RefCount;
61+
6162
ur_exp_command_buffer_handle_t_(ur_queue_handle_t hQueue,
6263
ur_context_handle_t hContext,
6364
ur_device_handle_t hDevice,
@@ -66,11 +67,7 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base {
6667
: handle_base(), hInternalQueue(hQueue), hContext(hContext),
6768
hDevice(hDevice), CLCommandBuffer(CLCommandBuffer),
6869
IsUpdatable(IsUpdatable), IsInOrder(IsInOrder), IsFinalized(false),
69-
RefCount(0), LastSubmission(nullptr) {}
70+
LastSubmission(nullptr) {}
7071

7172
~ur_exp_command_buffer_handle_t_();
72-
73-
uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
74-
uint32_t decrementReferenceCount() noexcept { return --RefCount; }
75-
uint32_t getReferenceCount() const noexcept { return RefCount; }
7673
};

unified-runtime/source/adapters/opencl/context.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName,
108108
return ReturnValue(&hContext->Devices[0], hContext->DeviceCount);
109109
}
110110
case UR_CONTEXT_INFO_REFERENCE_COUNT: {
111-
return ReturnValue(hContext->getReferenceCount());
111+
return ReturnValue(hContext->RefCount.getCount());
112112
}
113113
default:
114114
return UR_RESULT_ERROR_INVALID_ENUMERATION;
@@ -117,7 +117,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName,
117117

118118
UR_APIEXPORT ur_result_t UR_APICALL
119119
urContextRelease(ur_context_handle_t hContext) {
120-
if (hContext->decrementReferenceCount() == 0) {
120+
if (hContext->RefCount.release()) {
121121
delete hContext;
122122
}
123123

@@ -126,7 +126,7 @@ urContextRelease(ur_context_handle_t hContext) {
126126

127127
UR_APIEXPORT ur_result_t UR_APICALL
128128
urContextRetain(ur_context_handle_t hContext) {
129-
hContext->incrementReferenceCount();
129+
hContext->RefCount.retain();
130130
return UR_RESULT_SUCCESS;
131131
}
132132

unified-runtime/source/adapters/opencl/context.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "adapter.hpp"
1313
#include "common.hpp"
14+
#include "common/ur_ref_count.hpp"
1415
#include "device.hpp"
1516

1617
#include <vector>
@@ -20,8 +21,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base {
2021
native_type CLContext;
2122
std::vector<ur_device_handle_t> Devices;
2223
uint32_t DeviceCount;
23-
std::atomic<uint32_t> RefCount = 0;
2424
bool IsNativeHandleOwned = true;
25+
ur::RefCount RefCount;
2526

2627
ur_context_handle_t_(native_type Ctx, uint32_t DevCount,
2728
const ur_device_handle_t *phDevices)
@@ -30,15 +31,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base {
3031
Devices.emplace_back(phDevices[i]);
3132
urDeviceRetain(phDevices[i]);
3233
}
33-
RefCount = 1;
3434
}
3535

36-
uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
37-
38-
uint32_t decrementReferenceCount() noexcept { return --RefCount; }
39-
40-
uint32_t getReferenceCount() const noexcept { return RefCount; }
41-
4236
static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount,
4337
const ur_device_handle_t *phDevices,
4438
ur_context_handle_t &Context);

unified-runtime/source/adapters/opencl/device.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
10191019
return UR_RESULT_SUCCESS;
10201020
}
10211021
case UR_DEVICE_INFO_REFERENCE_COUNT: {
1022-
return ReturnValue(hDevice->getReferenceCount());
1022+
return ReturnValue(hDevice->RefCount.getCount());
10231023
}
10241024
case UR_DEVICE_INFO_PARTITION_MAX_SUB_DEVICES: {
10251025
CL_RETURN_ON_FAILURE(clGetDeviceInfo(hDevice->CLDevice,
@@ -1561,7 +1561,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition(
15611561
// Root devices ref count are unchanged through out the program lifetime.
15621562
UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) {
15631563
if (hDevice->ParentDevice) {
1564-
hDevice->incrementReferenceCount();
1564+
hDevice->RefCount.retain();
15651565
}
15661566

15671567
return UR_RESULT_SUCCESS;
@@ -1571,7 +1571,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) {
15711571
UR_APIEXPORT ur_result_t UR_APICALL
15721572
urDeviceRelease(ur_device_handle_t hDevice) {
15731573
if (hDevice->ParentDevice) {
1574-
if (hDevice->decrementReferenceCount() == 0) {
1574+
if (hDevice->RefCount.release()) {
15751575
delete hDevice;
15761576
}
15771577
}

unified-runtime/source/adapters/opencl/device.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma once
1111

1212
#include "common.hpp"
13+
#include "common/ur_ref_count.hpp"
1314
#include "device.hpp"
1415
#include "platform.hpp"
1516

@@ -19,13 +20,12 @@ struct ur_device_handle_t_ : ur::opencl::handle_base {
1920
ur_platform_handle_t Platform;
2021
cl_device_type Type = 0;
2122
ur_device_handle_t ParentDevice = nullptr;
22-
std::atomic<uint32_t> RefCount = 0;
2323
bool IsNativeHandleOwned = true;
24+
ur::RefCount RefCount;
2425

2526
ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat,
2627
ur_device_handle_t Parent)
2728
: handle_base(), CLDevice(Dev), Platform(Plat), ParentDevice(Parent) {
28-
RefCount = 1;
2929
if (Parent) {
3030
Type = Parent->Type;
3131
[[maybe_unused]] auto Res = clRetainDevice(CLDevice);
@@ -51,12 +51,6 @@ struct ur_device_handle_t_ : ur::opencl::handle_base {
5151
}
5252
}
5353

54-
uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
55-
56-
uint32_t decrementReferenceCount() noexcept { return --RefCount; }
57-
58-
uint32_t getReferenceCount() const noexcept { return RefCount; }
59-
6054
ur_result_t getDeviceVersion(oclv::OpenCLVersion &Version) {
6155
size_t DevVerSize = 0;
6256
CL_RETURN_ON_FAILURE(

unified-runtime/source/adapters/opencl/event.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle(
136136
}
137137

138138
UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) {
139-
if (hEvent->decrementReferenceCount() == 0) {
139+
if (hEvent->RefCount.release()) {
140140
delete hEvent;
141141
}
142142
return UR_RESULT_SUCCESS;
143143
}
144144

145145
UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) {
146-
hEvent->incrementReferenceCount();
146+
hEvent->RefCount.retain();
147147
return UR_RESULT_SUCCESS;
148148
}
149149

@@ -188,7 +188,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent,
188188
return ReturnValue(hEvent->Queue);
189189
}
190190
case UR_EVENT_INFO_REFERENCE_COUNT: {
191-
return ReturnValue(hEvent->getReferenceCount());
191+
return ReturnValue(hEvent->RefCount.getCount());
192192
}
193193
default: {
194194
size_t CheckPropSize = 0;

unified-runtime/source/adapters/opencl/event.hpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma once
1111

1212
#include "common.hpp"
13+
#include "common/ur_ref_count.hpp"
1314
#include "queue.hpp"
1415

1516
#include <vector>
@@ -19,13 +20,12 @@ struct ur_event_handle_t_ : ur::opencl::handle_base {
1920
native_type CLEvent;
2021
ur_context_handle_t Context;
2122
ur_queue_handle_t Queue;
22-
std::atomic<uint32_t> RefCount = 0;
2323
bool IsNativeHandleOwned = true;
24+
ur::RefCount RefCount;
2425

2526
ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx,
2627
ur_queue_handle_t Queue)
2728
: handle_base(), CLEvent(Event), Context(Ctx), Queue(Queue) {
28-
RefCount = 1;
2929
urContextRetain(Context);
3030
if (Queue) {
3131
urQueueRetain(Queue);
@@ -42,12 +42,6 @@ struct ur_event_handle_t_ : ur::opencl::handle_base {
4242
}
4343
}
4444

45-
uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
46-
47-
uint32_t decrementReferenceCount() noexcept { return --RefCount; }
48-
49-
uint32_t getReferenceCount() const noexcept { return RefCount; }
50-
5145
ur_result_t ensureQueue() {
5246
if (!Queue) {
5347
cl_command_queue native_queue;

0 commit comments

Comments
 (0)