You are on page 1of 12

The Thrust library

Will Landau

Getting started

Iterators

Containers

The Thrust library Algorithms

December 04, 2023


Lectures 31 and 34

The Thrust library 1 / 42


Outline
The Thrust library

Will Landau

Getting started

Iterators

Containers
Getting started
Algorithms

Iterators

Containers

Algorithm

s The Thrust library 2 / 42


Getting started

Outline
The Thrust library

Will Landau

Getting started

Iterators

Containers
Getting started
Algorithms

Iterators

Containers

Algorithm

s The Thrust library 3 / 42


Getting started

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.

The Thrust library 4 / 42


Getting started

The Thrust library


vecto r 1. cu
Will Landau

1 #i n c l u d e <t h r u s t / h o s t v e c t o r . h> Getting started


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 .
3 h> #i n c l u d e <i o s t r e a m > Iterators
4
5 i n t main ( v o i d ) { Containers
6
7 // H h a s s t o r a g e f o r 4 i n t e g Algorithms
8 e r s t h r u s t : : h o s t v e c t o r <i n t
> H( 4 ) ;
9

10 // init iali ze i n d i v i d u a l el ement s


11 H[ 0] = 14;
12 H[ 1] = 20;
13 H[ 2] = 38;
14 H[ 3] = 46;
15
16 // H . s i z e ( ) r e t u r n s t h e s i z e o f v e c t o r H
17 s t d : : c o u t << ”H h a s s i z e ” << H . s i z e ( ) std : : e n d l ;
<<
18
19 // p r i n t c o n t e n t s o f H
20 f o r ( i n t i = 0 ; i < H . s i z e ( ) ; i ++)
21 s t d : : c o u t << ”H [ ” << i << ” ] = ” << H [ i ] << s t d : : e n d
l;
22
23 // r e s i z e H
24 H. r e s i z e ( 2 ) ;
25 s t d : : c o u t << ”H now h a s s i z e ” << H . s i z e ( ) << s t d : : e n d
l;
26 The Thrust library 5 / 42
Getting started

The Thrust library


vecto r 1. cu
Will Landau

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

The Thrust library 6 / 42


Getting started

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

The Thrust library 7 / 42


Getting started

The Thrust library


vecto r 2. cu
Will Landau
1 # i n c l u d e < t h r u s t / h o s t v e c t o r . h>
2 #in clu de < thrust / d e v i c e ve c t or . Getting started
h>
3 # i n c l u d e < t h r u s t / c opy . h> Iterators
4 # i n c l u d e < t h r u s t / f i l l . h>
Containers
5 # i n c l u d e < t h r u s t / s e q u e n c e . h>
7 # i n c l u d e < i o s t re a m >
6 Algorithms
8 i n t main ( v o i d )
{
9 / / i n i t i a l i z e a l l te n in t eg er s of a de vi ce ve c t o r
10 t h r1u s t : : d e v i c e v e c t o r < i n t > D ( 1 0
to
11 , 1);
12 / / s e t t h e f i r s t se v e n e l emen t s o f a v e c t o r
13 t o 9 t h r u s t : : f i l l (D. b e g i n ( ) , D. b e g i n ( ) + 7 ,
14 9);
15 / / i n i t i a l i ze a ho st ve c t o r wi th the f i r s t f i v e el em en ts of
16 D t h r u s t : : h o s t ve c t o r <in t > H(D. b e g i n ( ) , D. b e g i n ( ) +
17 5);
18 / / s e t t h e e l emen t s o f H t o 0 , 1 , 2 ,
19 3 , . . . t h r u s t : : s e q u e n c e (H . b e g i n ( ) , H .
20 end ( ) ) ;
21 / / copy a l l o f H b ack t o t h e b e g i n n i n g o f
22 D t h r u s t : : c opy (H . b e g i n ( ) , H . end ( ) , D . b e
23 gin());
24 // pr int D
25 f o r ( i n t i = 0 ; i < D . s i z e ( ) ; i ++)
26 s t d : : c ou t << ” D [ ” << i << ” ] = ” << D[ i ] << s t d : :
27 endl;
28 re t urn
29 } 0;

The Thrust library 8 / 42


Getting started

The Thrust library


vecto r 2. cu
Will Landau

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

The Thrust library 9 / 42


Getting started

The Thrust library

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.

The Thrust library 10 / 42


Getting started

The vector template classes


The Thrust library

Will Landau

Getting started

Iterators

I Declaring vectors: Containers

I t h r u s t : : d e v i c e vector<T> D; creates a vector D Algorithms

with entries of data type T on the device.


I The analogous declaration for host vectors is
t h r u s t : : host vector<T> H;.
I An object D of the vector template class includes
the following features:
I A dynamic linear array of elements of type T.
I Two iterators:
I D.begin()
I D.end()

The Thrust library 11 / 42


Getting started

“TASK”
The Thrust library

Will Landau

(You have 5 Minutes Getting started

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

The Thrust library 12 / 42

You might also like