Professional Documents
Culture Documents
Will Landau
Getting started
Iterators
Containers
Will Landau
Getting started
Iterators
Containers
Getting started
Algorithms
Iterators
Containers
Algorithm
Outline
The Thrust library
Will Landau
Getting started
Iterators
Containers
Getting started
Algorithms
Iterators
Containers
Algorithm
Backgroun
The Thrust library
Will Landau
d
Getting started
Iterators
I Thrust is the CUDA analog of the Standard Template
Containers
Library (STL) of C++. It comes with any installation Algorithms
of CUDA 4.2 and above and features:
I Dynamic data structures
I An encapsulation of GPU/CPU communication,
memory management, and other low-level
tasks.
I High-performance GPU-accelerated algorithms
such as
sorting and reduction
I Brief history:
I Emerged from Komrade (deprecated) in 2009
I Maintained primarily by Jared Hoberock and
Nathan Bell of NVIDIA.
Getting started
29 / / e l e m en t s o f D can be m o d i Iterators
30 fie d D [ 0 ] = 9 9 ;
31 D[ 1 ] = 8 8 ; Containers
32
33 / / p r i n t co n t en t s of D Algorithms
34 f o r ( i n t i = 0 ; i < D . s i z e ( ) ; i ++)
35 s t d : : c ou t << ” D [ ” << i << ” ] = ” << D[ i ] << s t d : :
36 endl;
37 / / H and D ar e au t om a t i ca l ly d e l e t e d when the fu nc t i on re
38 t u r n s re t urn 0;
39 }
1 > nvc c v e c t o r 1 . cu —o v e c
tor1
2> . / v e c t o r 1
3 H has s i z e
4 4 H [ 0 ] = 14
5 H [ 1 ] = 20
6 H [ 2 ] = 38
7 H [ 3 ] = 46
8 H now has s i
ze 2
9 D [ 0 ] = 99
10 D [ 1 ] = 88
Notes
The Thrust library
Will Landau
Getting started
Iterators
I Thrust takes care of m a l l o c ( ) , cudaMalloc(),
Containers
f r e e ( ) , and cudaFree() for you without sacrificing Algorithms
performance.
I The “=” operator does a cudaMemcpy() if one vector
is on the host and one is on the device.
I t h r u s t : : and s t d : : clarify the namespace of the
function after the double colon. For example, we
need to distinguish between t h r u s t : : c o p y ( ) and
std::copy().
I The “<<” operator sends a value to an output
stream, the C++ alternative to p r i n tf ( ) .
Getting started
Iterators
Containers
30 D[ 0 ] = 0 Algorithms
34 D[ 1 ] = 1
35 D[ 2 ] = 2
36 D[ 3 ] = 3
37 D[ 4 ] = 4
38 D[ 5 ] = 9
39 D[ 6 ] = 9
40 D[ 7 ] = 1
41 D[ 8 ] = 1
42 D[ 9 ] = 1
Will Landau
Getting started
Iterators
Containers
Algorithms
I t h r u s t : : c o p y ( ) copies a section of one vector into
a section of another.
I t h r u s t : : fi l l ( ) sets a range of elements to
some fixed value.
I t h r u st : : s e q u e n c e ( ) assigns equally-spaced values
to a section of a vector.
Will Landau
Getting started
Iterators
“TASK”
The Thrust library
Will Landau
for it)
Iterators
Containers
Algorithms
W r i t e d o w n t h e c o d e
f o r i t e r a t o r s t o f i n d
a n e l e m e n t i n a v e c t o r