首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >推力sort_by_key使用tiled_range -不重新排序

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

Stack Overflow用户
提问于 2014-06-18 19:23:35
回答 1查看 143关注 0票数 0

我在库达中使用tiled_range和一个sort_by_key(),在sort_by_key期间键没有得到重新排序,只是值.

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

代码语言:javascript
复制
 Keys   0  1    2    3   4   5   

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

代码语言:javascript
复制
 Keys   0  1    2    3   4   5     0    1   2   3    4     5     

以及另一个表示值的向量:

代码语言:javascript
复制
 values 0 3382 1863 470 311 2017 3382   0  251 1394 5651  257

我正期待着和sort_by_keys一起重新订购这样的钥匙:

代码语言:javascript
复制
 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

我的代码用这种方式重新排列钥匙.

代码语言:javascript
复制
 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

我想知道,以这种方式重新排序的原因是什么,我能做些什么才能得到正确的订单?

以下是代码:

代码语言:javascript
复制
#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;

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-18 21:32:48

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

我注意到他们将tiled_range的值复制到另一个向量中,在复制这个新向量并将其放入sort_by_keys之后,它将键和值进行了完美的排序。我仍然不明白为什么把tiled_range的迭代器作为sort_by_keys的输入,会在上面造成这样的混乱。

以下是代码:

代码语言:javascript
复制
#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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24293538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档