alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
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
10#include "alpaka/idx/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# if __has_include(<cuda/atomic>)
24# define ALPAKA_CUDA_ATOMIC
25# include <cuda/atomic>
26# if ALPAKA_COMP_CLANG_CUDA && defined(_Float16)
27# pragma clang diagnostic push
28# pragma clang diagnostic ignored "-Wreserved-identifier"
29// We see errors when using clang as the CUDA compiler if TBB is also enabled
30// Errors occour inside TBB because the _Float16 macro is redefined and pulled in from <cuda/atomic>
31# undef _Float16
32# pragma clang diagnostic pop
33# endif
34# endif
35# endif
36
37# ifdef ALPAKA_ACC_GPU_HIP_ENABLED
38# if ALPAKA_COMP_HIP >= ALPAKA_VERSION_NUMBER(6, 2, 0) && ALPAKA_COMP_HIP < ALPAKA_VERSION_NUMBER(7, 0, 0)
39# define HIP_ENABLE_WARP_SYNC_BUILTINS
40# endif
41# include <hip/hip_runtime.h>
42# endif
43
44namespace alpaka
45{
46 namespace detail
47 {
49 tuple<char1, double1, float1, int1, long1, longlong1, short1, uchar1, uint1, ulong1, ulonglong1, ushort1>;
51 tuple<char2, double2, float2, int2, long2, longlong2, short2, uchar2, uint2, ulong2, ulonglong2, ushort2>;
52 using CudaHipBuiltinTypes3 = std::tuple<
53 char3,
54 dim3,
55 double3,
56 float3,
57 int3,
58 long3,
59 longlong3,
60 short3,
61 uchar3,
62 uint3,
63 ulong3,
64 ulonglong3,
65 ushort3
66// CUDA built-in variables have special types in clang native CUDA compilation
67// defined in cuda_builtin_vars.h
68# if ALPAKA_COMP_CLANG_CUDA
69 ,
70 __cuda_builtin_threadIdx_t,
71 __cuda_builtin_blockIdx_t,
72 __cuda_builtin_blockDim_t,
73 __cuda_builtin_gridDim_t
74# endif
75 >;
76 using CudaHipBuiltinTypes4 = std::tuple<
77 char4,
78 float4,
79 int4,
80 short4,
81 uchar4,
82 uint4,
83 ushort4,
84 // double4, long4, longlong4, ulong4, ulonglong4 is deprecated in
85 // CUDA 13.0 and will be removed in CUDA 14.0
86# if defined(CUDART_VERSION) && (ALPAKA_VVRRP_TO_VERSION(CUDART_VERSION) >= ALPAKA_VERSION_NUMBER(13, 0, 0))
87 double4_16a,
88 double4_32a,
89 long4_16a,
90 long4_32a,
91 longlong4_16a,
92 longlong4_32a,
93 ulong4_16a,
94 ulong4_32a,
95 ulonglong4_16a,
96 ulonglong4_32a
97# else
98 double4,
99 long4,
100 longlong4,
101 ulong4,
102 ulonglong4
103# endif
104 >;
105
106 using CudaHipBuiltinTypes = meta::
107 Concatenate<CudaHipBuiltinTypes1, CudaHipBuiltinTypes2, CudaHipBuiltinTypes3, CudaHipBuiltinTypes4>;
108
109 template<typename T>
111 } // namespace detail
112
113# ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
114 namespace cuda::trait
115 {
116 template<typename T>
117 inline constexpr auto isCudaBuiltInType = alpaka::detail::isCudaHipBuiltInType<T>;
118 } // namespace cuda::trait
119# endif
120
121# ifdef ALPAKA_ACC_GPU_HIP_ENABLED
122 namespace hip::trait
123 {
124 template<typename T>
125 inline constexpr auto isHipBuiltInType = alpaka::detail::isCudaHipBuiltInType<T>;
126 } // namespace hip::trait
127# endif
128
129 namespace trait
130 {
131 //! The CUDA/HIP vectors 1D dimension get trait specialization.
132 template<typename T>
133 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes1, T>::value>>
134 {
136 };
137
138 //! The CUDA/HIP vectors 2D dimension get trait specialization.
139 template<typename T>
140 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes2, T>::value>>
141 {
143 };
144
145 //! The CUDA/HIP vectors 3D dimension get trait specialization.
146 template<typename T>
147 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes3, T>::value>>
148 {
150 };
151
152 //! The CUDA/HIP vectors 4D dimension get trait specialization.
153 template<typename T>
154 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes4, T>::value>>
155 {
157 };
158
159 //! The CUDA/HIP vectors elem type trait specialization.
160 template<typename T>
161 struct ElemType<T, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<T>>>
162 {
163 using type = decltype(std::declval<T>().x);
164 };
165
166 template<typename TCudaHipBuiltin>
167 struct GetExtents<TCudaHipBuiltin, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TCudaHipBuiltin>>>
168 {
170 ALPAKA_FN_HOST_ACC auto operator()(TCudaHipBuiltin const& value) const
172 {
173 constexpr auto dim = Dim<TCudaHipBuiltin>::value;
174 if constexpr(dim == 1)
175 return {value.x};
176 else if constexpr(dim == 2)
177 return {value.y, value.x};
178 else if constexpr(dim == 3)
179 return {value.z, value.y, value.x};
180 else if constexpr(dim == 4)
181 return {value.w, value.z, value.y, value.x};
182 else
183 static_assert(sizeof(value) == 0, "Not implemented");
184
186 }
187 };
188
189 template<typename TCudaHipBuiltin>
190 struct GetOffsets<TCudaHipBuiltin, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TCudaHipBuiltin>>>
191 : GetExtents<TCudaHipBuiltin>
192 {
193 };
194
195 //! The CUDA/HIP vectors idx type trait specialization.
196 template<typename TIdx>
197 struct IdxType<TIdx, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TIdx>>>
198 {
199 using type = std::size_t;
200 };
201 } // namespace trait
202} // namespace alpaka
203
204#endif
#define ALPAKA_UNREACHABLE(...)
Before CUDA 11.5 nvcc is unable to correctly identify return statements in 'if constexpr' branches....
A n-dimensional vector.
Definition Vec.hpp:38
#define ALPAKA_FN_HOST_ACC
Definition Common.hpp:42
#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:85
constexpr auto isCudaBuiltInType
std::tuple< char3, dim3, double3, float3, int3, long3, longlong3, short3, uchar3, uint3, ulong3, ulonglong3, ushort3 > CudaHipBuiltinTypes3
std::tuple< char4, float4, int4, short4, uchar4, uint4, ushort4, double4, long4, longlong4, ulong4, ulonglong4 > CudaHipBuiltinTypes4
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
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
STL namespace.
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