The Thrust library
Will Landau
Getting started
Iterators
Containers The Thrust library Algorithms
Will Landau
Iowa State University
November 11, 2013
Will Landau (Iowa State University) The Thrust library November 11, 2013 1 / 42 Outline The Thrust library Will Landau
Getting started
Iterators
Containers Getting started Algorithms
Iterators
Containers
Algorithms
Will Landau (Iowa State University) The Thrust library November 11, 2013 2 / 42 Getting started Outline The Thrust library Will Landau
Getting started
Iterators
Containers Getting started Algorithms
Iterators
Containers
Algorithms
Will Landau (Iowa State University) The Thrust library November 11, 2013 3 / 42 Getting started Background The Thrust library Will Landau
Getting started
Iterators I Thrust is the CUDA analog of the Standard Template Containers Library (STL) of C++. It comes with any installation of Algorithms 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.
Will Landau (Iowa State University) The Thrust library November 11, 2013 4 / 42 Getting started vector1.cu The Thrust library Will Landau
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 5 / 42 Getting started vector1.cu The Thrust library Will Landau
Getting started
29 // elements of D can be modified Iterators 30 D[ 0 ] = 9 9 ; 31 D[ 1 ] = 8 8 ; Containers 32 33 // print contents of D Algorithms 34 for( int i=0;i< D. size () ; i++) 35 s t d : : cout << ”D[ ” << i << ” ] = ” << D[ i ] << std :: endl ; 36 37 // H and D are automatically deleted when the function returns 38 return 0; 39 }
1 > nvcc vector1 . cu ovector1 2 > ./ vector1 3Hhassize4 4H[0]=14 5H[1]=20 6H[2]=38 7H[3]=46 8Hnowhassize2 9D[0]=99 10 D[ 1 ] = 88
Will Landau (Iowa State University) The Thrust library November 11, 2013 6 / 42 Getting started Notes The Thrust library Will Landau
Getting started
Iterators I Thrust takes care of malloc(), cudaMalloc(), Containers free(), 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 thrust:: and std:: clarify the namespace of the function after the double colon. For example, we need to distinguish between thrust::copy() and std::copy().
I The “<<” operator sends a value to an output stream, the C++ alternative to printf().
Will Landau (Iowa State University) The Thrust library November 11, 2013 7 / 42 Getting started vector2.cu The Thrust library Will Landau 1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 8 / 42 Getting started vector2.cu The Thrust library Will Landau
Getting started
Iterators
Containers
30 [landau@impact1 vector2]$ make Algorithms 31 nvcc v e c t o r 2 . cu ovector2 32 [landau@impact1 vector2]$ ./vector2 33 D[ 0 ] = 0 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 43 [landau@impact1 vector2]$
Will Landau (Iowa State University) The Thrust library November 11, 2013 9 / 42 Getting started Assignment The Thrust library Will Landau
Getting started
Iterators
Containers
Algorithms I thrust::copy() copies a section of one vector into a section of another.
I thrust::fill() sets a range of elements to some fixed value.
I thrust::sequence() assigns equally-spaced values to a section of a vector.
Will Landau (Iowa State University) The Thrust library November 11, 2013 10 / 42 Getting started The vector template classes The Thrust library Will Landau
Getting started
Iterators I Declaring vectors: Containers
I thrust::device vector
I A dynamic linear array of elements of type T. I Two iterators:
I D.begin() I D.end()
Will Landau (Iowa State University) The Thrust library November 11, 2013 11 / 42 Iterators Outline The Thrust library Will Landau
Getting started
Iterators
Containers Getting started Algorithms
Iterators
Containers
Algorithms
Will Landau (Iowa State University) The Thrust library November 11, 2013 12 / 42 Iterators Basic iterators The Thrust library Will Landau
Getting started I An iterator is a pointer with a C++ wrapper around it. Iterators
The wrapper contains additional information, such as Containers
whether the vector is stored on the host or the device. Algorithms
1 // allocate device vector 2thrust::devicevector
Will Landau (Iowa State University) The Thrust library November 11, 2013 13 / 42 Iterators Iterators act like pointers. The Thrust library Will Landau
Getting started
Iterators 1 // allocate device vector 2thrust::devicevector
Will Landau (Iowa State University) The Thrust library November 11, 2013 14 / 42 Iterators Using iterators The Thrust library Will Landau
Getting started
Iterators
Containers
Algorithms 1 // allocate device vector 2thrust::devicevector
Will Landau (Iowa State University) The Thrust library November 11, 2013 15 / 42 Iterators Wrap pointers to make iterators. The Thrust library Will Landau
Getting started
Iterators
Containers 1 int N= 10; 2 Algorithms 3 // raw pointer to device memory 4 int raw ptr ; 5cudaMalloc((⇤ void )&rawptr , N sizeof( int)); 6 ⇤⇤ ⇤ 7 // wrap raw pointer with a device ptr 8thrust::deviceptr
Will Landau (Iowa State University) The Thrust library November 11, 2013 16 / 42 Iterators Unwrap iterators to extract pointers. The Thrust library Will Landau
Getting started
Iterators
Containers
Algorithms
1 // allocate device vector 2thrust::devicevector
Will Landau (Iowa State University) The Thrust library November 11, 2013 17 / 42 Iterators constant iterator The Thrust library Will Landau
Getting started
Iterators
Containers I A constant iterator is a pointer with some constant value associated with it. Algorithms
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 18 / 42 Iterators counting iterator The Thrust library Will Landau
Getting started
Iterators I A counting iterator is a pointer with the value Containers some constant + offset associated with it. Algorithms
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 19 / 42 Iterators transform iterator The Thrust library Will Landau I A transform iterator is a pointer with the value some function(vector entry) associated with it. Getting started Iterators 1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 20 / 42 Iterators permutation iterator The Thrust library Will Landau I A permutation iterator is a pointer associated with Getting started apermutedvector. Iterators
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 21 / 42 Iterators permutation iterator The Thrust library Will Landau
Getting started
Iterators
Containers
Algorithms 26 int p0 = pbegin [ 0 ] ; // source [ map [0]] = 40 27 int p1 = pbegin [ 1 ] ; // source [ map [1]] = 20 28 29 int sum = thrust::reduce(pbegin , pend); 30 31 / sum = 32 ⇤ source [ map [0]] + source [ map [1]] + source [ map [2]] + source [ map [3]] = 33 ⇤ source [3] + source [1] + source [0] + source [5] = 34 ⇤ 40 + 20 + 10 + 60 = 35 ⇤ 130 / ⇤ ⇤
Will Landau (Iowa State University) The Thrust library November 11, 2013 22 / 42 Iterators zip iterator The Thrust library Will Landau
Getting started I A zip iterator is a pointer associated with a vector of tuples. Iterators Containers
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 23 / 42 Iterators zip iterator The Thrust library Will Landau
Getting started
22 // typedef a tuple of these iterators Iterators 23 typedef thrust :: tuple
Will Landau (Iowa State University) The Thrust library November 11, 2013 24 / 42 Containers Outline The Thrust library Will Landau
Getting started
Iterators
Containers Getting started Algorithms
Iterators
Containers
Algorithms
Will Landau (Iowa State University) The Thrust library November 11, 2013 25 / 42 Containers
The Thrust library
Will Landau I Containers are fancy data storage classes used in the Getting started 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 tack I queue I priority queue I set I multiset I map I multimap I biset
Will Landau (Iowa State University) The Thrust library November 11, 2013 26 / 42 Containers container.cu The Thrust library Will Landau
I Thrust only implements vectors, but it’s still compatible Getting started with the rest of STL’s template classes. Iterators Containers
1 #include 4 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 27 / 42 Algorithms Outline The Thrust library Will Landau
Getting started
Iterators
Containers Getting started Algorithms
Iterators
Containers
Algorithms
Will Landau (Iowa State University) The Thrust library November 11, 2013 28 / 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 thrust::fill() I thrust::sequence() I thrust::replace() I thrust::transform()
Will Landau (Iowa State University) The Thrust library November 11, 2013 29 / 42 Algorithms transformations.cu The Thrust library Will Landau
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 30 / 42 Algorithms transformations.cu The Thrust library Will Landau
Getting started
28 // replace all the ones in Y with tens Iterators 29 thrust::replace(Y.begin(), Y.end(), 1, 10); 30 Containers 31 // print Y 32 thrust::copy(Y.begin(), Y.end(), std::ostream iterator
1 > nvcc transformations . cu otransformations 2 > ./ transformations 30 410 50 610 70 810 90 10 10 11 0 12 10 13 [landau@impact1 transformations]$
Will Landau (Iowa State University) The Thrust library November 11, 2013 31 / 42 Algorithms Reductions The Thrust library Will Landau
Getting started
I A reduction algorithm uses a binary operation to reduce Iterators an input vector to a single value. For example, here are Containers equivalent ways to code the pairwise sum: Algorithms
1 int sum = thrust ::reduce(D.begin(), D.end(), 2(int)0,thrust::plus
I The third argument is the starting value of the reduction.
I The fourth argument is the binary operation that defines the kind of reduction.
Will Landau (Iowa State University) The Thrust library November 11, 2013 32 / 42 Algorithms Counting The Thrust library Will Landau
Getting started
Iterators I Another reduction: use thrust::count() to count the Containers number of times a value appears in a vector. Algorithms
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 33 / 42 Algorithms Scans The Thrust library Will Landau I A scan, also called a prefix-sum, applies a function to multiple sub-ranges of a vector and returns the result in a Getting started vector of the same size. The default function is addition. Iterators Containers 1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 34 / 42 Algorithms Scans The Thrust library Will Landau
I There are exclusive scans in addition to inclusive scans. Getting started Iterators
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 35 / 42 Algorithms Reordering The Thrust library Will Landau
Getting started
Iterators
Containers
I The “Reordering” utilities provides subsetting and Algorithms partitioning tools:
I thrust::copy if(): copy the elements that make some logical function return true. I thrust::partition();reorderavectorsuchthat values returning true precede values returning false. I thrust::remove() and remove if(): remove elements that return false. I thrust::unique():removeduplicatesinavector.
Will Landau (Iowa State University) The Thrust library November 11, 2013 36 / 42 Algorithms Partitions The Thrust library Will Landau 1 #include
I Notice: I can use host arrays directly.
I However, arrays stored on the GPU must be converted into device vectors or iterators before usage in Thrust algorithms.
Will Landau (Iowa State University) The Thrust library November 11, 2013 37 / 42 Algorithms Sorting The Thrust library Will Landau
Getting started
Iterators I thrust::sort() Containers 1 #include
I thrust::sort by key()
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 38 / 42 Algorithms Sorting The Thrust library Will Landau
Getting started
Iterators
Containers
Algorithms I thrust::stable sort()
1 #include
Will Landau (Iowa State University) The Thrust library November 11, 2013 39 / 42 Algorithms Outline The Thrust library Will Landau
Getting started
Iterators
Containers Getting started Algorithms
Iterators
Containers
Algorithms
Will Landau (Iowa State University) The Thrust library November 11, 2013 40 / 42 Algorithms Resources The Thrust library Will Landau
Getting started
Iterators
I Guides: Containers 1. Bell N. and Hoberock J. Thrust. Algorithms http://developer.download.nvidia.com/CUDA/ training/webinarthrust1.mp4 2. Savitch W. Absolute C++. Ed. Hirsch M. 3rd Ed. Pearson, 2008. 3. CUDA Toolkit 4.2 Thrust Quick Start Guide. March 2012. http: //docs.nvidia.com/cuda/thrust/index.html
I Code from today is posted at http://will-landau.com/gpu/thrust.html.
Will Landau (Iowa State University) The Thrust library November 11, 2013 41 / 42 Algorithms That’s all for today. The Thrust library Will Landau
Getting started
Iterators
Containers
Algorithms
I Series materials are available at http://will-landau.com/gpu.
Will Landau (Iowa State University) The Thrust library November 11, 2013 42 / 42