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# endif
24
25# ifdef ALPAKA_ACC_GPU_HIP_ENABLED
26# if ALPAKA_COMP_HIP >= ALPAKA_VERSION_NUMBER(6, 2, 0) && ALPAKA_COMP_HIP < ALPAKA_VERSION_NUMBER(7, 0, 0)
27# define HIP_ENABLE_WARP_SYNC_BUILTINS
28# endif
29# include <hip/hip_runtime.h>
30# endif
31
32namespace alpaka
33{
34 namespace detail
35 {
37 tuple<char1, double1, float1, int1, long1, longlong1, short1, uchar1, uint1, ulong1, ulonglong1, ushort1>;
39 tuple<char2, double2, float2, int2, long2, longlong2, short2, uchar2, uint2, ulong2, ulonglong2, ushort2>;
40 using CudaHipBuiltinTypes3 = std::tuple<
41 char3,
42 dim3,
43 double3,
44 float3,
45 int3,
46 long3,
47 longlong3,
48 short3,
49 uchar3,
50 uint3,
51 ulong3,
52 ulonglong3,
53 ushort3
54// CUDA built-in variables have special types in clang native CUDA compilation
55// defined in cuda_builtin_vars.h
56# if ALPAKA_COMP_CLANG_CUDA
57 ,
58 __cuda_builtin_threadIdx_t,
59 __cuda_builtin_blockIdx_t,
60 __cuda_builtin_blockDim_t,
61 __cuda_builtin_gridDim_t
62# endif
63 >;
64 using CudaHipBuiltinTypes4 = std::tuple<
65 char4,
66 float4,
67 int4,
68 short4,
69 uchar4,
70 uint4,
71 ushort4,
72 // double4, long4, longlong4, ulong4, ulonglong4 is deprecated in
73 // CUDA 13.0 and will be removed in CUDA 14.0
74# if defined(CUDART_VERSION) && (ALPAKA_VVRRP_TO_VERSION(CUDART_VERSION) >= ALPAKA_VERSION_NUMBER(13, 0, 0))
75 double4_16a,
76 double4_32a,
77 long4_16a,
78 long4_32a,
79 longlong4_16a,
80 longlong4_32a,
81 ulong4_16a,
82 ulong4_32a,
83 ulonglong4_16a,
84 ulonglong4_32a
85# else
86 double4,
87 long4,
88 longlong4,
89 ulong4,
90 ulonglong4
91# endif
92 >;
93
94 using CudaHipBuiltinTypes = meta::
95 Concatenate<CudaHipBuiltinTypes1, CudaHipBuiltinTypes2, CudaHipBuiltinTypes3, CudaHipBuiltinTypes4>;
96
97 template<typename T>
99 } // namespace detail
100
101# ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
102 namespace cuda::trait
103 {
104 template<typename T>
105 inline constexpr auto isCudaBuiltInType = alpaka::detail::isCudaHipBuiltInType<T>;
106 } // namespace cuda::trait
107# endif
108
109# ifdef ALPAKA_ACC_GPU_HIP_ENABLED
110 namespace hip::trait
111 {
112 template<typename T>
113 inline constexpr auto isHipBuiltInType = alpaka::detail::isCudaHipBuiltInType<T>;
114 } // namespace hip::trait
115# endif
116
117 namespace trait
118 {
119 //! The CUDA/HIP vectors 1D dimension get trait specialization.
120 template<typename T>
121 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes1, T>::value>>
122 {
124 };
125
126 //! The CUDA/HIP vectors 2D dimension get trait specialization.
127 template<typename T>
128 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes2, T>::value>>
129 {
131 };
132
133 //! The CUDA/HIP vectors 3D dimension get trait specialization.
134 template<typename T>
135 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes3, T>::value>>
136 {
138 };
139
140 //! The CUDA/HIP vectors 4D dimension get trait specialization.
141 template<typename T>
142 struct DimType<T, std::enable_if_t<meta::Contains<alpaka::detail::CudaHipBuiltinTypes4, T>::value>>
143 {
145 };
146
147 //! The CUDA/HIP vectors elem type trait specialization.
148 template<typename T>
149 struct ElemType<T, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<T>>>
150 {
151 using type = decltype(std::declval<T>().x);
152 };
153
154 template<typename TCudaHipBuiltin>
155 struct GetExtents<TCudaHipBuiltin, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TCudaHipBuiltin>>>
156 {
158 ALPAKA_FN_HOST_ACC auto operator()(TCudaHipBuiltin const& value) const
160 {
161 constexpr auto dim = Dim<TCudaHipBuiltin>::value;
162 if constexpr(dim == 1)
163 return {value.x};
164 else if constexpr(dim == 2)
165 return {value.y, value.x};
166 else if constexpr(dim == 3)
167 return {value.z, value.y, value.x};
168 else if constexpr(dim == 4)
169 return {value.w, value.z, value.y, value.x};
170 else
171 static_assert(sizeof(value) == 0, "Not implemented");
172
174 }
175 };
176
177 template<typename TCudaHipBuiltin>
178 struct GetOffsets<TCudaHipBuiltin, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TCudaHipBuiltin>>>
179 : GetExtents<TCudaHipBuiltin>
180 {
181 };
182
183 //! The CUDA/HIP vectors idx type trait specialization.
184 template<typename TIdx>
185 struct IdxType<TIdx, std::enable_if_t<alpaka::detail::isCudaHipBuiltInType<TIdx>>>
186 {
187 using type = std::size_t;
188 };
189 } // namespace trait
190} // namespace alpaka
191
192#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