$darkmode
Eigen  5.0.1-dev
InteropHeaders.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  * InteropHeaders.h
15  *
16  * \brief:
17  * InteropHeaders
18  *
19  *****************************************************************/
20 
21 #ifndef EIGEN_INTEROP_HEADERS_SYCL_H
22 #define EIGEN_INTEROP_HEADERS_SYCL_H
23 
24 // IWYU pragma: private
25 #include "../../InternalHeaderCheck.h"
26 
27 namespace Eigen {
28 
29 #if !defined(EIGEN_DONT_VECTORIZE_SYCL)
30 
31 namespace internal {
32 
33 template <int has_blend, int lengths>
34 struct sycl_packet_traits : default_packet_traits {
35  enum {
36  Vectorizable = 1,
37  AlignedOnScalar = 1,
38  size = lengths,
39  HasDiv = 1,
40  HasLog = 1,
41  HasExp = 1,
42  HasSqrt = 1,
43  HasRsqrt = 1,
44  HasSin = 1,
45  HasCos = 1,
46  HasTan = 1,
47  HasASin = 1,
48  HasACos = 1,
49  HasATan = 1,
50  HasSinh = 1,
51  HasCosh = 1,
52  HasTanh = 1,
53  HasLGamma = 0,
54  HasDiGamma = 0,
55  HasZeta = 0,
56  HasPolygamma = 0,
57  HasErf = 0,
58  HasErfc = 0,
59  HasNdtri = 0,
60  HasIGamma = 0,
61  HasIGammac = 0,
62  HasBetaInc = 0,
63  HasBlend = has_blend,
64  // This flag is used to indicate whether packet comparison is supported.
65  // pcmp_eq, pcmp_lt and pcmp_le should be defined for it to be true.
66  HasCmp = 1,
67  HasMax = 1,
68  HasMin = 1,
69  HasMul = 1,
70  HasAdd = 1,
71  HasFloor = 1,
72  HasRound = 1,
73  HasRint = 1,
74  HasLog1p = 1,
75  HasExpm1 = 1,
76  HasCeil = 1,
77  };
78 };
79 
80 #ifdef SYCL_DEVICE_ONLY
81 #define SYCL_PACKET_TRAITS(packet_type, has_blend, unpacket_type, lengths) \
82  template <> \
83  struct packet_traits<unpacket_type> : sycl_packet_traits<has_blend, lengths> { \
84  typedef packet_type type; \
85  typedef packet_type half; \
86  };
87 
88 SYCL_PACKET_TRAITS(cl::sycl::cl_half8, 1, Eigen::half, 8)
89 SYCL_PACKET_TRAITS(cl::sycl::cl_half8, 1, const Eigen::half, 8)
90 SYCL_PACKET_TRAITS(cl::sycl::cl_float4, 1, float, 4)
91 SYCL_PACKET_TRAITS(cl::sycl::cl_float4, 1, const float, 4)
92 SYCL_PACKET_TRAITS(cl::sycl::cl_double2, 0, double, 2)
93 SYCL_PACKET_TRAITS(cl::sycl::cl_double2, 0, const double, 2)
94 #undef SYCL_PACKET_TRAITS
95 
96 // Make sure this is only available when targeting a GPU: we don't want to
97 // introduce conflicts between these packet_traits definitions and the ones
98 // we'll use on the host side (SSE, AVX, ...)
99 #define SYCL_ARITHMETIC(packet_type) \
100  template <> \
101  struct is_arithmetic<packet_type> { \
102  enum { value = true }; \
103  };
104 SYCL_ARITHMETIC(cl::sycl::cl_half8)
105 SYCL_ARITHMETIC(cl::sycl::cl_float4)
106 SYCL_ARITHMETIC(cl::sycl::cl_double2)
107 #undef SYCL_ARITHMETIC
108 
109 #define SYCL_UNPACKET_TRAITS(packet_type, unpacket_type, lengths) \
110  template <> \
111  struct unpacket_traits<packet_type> { \
112  typedef unpacket_type type; \
113  enum { size = lengths, vectorizable = true, alignment = Aligned16 }; \
114  typedef packet_type half; \
115  };
116 SYCL_UNPACKET_TRAITS(cl::sycl::cl_half8, Eigen::half, 8)
117 SYCL_UNPACKET_TRAITS(cl::sycl::cl_float4, float, 4)
118 SYCL_UNPACKET_TRAITS(cl::sycl::cl_double2, double, 2)
119 
120 #undef SYCL_UNPACKET_TRAITS
121 #endif
122 
123 } // end namespace internal
124 
125 #endif
126 
127 namespace TensorSycl {
128 namespace internal {
129 
130 template <typename PacketReturnType, int PacketSize>
131 struct PacketWrapper;
132 // This function should never get called on the device
133 #ifndef SYCL_DEVICE_ONLY
134 template <typename PacketReturnType, int PacketSize>
135 struct PacketWrapper {
136  typedef typename ::Eigen::internal::unpacket_traits<PacketReturnType>::type Scalar;
137  template <typename Index>
138  EIGEN_DEVICE_FUNC static Scalar scalarize(Index, PacketReturnType &) {
139  eigen_assert(false && "THERE IS NO PACKETIZE VERSION FOR THE CHOSEN TYPE");
140  abort();
141  }
142  EIGEN_DEVICE_FUNC static PacketReturnType convert_to_packet_type(Scalar in, Scalar) {
143  return ::Eigen::internal::template plset<PacketReturnType>(in);
144  }
145  EIGEN_DEVICE_FUNC static void set_packet(PacketReturnType, Scalar *) {
146  eigen_assert(false && "THERE IS NO PACKETIZE VERSION FOR THE CHOSEN TYPE");
147  abort();
148  }
149 };
150 
151 #elif defined(SYCL_DEVICE_ONLY)
152 template <typename PacketReturnType>
153 struct PacketWrapper<PacketReturnType, 4> {
154  typedef typename ::Eigen::internal::unpacket_traits<PacketReturnType>::type Scalar;
155  template <typename Index>
156  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static Scalar scalarize(Index index, PacketReturnType &in) {
157  switch (index) {
158  case 0:
159  return in.x();
160  case 1:
161  return in.y();
162  case 2:
163  return in.z();
164  case 3:
165  return in.w();
166  default:
167  // INDEX MUST BE BETWEEN 0 and 3.There is no abort function in SYCL kernel. so we cannot use abort here.
168  // The code will never reach here
169  __builtin_unreachable();
170  }
171  __builtin_unreachable();
172  }
173 
174  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType convert_to_packet_type(Scalar in, Scalar other) {
175  return PacketReturnType(in, other, other, other);
176  }
177  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static void set_packet(PacketReturnType &lhs, Scalar *rhs) {
178  lhs = PacketReturnType(rhs[0], rhs[1], rhs[2], rhs[3]);
179  }
180 };
181 
182 template <typename PacketReturnType>
183 struct PacketWrapper<PacketReturnType, 1> {
184  typedef typename ::Eigen::internal::unpacket_traits<PacketReturnType>::type Scalar;
185  template <typename Index>
186  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static Scalar scalarize(Index, PacketReturnType &in) {
187  return in;
188  }
189  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType convert_to_packet_type(Scalar in, Scalar) {
190  return PacketReturnType(in);
191  }
192  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static void set_packet(PacketReturnType &lhs, Scalar *rhs) { lhs = rhs[0]; }
193 };
194 
195 template <typename PacketReturnType>
196 struct PacketWrapper<PacketReturnType, 2> {
197  typedef typename ::Eigen::internal::unpacket_traits<PacketReturnType>::type Scalar;
198  template <typename Index>
199  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static Scalar scalarize(Index index, PacketReturnType &in) {
200  switch (index) {
201  case 0:
202  return in.x();
203  case 1:
204  return in.y();
205  default:
206  // INDEX MUST BE BETWEEN 0 and 1.There is no abort function in SYCL kernel. so we cannot use abort here.
207  // The code will never reach here
208  __builtin_unreachable();
209  }
210  __builtin_unreachable();
211  }
212 
213  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static PacketReturnType convert_to_packet_type(Scalar in, Scalar other) {
214  return PacketReturnType(in, other);
215  }
216  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE static void set_packet(PacketReturnType &lhs, Scalar *rhs) {
217  lhs = PacketReturnType(rhs[0], rhs[1]);
218  }
219 };
220 
221 #endif
222 
223 } // end namespace internal
224 } // end namespace TensorSycl
225 } // end namespace Eigen
226 
227 #endif // EIGEN_INTEROP_HEADERS_SYCL_H
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82