<cuda/std/simd>#
<cuda/std/simd> implements the C++ <simd> specification and provides vectors, masks, loads, stores, permutations, reductions, and element-wise functions.
cuda::std::simd::vec<T, N> and cuda::std::simd::mask<T, N> operate on N values in one host or CUDA thread by exploiting SIMD instructions where possible.
#include <cuda/std/simd>
namespace simd = cuda::std::simd;
int input[4] = {1, 2, 3, 4};
auto v = simd::unchecked_load<simd::vec<int, 4>>(input);
assert(simd::reduce(v + v) == 20);
Note
CCCL also provides additional extensions to exploit NVIDIA GPU-specific SIMD instructions in <cuda/simd>.
Supported Data Types#
The implementation supports the following types:
Standard C++ integer, character, and floating-point types are supported.
Complex elements can use
cuda::std::complex<T>,cuda::complex<T>, or, when the host standard library is available,std::complex<T>.Extended types include 128-bit integers and floating-point types when the platform supports them.
CUDA floating-point types such as
__halfand__nv_bfloat16.
Windows does not support __float128 or the 128-bit integer types __int128 (including an int128_t, uint128_t aliases).
Windows also doesn’t support complex<double> elements. See Host Compiler Type Extensions in the CUDA Programming Guide.
Supported Widths#
A fixed-size vector or mask has 1 to 64 lanes. The native ABI currently has one lane.
CUDA SIMD Instructions#
The implementation exploits CUDA SIMD instructions for the following element types and operations.
Elements |
Operations |
PTX instruction |
Conditions |
|---|---|---|---|
8-bit and 16-bit integers |
Bitwise AND, OR, XOR, and complement |
|
Two or more elements packed into 32-bit words |
16-bit integers |
Addition, subtraction, negation, increment, decrement, minimum, maximum |
|
|
8-bit integers |
Addition, subtraction, negation, increment, decrement, minimum, maximum |
|
Fixed-size vectors with at least two elements. Requires |
16-bit and 32-bit integers |
Nested minimum or maximum, such as |
|
|
32-bit floats |
Addition, subtraction, negation, increment, decrement, multiplication, fused multiply-add |
|
|
Half and Bfloat16 |
Addition, multiplication, fused multiply-add |
|
Packed Bfloat16 fused multiply-add requires |
Half and Bfloat16 |
Element-wise comparison |
|
On |
Half and Bfloat16 |
Nested |
|
Even a one-element vector can use a three-input |
Contiguous memory elements |
Vectorized loads and stores |
|
An alignment flag can enable a vector access when the type, total access size, and pointer alignment permit it.
On |
CUDA SIMD Extensions#
The following examples show representative PTX for functions in <cuda/simd>.
The linked function pages give the machine instructions for each target.
Function |
Elements |
Operations |
PTX instruction |
Conditions |
|---|---|---|---|---|
8-bit or 16-bit integers |
Add four pairs of bytes or two pairs of halfwords with saturation. |
|
|
|
8-bit × 8-bit integers |
Add four products to a 32-bit accumulator. |
|
||
16-bit × 8-bit integers, in either order |
Add two products to a 32-bit accumulator using the low or high byte pair of the packed 8-bit operand. |
|
||
8-bit integers |
Compute four unsigned absolute differences. |
|
|
|
16-bit integers |
Compute two unsigned absolute differences. |
|
|
|
32-bit integers |
Compute one unsigned absolute difference per element. |
|
|
|
Signed 8-bit, 16-bit, or 32-bit integers |
Take the minimum or maximum of two or three inputs per element, then clamp negative results to zero. |
|
|
|
16-bit or 32-bit integers |
Add two inputs per element, then take the minimum or maximum of the sum and the third input. |
|
|
|
Signed 16-bit or 32-bit integers |
Add two inputs per element, take the minimum or maximum with the third input, then clamp negative results to zero. |
|
|
Known Limitations#
The SIMD overloads for special mathematical functions, such as
assoc_laguerre()andcyl_bessel_j(), are unavailable because libcu++ does not provide the corresponding scalar functions.A SIMD math overloads support constant evaluation only if the corresponding scalar operation supports it.
On Windows with NVCC and MSVC,
simd::iota<V>is aninline constexprvariable in host storage. Device code cannot take its address or bind a runtime reference to it. This also affects expressions such assimd::iota<V> + x, because vector addition takes its operands by reference. The workaround consists of creating a local variable in device code, for exampleconstexpr V indices = simd::iota<V>;and useindices + x.CUDA device code doesn’t support C++ exceptions. This is relevant for SIMD reduction where reduction can raise exceptions.