Professional Documents
Culture Documents
Will Landau
Getting started
Iterators
Containers
Outline
The Thrust library
Will Landau
Getting started
Iterators
Containers
Getting started
Algorithms
Iterators
Containers
Algorithm
Map Container
The Thrust library
Will Landau
fruit_prices["apple"] = 2; Containers
fruit_prices["orange"] = 3; Algorithms
fruit_prices["banana"] = 1;
Basic iterators
The Thrust library
Will Landau
Getting started
I An iterator is a pointer with a C++ wrapper around Iterators
it. The wrapper contains additional information, such Containers
as whether the vector is stored on the host or the Algorithms
device.
1 / / a l l o c a t e de vi ce vector
2 t h r u s t : : d e v i c e ve c t o r <i n t > d vec ( 4 ) ;
3
4 d vec . b e g i n ( ) ; / / r e t u r n s i t e r a t o r at f i r s t elem ent of d vec
5 d vec . end ( ) ; / / re t u r n s i t e ra t o r one past the l a s t elem ent of d vec
6
7 // [ begin , end ) p a i r def in es a seq uenc e of 4 el emen t s
Will Landau
Getting started
Iterators
1 / / a l l o c a t e de vi ce vector
2 t h r u s t : : d e v i c e ve c t o r <i n t > d vec ( 4 ) ; Containers
3
4 t h r u s t : : d e v i c e ve c t o r <in t >:: i t e r a t o r b e g i n = d vec . Algorithms
5 b e g i n ( ) ; t h r u s t : : d e v i c e ve c t o r <in t >:: i t e r a t o r end = d
6 vec . end ( ) ;
7
8 in t l e n g t h = end — b e g i n ; / / compute t h e l e n g t h o f t h e v e
9
c t o r end = d vec . b e g i n ( ) + 3 ; / / def ine a se que nce of 3 el em en
Using iterators
The Thrust library
Will Landau
Getting started
Iterators
Containers
Algorithms
1 / / a l l o c a t e de vi ce vector
2 t h r u s t : : d e v i c e ve c t o r <i n t > d vec
(4);
4 t h r u s t : : d e v i c e ve c t o r <in t >:: i t e
r a t o r b e g i n = d vec . b e g i n ( ) ;
5
6 **b e g i n = 13; / / same as d v e c [ 0 ] =
13;
7 i n t temp = **b e g i n ; / / same as temp = d v e c [ 0 ]
;
9 b e g i n ++; / / advance i t e r a t o r one p o s i t i o n
10
11 **b e g i n = 25; / / same as d v e c [ 1 ] = 25;
Will Landau
Getting started
Iterators
Containers
Algorithms
Getting started
Iterators
Containers
I A constant i t e r a t o r is a pointer with some
Algorithms
constant value associated with it.
1 # i n c l u de < t h r u s t / i t e r a t o r / c o n s t a n t i t era t o
r . h> 2 . . .
3 / / c r ea t e i t e r a t o r s
4 t h r u s t : : c o n s t a n t i t er a t o r <in t > f i r s t ( 1 0 )
;
5 t h r u s t : : c o n s t a n t i t er a t o r < i n t > l a s t = f i r s t +
3;
7 f i r s t [ 0 ] ; / / r e t u r n s 10
8 f i r s t [ 1 ] ; / / r e t u r n s 10
9 f i r s t [ 1 0 0 ] ; / / re tu
r n s 10
11 / / sum o f [ f i r s t , l a s t )
12 t h r u s t : : r e d u c e ( f i r s t ,
l a s t ) ; / / re t u r n s 30 ( i .
e . 3 ⇤ 10)
Getting started
Iterators
value Algorithms
some constant + a d d i ti o n o f 1 t o i t .
1 # i n c l u d e < t h r u s t / i t e r a t o r / c o u n t i n g i t e r a t o r . h>
2 ...
3
4 / / c r ea t e i t e r a t o r s
5 t h r u s t : : c o u n t i n g i t er a t o r <in t > f i r s t ( 1 0 ) ;
6 t h r u s t : : c o u n t i n g i t er a t o r < i n t > l a s t = f i r s t + 3 ;
7
8 f i r s t [ 0 ] ; / / r e t u r n s 10
9 f i r s t [ 1 ] ; / / r e t u r n s 11
10 f i r s t [ 1 0 0 ] ; / / r e t u r n s 110
11
12 / / sum o f [ f i r s t , l a s t )
13 thrust ::reduce(first , l a s t ) ; // re t u r n s 33 ( i . e . 10 + 11 + 12)
/ / same t h i n g :
21 thrust ::reduce(
22 t h r u s t : : m a k e t r ans
for m i t era t or (
23 v ec . b e g i n ( ) ,
negat e< i n t > () ) ,
24 t h r u s t : : m a k e t r ans
for m i t era t or ( The Thrust library 10 / 42
Iterators
Getting started
I A z i p i t e r a t o r is a pointer associated with a
Iterators
vector of tuples. Containers
Getting started
22 / / type d e f a t u p l e of t h e s e i t e r a t o r s Iterators
23 ty p e de f t h r u s t : : tu p l e <In t It er at or , F l o a t I t e r a t o r , C h a r I t
e r a t o r > It er at or T u p le ; Containers
24
Algorithms
25 / / type d e f the z i p i t e r a t o r of t h i s t u p l e
26 ty p e de f t h r u s t : : z i p i t e r a t o r <It er at or T u p le > Z i p Ite r a t o r ;
27
28 / / f i n a l l y , crea t e the z i p i t e r a t o r
29 Z i p I t e r a t o r i t e r ( t h r u s t : : m a k e tuple ( i n t v . b e g i n ( ) , f l o a t v .
b e g i n ( ) , ch ar v . b e g i n ( ) ) ) ;
30
31 ⇤ it e r ;
32 i t e r [ 0 ] ; / / re t u r n s (0 , 0. 0 , ’ a
’ ) 33 i t e r [ 1 ] ; / / re t u r n s (1 , 1. 0 ,
’ b ’ ) 34 i t e r [ 2 ] ; / / re t u r n s (2 , 2.
0 , ’ c ’)
Outline
The Thrust library
Will Landau
Getting started
Iterators
Containers
Getting started
Algorithms
Iterators
Containers
Algorithm
Will Landau
I Containers are fancy data storage classes used in
Getting started
the Standard Template Library (STL), the CPU C+ Iterators
+ analog of Thrust. Containers
I Examples of containers include: Algorithms
I vector
I deque
I list
I set
I mu l ti set
I map
I multimap
I biset
Outline
The Thrust library
Will Landau
Getting started
Iterators
Containers
Getting started
Algorithms
Iterators
Containers
Algorithm
Transformations
The Thrust library
Will Landau
Getting started
Iterators
Containers
I A transformation is the application of a function to Algorithms
each element within a range of elements in a vector.
The results are stored as a range of elements in
another vector.
I Examples:
I t h r u s t : : fi l l ( )
I t h r u st : : seq u en c e( )
I thrust::replace()
I t h r u s t : : t ra n s fo r m ( )
Getting started
28 // r e p l a c e a l l t h e ones i n Y w i t h t e n s Iterators
29 t h r u s t : : r e p l a c e ( Y. b e g i n ( ) , Y. end ( ) ,
30 1 , 10 ) ; Containers
31
32 // pr int Y Algorithms
t h r u s t” \: : copy ( Y. b e g i n ( ) , Y. end ( ) , s t d : :
33 r es tt ur rena
o n”m) )i t;e r a t o r <in t >(s t d : : cout ,
34 } 0;
Getting started
28 // r e p l a c e a l l t h e ones i n Y w i t h t e n s Iterators
29 t h r u s t : : r e p l a c e ( Y. b e g i n ( ) , Y. end ( ) ,
30 1 , 10 ) ; Containers
31
32 // pr int Y Algorithms
t h r u s t” \: : copy ( Y. b e g i n ( ) , Y. end ( ) , s t d : :
33 r es tt ur rena
o n”m) )i t;e r a t o r <in t >(s t d : : cout ,
34 } 0;
1 > nvc c t r a n s f o r m a t i o n s . cu —o t r a n s f o r
mations
3 0 ./ t r a n s f o r m a t i o n s
2 >
4 10
5 0
6 10
7 0
8 10
9 0
10 10
11 0
12 10
13 [ l a n d a u @ i m p a c t 1 t r a n s f o r m a t i o
ns]$
Counting
The Thrust library
Will Landau
Getting started
Iterators
1 # i n c l u d e < t h r u s t / c ou nt . h>
2 # i n c l u d e < t h r u s t / d e v i c e v e c t o r . h>
3 ...
4
5 // put t h re e 1 s i n a d e v i c e v e c t o r
6 t h r u s t : : d e v i c e ve c t o r <in t >
7 vec ( 5 , 0 ) ;
8
9 v ec [ 1 ] = 1 ;
10 v ec [ 3 ] = 1 ;
11 v ec [ 4 ] = 1 ;
12
13 / / c o u nt t h e 1 s
14 in t re s u l t = t h r u s t : : count ( vec . b e
g i n ( ) , vec . end ( ) , 1 ) ;
/ / res u l t i s thre e
Sorting
The Thrust library
Will Landau
Getting started
Iterators
I thrust::sort()
Containers
1 #in clu de < thrust / s o r t .
h> 2 . . . Algorithms
3 con st i n t N = 6 ;
4 in t A[ N] = { 1, 4 , 2 ,
8, 5, 7} ;
5 t h r u s t : : s o r t (A , A +
N) ;
6 / / A is now { 1 , 2, 4, 5,
7, 8 }
Resource
The Thrust library
Will Landau
s
Getting started
Iterators
I Guides: Containers
Pearson, 2008.
2. CUDA Toolkit 4.2 Thrust Quick Start Guide.
March
2012. h tt p :
//docs.nvidia.com/cuda/thrust/index.html
I Code is posted at
htt p://will-landau.com/gpu/thrust.html.
Will Landau
Getting started
Iterators
Containers
Algorithms