【发布时间】:2012-02-20 17:08:48
【问题描述】:
我正在评估 CUDA,目前正在使用 Thrust 库对数字进行排序。
我想为推力::排序创建自己的比较器,但它的速度大大减慢! 我通过从 functional.h 复制代码创建了自己的 less 实现。 但是,它似乎以其他方式编译并且运行速度非常慢。
- 默认比较器:thrust::less() - 94ms
- 我自己的比较器:less() - 906ms
我使用的是 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 的排序功能。可能对您的分析有用。