c++ - 使用 tiled_range 推力 sort_by_key - 不重新排序

标签 c++ sorting cuda thrust

我在 cuda 中使用带有 sort_by_key() 的 tiled_range 和键 在 sort_by_key 期间不会重新排序,只是值...

例如:我有一个代表键的 vector :

 Keys   0  1    2    3   4   5   

稍后使用 tiled_range,我得到了具有重复值的新 vector 。

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

另一个表示值的 vector :

 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;

最佳答案

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

我注意到他们将 tiled_range 的值复制到另一个 vector 中,在复制这个新 vector 并将其放入 sort_by_keys 之后,它完美地对键和值进行了排序。我仍然不明白为什么将 tiled_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;
}

关于c++ - 使用 tiled_range 推力 sort_by_key - 不重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293538/

相关文章:

Java 快速排序算法

python - 给定一个升序排序的整数数组,编写一个算法将所有重复项推到后面。ex [1,2,2,4,5,5] 变为 [1,2,4,5,2,5]

multithreading - OpenCV 与 CUDA 对多个 GPU 使用 TBB 时出错

unit-testing - 是否可以模拟 GPU 以进行 CUDA/OpenCL 单元测试?

c++ - 异常如何在 C++ 中工作(在幕后)

c++ - shared_ptr 对容器的意义是什么?

c++ - DirectX 11:创建多个对象

c++ - 打印一个 map 的值,它有两个字符串作为键和 vector 作为值

mysql - 如何在对记录进行编号时使用 MySQL 选择多行?

C++/CUDA : Calculating maximum gridSize and blockSize dynamically