Sorting
Thrust提供一系列函数根据既定的准则来分类数据和整理数据。thrust::sort和thrust::stable_sort函数是在STL(Standard Template Library 标准模板库)中的sort和stable_sort非常相似。
# include <thrust / sort .h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust :: sort (A, A + N);
// A is now {1, 2, 4, 5, 7, 8}
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust :: sort (A, A + N);
// A is now {1, 2, 4, 5, 7, 8}
另外,Thrust提供thrust::sort_by_key和thrust::stable_sort_by_key,这两个函数将 key-value 分为两类,分别存储在不同的位置。
# 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 :: sort_by_key (keys , keys + N, values );
// keys is now { 1, 2, 4, 5, 7, 8}
// values is now {'a ', 'c ', 'b ', 'e ', 'f ', 'd '}
...
const int N = 6;
int keys [N] = { 1, 4, 2, 8, 5, 7};
char values [N] = {'a', 'b', 'c', 'd', 'e', 'f'};
thrust :: sort_by_key (keys , keys + N, values );
// keys is now { 1, 2, 4, 5, 7, 8}
// values is now {'a ', 'c ', 'b ', 'e ', 'f ', 'd '}
跟STL类似,sorting函数同样接受用户自定义对比操作。
# include <thrust / sort .h>
# include <thrust / functional .h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust :: stable_sort (A, A + N, thrust :: greater <int >());
// A is now {8, 7, 5, 4, 2, 1}
# include <thrust / functional .h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust :: stable_sort (A, A + N, thrust :: greater <int >());
// A is now {8, 7, 5, 4, 2, 1}
更多内容请点击:
CUDA专区:http://cuda.it168.com/
CUDA论坛:http://cudabbs.it168.com/