| | #include <thrust/host_vector.h> |
| | #include <thrust/device_vector.h> |
| | #include <thrust/generate.h> |
| | #include <thrust/sequence.h> |
| | #include <thrust/sort.h> |
| | #include <thrust/gather.h> |
| | #include <thrust/random.h> |
| | #include <iostream> |
| |
|
| | |
| | |
| | |
| |
|
| | template <typename KeyVector, typename PermutationVector> |
| | void update_permutation(KeyVector& keys, PermutationVector& permutation) |
| | { |
| | |
| | KeyVector temp(keys.size()); |
| |
|
| | |
| | thrust::gather(permutation.begin(), permutation.end(), keys.begin(), temp.begin()); |
| |
|
| | |
| | thrust::stable_sort_by_key(temp.begin(), temp.end(), permutation.begin()); |
| | } |
| |
|
| |
|
| | template <typename KeyVector, typename PermutationVector> |
| | void apply_permutation(KeyVector& keys, PermutationVector& permutation) |
| | { |
| | |
| | KeyVector temp(keys.begin(), keys.end()); |
| |
|
| | |
| | thrust::gather(permutation.begin(), permutation.end(), temp.begin(), keys.begin()); |
| | } |
| |
|
| |
|
| | thrust::host_vector<int> random_vector(size_t N) |
| | { |
| | thrust::host_vector<int> vec(N); |
| | static thrust::default_random_engine rng; |
| | static thrust::uniform_int_distribution<int> dist(0, 9); |
| |
|
| | for (size_t i = 0; i < N; i++) |
| | vec[i] = dist(rng); |
| |
|
| | return vec; |
| | } |
| |
|
| |
|
| | int main(void) |
| | { |
| | size_t N = 20; |
| |
|
| | |
| | thrust::device_vector<int> upper = random_vector(N); |
| | thrust::device_vector<int> middle = random_vector(N); |
| | thrust::device_vector<int> lower = random_vector(N); |
| | |
| | std::cout << "Unsorted Keys" << std::endl; |
| | for(size_t i = 0; i < N; i++) |
| | { |
| | std::cout << "(" << upper[i] << "," << middle[i] << "," << lower[i] << ")" << std::endl; |
| | } |
| |
|
| | |
| | thrust::device_vector<int> permutation(N); |
| | thrust::sequence(permutation.begin(), permutation.end()); |
| |
|
| | |
| | update_permutation(lower, permutation); |
| | update_permutation(middle, permutation); |
| | update_permutation(upper, permutation); |
| |
|
| | |
| | |
| | |
| | |
| | apply_permutation(lower, permutation); |
| | apply_permutation(middle, permutation); |
| | apply_permutation(upper, permutation); |
| |
|
| | std::cout << "Sorted Keys" << std::endl; |
| | for(size_t i = 0; i < N; i++) |
| | { |
| | std::cout << "(" << upper[i] << "," << middle[i] << "," << lower[i] << ")" << std::endl; |
| | } |
| |
|
| | return 0; |
| | } |
| |
|
| |
|