Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

The Thrust library

Will Landau

Getting started

Iterators

Containers

The Thrust library Algorithms

December 05, 2023


Lectures 33 and 35

The Thrust library 1 / 42


Iterators

Outline
The Thrust library

Will Landau

Getting started

Iterators

Containers
Getting started
Algorithms

Iterators

Containers

Algorithm

s The Thrust library 2 / 42


Iterators

Map Container
The Thrust library

Will Landau

int main() { Getting started

thrust::map<std::string, int> fruit_prices; Iterators

fruit_prices["apple"] = 2; Containers

fruit_prices["orange"] = 3; Algorithms

fruit_prices["banana"] = 1;

thrust::map<std::string, int>::iterator it = fruit_prices.find("apple");


if (it != fruit_prices.end()) {
std::cout << "Price of apple: " << it->value<< std::endl;
} else {
std::cout << "Key 'apple' not found" << std::endl;
}
return 0;
}

The Thrust library 3 / 42


Iterators

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

The Thrust library 4 / 42


Iterators

Iterators act like pointers.


The Thrust library

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

ts starting from beginning

The Thrust library 5 / 42


Iterators

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;

The Thrust library 6 / 42


Iterators

The Thrust library

Will Landau

Getting started

Iterators

Containers

Algorithms

Some Types of iterators

The Thrust library 7 / 42


Iterators

The Thrust library


constant i t e r a t o r
Will Landau

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)

The Thrust library 8 / 42


Iterators

The Thrust library


counti ng i t e r a t o r
Will Landau

Getting started

Iterators

I A counti ng i t e r a t o r is a pointer with the Containers

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)

The Thrust library 9 / 42


Iterators

The Thrust library


transform i t e r a t o r
Will Landau
I A transform i t e r a t o r is a pointer with the value
Getting started
some f unc ti o n( ve c to r e nt r y ) associated with it. Iterators
1 #in clu de < thrust / i t e r a t o r / t r a n s f o r m it e
Containers
r a t o r . h> 2 . . .
3 t h r u s t : : d e v i c e v e c t o r < i n t > vec ( 3 ) ; Algorithms
4 v ec [ 0 ] = 1 0 ;
5 v ec [ 1 ] = 2 0 ;
6 v ec [ 2 ] = 3 0 ;
7
8 / / c r ea t e i t e r a t o r
9 t h r u s t : : t r a n s f o r m i t e r a t o r <in t > f i r s t =
10 t h r u s t : : m a k e t r ans f or m i t era t o r ( vec . b e g i n ( ) , negate<in t
>() ) ;
12 t h r u s t : : t r a n s f o r m i t e r a t o r < i n t > l a s t =
13 t h r u s t : : m a k e t r ans f or m i t era t o r ( vec . end ( ) , negate<in t
>() ) ;
15 f i r s t [ 0 ] / / r e t u r n s —10
16 f i r s t [ 1 ] / / r e t u r n s —20
17 f i r s t [ 2 ] / / r e t u r n s —
30 18
19 t h r u s t : : r e d u c e ( f i r s
t , last);

/ / 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

The Thrust library


zip iterator
Will Landau

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

1 # 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> Algorithms


2 # i n c l u d e < t h r u s t / t u p l e . h>
3 #in clu de < thrust / i t e r a t o r / z i p i t e r a t o r .
h>
4 # i n c l u d e < i o s t re a m >
5
6 #in clu de < thrust / i t e r a t o r / z i p
i t e r a t o r . h> 7
8 i n t h main r u s t( :) :{ d e v i c e v e c t o r < i n t > i n t v
9 (3);
10 int v[0] = 0; int v[1] = 1; int v[2]
11 t=h 2r u; s t : : d e v i c e v e c t o r < f l o a t > f l o a t v ( 3 ) ;
12 float v[0] = 0.0; float v[1] = 1.0; float v[2]
13 = 2.0;
14
15 t h r u s t : : d e v i c e v e c t o r <ch ar> c h a r v ( 3 ) ;
16 char v[0] = ’ a’ ; char v[1] = ’ b’ ; char v[2] = ’
17 /c /’ ; t y p e d e f t h e se i t e r a t o r s f o r s h o r t h a n d
18 t y p e d e f t h r u s t : : d e v i c e v e c t o r < i n t > :: i t e r a t o r Int
19 I t e r a t o r ; ty p e de f t h r u s t : : d e v i c e ve c t o r < f l o a t >:: i t e r a t o r
20 F l o a t I t e r a t o r ; t y p e d e f t h r u s t : : d e v i c e v e c t o r <ch ar > :: i
21 terator CharIterator;

The Thrust library 11 / 42


Iterators

The Thrust library


zip iterator
Will Landau

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 ’)

