Common math functions¶
Motivating example (borrowed from https://llvm.org/docs/CompileCudaWithLLVM.html#standard-library-support)
// clang is OK with everything in this function.
__device__ void test() {
std::sin(0.); // nvcc - ok
std::sin(0); // nvcc - error, because no std::sin(int) override is available.
sin(0); // nvcc - same as above.
sinf(0.); // nvcc - ok
std::sinf(0.); // nvcc - no such function
}
Kokkos’ goal is to provide a consistent overload set that is available on host and device and that follows practice from the C++ numerics library.
Defined in header <Kokkos_MathematicalFunctions.hpp> which is included from <Kokkos_Core.hpp>
Provides most of the standard C mathematical functions from <cmath>, such as fabs, sqrt, and sin.
Math functions are available in the Kokkos:: namespace since version 3.7, in Kokkos::Experimental in previous versions.
Below is the synopsis for sqrt as an example of unary math function.
namespace Kokkos { // (since 3.7)
KOKKOS_FUNCTION float sqrt ( float x );
KOKKOS_FUNCTION float sqrtf( float x );
KOKKOS_FUNCTION double sqrt ( double x );
long double sqrt ( long double x );
long double sqrtl( long double x );
KOKKOS_FUNCTION double sqrt ( IntegralType x );
}
The function is overloaded for any argument of arithmetic type. Additional functions with f and l suffixes that work on float and long double respectively are also available. Please note, that long double overloads are not available on the device.
See below the list of common mathematical functions supported. We refer the reader to cppreference.com for the synopsis of each individual function.
func* see notes below
Basic operations abs fabs fmod remainder fma* fmax fmin fdim nan (currently not provided by Kokkos: remquo)
Exponential functions exp exp2 expm1 log log10 log2 log1p
Power functions pow sqrt cbrt hypot*
Trigonometric functions sin cos tan asin acos atan atan2
Hyperbolic functions sinh cosh tanh asinh acosh atanh
Error and gamma functions erf erfc tgamma lgamma
Nearest integer floating point operations ceil floor trunc round* nearbyint* (currently not provided by Kokkos: lround llround rint lrint llrint)
Floating point manipulation functions logb* nextafter* copysign* (currently not provided by Kokkos: frexp ldexp modf scalbn scalbln ilog nexttoward)
Classification and comparison isfinite isinf isnan signbit* (currently not provided by Kokkos: fpclassify isnormal isgreater isgreaterequal isless islessequal islessgreater isunordered)
Other math functions not provided by the C++ standard library
rsqrt(x) reciprocal square root (i.e. computes \(\frac{1}{\sqrt(x)}\)) (since Kokkos 4.1)
Notes¶
Feel free to open an issue if you need one of the functions that is currently not implemented. Issue #4767 is keeping track of these and has notes about implementability.
nearbyintis not available with the SYCL backendround,logb,nextafter,copysign, andsignbitare available since version 3.7three-argument version of
hypotis available since 4.0fmais available since 4.0Beware the using-directive
using namespace Kokkos;will cause compilation errors with unqualified calls to math functions. Use explicit qualification (Kokkos::sqrt) or using-declaration (using Kokkos::sqrt;) instead. (See known issues)Math functions were removed from the
Kokkos::Experimental::namespace in version 4.3Support for quadruple precision floating-point
__float128can be enabled via-DKokkos_ENABLE_LIBQUADMATH=ON.