Skip to content

Commit 05e74bb

Browse files
aarongreigkbenzie
authored andcommitted
Document expectations for adapter lifetime. (#17860)
1 parent 52c7410 commit 05e74bb

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

include/ur_api.h

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/core/PROG.rst

+27-21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@
1010
Programming Guide
1111
========================
1212

13+
Adapters
14+
========
15+
16+
The top level abstraction in the unified runtime is the adapter handle. It owns
17+
the platform handles as well as any backend specific global state, for instance
18+
if a backend has global setup/teardown type entry-points these will typically
19+
be tied to the adapter handle's constructor and destructor.
20+
21+
Adapter handles are reference counted via ${x}AdapterRetain and
22+
${x}AdapterRelease. Once their reference count reaches zero any objects
23+
associated with the adapter in question should be considered invalid. Calling
24+
${x}AdapterGet after any adapter handle's reference count has reached zero will
25+
result in undefined behavior. An adapter handle's reference count may be
26+
changed by the adapter itself.
27+
28+
Adapter implementers should ensure the adapter handle is dynamically allocated
29+
rather than having a static or global lifetime. Due to how destructor order
30+
works for static and global objects this is the only way to ensure the adapter
31+
handle stays alive as long as its reference count remains above zero.
32+
1333
Platforms and Devices
1434
======================
1535

@@ -31,6 +51,7 @@ A platform object represents a collection of physical devices in the system acce
3151
- More than one platform may be available in the system. For example, one platform may support two GPUs from one vendor, another platform supports a GPU from a different vendor, and finally a different platform may support an FPGA.
3252
- Platform objects are read-only, global constructs. i.e. multiple calls to ${x}PlatformGet will return identical platform handles.
3353
- A platform handle is primarily used during device discovery and during creation and management of contexts.
54+
- Platform handles are not reference counted, they are owned by the adapter handle and their lifetime is tied to the adapter handle's reference count.
3455

3556
Device
3657
------
@@ -41,6 +62,7 @@ A device object represents a physical device in the system that supports the pla
4162
- Device objects are read-only, global constructs. i.e. multiple calls to ${x}DeviceGet will return identical device handles.
4263
- A device handle is primarily used during creation and management of resources that are specific to a device.
4364
- Device may expose sub-devices that allow finer-grained control of physical or logical partitions of a device.
65+
- Device handles are not reference counted, they are owned by their respective platform handles, whose lifetime is in turn tied to the associated adapter handle.
4466

4567
The following diagram illustrates the relationship between the platform, device, context and other objects described in this document.
4668

@@ -79,27 +101,6 @@ Initialization and Discovery
79101
${x}DeviceGet(platforms[0], ${X}_DEVICE_TYPE_GPU, &deviceCount,
80102
devices.data(), devices.size());
81103
82-
Device handle lifetime
83-
----------------------
84-
85-
Device objects are reference-counted, using ${x}DeviceRetain and ${x}DeviceRelease.
86-
The ref-count of a device is automatically incremented when a device is obtained by ${x}DeviceGet.
87-
After a device is no longer needed by the application it must call ${x}DeviceRelease.
88-
When the ref-count of the underlying device handle becomes zero then that device object is deleted.
89-
Note that a Unified Runtime adapter may internally increment and decrement a device's ref-count.
90-
So after the call to ${x}DeviceRelease below, the device may stay active until other
91-
objects using it, such as a command-queue, are deleted. However, an application
92-
may not use the device after it releases its last reference.
93-
94-
.. parsed-literal::
95-
96-
// Get the handle of the first GPU device in the platform
97-
${x}_device_handle_t hDevice;
98-
uint32_t deviceCount = 1;
99-
${x}DeviceGet(hPlatforms[0], ${X}_DEVICE_TYPE_GPU, &deviceCount, &hDevice, 1);
100-
${x}DeviceRelease(hDevice);
101-
102-
103104
Retrieve info about device
104105
--------------------------
105106

@@ -162,6 +163,11 @@ An implementation will return "0" in the count if no further partitioning is sup
162163
if(count == 0){
163164
// no further partitioning allowed
164165
}
166+
167+
Note that unlike a normal device handle, sub-devices *are* reference counted
168+
objects. Their reference count is affected by ${x}DeviceRetain and
169+
${x}DeviceRelease, and once that reference count reaches zero the handle should
170+
no longer be considered valid.
165171

166172
Contexts
167173
========

scripts/core/adapter.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ name: AdapterRelease
5151
decl: static
5252
ordinal: "3"
5353
details:
54-
- "When the reference count of the adapter reaches zero, the adapter may perform adapter-specififc resource teardown. Resources
55-
must be left in a state where it safe for the adapter to be subsequently reinitialized with $xAdapterGet"
54+
- "When the reference count of the adapter reaches zero, the adapter may
55+
perform adapter-specififc resource teardown. Any objects associated with
56+
the adapter should be considered invalid after this point."
57+
- "Calling $xAdapterGet after any adapter handle's reference count has
58+
reached zero will result in undefined behaviour."
5659
params:
5760
- type: "$x_adapter_handle_t"
5861
name: hAdapter

source/loader/ur_libapi.cpp

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/ur_api.cpp

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)