alpaka
Abstraction Library for Parallel Kernel Acceleration
CudaHipCommon.hpp
Go to the documentation of this file.
1 /* Copyright 2023 Axel Hübl, Benjamin Worpitz, Matthias Werner, René Widera, Andrea Bocci, Bernhard Manfred Gruber,
2  Jan Stephan
3  * SPDX-License-Identifier: MPL-2.0
4  */
5 
6 #pragma once
7 
8 #include "alpaka/elem/Traits.hpp"
10 #include "alpaka/idx/Traits.hpp"
13 #include "alpaka/offset/Traits.hpp"
14 #include "alpaka/vec/Vec.hpp"
15 
16 #include <tuple>
17 
18 #if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) || defined(ALPAKA_ACC_GPU_HIP_ENABLED)
19 
20 # ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
21 # include <cuda.h>
22 # include <cuda_runtime.h>
23 # endif
24 
25 # ifdef ALPAKA_ACC_GPU_HIP_ENABLED
26 # include <hip/hip_runtime.h>
27 # endif
28 
29 namespace alpaka
30 {
31  namespace detail
32  {
33  using CudaHipBuiltinTypes1 = std::
34  tuple<char1, double1, float1, int1, long1, longlong1, short1, uchar1, uint1, ulong1, ulonglong1, ushort1>;
35  using CudaHipBuiltinTypes2 = std::
36  tuple<char2, double2, float2, int2, long2, longlong2, short2, uchar2, uint2, ulong2, ulonglong2, ushort2>;
37  using CudaHipBuiltinTypes3 = std::tuple<
38  char3,
39  dim3,
40  double3,
41  float3,
42  int3,
43  long3,
44  longlong3,
45  short3,
46  uchar3,
47  uint3,
48  ulong3,
49  ulonglong3,
50  ushort3
51 // CUDA built-in variables have special types in clang native CUDA compilation
52 // defined in cuda_builtin_vars.h
53 # if BOOST_COMP_CLANG_CUDA
54  ,
55  __cuda_builtin_threadIdx_t,
56  __cuda_builtin_blockIdx_t,
57  __cuda_builtin_blockDim_t,
58  __cuda_builtin_gridDim_t
59 # endif
60  >;
61  using CudaHipBuiltinTypes4 = std::
62  tuple<char4, double4, float4, int4, long4, longlong4, short4, uchar4, uint4, ulong4, ulonglong4, ushort4>;
63  using CudaHipBuiltinTypes = meta::
64  Concatenate<CudaHipBuiltinTypes1, CudaHipBuiltinTypes2, CudaHipBuiltinTypes3, CudaHipBuiltinTypes4>;
65 
66  template<typename T>
68  } // namespace detail
69 
70 # ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
71  namespace cuda::trait
72  {
73  template<typename T>
74  inline constexpr auto isCudaBuiltInType = alpaka::detail::isCudaHipBuiltInType<T>;
75  } // namespace cuda::trait
76 # endif
77 
78 # ifdef ALPAKA_ACC_GPU_HIP_ENABLED
79  namespace hip::trait
80  {
81  template<typename T>
82  inline constexpr auto isHipBuiltInType = alpaka::detail::isCudaHipBuiltInType<T>;
83  } // namespace hip::trait
84 # endif
85 
86  namespace trait
87  {
88  //! The CUDA/HIP vectors 1D dimension get trait specialization.
89  template<typename T>
90  struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes1, T>::value>>
91  {
92  using type = DimInt<1u>;
93  };
94 
95  //! The CUDA/HIP vectors 2D dimension get trait specialization.
96  template<typename T>
97  struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes2, T>::value>>
98  {
99  using type = DimInt<2u>;
100  };
101 
102  //! The CUDA/HIP vectors 3D dimension get trait specialization.
103  template<typename T>
104  struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes3, T>::value>>
105  {
106  using type = DimInt<3u>;
107  };
108 
109  //! The CUDA/HIP vectors 4D dimension get trait specialization.
110  template<typename T>
111  struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes4, T>::value>>
112  {
113  using type = DimInt<4u>;
114  };
115 
116  //! The CUDA/HIP vectors elem type trait specialization.
117  template<typename T>
118  struct ElemType<T, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<T>>>
119  {
120  using type = decltype(std::declval<T>().x);
121  };
122 
123  template<typename TCudaHipBuiltin>
124  struct GetExtents<TCudaHipBuiltin, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TCudaHipBuiltin>>>
125  {
127  ALPAKA_FN_HOST_ACC auto operator()(TCudaHipBuiltin const& value) const
129  {
130  constexpr auto dim = Dim<TCudaHipBuiltin>::value;
131  if constexpr(dim == 1)
132  return {value.x};
133  else if constexpr(dim == 2)
134  return {value.y, value.x};
135  else if constexpr(dim == 3)
136  return {value.z, value.y, value.x};
137  else if constexpr(dim == 4)
138  return {value.w, value.z, value.y, value.x};
139  else
140  static_assert(sizeof(value) == 0, "Not implemented");
141 
142  ALPAKA_UNREACHABLE({});
143  }
144  };
145 
146  template<typename TCudaHipBuiltin>
147  struct GetOffsets<TCudaHipBuiltin, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TCudaHipBuiltin>>>
148  : GetExtents<TCudaHipBuiltin>
149  {
150  };
151 
152  //! The CUDA/HIP vectors idx type trait specialization.
153  template<typename TIdx>
154  struct IdxType<TIdx, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TIdx>>>
155  {
156  using type = std::size_t;
157  };
158  } // namespace trait
159 } // namespace alpaka
160 
161 #endif
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
Definition: Unreachable.hpp:24
A n-dimensional vector.
Definition: Vec.hpp:38
#define ALPAKA_FN_HOST_ACC
Definition: Common.hpp:39
#define ALPAKA_NO_HOST_ACC_WARNING
Disable nvcc warning: 'calling a host function from host device function.' Usage: ALPAKA_NO_HOST_ACC_...
Definition: Common.hpp:82
constexpr auto isCudaBuiltInType
std::tuple< char3, dim3, double3, float3, int3, long3, longlong3, short3, uchar3, uint3, ulong3, ulonglong3, ushort3 > CudaHipBuiltinTypes3
constexpr auto isCudaHipBuiltInType
std::tuple< char2, double2, float2, int2, long2, longlong2, short2, uchar2, uint2, ulong2, ulonglong2, ushort2 > CudaHipBuiltinTypes2
std::tuple< char1, double1, float1, int1, long1, longlong1, short1, uchar1, uint1, ulong1, ulonglong1, ushort1 > CudaHipBuiltinTypes1
meta::Concatenate< CudaHipBuiltinTypes1, CudaHipBuiltinTypes2, CudaHipBuiltinTypes3, CudaHipBuiltinTypes4 > CudaHipBuiltinTypes
std::tuple< char4, double4, float4, int4, long4, longlong4, short4, uchar4, uint4, ulong4, ulonglong4, ushort4 > CudaHipBuiltinTypes4
The alpaka accelerator library.
typename trait::IdxType< T >::type Idx
Definition: Traits.hpp:29
std::integral_constant< std::size_t, N > DimInt
typename trait::DimType< T >::type Dim
The dimension type trait alias template to remove the ::type.
Definition: Traits.hpp:19
The dimension getter type trait.
Definition: Traits.hpp:14
The element type trait.
Definition: Traits.hpp:16
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto operator()(TCudaHipBuiltin const &value) const -> Vec< Dim< TCudaHipBuiltin >, Idx< TCudaHipBuiltin >>
The GetExtents trait for getting the extents of an object as an alpaka::Vec.
Definition: Traits.hpp:37
The GetOffsets trait for getting the offsets of an object as an alpaka::Vec.
Definition: Traits.hpp:33
The idx type trait.
Definition: Traits.hpp:25