GPU programming with Thrust
Thrust (http://code.google.com/p/thrust/) is a fantastic CUDA C++ interface matching within the C++ STL paradigm (templates, containers, iterators, generic algorithms,…).
Just an example from Thrust web site:
#include <thrust/device_vector.h> #include <thrust/transform.h> #include <thrust/sequence.h> #include <thrust/copy.h> #include <thrust/fill.h> #include <thrust/replace.h> #include <thrust/functional.h> #include <iostream> int main(void) { // allocate three device_vectors with 10 elements thrust::device_vector<int> X(10); thrust::device_vector<int> Y(10); thrust::device_vector<int> Z(10); //STL like vectors but allocated on the GPU.All commands in the thrust namespace are excuted on GPU. // initialize X to 0,1,2,3, .... thrust::sequence(X.begin(), X.end()); // compute Y = -X thrust::transform(X.begin(), X.end(), Y.begin(), thrust::negate<int>()); // fill Z with twos thrust::fill(Z.begin(), Z.end(), 2); // compute Y = X mod 2 thrust::transform(X.begin(), X.end(), Z.begin(), Y.begin(), thrust::modulus<int>()); // replace all the ones in Y with tens thrust::replace(Y.begin(), Y.end(), 1, 10); // print Y thrust::copy(Y.begin(), Y.end(), std::ostream_iterator<int>(std::cout, "\n")); return 0; }
To compile this vector_example.cu file on Ubuntu 64bits (see Cuda on Ubuntu 64bits), you just have to use the NVidia nvcc compiler:
nvcc vector_example.cu -o vector_example --compiler-bindir=/usr/bin/g++-4.3
A couple of related links:
blog/gputhrust.txt · Last modified: 2011/03/14 21:23 (external edit)


