1 / 22

Containers, Iterators, Algorithms, Thrust

Containers, Iterators, Algorithms, Thrust. Richard Kelley. we want faster programs!. problem we want to use the GPU we don’t want to use CUDA or OpenCL solution use a library that “acts like” something we already know. Thrust to the rescue!. Thrust. what is it? from the Thrust webpage:

kyran
Télécharger la présentation

Containers, Iterators, Algorithms, Thrust

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Containers, Iterators, Algorithms, Thrust Richard Kelley

  2. we want faster programs! • problem • we want to use the GPU • we don’t want to use CUDA or OpenCL • solution • use a library that “acts like” something we already know. • Thrust to the rescue!

  3. Thrust • what is it? • from the Thrust webpage: “Thrust is a CUDA library of parallel algorithms with an interface resembling the C++ Standard Template Library (STL). Thrust provides a high-level interface for GPU programming that greatly enhances developer productivity.” • exactly what we want. • but we need to be comfortable with the STL to use thrust.

  4. what is the STL? • precursor to the standard library • the major parts are still around • containers • iterators • algorithms • functors • thrust is based on these abstractions • not object-oriented, but functional and generic • we don’t combine data and operations, we explicitly separate them.

  5. containers • STL containers are what they sound like • objects that hold other objects • the primary goal (in this part of the standard library) is efficiency • error-checking takes a secondary role almost everywhere • there are containers for most of the data structures you’re likely to use • dynamic arrays, deques, linked lists • balanced binary trees • hash tables (new in C++11)

  6. container types (1/2) • three main types (as of c++11) • sequence containers • std::vector – a dynamically resizable array • std::deque – a dynamically resizable double-ended queue • std::list – a doubly linked list • associative containers (elements must implement <) • std::map – efficient key-value stores. keys must be unique • std::set – a set of things. elements must be unique • std::multimap – key-value stores, keys needn’t be unique • std::multiset – a multiset of things. elements needn’t be unique

  7. container types (2/2) • unordered associative containers (hash tables) • std::unordered_set • std::unordered_multiset • std::unordered_map • std::unordered_multimap • they say “unordered” to avoid name conflicts with libraries that made their own “hash_*” • this is new in C++11 – your mileage may vary.

  8. common operations on containers • constructor, destructor • constructor(beg, end) • size() • empty() • max_size() • begin(), end() • rbegin(), rend() • insert(pos, elem) • erase(beg, end) • clear()

  9. iterators • object that can iterate over a collection • duh • an iterator’s value represents a position in a container • an iterator is anything that acts like an iterator: • operator* • operator++ (sometimes operator--) • operators == and != • operator= • Containers have functions that return special iterators • begin • end

  10. iterators categories • C++ offers a few categories of iterator. • input iterators • output iterators • forward iterators • bidirectional iterators • random access iterators

  11. algorithms • STL contains several algorithms to do stuff with containers. • global functions • accept ranges defined by iterators

  12. algorithm types (1/6) • nonmodifying algorithms • count() • count_if() • min_element() • max_element() • find(), find_if() • …

  13. algorithm types (2/6) • modifying algorithms • for_each • copy, copy_backward • transform • merge • …

  14. algorithm types (3/6) • removing algorithms • remove, remove_if • unique • removes adjacent duplicates

  15. algorithm types (4/6) • mutating algorithms (change element orders, not values) • reverse • rotate • next_permutation, prev_permutation • random_shuffle • partition, stable_partition

  16. algorithm types (5/6) • sorting algorithms • sort • probably quicksort. maybe introsort (quicksort+heapsort). • stable_sort • probably mergesort. • partial_sort • just do the first n elements. probably heapsort. • make_heap • push_heap • pop_heap • sort_heap

  17. algorithm types (6/6) • algorithms for sorted ranges • binary_search • merge • set_union • set_intersection • set_difference • set_symmetric_difference

  18. functors (function objects) • any object that has overloaded operator() • yes, you can do that • also called “function objects” • can have internal state • (probably) faster than a function pointer • I haven’t checked this

  19. code! • let’s look at some

  20. what does Thrust give us? • thrust aims to “look like” STL, but runs on the GPU • you get speedup doing little beyond what you would do in STL. • sequence containers • host_vector • device_vector • iterators • algorithms • functors are just like standard C++ • thrust has some special functors

  21. thrust containers • we get vectors • we have to specify location • thrust::host_vector • resides on the CPU side • thrust::device_vector • resides on the GPU • “host” and “device” are common terms to distinguish between the CPU and the GPU. • we can build thrust containers from std containers

  22. thrust iterators & algorithms • basic iterator usage is the same as STL • thrust also has “fancy iterators” that perform additional functionality • (their name, not mine) • thrust algorithms are similar to STL • there are some built-ins that perform simple tasks. • let’s look at some code • add two vectors

More Related