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

时间:2012-04-09 17:55:50

标签: iterator cuda operator-overloading tuples 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;

    }
};

我得到的错误是:

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   ---

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

先谢谢, 森

2 个答案:

答案 0 :(得分: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;

    }
};

希望它也适合你:)

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());    
}

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

答案 1 :(得分:1)

这是一个完整的例子,说明当 <% comment.responses.each do |response| %> 处理的密钥是sort_by_key以及自定义的比较运算符时,如何使用tuple

zip_iterator