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¶
absolute value of a floating point value (\(|x|\)) |
|
remainder of the floating point division operation |
|
signed remainder of the division operation |
|
signed remainder as well as the three last bits of the division operation |
|
fused multiply-add operation |
|
larger of two floating-point values |
|
smaller of two floating point values |
|
positive difference of two floating point values (\(\max(0, x-y)\)) |
|
not-a-number (NaN) |
Exponential functions¶
returns \(e\) raised to the given power (\(e^x\)) |
|
returns \(2\) raised to the given power (\(2^x\)) |
|
returns \(e\) raised to the given power, minus \(1\) (\(e^x-1\)) |
|
base \(e\) logarithm of the given number (\(\log(x)\)) |
|
base \(10\) logarithm of the given number (\(\log_{10}(x)\)) |
|
base \(2\) logarithm of the given number (\(\log_{2}(x)\)) |
|
natural logarithm (to base \(e\)) of 1 plus the given number (\(\ln(1+x)\)) |
Power functions¶
raises a number to the given power (\(x^y\)) |
|
computes square root (\(\sqrt{x}\)) |
|
computes cube root (\(\sqrt[3]{x}\)) |
|
computes hypotenuse (\(\sqrt{x^2 + y^2}\) and \(\sqrt{x^2 + y^2 + z^2}\)) |
3-argument overload available since Kokkos 4.0
Trigonometric functions¶
Hyperbolic functions¶
computes hyperbolic sine (\(\sinh(x)\)) |
|
computes hyperbolic cosine (\(\cosh(x)\)) |
|
computes hyperbolic tangent (\(\tanh(x)\)) |
|
computes the inverse hyperbolic sine (\(\text{arsinh}(x)\)) |
|
computes the inverse hyperbolic cosine (\(\text{arcosh}(x)\)) |
|
computes the inverse hyperbolic tangent (\(\text{artanh}(x)\)) |
Error and gamma functions¶
Nearest integer floating point operations¶
nearest integer not less than the given value |
|
nearest integer not greater than the given value |
|
nearest integer not greater in magnitude than the given value |
|
nearest integer, rounding away from zero in halfway cases |
|
nearest integer using current rounding mode |
|
nearest integer using current rounding mode with exception if the result differs |
Floating point manipulation functions¶
decomposes a number into significand and base-\(2\) exponent |
|
multiplies a number by \(2\) raised to an integral power |
|
decomposes a number into integer and fractional parts |
|
multiplies a number by |
|
extracts exponent of the number |
|
extracts exponent of the number |
|
next representable floating-point value towards the given value |
|
copies the sign of a floating point value |
Classification and comparison¶
categorizes the given floating-point value |
|
checks if the given number has finite value |
|
checks if the given number is infinite |
|
checks if the given number is NaN |
|
checks if the given number is normal |
|
checks if the given number is negative |
|
checks if the first floating-point argument is greater than the second |
|
checks if the first floating-point argument is greater than or equal to the second |
|
checks if the first floating-point argument is less than the second |
|
checks if the first floating-point argument is less than or equal to the second |
|
checks if the first floating-point argument is less or greater than the second |
|
checks if two floating-point values are unordered |
Other (non-standard) functions¶
Note
These functions are not provided by the C++ standard library.
|
computes the reciprocal square root (\(1/\sqrt{x}\)) |
|
computes the reciprocal (\(1/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.
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.3Support for quadruple precision floating-point
__float128can be enabled via-DKokkos_ENABLE_LIBQUADMATH=ON.