我正在评估CUDA并正在使用Thrust库对数字进行排序。快速CUDA推力自定义比较运算符
我想为推力::排序创建我自己的比较器,但它会大大减慢速度! 我创建了我自己的减去通过从functional.h复制代码实现。然而,它似乎是以其他方式编译的,而且工作速度非常缓慢。
- 默认的比较:推力::以下() - 毫秒
- 我自己比较器:以下() - 毫秒
我使用Visual Studio 2010的什么我应该怎么做才能获得与选项1相同的性能?
完整代码:
#include <stdio.h>
#include <cuda.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
int myRand()
{
static int counter = 0;
if (counter++ % 10000 == 0)
srand(time(NULL)+counter);
return (rand()<<16) | rand();
}
template<typename T>
struct less : public thrust::binary_function<T,T,bool>
{
__host__ __device__ bool operator()(const T &lhs, const T &rhs) const {
return lhs < rhs;
}
};
int main()
{
thrust::host_vector<int> h_vec(10 * 1000 * 1000);
thrust::generate(h_vec.begin(), h_vec.end(), myRand);
thrust::device_vector<int> d_vec = h_vec;
int clc = clock();
thrust::sort(d_vec.begin(), d_vec.end(), less<int>());
printf("%dms\n", (clock()-clc) * 1000/CLOCKS_PER_SEC);
return 0;
}
好奇,如果你已经尝试ArrayFire的排序功能。可能对你的分析有用。 – arrayfire 2012-01-28 01:54:51