Skip to content

Commit fe8a292

Browse files
authored
[SYCL][Doc] Add spec to get device index (#20007)
Add a proposed extension specification that allows applications to easily get the index of a `device` object and to get a `device` object from its index.
1 parent 02c4600 commit fe8a292

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
= sycl_ext_oneapi_device_index
2+
3+
:source-highlighter: coderay
4+
:coderay-linenums-mode: table
5+
6+
// This section needs to be after the document title.
7+
:doctype: book
8+
:toc2:
9+
:toc: left
10+
:encoding: utf-8
11+
:lang: en
12+
:dpcpp: pass:[DPC++]
13+
:endnote: —{nbsp}end{nbsp}note
14+
15+
// Set the default source code type in this document to C++,
16+
// for syntax highlighting purposes. This is needed because
17+
// docbook uses c++ and html5 uses cpp.
18+
:language: {basebackend@docbook:c++:cpp}
19+
20+
21+
== Notice
22+
23+
[%hardbreaks]
24+
Copyright (C) 2025 Intel Corporation. All rights reserved.
25+
26+
Khronos(R) is a registered trademark and SYCL(TM) and SPIR(TM) are trademarks
27+
of The Khronos Group Inc. OpenCL(TM) is a trademark of Apple Inc. used by
28+
permission by Khronos.
29+
30+
31+
== Contact
32+
33+
To report problems with this extension, please open a new issue at:
34+
35+
https://github.com/intel/llvm/issues
36+
37+
38+
== Dependencies
39+
40+
This extension is written against the SYCL 2020 revision 10 specification.
41+
All references below to the "core SYCL specification" or to section numbers in
42+
the SYCL specification refer to that revision.
43+
44+
45+
== Status
46+
47+
This is a proposed extension specification, intended to gather community
48+
feedback.
49+
Interfaces defined in this specification may not be implemented yet or may be in
50+
a preliminary state.
51+
The specification itself may also change in incompatible ways before it is
52+
finalized.
53+
*Shipping software products should not rely on APIs defined in this
54+
specification.*
55+
56+
57+
== Overview
58+
59+
Some SYCL applications find it more convenient to represent a device as an
60+
integer value rather than using the `device` class.
61+
The core SYCL specification already allows an application to do this by using
62+
the `device` object's index in the list of all devices returned by
63+
`device::get_devices`.
64+
However, converting between a `device` object and its index could be expensive
65+
because the conversion requires a search through all of the devices returned by
66+
`get_devices`.
67+
This extension adds functions that make it easy and efficient to do this
68+
conversion.
69+
70+
71+
== Specification
72+
73+
=== Feature test macro
74+
75+
This extension provides a feature-test macro as described in the core SYCL
76+
specification.
77+
An implementation supporting this extension must predefine the macro
78+
`SYCL_EXT_ONEAPI_DEVICE_INDEX` to one of the values defined in the table below.
79+
Applications can test for the existence of this macro to determine if the
80+
implementation supports this feature, or applications can test the macro's value
81+
to determine which of the extension's features the implementation supports.
82+
83+
[%header,cols="1,5"]
84+
|===
85+
|Value
86+
|Description
87+
88+
|1
89+
|Initial version of this extension.
90+
|===
91+
92+
=== New member functions in the device class
93+
94+
This extension adds the following new member functions to the `device` class.
95+
96+
[frame=all,grid=none,separator="@"]
97+
|====
98+
a@
99+
[source,c++]
100+
----
101+
class device {
102+
// ...
103+
size_t ext_oneapi_to_index() const;
104+
static device ext_oneapi_from_index(size_t index);
105+
};
106+
----
107+
|====
108+
109+
'''
110+
111+
[frame=all,grid=none,separator="@"]
112+
|====
113+
a@
114+
[source,c++]
115+
----
116+
size_t ext_oneapi_to_index() const;
117+
----
118+
|====
119+
120+
_Returns:_ If this device is a root device as defined by the core SYCL
121+
specification, returns the index that it has in the `std::vector` that is
122+
returned when calling `device::get_devices()`.
123+
124+
_Throws:_ An `exception` with the `errc::invalid` error code if this device is
125+
not a root device.
126+
127+
'''
128+
129+
[frame=all,grid=none,separator="@"]
130+
|====
131+
a@
132+
[source,c++]
133+
----
134+
static device ext_oneapi_from_index(size_t index);
135+
----
136+
|====
137+
138+
_Returns:_ If the `index` is within range of the `std::vector` that is returned
139+
when calling `device::get_devices()`, returns a copy of the `device` object
140+
which has that index.
141+
142+
_Throws:_ An `exception` with the `errc::invalid` error code if the `index` is
143+
out of range.
144+
145+
'''
146+
147+
148+
== Example
149+
150+
[source,c++]
151+
----
152+
#include <sycl/sycl.hpp>
153+
154+
int main() {
155+
sycl::device d1; // Get the default device.
156+
// There is no guarantee this has index 0.
157+
158+
size_t index = d1.ext_oneapi_to_index();
159+
sycl::device d2 = sycl::device::ext_oneapi_from_index(index);
160+
assert(d1 == d2);
161+
}
162+
----

0 commit comments

Comments
 (0)