DeviceScan provides device-wide, parallel operations for computing a prefix scan across a sequence of data items residing within device-accessible memory.
- Overview
- Given a sequence of input elements and a binary reduction operator, a prefix scan produces an output sequence where each element is computed to be the reduction of the elements occurring earlier in the input sequence. Prefix sum connotes a prefix scan with the addition operator. The term inclusive indicates that the ith output reduction incorporates the ith input. The term exclusive indicates the ith input is not incorporated into the ith output reduction.
- As of CUB 1.0.1 (2013), CUB's device-wide scan APIs have implemented our "decoupled look-back" algorithm for performing global prefix scan with only a single pass through the input data, as described in our 2016 technical report [1]. The central idea is to leverage a small, constant factor of redundant work in order to overlap the latencies of global prefix propagation with local computation. As such, our algorithm requires only ~2n data movement (n inputs are read, n outputs are written), and typically proceeds at "memcpy" speeds.
- [1] Duane Merrill and Michael Garland. "Single-pass Parallel Prefix Scan with Decoupled Look-back", NVIDIA Technical Report NVR-2016-002, 2016.
- Usage Considerations
- \cdp_class{DeviceScan}
- Performance
- \linear_performance{prefix scan}
- The following chart illustrates DeviceScan::ExclusiveSum performance across different CUDA architectures for
int32
keys. \plots_below
Definition at line 89 of file device_scan.cuh.
|
|
template<typename InputIteratorT , typename OutputIteratorT > |
static CUB_RUNTIME_FUNCTION cudaError_t | ExclusiveSum (void *d_temp_storage, size_t &temp_storage_bytes, InputIteratorT d_in, OutputIteratorT d_out, int num_items, cudaStream_t stream=0, bool debug_synchronous=false) |
| Computes a device-wide exclusive prefix sum. The value of 0 is applied as the initial value, and is assigned to *d_out.
|
|
template<typename InputIteratorT , typename OutputIteratorT , typename ScanOpT , typename InitValueT > |
static CUB_RUNTIME_FUNCTION cudaError_t | ExclusiveScan (void *d_temp_storage, size_t &temp_storage_bytes, InputIteratorT d_in, OutputIteratorT d_out, ScanOpT scan_op, InitValueT init_value, int num_items, cudaStream_t stream=0, bool debug_synchronous=false) |
| Computes a device-wide exclusive prefix scan using the specified binary scan_op functor. The init_value value is applied as the initial value, and is assigned to *d_out.
|
|
|
template<typename InputIteratorT , typename OutputIteratorT > |
static CUB_RUNTIME_FUNCTION cudaError_t | InclusiveSum (void *d_temp_storage, size_t &temp_storage_bytes, InputIteratorT d_in, OutputIteratorT d_out, int num_items, cudaStream_t stream=0, bool debug_synchronous=false) |
| Computes a device-wide inclusive prefix sum.
|
|
template<typename InputIteratorT , typename OutputIteratorT , typename ScanOpT > |
static CUB_RUNTIME_FUNCTION cudaError_t | InclusiveScan (void *d_temp_storage, size_t &temp_storage_bytes, InputIteratorT d_in, OutputIteratorT d_out, ScanOpT scan_op, int num_items, cudaStream_t stream=0, bool debug_synchronous=false) |
| Computes a device-wide inclusive prefix scan using the specified binary scan_op functor.
|
|
template<typename InputIteratorT , typename OutputIteratorT , typename ScanOpT , typename InitValueT >
static CUB_RUNTIME_FUNCTION cudaError_t cub::DeviceScan::ExclusiveScan |
( |
void * |
d_temp_storage, |
|
|
size_t & |
temp_storage_bytes, |
|
|
InputIteratorT |
d_in, |
|
|
OutputIteratorT |
d_out, |
|
|
ScanOpT |
scan_op, |
|
|
InitValueT |
init_value, |
|
|
int |
num_items, |
|
|
cudaStream_t |
stream = 0 , |
|
|
bool |
debug_synchronous = false |
|
) |
| |
|
inlinestatic |
Computes a device-wide exclusive prefix scan using the specified binary scan_op
functor. The init_value
value is applied as the initial value, and is assigned to *d_out.
- Supports non-commutative scan operators.
- Provides "run-to-run" determinism for pseudo-associative reduction (e.g., addition of floating point types) on the same GPU device. However, results for pseudo-associative reduction may be inconsistent from one device to a another device of a different compute-capability because CUB can employ different tile-sizing for different architectures.
- \devicestorage
- Snippet
- The code snippet below illustrates the exclusive prefix min-scan of an
int
device vector
#include <cub/cub.cuh>
struct CustomMin
{
template <typename T>
CUB_RUNTIME_FUNCTION __forceinline__
T operator()(const T &a, const T &b) const {
return (b < a) ? b : a;
}
};
int *d_in;
CustomMin min_op
...
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
cudaMalloc(&d_temp_storage, temp_storage_bytes);
KeyT const ValueT ValueT OffsetT OffsetT num_items
[in] Total number of input data items
OutputIteratorT d_out
< [in] Pointer to the input sequence of data items
static CUB_RUNTIME_FUNCTION cudaError_t ExclusiveScan(void *d_temp_storage, size_t &temp_storage_bytes, InputIteratorT d_in, OutputIteratorT d_out, ScanOpT scan_op, InitValueT init_value, int num_items, cudaStream_t stream=0, bool debug_synchronous=false)
Computes a device-wide exclusive prefix scan using the specified binary scan_op functor....
- Template Parameters
-
InputIteratorT | [inferred] Random-access input iterator type for reading scan inputs \iterator |
OutputIteratorT | [inferred] Random-access output iterator type for writing scan outputs \iterator |
ScanOp | [inferred] Binary scan functor type having member T operator()(const T &a, const T &b) |
Identity | [inferred] Type of the identity value used Binary scan functor type having member T operator()(const T &a, const T &b) |
- Parameters
-
[in] | d_temp_storage | Device-accessible allocation of temporary storage. When NULL, the required allocation size is written to temp_storage_bytes and no work is done. |
[in,out] | temp_storage_bytes | Reference to size in bytes of d_temp_storage allocation |
[in] | d_in | Pointer to the input sequence of data items |
[out] | d_out | Pointer to the output sequence of data items |
[in] | scan_op | Binary scan functor |
[in] | init_value | Initial value to seed the exclusive scan (and is assigned to *d_out) |
[in] | num_items | Total number of input items (i.e., the length of d_in ) |
[in] | stream | [optional] CUDA stream to launch kernels within. Default is stream0. |
[in] | debug_synchronous | [optional] Whether or not to synchronize the stream after every kernel launch to check for errors. May cause significant slowdown. Default is false . |
Definition at line 243 of file device_scan.cuh.