alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
DeviceSpec.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include "alpaka/api/api.hpp"
9#include "alpaka/concepts.hpp"
11#include "alpaka/tag.hpp"
12
13#include <tuple>
14
15namespace alpaka::onHost
16{
17 /** @brief Concept for a combination of an API and device kind
18 *
19 * @details
20 * A device specification means the combination of an API and a device kind. Multiple instances of
21 * alpaka::onHost::Device can exist for the same device specification, for example in the form of multiple GPUs of
22 * the same type in one system.
23 *
24 * To check whether a specific combination is valid, i.e., whether an API can target a device kind, the static
25 * isValid() method can be used.
26 */
27 template<alpaka::concepts::Api T_Api, alpaka::concepts::DeviceKind T_DeviceKind>
29 {
30 public:
31 constexpr DeviceSpec(T_Api api, T_DeviceKind deviceType) : m_api(api), m_deviceType(deviceType)
32 {
33 }
34
35 template<alpaka::concepts::DeviceSpec T_DeviceSpec>
36 constexpr DeviceSpec(T_DeviceSpec any)
37 : m_api(alpaka::internal::getApi(any))
38 , m_deviceType(alpaka::internal::getDeviceKind(any))
39 {
40 }
41
42 constexpr DeviceSpec() = default;
43
44 constexpr T_DeviceKind getDeviceKind() const
45 {
46 return m_deviceType;
47 }
48
49 constexpr T_Api getApi() const
50 {
51 return m_api;
52 }
53
54 std::string getName() const
55 {
56 return m_api.getName() + " " + m_deviceType.getName();
57 }
58
59 /** Checks if the device kind and api combination is valid
60 *
61 * Reasons why a combination is valid can be that the api does not know how to talk to a device or that the
62 * required dependencies e.g. CUDA, HIP, or OneApi are not fulfilled.
63 *
64 * @return true if the device kind and api combination is valid, else false
65 */
66 static constexpr bool isValid()
67 {
70 else
71 return false;
72 }
73
74 private:
75 T_Api m_api;
76 T_DeviceKind m_deviceType;
77 };
78
79 template<alpaka::concepts::DeviceSpec T_DeviceSpec>
80 DeviceSpec(T_DeviceSpec const& deviceSpec)
81 -> DeviceSpec<ALPAKA_TYPEOF(getApi(deviceSpec)), ALPAKA_TYPEOF(getDeviceKind(deviceSpec))>;
82
83 /** Create a device specification object.
84 *
85 * @param any Any device specification description providing an api and device kind.
86 */
87 template<alpaka::concepts::DeviceSpec T_DeviceSpec>
88 inline auto makeDeviceSpec(T_DeviceSpec any)
89 {
90 return DeviceSpec{any};
91 }
92
93 /** list of enabled device specifications
94 *
95 * - device specifications can be dis-/enabled by the CMake options alpaka_<API>_<DeviceKindType>
96 * - the second way to disable a device specifications is to define the preprocessor define
97 * ALPAKA_DISABLE_<ApiType>_<DeviceKindType>, else the device specification is enabled
98 */
99 constexpr auto enabledDeviceSpecs = std::tuple_cat(
100 std::tuple<>{}
101#if !defined(ALPAKA_DISABLE_Host_Cpu)
102 ,
104#endif
105#if !defined(ALPAKA_DISABLE_Host_NumaCpu) && ALPAKA_HAS_HWLOC
106 ,
108#endif
109#if !defined(ALPAKA_DISABLE_OneApi_IntelGpu) && ALPAKA_LANG_ONEAPI
110 ,
112#endif
113#if !defined(ALPAKA_DISABLE_OneApi_NvidiaGpu) && ALPAKA_LANG_ONEAPI
114 ,
116#endif
117#if !defined(ALPAKA_DISABLE_OneApi_AmdGpu) && ALPAKA_LANG_ONEAPI
118 ,
120#endif
121#if !defined(ALPAKA_DISABLE_OneApi_Cpu) && ALPAKA_LANG_ONEAPI
122 ,
124#endif
125#if !defined(ALPAKA_DISABLE_Cuda_NvidiaGpu) && ALPAKA_LANG_CUDA
126 ,
128#endif
129#if !defined(ALPAKA_DISABLE_Hip_AmdGpu) && ALPAKA_LANG_HIP
130 ,
132#endif
133 );
134
135} // namespace alpaka::onHost
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
constexpr auto oneApi
Definition Api.hpp:29
constexpr auto hip
Definition Api.hpp:41
constexpr auto cuda
Definition Api.hpp:41
constexpr auto host
Definition Api.hpp:39
constexpr auto intelGpu
Definition tag.hpp:208
constexpr auto amdGpu
Definition tag.hpp:188
constexpr auto cpu
Definition tag.hpp:168
constexpr auto numaCpu
Definition tag.hpp:178
constexpr auto nvidiaGpu
Definition tag.hpp:198
Functionality which is usable on the host CPU controller thread.
Definition api.hpp:40
auto makeDeviceSpec(T_DeviceSpec any)
Create a device specification object.
DeviceSpec(T_DeviceSpec const &deviceSpec) -> DeviceSpec< ALPAKA_TYPEOF(getApi(deviceSpec)), ALPAKA_TYPEOF(getDeviceKind(deviceSpec))>
constexpr auto enabledDeviceSpecs
list of enabled device specifications
main alpaka namespace.
Definition alpaka.hpp:76
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
Concept for a combination of an API and device kind.
static constexpr bool isValid()
Checks if the device kind and api combination is valid.
std::string getName() const
constexpr T_DeviceKind getDeviceKind() const
constexpr DeviceSpec(T_DeviceSpec any)
constexpr T_Api getApi() const
constexpr DeviceSpec()=default
constexpr DeviceSpec(T_Api api, T_DeviceKind deviceType)