快速CUDA推力自定义比较运算符
我正在评估 CUDA,目前使用 Thrust 库对数字进行排序。
我想为推力::排序创建我自己的比较器,但它的速度大大减慢! 我通过从 function.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;
}
I'm evaluating CUDA and currently using Thrust library to sort numbers.
I'd like to create my own comparer for thrust::sort, but it slows down drammatically!
I created my own less implemetation by just copying code from functional.h.
However it seems to be compiled in some other way and works very slowly.
- default comparer: thrust::less() - 94ms
- my own comparer: less() - 906ms
I'm using Visual Studio 2010. What should I do to get the same performance as at option 1?
Complete code:
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您观察到性能差异的原因是 Thrust 根据提供给
thrust::sort
的参数使用不同的算法实现排序。在情况1.中,Thrust可以证明用基数排序可以在线性时间内实现排序。这是因为要排序的数据类型是内置数字类型 (
int
),而比较函数是内置小于运算 -- Thrust 识别出thrust: :less
将产生与x
等效的结果y。
在情况 2 中,Thrust 对您的用户提供的相当于
less
一无所知,并且必须使用基于比较排序的更保守的算法,该算法具有不同的渐近复杂度,即使实际上您的 < code>lessthrust::less
。一般来说,用户定义的比较运算符不能与限制性更强、速度更快的排序一起使用,这些排序操作数据的二进制表示形式,例如基数排序。在这些情况下,Thrust 会求助于更通用但速度较慢的排序。
The reason you are observing a difference in performance is because Thrust is implementing the sort with different algorithms depending on the arguments provided to
thrust::sort
.In case 1., Thrust can prove that the sort can be implemented in linear time with a radix sort. This is because the type of the data to sort is a built-in numeric type (
int
), and the comparison function is the built-in less than operation -- Thrust recognizes thatthrust::less<int>
will produce the equivalent result asx < y
.In case 2., Thrust knows nothing about your user-provided
less<int>
, and has to use a more conservative algorithm based on a comparison sort which has different asymptotic complexity, even though in truth yourless<int>
is equivalent tothrust::less<int>
.In general, user-defined comparison operators can't be used with more restrictive, faster sorts which manipulate the binary representation of data such as radix sort. In these cases, Thrust falls back on a more general, but slower sort.