21 #ifndef ROCPRIM_DETAIL_VARIOUS_HPP_ 22 #define ROCPRIM_DETAIL_VARIOUS_HPP_ 24 #include <type_traits> 26 #include "../config.hpp" 27 #include "../types.hpp" 28 #include "../type_traits.hpp" 30 #include <hip/hip_runtime.h> 34 #define __has_include(x) 0 37 #if __has_include(<version>) // version is only mandated in c++20 39 #if __cpp_lib_as_const >= 201510L 48 BEGIN_ROCPRIM_NAMESPACE
58 ROCPRIM_HOST_DEVICE
inline 59 constexpr
bool is_power_of_two(
const T x)
61 static_assert(::rocprim::is_integral<T>::value,
"T must be integer type");
62 return (x > 0) && ((x & (x - 1)) == 0);
66 ROCPRIM_HOST_DEVICE
inline 67 constexpr T next_power_of_two(
const T x,
const T acc = 1)
69 static_assert(::rocprim::is_unsigned<T>::value,
"T must be unsigned type");
70 return acc >= x ? acc : next_power_of_two(x, 2 * acc);
76 std::enable_if_t<::rocprim::is_integral<T>::value && ::rocprim::is_unsigned<U>::value,
int> = 0>
77 ROCPRIM_HOST_DEVICE
inline constexpr
auto ceiling_div(
const T a,
const U b)
79 return a / b + (a % b > 0 ? 1 : 0);
82 ROCPRIM_HOST_DEVICE
inline 83 size_t align_size(
size_t size,
size_t alignment = 256)
85 return ceiling_div(size, alignment) * alignment;
91 ROCPRIM_HOST_DEVICE
inline 92 constexpr T warp_size_in_class(
const T
warp_size)
100 ROCPRIM_HOST_DEVICE
inline 101 constexpr T get_min_warp_size(
const T
block_size,
const T max_warp_size)
103 static_assert(::rocprim::is_unsigned<T>::value,
"T must be unsigned type");
104 return block_size >= max_warp_size ? max_warp_size : next_power_of_two(block_size);
107 template<
unsigned int WarpSize>
109 static const bool value = detail::is_power_of_two(WarpSize);
114 template<
class T,
unsigned int N>
117 static constexpr
unsigned int size =
sizeof(T) * N;
118 using vector_base_type =
119 typename std::conditional<
122 typename std::conditional<
129 using vector_4 =
typename make_vector_type<vector_base_type, 4>::type;
130 using vector_2 =
typename make_vector_type<vector_base_type, 2>::type;
131 using vector_1 =
typename make_vector_type<vector_base_type, 1>::type;
134 typename std::conditional<
135 size %
sizeof(vector_4) == 0,
137 typename std::conditional<
138 size %
sizeof(vector_2) == 0,
146 template<
class T,
unsigned int Items>
147 struct is_vectorizable : std::integral_constant<bool, (Items % 2 == 0) &&(sizeof(T) < sizeof(typename match_vector_type<T, Items>::type))> {};
151 constexpr unsigned int get_lds_banks_no()
160 struct match_fundamental_type
163 typename std::conditional<
166 typename std::conditional<
169 typename std::conditional<
179 template <typename T>
183 typedef typename detail::match_fundamental_type<T>::type device_word;
186 device_word storage[sizeof(T) / sizeof(device_word)];
189 ROCPRIM_HOST_DEVICE T& get()
191 return reinterpret_cast<T&>(*this);
194 ROCPRIM_HOST_DEVICE const T& get() const
196 return reinterpret_cast<const T&>(*this);
201 template<class Iterator1, class Iterator2>
202 inline bool can_iterators_alias(Iterator1, Iterator2, const size_t size)
208 template<typename Value1, typename Value2>
209 inline bool can_iterators_alias(Value1* iter1, Value2* iter2, const size_t size)
211 const uintptr_t start1 = reinterpret_cast<uintptr_t>(iter1);
212 const uintptr_t start2 = reinterpret_cast<uintptr_t>(iter2);
213 const uintptr_t end1 = reinterpret_cast<uintptr_t>(iter1 + size);
214 const uintptr_t end2 = reinterpret_cast<uintptr_t>(iter2 + size);
215 return start1 < end2 && start2 < end1;
222 struct type_identity {
226 template<class T, class = void>
227 struct extract_type_impl : type_identity<T> { };
230 struct extract_type_impl<T, void_t<typename T::type> > : extract_type_impl<typename T::type> { };
232 template <typename T>
233 using extract_type = typename extract_type_impl<T>::type;
235 template<bool Value, class T>
236 struct select_type_case
238 static constexpr bool value = Value;
242 template<class Case, class... OtherCases>
243 struct select_type_impl
246 type_identity<extract_type<typename Case::type>>,
247 select_type_impl<OtherCases...>
251 struct select_type_impl<select_type_case<true, T>> : type_identity<extract_type<T>> { };
254 struct select_type_impl<select_type_case<false, T>>
258 "Cannot select any case. "
259 "The last case must have true condition or be a fallback type."
263 template<class Fallback>
264 struct select_type_impl<Fallback> : type_identity<extract_type<Fallback>> { };
266 template <typename... Cases>
267 using select_type = typename select_type_impl<Cases...>::type;
269 template <bool Value>
270 using bool_constant = std::integral_constant<bool, Value>;
286 inline hipError_t memcpy_and_sync(
287 void* dst, const void* src, size_t size_bytes, hipMemcpyKind kind, hipStream_t stream)
290 #if(HIP_VERSION_MAJOR == 3 && HIP_VERSION_MINOR >= 1) || HIP_VERSION_MAJOR > 3
291 return hipMemcpyWithStream(dst, src, size_bytes, kind, stream);
293 const hipError_t result = hipMemcpyAsync(dst src, size_bytes, kind, stream);
294 if(hipSuccess != result)
298 return hipStreamSynchronize(stream);
302 #if __cpp_lib_as_const >= 201510L
303 using ::std::as_const;
306 constexpr std::add_const_t<T>& as_const(T& t) noexcept
311 void as_const(
const T&& t) =
delete;
326 template<
class... Types,
class Function,
size_t... Indices>
327 ROCPRIM_HOST_DEVICE
inline void for_each_in_tuple_impl(::rocprim::tuple<Types...>& t,
329 ::rocprim::index_sequence<Indices...>)
331 auto swallow = {(f(::rocprim::get<Indices>(t)), 0)...};
335 template<
class... Types,
class Function>
336 ROCPRIM_HOST_DEVICE
inline void for_each_in_tuple(::rocprim::tuple<Types...>& t, Function f)
338 for_each_in_tuple_impl(t, f, ::rocprim::index_sequence_for<Types...>());
342 END_ROCPRIM_NAMESPACE
344 #endif // ROCPRIM_DETAIL_VARIOUS_HPP_ Definition: various.hpp:147
Deprecated: Configuration of device-level scan primitives.
Definition: block_histogram.hpp:62
ROCPRIM_HOST_DEVICE constexpr unsigned int warp_size()
[DEPRECATED] Returns a number of threads in a hardware warp.
Definition: thread.hpp:42
Definition: various.hpp:52
Definition: various.hpp:115
ROCPRIM_DEVICE ROCPRIM_INLINE unsigned int block_size()
Returns block size in a multidimensional grid by dimension.
Definition: thread.hpp:268
constexpr std::add_const_t< T > * as_const_ptr(T *ptr)
Add const to the top level pointed to object type.
Definition: various.hpp:321
Definition: various.hpp:108