簡體   English   中英

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

[英]Thrust sort_by_key using tiled_range - not re-ordered

我在CUDA中使用帶有sort_by_key()的tiled_range,並且在sort_by_key期間鍵沒有重新排序,只是值...

例如:我有一個代表鍵的向量:

 Keys   0  1    2    3   4   5   

稍后使用tiled_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;

}

問題解決了! 我正在從以下位置檢查一些代碼: https : //groups.google.com/forum/#!msg/thrust-users/GqufHKoaizc/qx9cv-6xCg4J

我注意到他們將tiled_range的值復制到另一個向量中,在復制了這個新向量並將其放入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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM