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.
denotes functions that are currently not provided by Kokkosfunc
func* see notes below
Basic operations
abs
fabs
fmod
remainder
remquofma*
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*
lroundllroundnearbyint*
rintlrintllrint
Floating point manipulation functions
frexpldexpmodfscalbnscalblniloglogb*
nextafter*
nexttowardcopysign*
Classification and comparison
fpclassifyisfinite
isinf
isnan
isnormalsignbit*
isgreaterisgreaterequalislessislessequalislessgreaterisunordered
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.0