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.


Basic operations

abs fabs

absolute value of a floating point value (\(|x|\))

fmod

remainder of the floating point division operation

remainder

signed remainder of the division operation

remquo [1]

signed remainder as well as the three last bits of the division operation

fma [2]

fused multiply-add operation

fmax

larger of two floating-point values

fmin

smaller of two floating point values

fdim

positive difference of two floating point values (\(\max(0, x-y)\))

nan

not-a-number (NaN)

Exponential functions

exp

returns \(e\) raised to the given power (\(e^x\))

exp2

returns \(2\) raised to the given power (\(2^x\))

expm1

returns \(e\) raised to the given power, minus \(1\) (\(e^x-1\))

log

base \(e\) logarithm of the given number (\(\log(x)\))

log10

base \(10\) logarithm of the given number (\(\log_{10}(x)\))

log2

base \(2\) logarithm of the given number (\(\log_{2}(x)\))

log1p

natural logarithm (to base \(e\)) of 1 plus the given number (\(\ln(1+x)\))

Power functions

pow

raises a number to the given power (\(x^y\))

sqrt

computes square root (\(\sqrt{x}\))

cbrt

computes cube root (\(\sqrt[3]{x}\))

hypot [5]

computes hypotenuse (\(\sqrt{x^2 + y^2}\) and \(\sqrt{x^2 + y^2 + z^2}\))

Trigonometric functions

sin

computes sine (\(\sin(x)\))

cos

computes cosine (\(\cos(x)\))

tan

computes tangent (\(\tan(x)\))

asin

computes arc sine (\(\arcsin(x)\))

acos

computes arc cosine (\(\arccos(x)\))

atan

computes arc tangent (\(\arctan(x)\))

atan2

arc tangent, using signs to determine quadrants

Hyperbolic functions

sinh

computes hyperbolic sine (\(\sinh(x)\))

cosh

computes hyperbolic cosine (\(\cosh(x)\))

tanh

computes hyperbolic tangent (\(\tanh(x)\))

asinh

computes the inverse hyperbolic sine (\(\text{arsinh}(x)\))

acosh

computes the inverse hyperbolic cosine (\(\text{arcosh}(x)\))

atanh

computes the inverse hyperbolic tangent (\(\text{artanh}(x)\))

Error and gamma functions

erf

error function

erfc

complementary error function

tgamma

gamma function

lgamma

natural logarithm of the gamma function

Nearest integer floating point operations

ceil

nearest integer not less than the given value

floor

nearest integer not greater than the given value

trunc

nearest integer not greater in magnitude than the given value

round lround [1] [4] llround [1] [4]

nearest integer, rounding away from zero in halfway cases

nearbyint [4]

nearest integer using current rounding mode

rint [1] lrint [1] [4] llrint [1] [4]

nearest integer using current rounding mode with exception if the result differs

Floating point manipulation functions

frexp [1]

decomposes a number into significand and base-\(2\) exponent

ldexp [1]

multiplies a number by \(2\) raised to an integral power

modf [1]

decomposes a number into integer and fractional parts

scalbn [1] scalbln [1]

multiplies a number by FLT_RADIX raised to a power

ilogb [1]

extracts exponent of the number

logb

extracts exponent of the number

nextafter nexttoward [3]

next representable floating-point value towards the given value

copysign

copies the sign of a floating point value

Classification and comparison

fpclassify [1]

categorizes the given floating-point value

isfinite

checks if the given number has finite value

isinf

checks if the given number is infinite

isnan

checks if the given number is NaN

isnormal [1]

checks if the given number is normal

signbit

checks if the given number is negative

isgreater [1]

checks if the first floating-point argument is greater than the second

isgreaterequal [1]

checks if the first floating-point argument is greater than or equal to the second

isless [1]

checks if the first floating-point argument is less than the second

islessequal [1]

checks if the first floating-point argument is less than or equal to the second

islessgreater [1]

checks if the first floating-point argument is less or greater than the second

isunordered [1]

checks if two floating-point values are unordered


Other (non-standard) functions

Note

These functions are not provided by the C++ standard library.

rsqrt [6]

computes the reciprocal square root (\(1/\sqrt{x}\))

rcp [1]

computes the reciprocal (\(1/x\))


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.

  • 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

  • Support for quadruple precision floating-point __float128 can be enabled via -DKokkos_ENABLE_LIBQUADMATH=ON.


See also

Mathematical constant

Numeric traits