5

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

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

无效推力排序(
    无符号整数 * 主键,
    浮动 * 次要键,
    无符号整数 * 值,
    unsigned int numberOfPoints)
{
    推力::device_ptr dev_ptr_pkey = 推力::device_pointer_cast(primaryKey);
    推力::device_ptr dev_ptr_skey = 推力::device_pointer_cast(secondaryKey);
    推力::device_ptr dev_ptr_values = 推力::device_pointer_cast(values);

    推力::元组,推力::device_ptr> keytup_begin =
        推力::make_tuple,推力::device_ptr>(dev_ptr_pkey, dev_ptr_skey);

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

    推力::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;

    }
};

我得到的错误是:

错误 1 ​​错误:没有构造函数实例“thrust::device_ptr::device_ptr [with T=unsigned int]”匹配参数列表 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include\thrust\detail \元组.inl 309 1 ---
错误 2 错误:没有构造函数实例“thrust::device_ptr::device_ptr [with T=float]”匹配参数列表 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include\thrust\detail\元组.inl 401 1 ---

有什么想法可能导致这种情况/我如何编写一个确实有效的谓词?

提前致谢, 内森

4

2 回答 2

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

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

于 2012-04-18T00:40:59.887 回答
1

这是一个完整的示例,说明如何在由's 和自定义比较运算符处理sort_by_key键时使用。tuplezip_iterator

#include <thrust/device_vector.h>
#include <thrust/sort.h>

#include "Utilities.cuh"

// --- Defining tuple type
typedef thrust::tuple<int, int> Tuple;

/**************************/
/* TUPLE ORDERING FUNCTOR */
/**************************/
struct TupleComp
{
    __host__ __device__ bool operator()(const Tuple& t1, const Tuple& 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>();
    }
};

/********/
/* MAIN */
/********/
int main()
{
    const int N = 8;

    // --- Keys and values on the host: allocation and definition
    int h_keys1[N]      = { 1, 3, 3, 3, 2, 3, 2, 1 };                                         
    int h_keys2[N]      = { 1, 5, 3, 8, 2, 8, 1, 1 };                                         
    float h_values[N]   = { 0.3, 5.1, 3.2, -0.08, 2.1, 5.2, 1.1, 0.01};

    printf("\n\n");
    printf("Original\n");
    for (int i = 0; i < N; i++) {
        printf("%i %i %f\n", h_keys1[i], h_keys2[i], h_values[i]);
    }

    // --- Keys and values on the device: allocation
    int *d_keys1;       gpuErrchk(cudaMalloc(&d_keys1, N * sizeof(int)));
    int *d_keys2;       gpuErrchk(cudaMalloc(&d_keys2, N * sizeof(int)));
    float *d_values;    gpuErrchk(cudaMalloc(&d_values, N * sizeof(float)));

    // --- Keys and values: host -> device
    gpuErrchk(cudaMemcpy(d_keys1, h_keys1, N * sizeof(int), cudaMemcpyHostToDevice));
    gpuErrchk(cudaMemcpy(d_keys2, h_keys2, N * sizeof(int), cudaMemcpyHostToDevice));
    gpuErrchk(cudaMemcpy(d_values, h_values, N * sizeof(float), cudaMemcpyHostToDevice));

    // --- From raw pointers to device_ptr
    thrust::device_ptr<int> dev_ptr_keys1 = thrust::device_pointer_cast(d_keys1);
    thrust::device_ptr<int> dev_ptr_keys2 = thrust::device_pointer_cast(d_keys2);
    thrust::device_ptr<float> dev_ptr_values = thrust::device_pointer_cast(d_values);

    // --- Declare outputs
    thrust::device_vector<float> d_values_output(N);
    thrust::device_vector<Tuple> d_keys_output(N);

    auto begin_keys = thrust::make_zip_iterator(thrust::make_tuple(dev_ptr_keys1, dev_ptr_keys2));
    auto end_keys = thrust::make_zip_iterator(thrust::make_tuple(dev_ptr_keys1 + N, dev_ptr_keys2 + N));

    thrust::sort_by_key(begin_keys, end_keys, dev_ptr_values, TupleComp());

    int *h_keys1_output = (int *)malloc(N * sizeof(int));
    int *h_keys2_output = (int *)malloc(N * sizeof(int));
    float *h_values_output = (float *)malloc(N * sizeof(float));

    gpuErrchk(cudaMemcpy(h_keys1_output, d_keys1, N * sizeof(int), cudaMemcpyDeviceToHost));
    gpuErrchk(cudaMemcpy(h_keys2_output, d_keys2, N * sizeof(int), cudaMemcpyDeviceToHost));
    gpuErrchk(cudaMemcpy(h_values_output, d_values, N * sizeof(float), cudaMemcpyDeviceToHost));

    printf("\n\n");
    printf("Ordered\n");
    for (int i = 0; i < N; i++) {
        printf("%i %i %f\n", h_keys1_output[i], h_keys2_output[i], h_values_output[i]);
    }

}
于 2016-06-24T09:05:35.430 回答