TensorSyclTuple.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Mehdi Goli Codeplay Software Ltd.
5 // Ralph Potter Codeplay Software Ltd.
6 // Luke Iwanski Codeplay Software Ltd.
7 // Contact: <eigen@codeplay.com>
8 //
9 // This Source Code Form is subject to the terms of the Mozilla
10 // Public License v. 2.0. If a copy of the MPL was not distributed
11 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 
13 /*****************************************************************
14  * TensroSyclTuple.h
15  *
16  * \brief:
17  * Minimal implementation of std::tuple that can be used inside a SYCL kernel.
18  *
19 *****************************************************************/
20 
21 #ifndef UNSUPPORTED_EIGEN_CXX11_SRC_TENSOR_TENSORSYCL_TUPLE_HPP
22 #define UNSUPPORTED_EIGEN_CXX11_SRC_TENSOR_TENSORSYCL_TUPLE_HPP
23 namespace utility {
24 namespace tuple {
28 template <bool, typename T = void> struct StaticIf;
30 template <typename T>
31 struct StaticIf<true, T> {
32  typedef T type;
33 };
34 
39 template <class... Ts>
40 struct Tuple {};
41 
46 template <class T, class... Ts>
47 struct Tuple<T, Ts...> {
48  Tuple(T t, Ts... ts) : head(t), tail(ts...) {}
49  T head;
50  Tuple<Ts...> tail;
51 };
52 
58 template <size_t, class>
60 
63 template <class T, class... Ts>
64 struct ElemTypeHolder<0, Tuple<T, Ts...> > {
65  typedef T type;
66 };
67 
74 template <size_t k, class T, class... Ts>
75 struct ElemTypeHolder<k, Tuple<T, Ts...> > {
76  typedef typename ElemTypeHolder<k - 1, Tuple<Ts...> >::type type;
77 };
78 
85 
86 #define TERMINATE_CONDS_TUPLE_GET(CVQual) \
87 template <size_t k, class... Ts> \
88 typename StaticIf<k == 0, CVQual typename ElemTypeHolder<0, Tuple<Ts...> >::type &>::type \
89 get(CVQual Tuple<Ts...> &t) { \
90  static_assert(sizeof...(Ts)!=0, "The requseted value is bigger than the size of the tuple"); \
91  return t.head; \
92 }
93 
94 TERMINATE_CONDS_TUPLE_GET(const)
95 TERMINATE_CONDS_TUPLE_GET()
96 #undef TERMINATE_CONDS_TUPLE_GET
97 #define RECURSIVE_TUPLE_GET(CVQual) \
105 template <size_t k, class T, class... Ts> \
106 typename StaticIf<k != 0, CVQual typename ElemTypeHolder<k, Tuple<T, Ts...> >::type &>::type \
107 get(CVQual Tuple<T, Ts...> &t) { \
108  return utility::tuple::get<k - 1>(t.tail); \
109 }
110 RECURSIVE_TUPLE_GET(const)
111 RECURSIVE_TUPLE_GET()
112 #undef RECURSIVE_TUPLE_GET
113 
120 template <typename... Args>
121 Tuple<Args...> make_tuple(Args... args) {
122  return Tuple<Args...>(args...);
123 }
124 
130 template <typename... Args>
131 static constexpr size_t size(Tuple<Args...> &) {
132  return sizeof...(Args);
133 }
134 
138 template <size_t... Is>
139 struct IndexList {};
140 
147 template <size_t MIN, size_t N, size_t... Is>
149 
154 template <size_t MIN, size_t... Is>
155 struct RangeBuilder<MIN, MIN, Is...> {
156  typedef IndexList<Is...> type;
157 };
158 
165 template <size_t MIN, size_t N, size_t... Is>
166 struct RangeBuilder : public RangeBuilder<MIN, N - 1, N - 1, Is...> {};
167 
171 template <size_t MIN, size_t MAX>
172 struct IndexRange: RangeBuilder<MIN, MAX>::type {};
173 
183 template <typename... Args, typename T, size_t... I>
184 Tuple<Args..., T> append_base(Tuple<Args...> t, T a,IndexList<I...>) {
185  return utility::tuple::make_tuple(get<I>(t)..., a);
186 }
187 
196 template <typename... Args, typename T>
197 Tuple<Args..., T> append(Tuple<Args...> t, T a) {
198  return utility::tuple::append_base(t, a, IndexRange<0, sizeof...(Args)>());
199 }
200 
214 template <typename... Args1, typename... Args2, size_t... I1, size_t... I2>
215 Tuple<Args1..., Args2...> append_base(Tuple<Args1...> t1, Tuple<Args2...> t2, IndexList<I1...>, IndexList<I2...>) {
216  return utility::tuple::make_tuple(get<I1>(t1)...,get<I2>(t2)...);
217 }
218 
228 template <typename... Args1, typename... Args2>
229 Tuple<Args1..., Args2...> append(Tuple<Args1...> t1,Tuple<Args2...> t2) {
230  return utility::tuple::append_base(t1, t2, IndexRange<0, sizeof...(Args1)>(), IndexRange<0, sizeof...(Args2)>());
231 }
232 } // tuple
233 } // utility
234 #endif // UNSUPPORTED_EIGEN_CXX11_SRC_TENSOR_TENSORSYCL_TUPLE_HPP
The StaticIf struct is used to statically choose the type based on the condition. ...
Definition: TensorSyclTuple.h:28
ElemTypeHolder class is used to specify the types of the elements inside the tuple.
Definition: TensorSyclTuple.h:59
Collects internal details for generating index ranges [MIN, MAX) Declare primary template for index r...
Definition: TensorSyclTuple.h:148
IndexRange that returns a [MIN, MAX) index range.
Definition: TensorSyclTuple.h:172
is a fixed-size collection of heterogeneous values Ts... - the types of the elements that the tuple ...
Definition: TensorSyclTuple.h:40
Definition: TensorSyclTuple.h:23
Creates a list of index from the elements in the tuple.
Definition: TensorSyclTuple.h:139