Thrust Parallel Algorithms Library
Like C++ Standard Template Library (STL) … but for GPUs!
Vectors
Thrust Vector containers are provided by:
#include <thrust/host_vector.h> #include <thrust/device_vector.h>
Using this is very similar to C++ STL Vectors:
std::vector<type> array(size);
where type
is the data type, array
is the vector name, and size
is the number of elements in the array.
In Thrust, this is given by:
thrust::host_vector<type> array(size); thrust::device_vector<type> array(size);
Data transfers
Data transfers are done transparently using the =
operator. For example:
// transfer data to the device thrust::device_vector<int> d_vec = h_vec;
This creates a new device side vector, and copies the host vector to the device vector. Essentially, it does cudaMalloc
and cudaMemCpy
all at once!
Besides the =
operator, there is also thrust::copy
, and thrust::fill
which can be useful.
For example, to copy Device to Host:
thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
For example, to initialize an array to 0:
thrust::fill(h_vec.begin(), h_vec.end(), 0);
Parallel Algorithms
Reduction
The reduction algorithm basically adds every element in the array together. You simply pass the array, an initial value, and the type of operation (can be plus, maximum, etc.).
int sum = thrust::reduce(D.begin(), D.end(), (int) 0, thrust::plus<int>());
For more information: http://www.danielwong.org/classes/_media/csee217_f17/9-reduction.pptx
Prefix-Sum
Prefix-Sum, also common called the Scan operation, adds all values in indexes lower than your index.
For example, an inclusive scan gives the following:
In: {1, 0, 2, 2, 1, 3} Out: {1, 1, 3, 5, 6, 9}
For example, an exclusive scan gives the following:
In: {1, 0, 2, 2, 1, 3} Out: {0, 1, 1, 3, 5, 6}
thrust::inclusive_scan(array.begin(), array.end(), output_array); thrust::exclusive_scan(array.begin(), array.end(), output_array);
For more information: http://www.danielwong.org/classes/_media/csee217_f17/10-scan.pptx
Sort
It sorts an array…there's lots of ways to sort an array: https://thrust.github.io/doc/group__sorting.html https://docs.nvidia.com/cuda/thrust/index.html#sorting
The simpliest sort is to sort in ascending order:
thrust::sort(array.begin(), array.end());