Approach 2 – Thrust-based radix sort

Thrust-based radix sort is a generic implementation of radix sort and works pretty well for different types of data, such as integer, float, or key-value pairs. We would like to reemphasize the fact that sorting is a heavily studied algorithm and so has its parallel implementation. Therefore, we recommend that you reuse existing libraries before implementing one on your own.

The steps involved in making use of Thrust for radix sort are as follows:

  1. Import the relevant header files (Thrust is a header-only library, similar to STL):
#include <thrust/device_vector.h>
#include <thrust/sort.h>
  1. Declare and initialize a device vector:
//declare a device vector of size N
thrust::device_vector<int> keys(N);
//Generate a random number generator engine
thrust::default_random_engine r(12);
//create a distribution engine which will create integer values
thrust::uniform_int_distribution<int> d(10, 99);
//Fill the array with randon values
for(size_t i = 0; i < v.size(); i++)
v[i] = d(r);
  1. Perform sorting on the initialized device vector:
thrust::sort(keys.begin(), keys.end());

Using this library provides an easier and robust approach. Thrust provides different types of sorting methods, including radix sort for integers and floats. Alternatively, you can create a custom comparator to do customized sortings, such as sorting all the event numbers followed by odd numbers, sorting in descending order, and so on. You are advised to look at sample examples that have been provided by CUDA if you want to learn more about Thrust-based sorting examples. 

Now, we have looked at both approaches to implementing radix sort on GPUs. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.193.129