The Thrust library 12 / 42


Containers

Outline
The Thrust library

Will Landau

Getting started

Iterators

Containers
Getting started
Algorithms

Iterators

Containers

Algorithm

s The Thrust library 13 / 42


Containers

The Thrust library

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

The Thrust library 14 / 42


Algorithms

Outline
The Thrust library

Will Landau

Getting started

Iterators

Containers
Getting started
Algorithms

Iterators

Containers

Algorithm

s The Thrust library 15 / 42


Algorithms

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 ( )

The Thrust library 16 / 42


Algorithms

The Thrust library


transformati ons.cu
Will Landau

1 #in clu de < thrust / d e v i c e ve c t or . Getting started


h>
2 # i n c l u d e < t h r u s t / t r a n s f o r m . h> Iterators
3 # i n c l u d e < t h r u s t / s e q u e n c e . h>
4 # i n c l u d e < t h r u s t / c opy . h> Containers
5 # i n c l u d e < t h r u s t / f i l l . h>
6 # i n c l u d e < t h r u s t / r e p l a c e . h> Algorithms
7 # i n c l u d e < t h r u s t / f u n c t i o n a l . h>
9 # i n c l u d e < i o s t re a m >
8
10 i n t main ( v o i d )
{
11 / / a l l o c a t e t h re e d e v i c e v e c t o r s wit h 10 el emen t
12 st h r u s t : : d e v i c e v e c t o r < i n t >
13 X( 1 0 ) ; t h r u s t : : d e v i c e ve c t or
14 <in t > Y ( 1 0 ) ; t h r u s t : : d e v i c e
15 ve c t or <in t > Z ( 1 0 ) ;
16 / / i n i t i a l i z e X to 0 , 1 , 2 , 3 , . . . .
17 t h r u s t : : s e q u e n c e (X . b e g i n ( ) ,
18 X . end ( ) ) ;
19 / / compute Y = —X
20 t h r u s t : : t r a n s f o r m ( X . b e g i n ( ) , X . end ( ) , Y. b e g i n ( ) , t h r u s t : :
negate<in); t >()
21
22 / / f i l l Z w i t h t w os
23 t h r u s t : : f i l l ( Z . b e g i n ( ) , Z . end
24 (), 2);
25 / / compute Y = X mod 2
26 t h r u s t : : t r a n s f o r m ( X . b e g i n ( ) , X . end ( ) , Z . b e g i n
27 (),
Y. b e g i n ( ) , t h r u s t : : modulus<in t
>() ) ;

The Thrust library 17 / 42


Algorithms

The Thrust library


transformati ons.cu
Will Landau

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;

The Thrust library 18 / 42


Algorithms

The Thrust library


transformati ons.cu
Will Landau

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]$

The Thrust library 19 / 42


Algorithms

Counting
The Thrust library

Will Landau

Getting started

Iterators

I t h r u s t : : c o u n t ( ) to count the number of times a Containers

value appears in a vector. Algorithms

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

The Thrust library 20 / 42


Algorithms

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 }

The Thrust library 21 / 42


Algorithms

Resource
The Thrust library

Will Landau
s
Getting started

Iterators

I Guides: Containers

1. Savitch W. Absolute C++. Ed. Hirsch M. 3rd Ed. Algorithms

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.

The Thrust library 22 / 42


Algorithms

The Thrust library

Will Landau

Getting started

Iterators

Containers

Algorithms

That’s all for today, and also


the end of CUDA ☺☺

The Thrust library 23 / 42

You might also like