Thrust库的使用 - radix sort的具体实现 写作sort by key

https://thrust.github.io/doc/group__sorting.html

https://devtalk.nvidia.com/default/topic/951795/fastest-sorting-algorithm-on-gpu-currently/

Precondition

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys using the thrust::host execution policy for parallelization. The keys are sorted in descending order using the greater<int> comparison operator.

#include <thrust/sort.h>

#include <thrust/execution_policy.h>

...

const int N = 6;

int keys[N] = { 1, 4, 2, 8, 5, 7};

char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};

thrust::stable_sort_by_key(thrust::host, keys, keys + N, values, thrust::greater<int>());

// keys is now { 8, 7, 5, 4, 2, 1}

// values is now {'d', 'f', 'e', 'b', 'c', 'a'}

See Also

http://www.sgi.com/tech/stl/sort.html

sort_by_key

stable_sort

template<typename RandomAccessIterator1 , typename RandomAccessIterator2 , typename StrictWeakOrdering >

void thrust::stable_sort_by_key(RandomAccessIterator1 keys_first,
  RandomAccessIterator1 keys_last,
  RandomAccessIterator2 values_first,
  StrictWeakOrdering comp 
 )  

stable_sort_by_key performs a key-value sort. That is, stable_sort_by_key sorts the elements in [keys_first, keys_last) and [values_first, values_first + (keys_last - keys_first)) into ascending key order, meaning that if i and j are any two valid iterators in [keys_first, keys_last) such that i precedes j, and p and q are iterators in [values_first, values_first + (keys_last - keys_first))corresponding to i and j respectively, then *j is not less than *i.

As the name suggests, stable_sort_by_key is stable: it preserves the relative ordering of equivalent elements. That is, if x and y are elements in [keys_first, keys_last) such that x precedes y, and if the two elements are equivalent (neither x < y nor y < x) then a postcondition of stable_sort_by_key is that x still precedes y.

This version of stable_sort_by_key compares key objects using the function object comp.

Parameters

keys_firstThe beginning of the key sequence.
keys_lastThe end of the key sequence.
values_firstThe beginning of the value sequence.
compComparison operator.

Template Parameters

RandomAccessIterator1is a model of Random Access IteratorRandomAccessIterator1 is mutable, and RandomAccessIterator1's value_type is convertible to StrictWeakOrdering'sfirst_argument_type and second_argument_type.
RandomAccessIterator2is a model of Random Access Iterator, and RandomAccessIterator2 is mutable.
StrictWeakOrderingis a model of Strict Weak Ordering.

Precondition

The range [keys_first, keys_last)) shall not overlap the range [values_first, values_first + (keys_last - keys_first)).

The following code snippet demonstrates how to use sort_by_key to sort an array of character values using integers as sorting keys. The keys are sorted in descending order using the greater<int> comparison operator.

#include <thrust/sort.h>

...

const int N = 6;

int keys[N] = { 1, 4, 2, 8, 5, 7};

char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};

thrust::stable_sort_by_key(keys, keys + N, values, thrust::greater<int>());

// keys is now { 8, 7, 5, 4, 2, 1}

// values is now {'d', 'f', 'e', 'b', 'c', 'a'}

See Also

http://www.sgi.com/tech/stl/sort.html

sort_by_key

stable_sort


Generated on Mon Mar 2 2015 13:21:30 for thrust by   doxygen 1.8.6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值