alpaka
Abstraction Library for Parallel Kernel Acceleration
Sycl.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Jan Stephan, Luca Ferragina, Aurora Perego, Andrea Bocci
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/elem/Traits.hpp"
9 #include "alpaka/idx/Traits.hpp"
11 #include "alpaka/offset/Traits.hpp"
12 #include "alpaka/vec/Vec.hpp"
13 
14 #include <array>
15 #include <cstddef>
16 #include <cstdio> // the #define printf(...) breaks <cstdio> if it is included afterwards
17 #include <iostream>
18 #include <stdexcept>
19 #include <string>
20 #include <type_traits>
21 #include <utility>
22 
23 #ifdef ALPAKA_ACC_SYCL_ENABLED
24 
25 # include <sycl/sycl.hpp>
26 
27 // if SYCL is enabled with the AMD backend the printf will be killed because of missing compiler support
28 # ifdef __AMDGCN__
29 # define printf(...)
30 # else
31 
32 # ifdef __SYCL_DEVICE_ONLY__
33 using AlpakaFormat = char const* [[clang::opencl_constant]];
34 # else
35 using AlpakaFormat = char const*;
36 # endif
37 
38 # if BOOST_COMP_CLANG
39 # pragma clang diagnostic push
40 # pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
41 # endif
42 
43 # define printf(FORMAT, ...) \
44  do \
45  { \
46  static auto const format = AlpakaFormat{FORMAT}; \
47  sycl::ext::oneapi::experimental::printf(format, ##__VA_ARGS__); \
48  } while(false)
49 
50 # if BOOST_COMP_CLANG
51 # pragma clang diagnostic pop
52 # endif
53 
54 # endif
55 
56 // SYCL vector types trait specializations.
57 namespace alpaka
58 {
59  namespace detail
60  {
61  // Remove std::is_same boilerplate
62  template<typename T, typename... Ts>
63  struct is_any : std::bool_constant<(std::is_same_v<T, Ts> || ...)>
64  {
65  };
66  } // namespace detail
67 
68  //! In contrast to CUDA SYCL doesn't know 1D vectors. It does
69  //! support OpenCL's data types which have additional requirements
70  //! on top of those in the C++ standard. Note that SYCL's equivalent
71  //! to CUDA's dim3 type is a different class type and thus not used
72  //! here.
73  template<typename T>
74  struct IsSyclBuiltInType
75  : detail::is_any<
76  T,
77  // built-in scalar types - these are the standard C++ built-in types, std::size_t, std::byte and
78  // sycl::half
79  sycl::half,
80 
81  // 2 component vector types
82  sycl::char2,
83  sycl::uchar2,
84  sycl::short2,
85  sycl::ushort2,
86  sycl::int2,
87  sycl::uint2,
88  sycl::long2,
89  sycl::ulong2,
90  sycl::float2,
91  sycl::double2,
92  sycl::half2,
93 
94  // 3 component vector types
95  sycl::char3,
96  sycl::uchar3,
97  sycl::short3,
98  sycl::ushort3,
99  sycl::int3,
100  sycl::uint3,
101  sycl::long3,
102  sycl::ulong3,
103  sycl::float3,
104  sycl::double3,
105  sycl::half3,
106 
107  // 4 component vector types
108  sycl::char4,
109  sycl::uchar4,
110  sycl::short4,
111  sycl::ushort4,
112  sycl::int4,
113  sycl::uint4,
114  sycl::long4,
115  sycl::ulong4,
116  sycl::float4,
117  sycl::double4,
118  sycl::half4,
119 
120  // 8 component vector types
121  sycl::char8,
122  sycl::uchar8,
123  sycl::short8,
124  sycl::ushort8,
125  sycl::int8,
126  sycl::uint8,
127  sycl::long8,
128  sycl::ulong8,
129  sycl::float8,
130  sycl::double8,
131  sycl::half8,
132 
133  // 16 component vector types
134  sycl::char16,
135  sycl::uchar16,
136  sycl::short16,
137  sycl::ushort16,
138  sycl::int16,
139  sycl::uint16,
140  sycl::long16,
141  sycl::ulong16,
142  sycl::float16,
143  sycl::double16,
144  sycl::half16>
145  {
146  };
147 } // namespace alpaka
148 
149 namespace alpaka::trait
150 {
151  //! SYCL's types get trait specialization.
152  template<typename T>
153  struct DimType<T, std::enable_if_t<IsSyclBuiltInType<T>::value>>
154  {
155  using type = std::conditional_t<std::is_scalar_v<T>, DimInt<std::size_t{1}>, DimInt<T::size()>>;
156  };
157 
158  //! The SYCL vectors' elem type trait specialization.
159  template<typename T>
160  struct ElemType<T, std::enable_if_t<IsSyclBuiltInType<T>::value>>
161  {
162  using type = std::conditional_t<std::is_scalar_v<T>, T, typename T::element_type>;
163  };
164 
165  //! The SYCL vectors' extent get trait specialization.
166  template<typename T>
167  struct GetExtents<T, std::enable_if_t<IsSyclBuiltInType<T>::value>>
168  {
169  auto operator()(T const& value) const
170  {
171  if constexpr(std::is_scalar_v<T>)
172  return value;
173  else
174  return impl(value, std::make_index_sequence<Dim<T>::value>{});
175  }
176 
177  private:
178  template<std::size_t... Is>
179  auto impl(T const& value, std::index_sequence<Is...>) const
180  {
181  return Vec{value.template swizzle<Is>()...};
182  }
183  };
184 
185  //! The SYCL vectors' offset get trait specialization.
186  template<typename T>
187  struct GetOffsets<T, std::enable_if_t<IsSyclBuiltInType<T>::value>> : GetExtents<T>
188  {
189  };
190 
191  //! The SYCL vectors' idx type trait specialization.
192  template<typename TIdx>
193  struct IdxType<TIdx, std::enable_if_t<IsSyclBuiltInType<TIdx>::value>>
194  {
195  using type = std::size_t;
196  };
197 } // namespace alpaka::trait
198 
199 #endif
The accelerator traits.
The alpaka accelerator library.
ALPAKA_FN_HOST_ACC Vec(TFirstIndex &&, TRestIndices &&...) -> Vec< DimInt< 1+sizeof...(TRestIndices)>, std::decay_t< TFirstIndex >>
std::integral_constant< std::size_t, N > DimInt