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)


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.

  • nearbyint is not available with the SYCL backend

  • round, logb, nextafter, copysign, and signbit are available since version 3.7

  • three-argument version of hypot is available since 4.0

  • fma is available since 4.0

  • Beware 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.3


See also#

Mathematical constant

Numeric traits