Skip to content

Mathematical Functions

Damien L-G edited this page Mar 1, 2022 · 1 revision

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::Experimental namespace.

Below is the synopsis for sqrt as an example of unary math function.

namespace Kokkos::Experimental {
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 denotes functions that are currently not provided by Kokkos

func* denotes functions not available with the SYCL backend

Basic operations abs fabs fmod remainder remquo fma fmax fmin fdim nan

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 lround llround nearbyint* rint lrint llrint

Floating point manipulation functions frexp ldexp modf scalbn scalbln ilog logb nextafter nexttoward copysign

Classification and comparison fpclassify isfinite isinf isnan isnormal isgreater isgreaterequal isless islessequal islessgreater isunordered


NOTE 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.


See also
Mathematical constants
Numeric traits

Clone this wiki locally