当密钥是由zip_iterator处理的元组时,CUDA Thrust sort_by_key具有自定义比较谓词

Nat*_*man 5 iterator cuda tuples operator-overloading thrust

我在这里看了很多类似的问题并且有很多问题,尽管有一个小的改变.我正在尝试使用zip_iterator作为复合键对值进行排序.

具体来说,我有以下功能:

void thrustSort(
    unsigned int * primaryKey,
    float * secondaryKey,
    unsigned int * values,
    unsigned int numberOfPoints)
{
    thrust::device_ptr dev_ptr_pkey = thrust::device_pointer_cast(primaryKey);
    thrust::device_ptr dev_ptr_skey = thrust::device_pointer_cast(secondaryKey); 
    thrust::device_ptr dev_ptr_values = thrust::device_pointer_cast(values);

    thrust::tuple,thrust::device_ptr> keytup_begin =
        thrust::make_tuple,thrust::device_ptr>(dev_ptr_pkey, dev_ptr_skey);

    thrust::zip_iterator, thrust::device_ptr > > first =
        thrust::make_zip_iterator, thrust::device_ptr > >(keytup_begin);

    thrust::sort_by_key(first, first + numberOfPoints, dev_ptr_values, ZipComparator());    
}

和这个自定义谓词:

typedef thrust::device_ptr<unsigned int> tdp_uint ;
typedef thrust::device_ptr<float> tdp_float ;
typedef thrust::tuple<tdp_uint, tdp_float> tdp_uif_tuple ;

struct ZipComparator
{
    __host__ __device__
    inline bool operator() (const tdp_uif_tuple &a, const tdp_uif_tuple &b)
    {
        if(a.head < b.head) return true;
        if(a.head == b.head) return a.tail < b.tail;
        return false;

    }
};
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Error   1   error : no instance of constructor "thrust::device_ptr::device_ptr [with T=unsigned int]" matches the argument list  C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include\thrust\detail\tuple.inl 309 1   ---
Error   2   error : no instance of constructor "thrust::device_ptr::device_ptr [with T=float]" matches the argument list C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include\thrust\detail\tuple.inl 401 1   ---

任何想法可能导致这个/如何编写确实有效的谓词?

Nathan,先谢谢你

Dev*_*ane 2

比较器采用 类型的参数const thrust::tuple<unsigned int, float>&。您定义的类型const tdp_uif_tuple&扩展为const thrust::tuple<thrust::device_ptr<unsigned int>, thrust:device_ptr<float> >&

下面的代码为我编译:

struct ZipComparator
{
    __host__ __device__
    inline bool operator() (const thrust::tuple<unsigned int, float> &a, const thrust::tuple<unsigned int, float> &b)
    {
        if(a.head < b.head) return true;
        if(a.head == b.head) return a.tail < b.tail;
        return false;

    }
};
Run Code Online (Sandbox Code Playgroud)

希望它也适合你:)

http://code.google.com/p/thrust/wiki/QuickStartGuide#zip_iterator有关于 zip 迭代器的更多详细信息。

不是必需的,但如果您想要清理这些模板的长度,您可以这样做:

void thrustSort(
    unsigned int * primaryKey,
    float * secondaryKey,
    unsigned int * values,
    unsigned int numberOfPoints)
{
    tdp_uint dev_ptr_pkey(primaryKey);
    tdp_float dev_ptr_skey(secondaryKey);   
    tdp_uint dev_ptr_values(values);

    thrust::tuple<tdp_uint, tdp_float> keytup_begin = thrust::make_tuple(dev_ptr_pkey, dev_ptr_skey);

    thrust::zip_iterator<thrust::tuple<tdp_uint, tdp_float> > first =
    thrust::make_zip_iterator(keytup_begin);

    thrust::sort_by_key(first, first + numberOfPoints, dev_ptr_values, ZipComparator());    
}
Run Code Online (Sandbox Code Playgroud)

许多模板参数可以从参数中推断出来。