Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对元组的推力排序非常慢_C++_Sorting_Cuda_Tuples_Thrust - Fatal编程技术网

C++ 对元组的推力排序非常慢

C++ 对元组的推力排序非常慢,c++,sorting,cuda,tuples,thrust,C++,Sorting,Cuda,Tuples,Thrust,我需要对一个元组数组进行排序,因此我正在为元组定义一个操作符,并使用推力::sort进行排序 所以我发现对元组数组进行排序要比对数字数组进行排序慢得多。这是我的密码: #include <thrust/device_vector.h> #include <thrust/host_vector.h> #include <thrust/set_operations.h> #include <thrust/reduce.h> #include <t

我需要对一个元组数组进行排序,因此我正在为元组定义一个操作符,并使用
推力::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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
静态常数int size=100000;
#定义mzi(x)推力::make_-zip_迭代器(x)
#定义mt(…)推力::生成元组(uu-VA_-ARGS)
typedef推力::tuple IntTuple;
typedef推力::设备向量::迭代器元组迭代器;
typedef推力::设备\向量::迭代器-迭代器;
typedef推力::元组迭代器元组;
typedef推力::zip_迭代器ZipIterator;
结构元组
{
__主机设备__
bool运算符()(常量IntTuple&t1、常量IntTuple&t2)
{
返回t1.get()!=t2.get()?t1.get()t2.get();
}
};
int main()
{
timespec启动;
时钟获取时间(0,&开始);
推力:设备_矢量数据a1(尺寸);
推力:装置矢量数据A2(尺寸);
推力:设备_矢量数据1(尺寸);
推力:设备_矢量数据2(尺寸);
srand(时间(空));
对于(int i=0;i很抱歉,但是Nsight完全把我弄糊涂了,我一直认为我处于发布模式,但是运行配置本身设置为运行调试模式

现在我已经确定了一切都已准备好发布,而且效果更好

int-sort和tuple-sort之间的差异只有约150%,这更有意义。我不确定还能做些什么来提高性能,但已经足够好了


结论是:小心eclipse首选项,尤其是在linux上。

接下来,如果编译器无法内联调用
函数,则它们的使用速度会变慢(这可能是由于各种原因,如单独的编译单元)。一个好的资源是

比较似乎不公平,因为它们不使用相同的顺序逻辑或相同的数据…为什么不?这些结果是一些测试的平均结果。它们都不依赖技巧或精确的数组顺序,都是随机和公平的。更改比较器确实提高了排序的平均速度,为什么不公平?如果我不理解没错,我做了各种排序,但在我写的结果中,我只比较了排序(zip与tuple,mycomp),这样比较是公平的,我还对其他代码进行了注释,以分别查看每个排序的性能。我发布的代码一次完成了所有测试。
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>();
    }
};