HIP: Heterogenous-computing Interface for Portability
|
__global__
Functions__launch_bounds__
HIP provides a C++ syntax that is suitable for compiling most code that commonly appears in compute kernels, including classes, namespaces, operator overloading, templates and more. Additionally, it defines other language features designed specifically to target accelerators, such as the following:
This section describes the built-in variables and functions accessible from the HIP kernel. It’s intended for readers who are familiar with Cuda kernel syntax and want to understand how HIP is different.
Features are marked with one of the following keywords:
Supported __device__
functions are
The __device__
keyword can combine with the host keyword (see host).
Supported __global__
functions are
HIP __global__
functions must have a void
return type, and the first parameter to a HIP __global__
function must have the type hipLaunchParm
. See Kernel-Launch Example.
HIP lacks dynamic-parallelism support, so __global__
functions cannot be called from the device.
Supported __host__
functions are
__host__
can combine with __device__
, in which case the function compiles for both the host and device. These functions cannot use the HIP grid coordinate functions (for example, "hipThreadIdx_x"). A possible workaround is to pass the necessary coordinate info as an argument to the function.
__host__
cannot combine with __global__
.
HIP parses the __noinline__
and __forceinline__
keywords and converts them to the appropriate Clang attributes.
__global__
functions are often referred to as kernels, and calling one is termed launching the kernel. These functions require the caller to specify an "execution configuration" that includes the grid and block dimensions. The execution configuration can also include other information for the launch, such as the amount of additional shared memory to allocate and the stream where the kernel should execute. HIP introduces a standard C++ calling convention to pass the execution configuration to the kernel in addition to the Cuda <<< >>> syntax. In HIP,
The hipLaunchKernel macro always starts with the five parameters specified above, followed by the kernel arguments. HIPIFY tools optionally convert Cuda launch syntax to hipLaunchKernel, including conversion of optional arguments in <<< >>> to the five required hipLaunchKernel parameters. The dim3 constructor accepts zero to three arguments and will by default initialize unspecified dimensions to 1. See dim3. The kernel uses the coordinate built-ins (hipThread*, hipBlock*, hipGrid*) to determine coordinate index and coordinate bounds of the work item that’s currently executing. See Coordinate Built-Ins.
The __constant__
keyword is supported. The host writes constant memory before launching the kernel; from the GPU, this memory is read-only during kernel execution. The functions for accessing constant memory (hipGetSymbolAddress(), hipGetSymbolSize(), hipMemcpyToSymbol(), hipMemcpyToSymbolAsync(), hipMemcpyFromSymbol(), hipMemcpyFromSymbolAsync()) are available.
The __shared__
keyword is supported.
extern __shared__
allows the host to dynamically allocate shared memory and is specified as a launch parameter. HIP uses an alternate syntax based on the HIP_DYNAMIC_SHARED macro.
Managed memory, including the __managed__
keyword, are not supported in HIP.
The __restrict__
keyword tells the compiler that the associated memory pointer will not alias with any other pointer in the kernel or function. This feature can help the compiler generate better code. In most cases, all pointer arguments must use this keyword to realize the benefit.
These built-ins determine the coordinate of the active work item in the execution grid. They are defined in hip_runtime.h (rather than being implicitly defined by the compiler).
HIP Syntax | Cuda Syntax |
---|---|
hipThreadIdx_x | threadIdx.x |
hipThreadIdx_y | threadIdx.y |
hipThreadIdx_z | threadIdx.z |
hipBlockIdx_x | blockIdx.x |
hipBlockIdx_y | blockIdx.y |
hipBlockIdx_z | blockIdx.z |
hipBlockDim_x | blockDim.x |
hipBlockDim_y | blockDim.y |
hipBlockDim_z | blockDim.z |
hipGridDim_x | gridDim.x |
hipGridDim_y | gridDim.y |
hipGridDim_z | gridDim.z |
The warpSize variable is of type int and contains the warp size (in threads) for the target device. Note that all current Nvidia devices return 32 for this variable, and all current AMD devices return 64. Device code should use the warpSize built-in to develop portable wave-aware code.
Note that these types are defined in hip_runtime.h and are not automatically provided by the compiler.
Short vector types derive from the basic integer and floating-point types. They are structures defined in hip_vector_types.h. The first, second, third and fourth components of the vector are accessible through the x
, y
, z
and w
fields, respectively. All the short vector types support a constructor function of the form make_<type_name>()
. For example, float4 make_float4(float x, float y, float z, float w)
creates a vector of type float4
and value (x,y,z,w)
.
HIP supports the following short vector formats:
dim3 is a three-dimensional integer vector type commonly used to specify grid and group dimensions. Unspecified dimensions are initialized to 1.
HIP supports __threadfence() and __threadfence_block().
HIP provides workaround for threadfence_system() under the HIP-Clang path. To enable the workaround, HIP should be built with environment variable HIP_COHERENT_HOST_ALLOC enabled. In addition,the kernels that use __threadfence_system() should be modified as follows:
The __syncthreads() built-in function is supported in HIP. The __syncthreads_count(int), __syncthreads_and(int) and __syncthreads_or(int) functions are under development.
HIP-Clang supports a set of math operations callable from the device.
Following is the list of supported single precision mathematical functions.
Function | Supported on Host | Supported on Device |
---|---|---|
float acosf ( float x ) Calculate the arc cosine of the input argument. | ✓ | ✓ |
float acoshf ( float x ) Calculate the nonnegative arc hyperbolic cosine of the input argument. | ✓ | ✓ |
float asinf ( float x ) Calculate the arc sine of the input argument. | ✓ | ✓ |
float asinhf ( float x ) Calculate the arc hyperbolic sine of the input argument. | ✓ | ✓ |
float atan2f ( float y, float x ) Calculate the arc tangent of the ratio of first and second input arguments. | ✓ | ✓ |
float atanf ( float x ) Calculate the arc tangent of the input argument. | ✓ | ✓ |
float atanhf ( float x ) Calculate the arc hyperbolic tangent of the input argument. | ✓ | ✓ |
float cbrtf ( float x ) Calculate the cube root of the input argument. | ✓ | ✓ |
float ceilf ( float x ) Calculate ceiling of the input argument. | ✓ | ✓ |
float copysignf ( float x, float y ) Create value with given magnitude, copying sign of second value. | ✓ | ✓ |
float cosf ( float x ) Calculate the cosine of the input argument. | ✓ | ✓ |
float coshf ( float x ) Calculate the hyperbolic cosine of the input argument. | ✓ | ✓ |
float erfcf ( float x ) Calculate the complementary error function of the input argument. | ✓ | ✓ |
float erff ( float x ) Calculate the error function of the input argument. | ✓ | ✓ |
float exp10f ( float x ) Calculate the base 10 exponential of the input argument. | ✓ | ✓ |
float exp2f ( float x ) Calculate the base 2 exponential of the input argument. | ✓ | ✓ |
float expf ( float x ) Calculate the base e exponential of the input argument. | ✓ | ✓ |
float expm1f ( float x ) Calculate the base e exponential of the input argument, minus 1. | ✓ | ✓ |
float fabsf ( float x ) Calculate the absolute value of its argument. | ✓ | ✓ |
float fdimf ( float x, float y ) Compute the positive difference between x and y . | ✓ | ✓ |
float floorf ( float x ) Calculate the largest integer less than or equal to x . | ✓ | ✓ |
float fmaf ( float x, float y, float z ) Compute x × y + z as a single operation. | ✓ | ✓ |
float fmaxf ( float x, float y ) Determine the maximum numeric value of the arguments. | ✓ | ✓ |
float fminf ( float x, float y ) Determine the minimum numeric value of the arguments. | ✓ | ✓ |
float fmodf ( float x, float y ) Calculate the floating-point remainder of x / y . | ✓ | ✓ |
float frexpf ( float x, int* nptr ) Extract mantissa and exponent of a floating-point value. | ✓ | ✗ |
float hypotf ( float x, float y ) Calculate the square root of the sum of squares of two arguments. | ✓ | ✓ |
int ilogbf ( float x ) Compute the unbiased integer exponent of the argument. | ✓ | ✓ |
__RETURN_TYPE1 isfinite ( float a ) Determine whether argument is finite. | ✓ | ✓ |
__RETURN_TYPE1 isinf ( float a ) Determine whether argument is infinite. | ✓ | ✓ |
__RETURN_TYPE1 isnan ( float a ) Determine whether argument is a NaN. | ✓ | ✓ |
float ldexpf ( float x, int exp ) Calculate the value of x ⋅ 2exp. | ✓ | ✓ |
float log10f ( float x ) Calculate the base 10 logarithm of the input argument. | ✓ | ✓ |
float log1pf ( float x ) Calculate the value of loge( 1 + x ). | ✓ | ✓ |
float logbf ( float x ) Calculate the floating point representation of the exponent of the input argument. | ✓ | ✓ |
float log2f ( float x ) Calculate the base 2 logarithm of the input argument. | ✓ | ✓ |
float logf ( float x ) Calculate the natural logarithm of the input argument. | ✓ | ✓ |
float modff ( float x, float* iptr ) Break down the input argument into fractional and integral parts. | ✓ | ✗ |
float nanf ( const char* tagp ) Returns "Not a Number" value. | ✗ | ✓ |
float nearbyintf ( float x ) Round the input argument to the nearest integer. | ✓ | ✓ |
float powf ( float x, float y ) Calculate the value of first argument to the power of second argument. | ✓ | ✓ |
float remainderf ( float x, float y ) Compute single-precision floating-point remainder. | ✓ | ✓ |
float remquof ( float x, float y, int* quo ) Compute single-precision floating-point remainder and part of quotient. | ✓ | ✗ |
float roundf ( float x ) Round to nearest integer value in floating-point. | ✓ | ✓ |
float scalbnf ( float x, int n ) Scale floating-point input by integer power of two. | ✓ | ✓ |
__RETURN_TYPE1 signbit ( float a ) Return the sign bit of the input. | ✓ | ✓ |
void sincosf ( float x, float* sptr, float* cptr ) Calculate the sine and cosine of the first input argument. | ✓ | ✗ |
float sinf ( float x ) Calculate the sine of the input argument. | ✓ | ✓ |
float sinhf ( float x ) Calculate the hyperbolic sine of the input argument. | ✓ | ✓ |
float sqrtf ( float x ) Calculate the square root of the input argument. | ✓ | ✓ |
float tanf ( float x ) Calculate the tangent of the input argument. | ✓ | ✓ |
float tanhf ( float x ) Calculate the hyperbolic tangent of the input argument. | ✓ | ✓ |
float truncf ( float x ) Truncate input argument to the integral part. | ✓ | ✓ |
float tgammaf ( float x ) Calculate the gamma function of the input argument. | ✓ | ✓ |
float erfcinvf ( float y ) Calculate the inverse complementary function of the input argument. | ✓ | ✓ |
float erfcxf ( float x ) Calculate the scaled complementary error function of the input argument. | ✓ | ✓ |
float erfinvf ( float y ) Calculate the inverse error function of the input argument. | ✓ | ✓ |
float fdividef ( float x, float y ) Divide two floating point values. | ✓ | ✓ |
float frexpf ( float x, int *nptr ) Extract mantissa and exponent of a floating-point value. | ✓ | ✓ |
float j0f ( float x ) Calculate the value of the Bessel function of the first kind of order 0 for the input argument. | ✓ | ✓ |
float j1f ( float x ) Calculate the value of the Bessel function of the first kind of order 1 for the input argument. | ✓ | ✓ |
float jnf ( int n, float x ) Calculate the value of the Bessel function of the first kind of order n for the input argument. | ✓ | ✓ |
float lgammaf ( float x ) Calculate the natural logarithm of the absolute value of the gamma function of the input argument. | ✓ | ✓ |
long long int llrintf ( float x ) Round input to nearest integer value. | ✓ | ✓ |
long long int llroundf ( float x ) Round to nearest integer value. | ✓ | ✓ |
long int lrintf ( float x ) Round input to nearest integer value. | ✓ | ✓ |
long int lroundf ( float x ) Round to nearest integer value. | ✓ | ✓ |
float modff ( float x, float *iptr ) Break down the input argument into fractional and integral parts. | ✓ | ✓ |
float nextafterf ( float x, float y ) Returns next representable single-precision floating-point value after argument. | ✓ | ✓ |
float norm3df ( float a, float b, float c ) Calculate the square root of the sum of squares of three coordinates of the argument. | ✓ | ✓ |
float norm4df ( float a, float b, float c, float d ) Calculate the square root of the sum of squares of four coordinates of the argument. | ✓ | ✓ |
float normcdff ( float y ) Calculate the standard normal cumulative distribution function. | ✓ | ✓ |
float normcdfinvf ( float y ) Calculate the inverse of the standard normal cumulative distribution function. | ✓ | ✓ |
float normf ( int dim, const float *a ) Calculate the square root of the sum of squares of any number of coordinates. | ✓ | ✓ |
float rcbrtf ( float x ) Calculate the reciprocal cube root function. | ✓ | ✓ |
float remquof ( float x, float y, int *quo ) Compute single-precision floating-point remainder and part of quotient. | ✓ | ✓ |
float rhypotf ( float x, float y ) Calculate one over the square root of the sum of squares of two arguments. | ✓ | ✓ |
float rintf ( float x ) Round input to nearest integer value in floating-point. | ✓ | ✓ |
float rnorm3df ( float a, float b, float c ) Calculate one over the square root of the sum of squares of three coordinates of the argument. | ✓ | ✓ |
float rnorm4df ( float a, float b, float c, float d ) Calculate one over the square root of the sum of squares of four coordinates of the argument. | ✓ | ✓ |
float rnormf ( int dim, const float *a ) Calculate the reciprocal of square root of the sum of squares of any number of coordinates. | ✓ | ✓ |
float scalblnf ( float x, long int n ) Scale floating-point input by integer power of two. | ✓ | ✓ |
void sincosf ( float x, float *sptr, float *cptr ) Calculate the sine and cosine of the first input argument. | ✓ | ✓ |
void sincospif ( float x, float *sptr, float *cptr ) Calculate the sine and cosine of the first input argument multiplied by PI. | ✓ | ✓ |
float y0f ( float x ) Calculate the value of the Bessel function of the second kind of order 0 for the input argument. | ✓ | ✓ |
float y1f ( float x ) Calculate the value of the Bessel function of the second kind of order 1 for the input argument. | ✓ | ✓ |
float ynf ( int n, float x ) Calculate the value of the Bessel function of the second kind of order n for the input argument. | ✓ | ✓ |
[1] __RETURN_TYPE is dependent on compiler. It is usually 'int' for C compilers and 'bool' for C++ compilers. ↩
Following is the list of supported double precision mathematical functions.
Function | Supported on Host | Supported on Device |
---|---|---|
double acos ( double x ) Calculate the arc cosine of the input argument. | ✓ | ✓ |
double acosh ( double x ) Calculate the nonnegative arc hyperbolic cosine of the input argument. | ✓ | ✓ |
double asin ( double x ) Calculate the arc sine of the input argument. | ✓ | ✓ |
double asinh ( double x ) Calculate the arc hyperbolic sine of the input argument. | ✓ | ✓ |
double atan ( double x ) Calculate the arc tangent of the input argument. | ✓ | ✓ |
double atan2 ( double y, double x ) Calculate the arc tangent of the ratio of first and second input arguments. | ✓ | ✓ |
double atanh ( double x ) Calculate the arc hyperbolic tangent of the input argument. | ✓ | ✓ |
double cbrt ( double x ) Calculate the cube root of the input argument. | ✓ | ✓ |
double ceil ( double x ) Calculate ceiling of the input argument. | ✓ | ✓ |
double copysign ( double x, double y ) Create value with given magnitude, copying sign of second value. | ✓ | ✓ |
double cos ( double x ) Calculate the cosine of the input argument. | ✓ | ✓ |
double cosh ( double x ) Calculate the hyperbolic cosine of the input argument. | ✓ | ✓ |
double erf ( double x ) Calculate the error function of the input argument. | ✓ | ✓ |
double erfc ( double x ) Calculate the complementary error function of the input argument. | ✓ | ✓ |
double exp ( double x ) Calculate the base e exponential of the input argument. | ✓ | ✓ |
double exp10 ( double x ) Calculate the base 10 exponential of the input argument. | ✓ | ✓ |
double exp2 ( double x ) Calculate the base 2 exponential of the input argument. | ✓ | ✓ |
double expm1 ( double x ) Calculate the base e exponential of the input argument, minus 1. | ✓ | ✓ |
double fabs ( double x ) Calculate the absolute value of the input argument. | ✓ | ✓ |
double fdim ( double x, double y ) Compute the positive difference between x and y . | ✓ | ✓ |
double floor ( double x ) Calculate the largest integer less than or equal to x . | ✓ | ✓ |
double fma ( double x, double y, double z ) Compute x × y + z as a single operation. | ✓ | ✓ |
double fmax ( double , double ) Determine the maximum numeric value of the arguments. | ✓ | ✓ |
double fmin ( double x, double y ) Determine the minimum numeric value of the arguments. | ✓ | ✓ |
double fmod ( double x, double y ) Calculate the floating-point remainder of x / y . | ✓ | ✓ |
double frexp ( double x, int* nptr ) Extract mantissa and exponent of a floating-point value. | ✓ | ✗ |
double hypot ( double x, double y ) Calculate the square root of the sum of squares of two arguments. | ✓ | ✓ |
int ilogb ( double x ) Compute the unbiased integer exponent of the argument. | ✓ | ✓ |
__RETURN_TYPE1 isfinite ( double a ) Determine whether argument is finite. | ✓ | ✓ |
__RETURN_TYPE1 isinf ( double a ) Determine whether argument is infinite. | ✓ | ✓ |
__RETURN_TYPE1 isnan ( double a ) Determine whether argument is a NaN. | ✓ | ✓ |
double ldexp ( double x, int exp ) Calculate the value of x ⋅ 2exp. | ✓ | ✓ |
double log ( double x ) Calculate the base e logarithm of the input argument. | ✓ | ✓ |
double log10 ( double x ) Calculate the base 10 logarithm of the input argument. | ✓ | ✓ |
double log1p ( double x ) Calculate the value of loge( 1 + x ). | ✓ | ✓ |
double log2 ( double x ) Calculate the base 2 logarithm of the input argument. | ✓ | ✓ |
double logb ( double x ) Calculate the floating point representation of the exponent of the input argument. | ✓ | ✓ |
double modf ( double x, double* iptr ) Break down the input argument into fractional and integral parts. | ✓ | ✗ |
double nan ( const char* tagp ) Returns "Not a Number" value. | ✗ | ✓ |
double nearbyint ( double x ) Round the input argument to the nearest integer. | ✓ | ✓ |
double pow ( double x, double y ) Calculate the value of first argument to the power of second argument. | ✓ | ✓ |
double remainder ( double x, double y ) Compute double-precision floating-point remainder. | ✓ | ✓ |
double remquo ( double x, double y, int* quo ) Compute double-precision floating-point remainder and part of quotient. | ✓ | ✗ |
double round ( double x ) Round to nearest integer value in floating-point. | ✓ | ✓ |
double scalbn ( double x, int n ) Scale floating-point input by integer power of two. | ✓ | ✓ |
__RETURN_TYPE1 signbit ( double a ) Return the sign bit of the input. | ✓ | ✓ |
double sin ( double x ) Calculate the sine of the input argument. | ✓ | ✓ |
void sincos ( double x, double* sptr, double* cptr ) Calculate the sine and cosine of the first input argument. | ✓ | ✗ |
double sinh ( double x ) Calculate the hyperbolic sine of the input argument. | ✓ | ✓ |
double sqrt ( double x ) Calculate the square root of the input argument. | ✓ | ✓ |
double tan ( double x ) Calculate the tangent of the input argument. | ✓ | ✓ |
double tanh ( double x ) Calculate the hyperbolic tangent of the input argument. | ✓ | ✓ |
double tgamma ( double x ) Calculate the gamma function of the input argument. | ✓ | ✓ |
double trunc ( double x ) Truncate input argument to the integral part. | ✓ | ✓ |
double erfcinv ( double y ) Calculate the inverse complementary function of the input argument. | ✓ | ✓ |
double erfcx ( double x ) Calculate the scaled complementary error function of the input argument. | ✓ | ✓ |
double erfinv ( double y ) Calculate the inverse error function of the input argument. | ✓ | ✓ |
double frexp ( float x, int *nptr ) Extract mantissa and exponent of a floating-point value. | ✓ | ✓ |
double j0 ( double x ) Calculate the value of the Bessel function of the first kind of order 0 for the input argument. | ✓ | ✓ |
double j1 ( double x ) Calculate the value of the Bessel function of the first kind of order 1 for the input argument. | ✓ | ✓ |
double jn ( int n, double x ) Calculate the value of the Bessel function of the first kind of order n for the input argument. | ✓ | ✓ |
double lgamma ( double x ) Calculate the natural logarithm of the absolute value of the gamma function of the input argument. | ✓ | ✓ |
long long int llrint ( double x ) Round input to nearest integer value. | ✓ | ✓ |
long long int llround ( double x ) Round to nearest integer value. | ✓ | ✓ |
long int lrint ( double x ) Round input to nearest integer value. | ✓ | ✓ |
long int lround ( double x ) Round to nearest integer value. | ✓ | ✓ |
double modf ( double x, double *iptr ) Break down the input argument into fractional and integral parts. | ✓ | ✓ |
double nextafter ( double x, double y ) Returns next representable single-precision floating-point value after argument. | ✓ | ✓ |
double norm3d ( double a, double b, double c ) Calculate the square root of the sum of squares of three coordinates of the argument. | ✓ | ✓ |
float norm4d ( double a, double b, double c, double d ) Calculate the square root of the sum of squares of four coordinates of the argument. | ✓ | ✓ |
double normcdf ( double y ) Calculate the standard normal cumulative distribution function. | ✓ | ✓ |
double normcdfinv ( double y ) Calculate the inverse of the standard normal cumulative distribution function. | ✓ | ✓ |
double rcbrt ( double x ) Calculate the reciprocal cube root function. | ✓ | ✓ |
double remquo ( double x, double y, int *quo ) Compute single-precision floating-point remainder and part of quotient. | ✓ | ✓ |
double rhypot ( double x, double y ) Calculate one over the square root of the sum of squares of two arguments. | ✓ | ✓ |
double rint ( double x ) Round input to nearest integer value in floating-point. | ✓ | ✓ |
double rnorm3d ( double a, double b, double c ) Calculate one over the square root of the sum of squares of three coordinates of the argument. | ✓ | ✓ |
double rnorm4d ( double a, double b, double c, double d ) Calculate one over the square root of the sum of squares of four coordinates of the argument. | ✓ | ✓ |
double rnorm ( int dim, const double *a ) Calculate the reciprocal of square root of the sum of squares of any number of coordinates. | ✓ | ✓ |
double scalbln ( double x, long int n ) Scale floating-point input by integer power of two. | ✓ | ✓ |
void sincos ( double x, double *sptr, double *cptr ) Calculate the sine and cosine of the first input argument. | ✓ | ✓ |
void sincospi ( double x, double *sptr, double *cptr ) Calculate the sine and cosine of the first input argument multiplied by PI. | ✓ | ✓ |
double y0f ( double x ) Calculate the value of the Bessel function of the second kind of order 0 for the input argument. | ✓ | ✓ |
double y1 ( double x ) Calculate the value of the Bessel function of the second kind of order 1 for the input argument. | ✓ | ✓ |
double yn ( int n, double x ) Calculate the value of the Bessel function of the second kind of order n for the input argument. | ✓ | ✓ |
[1] __RETURN_TYPE is dependent on compiler. It is usually 'int' for C compilers and 'bool' for C++ compilers. ↩
Following is the list of supported integer intrinsics. Note that intrinsics are supported on device only.
Function |
---|
unsigned int __brev ( unsigned int x ) Reverse the bit order of a 32 bit unsigned integer. |
unsigned long long int __brevll ( unsigned long long int x ) Reverse the bit order of a 64 bit unsigned integer. |
int __clz ( int x ) Return the number of consecutive high-order zero bits in a 32 bit integer. |
unsigned int __clz(unsigned int x) Return the number of consecutive high-order zero bits in 32 bit unsigned integer. |
int __clzll ( long long int x ) Count the number of consecutive high-order zero bits in a 64 bit integer. |
unsigned int __clzll(long long int x) Return the number of consecutive high-order zero bits in 64 bit signed integer. |
unsigned int __ffs(unsigned int x) Find the position of least signigicant bit set to 1 in a 32 bit unsigned integer.1 |
unsigned int __ffs(int x) Find the position of least signigicant bit set to 1 in a 32 bit signed integer. |
unsigned int __ffsll(unsigned long long int x) Find the position of least signigicant bit set to 1 in a 64 bit unsigned integer.1 |
unsigned int __ffsll(long long int x) Find the position of least signigicant bit set to 1 in a 64 bit signed integer. |
unsigned int __popc ( unsigned int x ) Count the number of bits that are set to 1 in a 32 bit integer. |
int __popcll ( unsigned long long int x ) Count the number of bits that are set to 1 in a 64 bit integer. |
int __mul24 ( int x, int y ) Multiply two 24bit integers. |
unsigned int __umul24 ( unsigned int x, unsigned int y ) Multiply two 24bit unsigned integers. |
[1] The HIP-Clang implementation of __ffs() and __ffsll() contains code to add a constant +1 to produce the ffs result format. For the cases where this overhead is not acceptable and programmer is willing to specialize for the platform, HIP-Clang provides __lastbit_u32_u32(unsigned int input) and __lastbit_u32_u64(unsigned long long int input). The index returned by __lastbit_ instructions starts at -1, while for ffs the index starts at 0.
Following is the list of supported floating-point intrinsics. Note that intrinsics are supported on device only.
Function |
---|
float __cosf ( float x ) Calculate the fast approximate cosine of the input argument. |
float __expf ( float x ) Calculate the fast approximate base e exponential of the input argument. |
float __frsqrt_rn ( float x ) Compute 1 / √x in round-to-nearest-even mode. |
float __fsqrt_rd ( float x ) Compute √x in round-down mode. |
float __fsqrt_rn ( float x ) Compute √x in round-to-nearest-even mode. |
float __fsqrt_ru ( float x ) Compute √x in round-up mode. |
float __fsqrt_rz ( float x ) Compute √x in round-towards-zero mode. |
float __log10f ( float x ) Calculate the fast approximate base 10 logarithm of the input argument. |
float __log2f ( float x ) Calculate the fast approximate base 2 logarithm of the input argument. |
float __logf ( float x ) Calculate the fast approximate base e logarithm of the input argument. |
float __powf ( float x, float y ) Calculate the fast approximate of xy. |
float __sinf ( float x ) Calculate the fast approximate sine of the input argument. |
float __tanf ( float x ) Calculate the fast approximate tangent of the input argument. |
double __dsqrt_rd ( double x ) Compute √x in round-down mode. |
double __dsqrt_rn ( double x ) Compute √x in round-to-nearest-even mode. |
double __dsqrt_ru ( double x ) Compute √x in round-up mode. |
double __dsqrt_rz ( double x ) Compute √x in round-towards-zero mode. |
Texture functions are not supported.
Surface functions are not supported.
HIP provides the following built-in functions for reading a high-resolution timer from the device.
Returns the value of counter that is incremented every clock cycle on device. Difference in values returned provides the cycles used.
Atomic functions execute as read-modify-write operations residing in global or shared memory. No other device or thread can observe or modify the memory location during an atomic operation. If multiple instructions from different devices or threads target the same memory location, the instructions are serialized in an undefined order.
HIP supports the following atomic operations.
Function | Supported in HIP | Supported in CUDA |
---|---|---|
int atomicAdd(int* address, int val) | ✓ | ✓ |
unsigned int atomicAdd(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicAdd(unsigned long long int* address,unsigned long long int val) | ✓ | ✓ |
float atomicAdd(float* address, float val) | ✓ | ✓ |
int atomicSub(int* address, int val) | ✓ | ✓ |
unsigned int atomicSub(unsigned int* address,unsigned int val) | ✓ | ✓ |
int atomicExch(int* address, int val) | ✓ | ✓ |
unsigned int atomicExch(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicExch(unsigned long long int* address,unsigned long long int val) | ✓ | ✓ |
float atomicExch(float* address, float val) | ✓ | ✓ |
int atomicMin(int* address, int val) | ✓ | ✓ |
unsigned int atomicMin(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicMin(unsigned long long int* address,unsigned long long int val) | ✓ | ✓ |
int atomicMax(int* address, int val) | ✓ | ✓ |
unsigned int atomicMax(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicMax(unsigned long long int* address,unsigned long long int val) | ✓ | ✓ |
unsigned int atomicInc(unsigned int* address) | ✗ | ✓ |
unsigned int atomicDec(unsigned int* address) | ✗ | ✓ |
int atomicCAS(int* address, int compare, int val) | ✓ | ✓ |
unsigned int atomicCAS(unsigned int* address,unsigned int compare,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicCAS(unsigned long long int* address,unsigned long long int compare,unsigned long long int val) | ✓ | ✓ |
int atomicAnd(int* address, int val) | ✓ | ✓ |
unsigned int atomicAnd(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicAnd(unsigned long long int* address,unsigned long long int val) | ✓ | ✓ |
int atomicOr(int* address, int val) | ✓ | ✓ |
unsigned int atomicOr(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicOr(unsigned long long int* address,unsigned long long int val) | ✓ | ✓ |
int atomicXor(int* address, int val) | ✓ | ✓ |
unsigned int atomicXor(unsigned int* address,unsigned int val) | ✓ | ✓ |
unsigned long long int atomicXor(unsigned long long int* address,unsigned long long int val)) | ✓ | ✓ |
Warp cross-lane functions operate across all lanes in a warp. The hardware guarantees that all warp lanes will execute in lockstep, so additional synchronization is unnecessary, and the instructions use no shared memory.
Note that Nvidia and AMD devices have different warp sizes, so portable code should use the warpSize built-ins to query the warp size. Hipified code from the Cuda path requires careful review to ensure it doesn’t assume a waveSize of 32. "Wave-aware" code that assumes a waveSize of 32 will run on a wave-64 machine, but it will utilize only half of the machine resources. In addition to the warpSize device function, host code can obtain the warpSize from the device properties:
Threads in a warp are referred to as lanes and are numbered from 0 to warpSize – 1. For these functions, each warp lane contributes 1 – the bit value (the predicate), which is efficiently broadcast to all lanes in the warp. The 32-bit int predicate from each lane reduces to a 1-bit value: 0 (predicate = 0) or 1 (predicate != 0). __any
and __all
provide a summary view of the predicates that the other warp lanes contribute:
__any()
returns 1 if any warp lane contributes a nonzero predicate, or 0 otherwise__all()
returns 1 if all other warp lanes contribute nonzero predicates, or 0 otherwiseApplications can test whether the target platform supports the any/all instruction using the hasWarpVote
device property or the HIP_ARCH_HAS_WARP_VOTE compiler define.
__ballot
provides a bit mask containing the 1-bit predicate value from each lane. The nth bit of the result contains the 1 bit contributed by the nth warp lane. Note that HIP's __ballot
function supports a 64-bit return value (compared with Cuda’s 32 bits). Code ported from Cuda should support the larger warp sizes that the HIP version of this instruction supports. Applications can test whether the target platform supports the ballot instruction using the hasWarpBallot
device property or the HIP_ARCH_HAS_WARP_BALLOT compiler define.
Half-float shuffles are not supported. The default width is warpSize—see Warp Cross-Lane Functions. Applications should not assume the warpSize is 32 or 64.
Cooperative groups is a mechanism for forming and communicating between groups of threads at a granularity different than the block. This feature was introduced in Cuda 9.
HIP does not support any of the kernel language cooperative groups types or functions.
Function | Supported in HIP | Supported in CUDA |
---|---|---|
void thread_group.sync() | ✓ | |
unsigned thread_group.size() | ✓ | |
unsigned thread_group.thread_rank() | ✓ | |
bool thread_group.is_valid() | ✓ | |
thread_group tiled_partition(thread_group, size) | ✓ | |
thread_block_tile<N> tiled_partition<N>(thread_group) | ✓ | |
thread_block this_thread_block() | ✓ | |
T thread_block_tile.shfl() | ✓ | |
T thread_block_tile.shfl_down() | ✓ | |
T thread_block_tile.shfl_up() | ✓ | |
T thread_block_tile.shfl_xor() | ✓ | |
T thread_block_tile.any() | ✓ | |
T thread_block_tile.all() | ✓ | |
T thread_block_tile.ballot() | ✓ | |
T thread_block_tile.match_any() | ✓ | |
T thread_block_tile.match_all() | ✓ | |
coalesced_group coalesced_threads() | ✓ | |
grid_group this_grid() | ✓ | |
void grid_group.sync() | ✓ | |
unsigned grid_group.size() | ✓ | |
unsigned grid_group.thread_rank() | ✓ | |
bool grid_group.is_valid() | ✓ | |
multi_grid_group this_multi_grid() | ✓ | |
void multi_grid_group.sync() | ✓ | |
unsigned multi_grid_group.size() | ✓ | |
unsigned multi_grid_group.thread_rank() | ✓ | |
bool multi_grid_group.is_valid() | ✓ |
Warp matrix functions allow a warp to cooperatively operate on small matrices whose elements are spread over the lanes in an unspecified manner. This feature was introduced in Cuda 9.
HIP does not support any of the kernel language warp matrix types or functions.
Function | Supported in HIP | Supported in CUDA |
---|---|---|
void load_matrix_sync(fragment<...> &a, const T* mptr, unsigned lda) | ✓ | |
void load_matrix_sync(fragment<...> &a, const T* mptr, unsigned lda, layout_t layout) | ✓ | |
void store_matrix_sync(T* mptr, fragment<...> &a, unsigned lda, layout_t layout) | ✓ | |
void fill_fragment(fragment<...> &a, const T &value) | ✓ | |
void mma_sync(fragment<...> &d, const fragment<...> &a, const fragment<...> &b, const fragment<...> &c , bool sat) | ✓ |
The hardware support for independent thread scheduling introduced in certain architectures supporting Cuda allows threads to progress independently of each other and enables intra-warp synchronizations that were previously not allowed.
HIP does not support this type of scheduling.
The Cuda __prof_trigger()
instruction is not supported.
The assert function is under development. HIP does support an "abort" call which will terminate the process execution from inside the kernel.
The printf function is under development.
Device-side dynamic global memory allocation is under development. HIP now includes a preliminary implementation of malloc and free that can be called from device functions.
GPU multiprocessors have a fixed pool of resources (primarily registers and shared memory) which are shared by the actively running warps. Using more resources can increase IPC of the kernel but reduces the resources available for other warps and limits the number of warps that can be simulaneously running. Thus GPUs have a complex relationship between resource usage and performance.
launch_bounds allows the application to provide usage hints that influence the resources (primarily registers) used by the generated code. It is a function attribute that must be attached to a global function:
launch_bounds supports two parameters:
The compiler uses these parameters as follows:
A compute unit (CU) is responsible for executing the waves of a work-group. It is composed of one or more execution units (EU) which are responsible for executing waves. An EU can have enough resources to maintain the state of more than one executing wave. This allows an EU to hide latency by switching between waves in a similar way to symmetric multithreading on a CPU. In order to allow the state for multiple waves to fit on an EU, the resources used by a single wave have to be limited. Limiting such resources can allow greater latency hiding, but can result in having to spill some register state to memory. This attribute allows an advanced developer to tune the number of waves that are capable of fitting within the resources of an EU. It can be used to ensure at least a certain number will fit to help hide latency, and can also be used to ensure no more than a certain number will fit to limit cache thrashing.
CUDA defines a __launch_bounds which is also designed to control occupancy:
The key differences in the interface are:
Unlike nvcc, HIP-Clang does not support the "--maxregcount" option. Instead, users are encouraged to use the hip_launch_bounds directive since the parameters are more intuitive and portable than micro-architecture details like registers, and also the directive allows per-kernel control rather than an entire file. hip_launch_bounds works on both HIP-Clang and nvcc targets.
The register keyword is deprecated in C++, and is silently ignored by both nvcc and HIP-Clang. You can pass the option -Wdeprecated-register
the compiler warning message.
Unroll with a bounds that is known at compile-time is supported. For example:
GCN ISA In-line assembly, is supported. For example:
We insert the GCN isa into the kernel using asm()
Assembler statement. volatile
keyword is used so that the optimizers must not change the number of volatile operations or change their order of execution relative to other volatile operations. v_mac_f32_e32
is the GCN instruction, for more information please refer - AMD GCN3 ISA architecture manual Index for the respective operand in the ordered fashion is provided by %
followed by position in the list of operands "v"
is the constraint code (for target-specific AMDGPU) for 32-bit VGPR register, for more info please refer - Supported Constraint Code List for AMDGPU Output Constraints are specified by an "="
prefix as shown above ("=v"). This indicate that assemby will write to this operand, and the operand will then be made available as a return value of the asm expression. Input constraints do not have a prefix - just the constraint code. The constraint string of "0"
says to use the assigned register for output as an input as well (it being the 0'th constraint).
The following C++ features are not supported:
hipcc now supports compiling C++/HIP kernels to binary code objects. The file format for binary is .co
which means Code Object. The following command builds the code object using hipcc
.
hipcc --genco --offload-arch=[TARGET GPU] [INPUT FILE] -o [OUTPUT FILE]
Clang defined 'gfx*' macros can be used to execute gfx arch specific codes inside the kernel. Refer to the sample 14_gpu_arch in samples/2_Cookbook.