alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
interface.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
8#include "alpaka/Vec.hpp"
13
14namespace alpaka
15{
16 /** alpaka internal implementations.
17 *
18 * @attention do not use any functions from this namespace in our user applications.
19 * The interface can change at any time without further notice and is for internal use only.
20 */
21 namespace internal
22 {
23 struct GetStaticName
24 {
25 template<typename T_Any>
26 struct Op
27 {
28 auto operator()([[maybe_unused]] T_Any const& any) const
29 {
30 if constexpr(requires { T_Any::getName(); })
31 return T_Any::getName();
32 else
33 return onHost::demangledName(any);
34 }
35 };
36 };
37
38 struct GetName
39 {
40 template<typename T_Any>
41 struct Op
42 {
43 auto operator()(T_Any const& any) const
44 {
45 return any.getName();
46 }
47 };
48 };
49
50 struct GetApi
51 {
52 template<typename T_Any>
53 struct Op
54 {
55 inline constexpr auto operator()(auto&& any) const -> decltype(any.getApi())
56 {
57 return any.getApi();
58 }
59 };
60 };
61
62 inline constexpr auto getApi(auto&& any) -> decltype(GetApi::Op<ALPAKA_TYPEOF(any)>{}(any))
63 {
64 return GetApi::Op<std::decay_t<decltype(any)>>{}(any);
65 }
66
67 template<typename T_Any>
68 inline constexpr auto getApi(onHost::Handle<T_Any>&& anyHandle)
69 {
70 return GetApi::Op<ALPAKA_TYPEOF(*anyHandle.get())>{}(*anyHandle.get());
71 }
72
73 struct GetExecutor
74 {
75 template<typename T_Any>
76 struct Op
77 {
78 inline constexpr auto operator()(auto&& any) const -> decltype(any.getExecutor())
79 {
80 return any.getExecutor();
81 }
82 };
83 };
84
85 template<typename T_Any>
86 inline constexpr auto getExecutor(T_Any&& any) -> decltype(GetExecutor::Op<ALPAKA_TYPEOF(any)>{}(any))
87 {
88 return GetExecutor::Op<std::decay_t<T_Any>>{}(ALPAKA_FORWARD(any));
89 }
90
91 template<typename T_Any>
92 inline constexpr auto getExecutor(onHost::Handle<T_Any>&& anyHandle)
93 {
94 return GetExecutor::Op<ALPAKA_TYPEOF(*anyHandle.get())>{}(*anyHandle.get());
95 }
96
97 struct GetDeviceType
98 {
99 template<typename T_Any>
100 struct Op
101 {
102 inline constexpr auto operator()(auto&& any) const -> decltype(any.getDeviceKind())
103 {
104 return any.getDeviceKind();
105 }
106 };
107 };
108
109 inline constexpr auto getDeviceKind(auto&& any) -> decltype(GetDeviceType::Op<ALPAKA_TYPEOF(any)>{}(any))
110 {
111 return GetDeviceType::Op<ALPAKA_TYPEOF(any)>{}(any);
112 }
113
114 struct GetAlignment
115 {
116 template<typename T_Any>
117 struct Op
118 {
119 constexpr auto operator()(auto&& any) const requires requires { any.getAlignment(); }
120 {
121 return any.getAlignment();
122 }
123
124 constexpr auto operator()(auto&& any) const
125 {
126 alpaka::unused(any);
127 return Alignment<>{};
128 }
129 };
130 };
131
132 constexpr auto getAlignment(auto&& any)
133 {
134 return GetAlignment::Op<std::decay_t<decltype(any)>>{}(any);
135 }
136
137 /** Load data from a data source as SIMD vector
138 *
139 * A data source is not required to have physical stored data, it can also be a generator, therefore only the
140 * data source knows how load create aSIMD vector.
141 */
142 struct LoadAsSimd
143 {
144 template<typename T_AnyDataSource, alpaka::concepts::Alignment T_Alignment, alpaka::concepts::Vector T_Idx>
145 struct Op
146 {
147 /** Get data as SIMD vector
148 *
149 * @see loadAsSimd for more details.
150 */
151 template<uint32_t T_simdWidth>
152 constexpr auto load(auto&& anyDataSource, T_Alignment dataAlignment, T_Idx const& index) const;
153 };
154 };
155
156 /** Get data as SIMD vector
157 *
158 * Load T_simdWidth contiguous data staring from index. The data is contiguous in the fast moving dimension of
159 * index.
160 *
161 * @tparam T_simdWidth number of elements in the SIMD vector
162 * @param anyDataSource data source to load data from
163 * @param dataAlignment Alignment of the data source resulting SIMD vector. This can be smaller or equal
164 * compared to the data source alignment due to possible offsets applied before.
165 * @param index Offset index relative to the first element of data source.
166 * @return SIMD vector with data loaded from the data source, aligned to dataAlignment
167 */
168 template<uint32_t T_simdWidth>
169 constexpr auto loadAsSimd(auto&& anyDataSource, auto dataAlignment, auto const& index)
170 {
171 return LoadAsSimd::Op<ALPAKA_TYPEOF(anyDataSource), ALPAKA_TYPEOF(dataAlignment), ALPAKA_TYPEOF(index)>{}
172 .template load<T_simdWidth>(ALPAKA_FORWARD(anyDataSource), dataAlignment, index);
173 }
174 } // namespace internal
175} // namespace alpaka
#define ALPAKA_TYPEOF(...)
Get the type of instance.
Definition common.hpp:154
#define ALPAKA_FORWARD(instance)
Perfectly forward an instance as argument.
Definition common.hpp:148
use source_location to derive the demangled type name based on: https://www.reddit....
constexpr bool any(alpaka::onAcc::concepts::Acc auto const &acc, int32_t predicate)
Evaluates predicate for all active threads of the warp.
Definition warp.hpp:83
std::shared_ptr< T > Handle
Definition Handle.hpp:30
constexpr auto demangledName()
main alpaka namespace.
Definition alpaka.hpp:76