alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
DeviceSelector.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera, Tim Hanel
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/concepts.hpp"
10#include "alpaka/utility.hpp"
11
12namespace alpaka::onHost
13{
14 template<alpaka::concepts::Api T_Api, alpaka::concepts::DeviceKind T_DeviceKind>
16 {
17 public:
18 static_assert(
20 "Invalid combination of device kind and api. The api does not know how to talk to the device or the "
21 "required dependencies to enable the api are not fulfilled.");
22
24 : m_platform(internal::makePlatform(getApi(deviceSpec), getDeviceKind(deviceSpec)))
25 , m_deviceSpec(deviceSpec)
26 {
27 }
28
29 constexpr DeviceSelector(T_Api api, T_DeviceKind devType) : DeviceSelector(DeviceSpec{api, devType})
30 {
31 }
32
33 /** Get the number of available devices for the given api and device kind.
34 *
35 * @attention In case the compiler flags you used to build your application were wrong, kernels for the given
36 * deviceKind cannot be built and the number of available devices will be zero. This can happen, e.g., if you
37 * compile for OneAPI SYCL: you can compile the application, but whether you can run on a device is evaluated
38 * at runtime.
39 *
40 * @return number of devices
41 */
42 uint32_t getDeviceCount() const
43 {
44 return internal::GetDeviceCount::Op<ALPAKA_TYPEOF(*m_platform.get())>{}(*m_platform.get());
45 }
46
47 bool isAvailable() const
48 {
49 return getDeviceCount() != 0;
50 }
51
53 {
54 return internal::GetDeviceProperties::Op<ALPAKA_TYPEOF(*m_platform.get())>{}(*m_platform.get(), idx);
55 }
56
57 /** Get a device
58 *
59 * @param idx device index (range [0;number of devices), invalid index will throw an exception
60 * @return @see onHost::Device
61 */
62 auto makeDevice(uint32_t idx)
63 {
64 return Device{internal::MakeDevice::Op<ALPAKA_TYPEOF(*m_platform.get())>{}(*m_platform.get(), idx)};
65 }
66
67 private:
68 ALPAKA_TYPEOF(internal::makePlatform(T_Api{}, T_DeviceKind{})) m_platform;
70 };
71
72 template<alpaka::concepts::DeviceSpec T_DeviceSpec>
73 DeviceSelector(T_DeviceSpec const& deviceSpec)
74 -> DeviceSelector<ALPAKA_TYPEOF(getApi(deviceSpec)), ALPAKA_TYPEOF(getDeviceKind(deviceSpec))>;
75
76 /** create an object to get access to devices */
77 template<alpaka::concepts::DeviceSpec T_DeviceSpec>
78 inline auto makeDeviceSelector(T_DeviceSpec deviceSpec)
79 {
80 return DeviceSelector{deviceSpec};
81 }
82
84 {
85 return DeviceSelector{api, deviceTag};
86 }
87
88 template<typename deferEvaluation = void>
89 inline auto makeHostDevice()
90 {
91 return DeviceSelector{
92 std::conditional_t<std::is_same_v<deferEvaluation, bool>, api::Host, api::Host>{},
94 .makeDevice(0);
95 }
96} // namespace alpaka::onHost
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
Concept to check for APIs.
Definition api.hpp:42
Concept to check if something is a device kind.
Definition tag.hpp:145
Concept to check that a device specification with an API and device kind can be extracted.
Definition concepts.hpp:58
constexpr auto cpu
Definition tag.hpp:168
static auto makePlatform(auto api, alpaka::concepts::DeviceKind auto deviceType)
Definition interface.hpp:37
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
DeviceSelector(T_DeviceSpec const &deviceSpec) -> DeviceSelector< ALPAKA_TYPEOF(getApi(deviceSpec)), ALPAKA_TYPEOF(getDeviceKind(deviceSpec))>
auto makeDeviceSelector(T_DeviceSpec deviceSpec)
create an object to get access to devices
constexpr decltype(auto) getDeviceKind(auto &&any)
Get the device type of an object.
Definition interface.hpp:78
constexpr decltype(auto) getApi(auto &&any)
Get the API an object depends on.
Definition interface.hpp:42
auto makeDevice(uint32_t idx)
Get a device.
uint32_t getDeviceCount() const
Get the number of available devices for the given api and device kind.
DeviceProperties getDeviceProperties(uint32_t idx) const
constexpr DeviceSelector(T_Api api, T_DeviceKind devType)
constexpr DeviceSelector(alpaka::concepts::DeviceSpec auto deviceSpec)
ALPAKA_TYPEOF(internal::makePlatform(T_Api{}, T_DeviceKind{})) m_platform
DeviceSpec< T_Api, T_DeviceKind > m_deviceSpec
Concept for a combination of an API and device kind.
static constexpr bool isValid()
Checks if the device kind and api combination is valid.
Description of a specific device that one can schedule kernels on.
Definition Device.hpp:32