使用tiles_range推送sort_by_key - 不重新排序

时间:2014-06-18 19:23:36

标签: c++ sorting cuda thrust

我在cuda和密钥中使用了tile_range和sort_by_key() 在sort_by_key期间没有重新排序,只是值......

例如:我有一个代表键的向量:

 Keys   0  1    2    3   4   5   

稍后使用tiles_range,我有一个重复值的新向量。

 Keys   0  1    2    3   4   5     0    1   2   3    4     5     

另一个代表值的向量:

 values 0 3382 1863 470 311 2017 3382   0  251 1394 5651  257

我期待sort_by_keys重新排序这样的键:

 Keys      0    0      1    1     2      2     3    3   4    4     5    5

 Values    0   3382   3382  0    1863   251   470  1394 311 5651  2017 257

我的代码以这种方式重新订购密钥......

 Keys      3    3      4    4     5      5     3    3   4    4     5    5 

 Values    0   3382   3382  0    1863   251   470  1394 311 5651  2017 257

我在想,这可能是因为以这种方式重新订购,我该怎样做才能获得正确的订单?

以下是代码:

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

using namespace thrust::placeholders;

template<typename Iterator>
class tiled_range
//Code of tiled_range....
// 

int main(void)
{
    thrust::device_vector<int> data(6);
    data[0] = 0; data[1] = 1; data[2] = 2; data[3] = 3; data[4] = 4; data[5] = 5;

    thrust::device_vector<float> values(12);
    values[0] = 0;          values[6] = 3382;
    values[1] = 3382;       values[7] = 0;
    values[2] = 1863;       values[8] = 251;
    values[3] = 470;        values[9] = 1394;
    values[4] = 311;        values[10] = 5651;
    values[5] = 2017;       values[11] = 257;

    tiled_range<thrust::device_vector<int>::iterator>  keys(data.begin(), data.end(), 2);

    std::cout << "Keys: " << std::endl;
    thrust::copy(keys.begin(), keys.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    thrust::sort_by_key(keys.begin(), keys.end(), values.begin());

    std::cout << "Keys: " << std::endl;
    thrust::copy(keys.begin(), keys.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "Values: " << std::endl;
    thrust::copy(values.begin(), values.end(), std::ostream_iterator<int>   (std::cout, " "));
    std::cout << std::endl;

return 0;

}

1 个答案:

答案 0 :(得分:2)

问题解决了!我正在检查以下代码: https://groups.google.com/forum/#!msg/thrust-users/GqufHKoaizc/qx9cv-6xCg4J

我注意到他们将tiles_range的值复制到另一个向量中,在复制这个新向量并将其放入sort_by_keys之后,它正在对键和值进行完美排序。我仍然不明白为什么通过将tiles_range的迭代器作为sort_by_keys的输入,它导致上面的混乱......

以下是代码:

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

using namespace thrust::placeholders;

template<typename Iterator>
class tiled_range
//Code of tiled_range....
// 

int main(void)
{
    thrust::device_vector<int> data(6);
    data[0] = 0; data[1] = 1; data[2] = 2; data[3] = 3; data[4] = 4; data[5] = 5;

    thrust::device_vector<float> values(12);
    values[0] = 0;          values[6] = 3382;
    values[1] = 3382;       values[7] = 0;
    values[2] = 1863;       values[8] = 251;
    values[3] = 470;        values[9] = 1394;
    values[4] = 311;        values[10] = 5651;
    values[5] = 2017;       values[11] = 257;

    tiled_range<thrust::device_vector<int>::iterator>  keys(data.begin(), data.end(), 2);
    thrust::device_vector<int> keysN(keys.begin(), keys.end());

    std::cout << "Keys: " << std::endl;
    thrust::copy(keysN.begin(), keysN.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    thrust::sort_by_key(keysN.begin(), keysN.end(), values.begin());

    std::cout << "Keys: " << std::endl;
    thrust::copy(keys.begin(), keys.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "Values: " << std::endl;
    thrust::copy(values.begin(), values.end(), std::ostream_iterator<int>   (std::cout, " "));
    std::cout << std::endl;

return 0;
}