Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions sycl/doc/extensions/proposed/sycl_ext_oneapi_device_index.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
= sycl_ext_oneapi_device_index

:source-highlighter: coderay
:coderay-linenums-mode: table

// This section needs to be after the document title.
:doctype: book
:toc2:
:toc: left
:encoding: utf-8
:lang: en
:dpcpp: pass:[DPC++]
:endnote: —{nbsp}end{nbsp}note

// Set the default source code type in this document to C++,
// for syntax highlighting purposes. This is needed because
// docbook uses c++ and html5 uses cpp.
:language: {basebackend@docbook:c++:cpp}


== Notice

[%hardbreaks]
Copyright (C) 2025 Intel Corporation. All rights reserved.

Khronos(R) is a registered trademark and SYCL(TM) and SPIR(TM) are trademarks
of The Khronos Group Inc. OpenCL(TM) is a trademark of Apple Inc. used by
permission by Khronos.


== Contact

To report problems with this extension, please open a new issue at:

https://github.com/intel/llvm/issues


== Dependencies

This extension is written against the SYCL 2020 revision 10 specification.
All references below to the "core SYCL specification" or to section numbers in
the SYCL specification refer to that revision.


== Status

This is a proposed extension specification, intended to gather community
feedback.
Interfaces defined in this specification may not be implemented yet or may be in
a preliminary state.
The specification itself may also change in incompatible ways before it is
finalized.
*Shipping software products should not rely on APIs defined in this
specification.*


== Overview

Some SYCL applications find it more convenient to represent a device as an
integer value rather than using the `device` class.
The core SYCL specification already allows an application to do this by using
the `device` object's index in the list of all devices returned by
`device::get_devices`.
However, converting between a `device` object and its index could be expensive
because the conversion requires a search through all of the devices returned by
`get_devices`.
This extension adds functions that make it easy and efficient to do this
conversion.


== Specification

=== Feature test macro

This extension provides a feature-test macro as described in the core SYCL
specification.
An implementation supporting this extension must predefine the macro
`SYCL_EXT_ONEAPI_DEVICE_INDEX` to one of the values defined in the table below.
Applications can test for the existence of this macro to determine if the
implementation supports this feature, or applications can test the macro's value
to determine which of the extension's features the implementation supports.

[%header,cols="1,5"]
|===
|Value
|Description

|1
|Initial version of this extension.
|===

=== New member functions in the device class

This extension adds the following new member functions to the `device` class.

[frame=all,grid=none,separator="@"]
|====
a@
[source,c++]
----
class device {
// ...
int ext_oneapi_to_index() const;
static device ext_oneapi_from_index(int index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think ext_oneapi_from_index(i) is a little unnecessary. It is effectively just a "shorthand" for device::get_devices()[i], but not currently actually shorter:

device::ext_oneapi_from_index(i)
device::get_devices()[i]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this, but it seemed better to add a pair of APIs to convert back and forth between index and device, rather than just adding a single API to do a one-way conversion. In addition, it is a bit wasteful to construct a std::vector if the user just wants a single element from that vector.

Copy link
Contributor

@steffenlarsen steffenlarsen Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this, but it seemed better to add a pair of APIs to convert back and forth between index and device, rather than just adding a single API to do a one-way conversion.

Since the API is already talking about the return value being an index into the vector of devices, I am not personally too much against just having the one-way API added here. It is a minor thing, but it is also an additional API to maintain. I suppose we could maybe just reword it to say "Same as calling device::get_devices()[i]" or something along those lines.

In addition, it is a bit wasteful to construct a std::vector if the user just wants a single element from that vector.

That is a good point. I would be tempted to say we would want to do that internally as well, simply to make sure the sets of devices don't suddenly change due to the adapters not making any ordering guarantees, but maybe some implementations could avoid that.

That said, even if we did keep an internal version for book-keeping, the API for get_devices() would return a copy of it, so you are right that it would be wasteful.

};
----
|====

'''

[frame=all,grid=none,separator="@"]
|====
a@
[source,c++]
----
int ext_oneapi_to_index() const;
----
|====

_Returns:_ If this device is a root device as defined by the core SYCL
specification, returns the index that it has in the `std::vector` that is
returned when calling `device::get_devices()`.

_Throws:_ An `exception` with the `errc::invalid` error code if this device is
not a root device.

'''

[frame=all,grid=none,separator="@"]
|====
a@
[source,c++]
----
static device ext_oneapi_from_index(int index);
----
|====

_Returns:_ If the `index` is within range of the `std::vector` that is returned
when calling `device::get_devices()`, returns a copy of the `device` object
which has that index.

_Throws:_ An `exception` with the `errc::invalid` error code if the `index` is
out of range.

'''


== Example

[source,c++]
----
#include <sycl/sycl.hpp>

int main() {
sycl::device d1; // Get the default device.
// There is no guarantee this has index 0.

int index = d1.ext_oneapi_to_index();
sycl::device d2 = sycl::device::ext_oneapi_from_index(index);
assert(d1 == d2);
}
----