我需要对元组数组进行排序,因此我使用thrust::sort
为元组定义运算符并进行排序。
所以我发现排序一个元组数组对数组数组的排序要慢得多。这是我的代码:
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/set_operations.h>
#include <thrust/reduce.h>
#include <thrust/unique.h>
#include <thrust/binary_search.h>
#include <thrust/gather.h>
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <iostream>
static const int size = 100000;
#define mzi(x) thrust::make_zip_iterator(x)
#define mt(...) thrust::make_tuple(__VA_ARGS__)
typedef thrust::tuple<int, int> IntTuple;
typedef thrust::device_vector<IntTuple>::iterator TupleIterator;
typedef thrust::device_vector<int>::iterator IntIterator;
typedef thrust::tuple<IntIterator, IntIterator> IteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
struct TupleComp
{
__host__ __device__
bool operator()(const IntTuple& t1, const IntTuple& t2)
{
return t1.get<0>() != t2.get<0>() ? t1.get<0>() < t2.get<0>() : t1.get<1>() > t2.get<1>();
}
};
int main()
{
timespec start;
clock_gettime(0, &start);
thrust::device_vector<int> dataA1(size);
thrust::device_vector<int> dataA2(size);
thrust::device_vector<int> dataB1(size);
thrust::device_vector<int> dataB2(size);
srand(time(NULL));
for (int i = 0; i < size; i++)
{
//dataA[i] = dataA[i - 1] + (rand() % 100);
dataA1[i] = (rand() % 100);
dataA2[i] = (rand() % 100);
dataB1[i] = (rand() % 100);
dataB2[i] = (rand() % 100);
std::cout << dataA1[i] << "\t" << dataA2[i] << "\t" << dataB1[i] << "\t" << dataB2[i];
std::cout << std::endl;
}
timespec end;
clock_gettime(0, &end);
std::cout << "gendb took: " << end.tv_sec - start.tv_sec << "s" << end.tv_nsec - start.tv_nsec << "ns" << std::endl;
ZipIterator beginA = mzi(mt(dataA1.begin(), dataA2.begin()));
ZipIterator beginB = mzi(mt(dataB1.begin(), dataB2.begin()));
ZipIterator endA = mzi(mt(dataA1.end(), dataA2.end()));
ZipIterator endB = mzi(mt(dataB1.end(), dataB2.end()));
thrust::device_vector<IntTuple> A(size);
thrust::device_vector<IntTuple> B(size);
clock_gettime(0, &start);
thrust::copy(beginA, endA, A.begin());
thrust::copy(beginB, endB, B.begin());
clock_gettime(0, &end);
std::cout << "thrust::copy took: " << end.tv_sec - start.tv_sec << "s" << end.tv_nsec - start.tv_nsec << "ns" << std::endl;
clock_gettime(0, &start);
thrust::sort(A.begin(), A.end());
clock_gettime(0, &end);
std::cout << "A thrust::sort took: " << end.tv_sec - start.tv_sec << "s" << end.tv_nsec - start.tv_nsec << "ns" << std::endl;
clock_gettime(0, &start);
thrust::sort(B.begin(), B.end(), TupleComp());
clock_gettime(0, &end);
std::cout << "B thrust::sort took: " << end.tv_sec - start.tv_sec << "s" << end.tv_nsec - start.tv_nsec << "ns" << std::endl;
clock_gettime(0, &start);
thrust::sort(dataA1.begin(), dataA1.end());
clock_gettime(0, &end);
std::cout << "regular thrust::sort took: " << end.tv_sec - start.tv_sec << "s" << end.tv_nsec - start.tv_nsec << "ns" << std::endl;
clock_gettime(0, &start);
thrust::sort(beginA, endA, TupleComp());
thrust::sort(beginB, endB, TupleComp());
clock_gettime(0, &end);
std::cout << "thrust::sort took: " << end.tv_sec - start.tv_sec << "s" << end.tv_nsec - start.tv_nsec << "ns" << std::endl;
}
我得到的元组排序比常规排序慢<〜> 10倍。
我不明白为什么。推力的复杂性是否直接受到操作员的影响?尽管如此,我的操作员并不比常规比较器快10倍。
注意: 它不只是慢10倍: 100000它慢了~10倍 对于1000000,它的速度要慢20倍
我还发现将两个数组复制到一个元组数组中并对该数组进行排序的速度提高了大约150%,而thrust :: copy几乎没有任何内容(0.3为1M)。
注2:
我将操作员更改为:
struct TupleComp
{
__host__ __device__
bool operator()(const IntTuple& t1, const IntTuple& t2)
{
if(t1.get<0>() < t2.get<0>())
return true;
if(t1.get<0>() > t2.get<0>())
return false;
return t1.get<1>() > t2.get<1>();
}
};
现在排序大约快了112.5%,这可能是因为第一个值上的equals
很少发生,这样一般来说检查运算符的if
更少。
答案 0 :(得分:2)
很抱歉,但是Nsight完全困惑了我,所有这一次我都认为我处于发布模式,但运行配置它自己被设置为运行调试模式。
现在我已经确定所有内容都已发布,并且效果会更好。
int sort和tuple排序之间的差异只有~150%,这更有意义。不知道我还能做些什么来提高性能,但已经足够好了。
结论是:小心eclipse首选项,特别是在Linux上。
答案 1 :(得分:0)
只需对此进行跟进,如果编译器无法调用它们,则functors
的使用会变慢(这可能由于各种原因(如单独的编译单元)而发生)。一个好的资源是here