Professional Documents
Culture Documents
Thrust
Thrust
Algorithm Classes
frequently used algorithms
sort, find, binary search, ...
Iterator Classes
Vector containers
Thrust provides
thrust::host_vector
thrust::device_vector
int main(void)
{
// H has storage for 4 integers
thrust::host_vector<int> H(4);
// resize H
H.resize(2);
// print contents of D
for(int i = 0; i < D.size(); i++)
std::cout << "D[" << i << "] = " << D[i] << std::endl;
Capacity:
size Return size
resize Change size
Modifiers:
assign Assign vector content
push_back Add element at the end
pop_back Delete last element
insert Insert elements
erase Erase elements
An important question
Can I create a thrust::device_vector from memory
I've allocated myself?
int main(void)
{
size_t N = 10;
// free memory
cudaFree(raw_ptr);
return 0;
}
Thrust algorithms
● Linear Search
find, find_if ...
● Subsequence Matching
count, count_if
● for_each
equal, mismatch …
● Generalized Numeric Algorithms
reverse
● Others
● Search
● Sort
● Transformations
● Numeric
Function(Iter_begin,Iter_end, ...)
sort.cu
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <cstdlib>
#include <math.h>
int main(void)
{
int k,n;
k=10;
n=int(pow(2.0,k));
// transfer to device
thrust::device_vector<int> d_vec = h_vec;
// sort on device
thrust::sort(d_vec.begin(), d_vec.end());
return 0;
}
sort.cu
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <cstdlib>
#include <math.h>
int main(void)
{
int k,n;
k=10;
n=int(pow(2.0,k));
// transfer to device
thrust::device_vector<int> d_vec = h_vec;
// sort on device
thrust::sort(d_vec.begin(), d_vec.end());
return 0;
}
sum.cu
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <cstdlib>
int main(void)
{
// generate random data on the host
thrust::host_vector<int> h_vec(100);
thrust::generate(h_vec.begin(), h_vec.end(), rand);
thrust::device_vector<int> d_vec;
thrust::device_vector<int>::iterator vecIterator;
Iterators
Thrust provides
constant_iterator
counting_iterator
constant_iterator.cu
#include <thrust/iterator/constant_iterator.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>
// for printing
#include <thrust/copy.h>
#include <iterator>
int main(void)
{
thrust::device_vector<int> data(4);
data[0] = 3;
data[1] = 7;
data[2] = 2;
data[3] = 5;
// print result
thrust::copy(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
Iterators
Thrust provides
transform_iterator
zip_iterator
Zip_iterator.cu
#include <thrust/iterator/zip_iterator.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/device_vector.h>
// for printing
#include <thrust/copy.h>
#include <iterator>
using namespace thrust;
int main(void)
{
device_vector<int> A(3);
device_vector<char> B(3);
A[0] = 10; A[1] = 20; A[2] = 30;
B[0] = ‘x’; B[1] = ‘y’; B[2] = ‘z’;