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

THRUST & THE C++

STANDARD ALGORITHMS
Week11_Lec32
11/28/2023
AGENDA

Introduction
• What is Thrust? What are the C++ standard algorithms?
The Difference
• set_difference(Thrust, C++ standard algorithms)
thrust::reduce vs std::accumulate
• How to parallelize reductions
Thrust Algorithms with Stencils
• A super useful overload
Fancy Iterators
• thrust::counting_iterator & friends

4
INTRODUCTION

7
WHAT IS THRUST?

• Thrust is a parallel algorithms library which resembles the C++


standard algorithms
• Thrust’s high-level interface greatly enhances programmer
productivity while enabling performance portability between GPUs
and multicore CPUs.
• Interoperability with established technologies (such as CUDA and
OpenMP) facilitates integration with existing software.
• Develop high-performance applications rapidly with Thrust!

8
WHAT ARE THE C++ STANDARD ALGORITHMS?

• The algorithms library defines functions for a variety of purposes


(e.g. searching, sorting, counting, manipulating) that operate on
ranges of elements.
• Algorithms can be found in:
• <algorithm>
• <numeric>
• <memory>

9
RESOURCES

10
THE DIFFERENCE

13
thrust::sequence()

8
int main() {

thrust::device_vector<int> myVector(10);

thrust::sequence(myVector.begin(),
myVector.end()); thrust::sequence()
for (int i : myVector) {
std::cout << i << " ";
std::iota()
}

return 0;
}

9
Problem #1

What is
std::iota +
std::transform ?
10
Problem #1

What is
thrust:sequence +
thrust::transform ?
11
Problem #1

thrust::tabulate()

12
Problem #1

First 10
odd numbers

13
Problem #1

auto odds = vector<int>(10);

iota(odds.begin(), odds.end(), 0);


transform(
odds.begin(),
odds.end(),
odds.begin(),
[](auto e) { return e * 2 + 1;
});
Output: 1 3 5 7 9 11 13 15 17 19
14
Problem #1

auto odds = device_vector<int>(10);

thrust::tabulate( odds.begin
(), odds.end(),
[] (auto e) { return e * 2 +
1; });

Output: 1 3 5 7 9 11 13 15 17 19
15

You might also like