Device selectors

Devices selectors allow the SYCL runtime to choose the device.

A device selector can be passed to sycl::queue, sycl::platform, and other constructors to control the selection of a device. A program may use Built-in Device Selectors or define its own device_selector for full control.

sycl::device_selector

class device_selector;

Abstract class for device selectors.

This is the base class for the Built-in Device Selectors. To define a custom device selector, create a derived class that defines the () operator.

(constructors)

device_selector();
device_selector(const sycl::device_selector &rhs);

Construct a device_selector.

A device selector can be created from another by passing rhs.

select_device

sycl::device select_device() const;

Returns the device with the highest score as determined by calling operator().

Exceptions

Throws a runtime error if all devices have a negative score.

operator=

sycl::device_selector &operator=(const sycl::device_selector &rhs);

Create a device selector by copying another one.

operator()

virtual int operator()(const sycl::device &device) const = 0;

Scoring function for devices.

All derived device selectors must define this operator. select_device calls this operator for every device, and selects the device with highest score. Return a negative score if a device should not be selected.

Built-in Device Selectors

SYCL provides built-in device selectors for convenience. They use device_selector as a base class.

default_selector

Selects device according to implementation-defined heuristic or host device if no device can be found.

gpu_selector

Select a GPU

accelerator_selector

Select an accelerator

cpu_selector

Select a CPU device

host_selector

Select the host device

Create a device selector by copying another one.

See also

SYCL Specification Section 4.6.1.1

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <CL/sycl.hpp>

using namespace sycl;

int main() {
  device d;
  
  try {
    d = device(gpu_selector());
  } catch (exception const& e) {
    std::cout << "Cannot select a GPU\n" << e.what() << "\n";
    std::cout << "Using a CPU device\n";
    d = device(cpu_selector());
  }

  std::cout << "Using " << d.get_info<sycl::info::device::name>();
}

Output on a system without a GPU:

Cannot select a GPU
No device of requested type available. Please check https://software.intel.com/en-us/articles/intel-oneapi-dpcpp-compiler-system-requirements-beta -1 (CL_DEVICE_NOT_FOUND)
Using a CPU device
Using Intel(R) Core(TM) i5-7300U CPU @ 2.60GHz