当密钥是zip_iterator使用自定义比较谓词处理的元组时,CUDA Thrust sort_by_key [英] CUDA Thrust sort_by_key when the key is a tuple dealt with by zip_iterator's with custom comparison predicate

查看:567
本文介绍了当密钥是zip_iterator使用自定义比较谓词处理的元组时,CUDA Thrust sort_by_key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've looked through a lot of questions here for something similar and there are quite a few, albeit with one minor change. I'm trying to sort values with a zip_iterator as a compound key.

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

Specifically, I have the following function:


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

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

Any ideas what might cause this / how do I write a predicate that indeed works?

在此先感谢,
Nathan

Thanks in Advance, Nathan

推荐答案

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

The comparator takes arguments of type const thrust::tuple<unsigned int, float>&. The const tdp_uif_tuple& type you defined expands to 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/快速入门指南#zip_iterator 有关于zip迭代器的更多详细信息。

http://code.google.com/p/thrust/wiki/QuickStartGuide#zip_iterator has more details on the zip iterator.

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

Not required, but if you're looking to clean up the length of those templates, you can do this:

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

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

A lot of the template arguments can be inferred from the arguments.

这篇关于当密钥是zip_iterator使用自定义比较谓词处理的元组时,CUDA Thrust sort_by_key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