gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:46:15 24 4
gpt4 key购买 nike

我在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com