Mathematical Functions#
The math functions APIs guarantee both CUDA and CPU compatibility, making it more straightforward to write __host__ __device__
functions without being concerned whether the underlying intrinsics will build and work.
#include <raft/core/math.hpp>
namespace raft::core
-
template<typename T1, typename T2, std::enable_if_t<CUDA_CONDITION_ELSE_TRUE(RAFT_DEPAREN(((!std::is_same_v<T1, __half> && !std::is_same_v<T2, __half>) || (!std::is_same_v<T1, nv_bfloat16> && !std::is_same_v<T2, nv_bfloat16>)))), int> = 0>
RAFT_INLINE_FUNCTION auto max(const T1 &x, const T2 &y)# The CUDA Math API has overloads for all combinations of float/double. We provide similar functionality while wrapping around std::max, which only supports arguments of the same type. However, though the CUDA Math API supports combinations of unsigned and signed integers, this is very error-prone so we do not support that and require the user to cast instead. (e.g the max of -1 and 1u is 4294967295u…)
When no overload matches, we provide a generic implementation but require that both types be the same (and that the less-than operator be defined).
-
template<typename T1, typename T2, typename ...Args>
RAFT_INLINE_FUNCTION auto max(const T1 &x, const T2 &y, Args&&... args)# Many-argument overload to avoid verbose nested calls or use with variadic arguments
-
template<typename T>
constexpr RAFT_INLINE_FUNCTION auto max(const T &x)# One-argument overload for convenience when using with variadic arguments
-
template<typename T1, typename T2, std::enable_if_t<CUDA_CONDITION_ELSE_TRUE(RAFT_DEPAREN(((!std::is_same_v<T1, __half> && !std::is_same_v<T2, __half>) || (!std::is_same_v<T1, nv_bfloat16> && !std::is_same_v<T2, nv_bfloat16>)))), int> = 0>
RAFT_INLINE_FUNCTION auto min(const T1 &x, const T2 &y)# Minimum Minimum of two or more values.
The CUDA Math API has overloads for all combinations of float/double. We provide similar functionality while wrapping around std::min, which only supports arguments of the same type. However, though the CUDA Math API supports combinations of unsigned and signed integers, this is very error-prone so we do not support that and require the user to cast instead. (e.g the min of -1 and 1u is 1u…)
When no overload matches, we provide a generic implementation but require that both types be the same (and that the less-than operator be defined).
-
template<typename T1, typename T2, typename ...Args>
RAFT_INLINE_FUNCTION auto min(const T1 &x, const T2 &y, Args&&... args)# Many-argument overload to avoid verbose nested calls or use with variadic arguments
-
template<typename T>
constexpr RAFT_INLINE_FUNCTION auto min(const T &x)# One-argument overload for convenience when using with variadic arguments
-
template<typename T, std::enable_if_t<CUDA_CONDITION_ELSE_TRUE(((!std::is_same_v<T, __half> && (!std::is_same_v<T, nv_bfloat16>)))), int> = 0>
RAFT_INLINE_FUNCTION auto sqrt(T x)# Square root
-
template<typename T>
RAFT_INLINE_FUNCTION auto abs(T x) -> std::enable_if_t<std::is_same_v<float, T> || std::is_same_v<double, T> || std::is_same_v<int, T> || std::is_same_v<long int, T> || std::is_same_v<long long int, T>, T>#
-
template<typename T, std::enable_if_t<CUDA_CONDITION_ELSE_TRUE(((!std::is_same_v<T, __half> && (!std::is_same_v<T, nv_bfloat16>)))), int> = 0>
RAFT_INLINE_FUNCTION auto cos(T x)# Cosine
-
template<typename T, std::enable_if_t<CUDA_CONDITION_ELSE_TRUE(((!std::is_same_v<T, __half> && (!std::is_same_v<T, nv_bfloat16>)))), int> = 0>
RAFT_INLINE_FUNCTION auto sin(T x)# Sine
- template<typename T> RAFT_INLINE_FUNCTION std::enable_if_t< std::is_same_v< float, T >||std::is_same_v< double, T > > sincos (const T &x, T *s, T *c)
Sine and cosine