Skip to content

Commit 72564ef

Browse files
committed
Replace UR OpenCL reference counting with new URRefCount class.
1 parent bac2f44 commit 72564ef

20 files changed

+89
-100
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->getRefCount().increment();
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->getRefCount().increment();
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->getRefCount().decrementAndTest()) {
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->getRefCount().getCount());
123123
case UR_ADAPTER_INFO_VERSION:
124124
return ReturnValue(uint32_t{1});
125125
default:

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

Lines changed: 6 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,7 +25,6 @@ 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

@@ -37,6 +37,11 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base {
3737
#define CL_CORE_FUNCTION(FUNC) decltype(::FUNC) *FUNC = nullptr;
3838
#include "core_functions.def"
3939
#undef CL_CORE_FUNCTION
40+
41+
URRefCount &getRefCount() noexcept { return RefCount; }
42+
43+
private:
44+
URRefCount RefCount;
4045
};
4146

4247
namespace ur {

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->getRefCount().increment();
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->getRefCount().decrementAndTest()) {
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->getRefCount().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: 6 additions & 6 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,8 +54,6 @@ 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

@@ -66,11 +65,12 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base {
6665
: handle_base(), hInternalQueue(hQueue), hContext(hContext),
6766
hDevice(hDevice), CLCommandBuffer(CLCommandBuffer),
6867
IsUpdatable(IsUpdatable), IsInOrder(IsInOrder), IsFinalized(false),
69-
RefCount(0), LastSubmission(nullptr) {}
68+
LastSubmission(nullptr) {}
7069

7170
~ur_exp_command_buffer_handle_t_();
7271

73-
uint32_t incrementReferenceCount() noexcept { return ++RefCount; }
74-
uint32_t decrementReferenceCount() noexcept { return --RefCount; }
75-
uint32_t getReferenceCount() const noexcept { return RefCount; }
72+
URRefCount &getRefCount() noexcept { return RefCount; }
73+
74+
private:
75+
URRefCount RefCount;
7676
};

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->getRefCount().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->getRefCount().decrementAndTest()) {
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->getRefCount().increment();
130130
return UR_RESULT_SUCCESS;
131131
}
132132

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

Lines changed: 6 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,7 +21,6 @@ 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;
2525

2626
ur_context_handle_t_(native_type Ctx, uint32_t DevCount,
@@ -30,15 +30,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base {
3030
Devices.emplace_back(phDevices[i]);
3131
urDeviceRetain(phDevices[i]);
3232
}
33-
RefCount = 1;
3433
}
3534

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-
4235
static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount,
4336
const ur_device_handle_t *phDevices,
4437
ur_context_handle_t &Context);
@@ -56,4 +49,9 @@ struct ur_context_handle_t_ : ur::opencl::handle_base {
5649
clReleaseContext(CLContext);
5750
}
5851
}
52+
53+
URRefCount &getRefCount() noexcept { return RefCount; }
54+
55+
private:
56+
URRefCount RefCount;
5957
};

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->getRefCount().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->getRefCount().increment();
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->getRefCount().decrementAndTest()) {
15751575
delete hDevice;
15761576
}
15771577
}

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

Lines changed: 6 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,11 @@ 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;
2424

2525
ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat,
2626
ur_device_handle_t Parent)
2727
: handle_base(), CLDevice(Dev), Platform(Plat), ParentDevice(Parent) {
28-
RefCount = 1;
2928
if (Parent) {
3029
Type = Parent->Type;
3130
[[maybe_unused]] auto Res = clRetainDevice(CLDevice);
@@ -51,12 +50,6 @@ struct ur_device_handle_t_ : ur::opencl::handle_base {
5150
}
5251
}
5352

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-
6053
ur_result_t getDeviceVersion(oclv::OpenCLVersion &Version) {
6154
size_t DevVerSize = 0;
6255
CL_RETURN_ON_FAILURE(
@@ -114,4 +107,9 @@ struct ur_device_handle_t_ : ur::opencl::handle_base {
114107

115108
return UR_RESULT_SUCCESS;
116109
}
110+
111+
URRefCount &getRefCount() noexcept { return RefCount; }
112+
113+
private:
114+
URRefCount RefCount;
117115
};

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->getRefCount().decrementAndTest()) {
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->getRefCount().increment();
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->getRefCount().getCount());
192192
}
193193
default: {
194194
size_t CheckPropSize = 0;

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

Lines changed: 6 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,11 @@ 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;
2424

2525
ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx,
2626
ur_queue_handle_t Queue)
2727
: handle_base(), CLEvent(Event), Context(Ctx), Queue(Queue) {
28-
RefCount = 1;
2928
urContextRetain(Context);
3029
if (Queue) {
3130
urQueueRetain(Queue);
@@ -42,12 +41,6 @@ struct ur_event_handle_t_ : ur::opencl::handle_base {
4241
}
4342
}
4443

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-
5144
ur_result_t ensureQueue() {
5245
if (!Queue) {
5346
cl_command_queue native_queue;
@@ -60,6 +53,11 @@ struct ur_event_handle_t_ : ur::opencl::handle_base {
6053

6154
return UR_RESULT_SUCCESS;
6255
}
56+
57+
URRefCount &getRefCount() noexcept { return RefCount; }
58+
59+
private:
60+
URRefCount RefCount;
6361
};
6462

6563
inline cl_event *ifUrEvent(ur_event_handle_t *ReturnedEvent, cl_event &Event) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelGetInfo(ur_kernel_handle_t hKernel,
152152
return ReturnValue(hKernel->Context);
153153
}
154154
case UR_KERNEL_INFO_REFERENCE_COUNT: {
155-
return ReturnValue(hKernel->getReferenceCount());
155+
return ReturnValue(hKernel->getRefCount().getCount());
156156
}
157157
default: {
158158
size_t CheckPropSize = 0;
@@ -343,13 +343,13 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
343343
}
344344

345345
UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) {
346-
hKernel->incrementReferenceCount();
346+
hKernel->getRefCount().increment();
347347
return UR_RESULT_SUCCESS;
348348
}
349349

350350
UR_APIEXPORT ur_result_t UR_APICALL
351351
urKernelRelease(ur_kernel_handle_t hKernel) {
352-
if (hKernel->decrementReferenceCount() == 0) {
352+
if (hKernel->getRefCount().decrementAndTest()) {
353353
delete hKernel;
354354
}
355355
return UR_RESULT_SUCCESS;

0 commit comments

Comments
 (0)