在推力中处理交错数据的最佳方法是什么,比方说我想要将交错长度等于3的值添加,例如:
[1, 2, 3, 4, 5, 6]
会给出
[6, 15]
或解交织数据,所以
[1, 2, 3, 4, 5, 6, 7, 8, 9]
会给出
[1, 4, 7, 2, 5, 8, 3, 6, 9]
感谢
答案 0 :(得分:5)
这里有两个问题。第一部分询问如何对数据集执行结构化缩减,第二部分询问如何在给定映射的情况下对数据集重新排序。
第一个问题可以通过将数据集逻辑分区为常规大小的子集集合,然后对每个子集执行减少来解决。在推力中,可以通过将reduce_by_key
与已转换的counting_iterator
相结合来完成。我们的想法是用每个数据的子集索引“键入”每个数据。 reduce_by_key
用相同的键对每个连续的数据求和。
第二个问题可以通过置换数据集的顺序来解决。您可以拨打gather
来执行此操作。这里,变换后的counting_iterator
可以将索引从原始数组的映射传递到置换数组。您还可以使用transform
将此类收集操作与其他算法(例如permutation_iterator
)融合。请查看example program,了解有关如何操作的建议。
也就是说,由于内存合并问题,在GPU上置换阵列的成本很高,所以你应该谨慎使用。
这是解决您的两个问题的完整计划:
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/gather.h>
#include <thrust/functional.h>
struct divide_by_three
: thrust::unary_function<unsigned int, unsigned int>
{
__host__ __device__
unsigned int operator()(unsigned int i)
{
return i / 3;
}
};
struct deinterleave_index
: thrust::unary_function<unsigned int, unsigned int>
{
__host__ __device__
unsigned int operator()(unsigned int i)
{
return (i/3) + 3 * (i%3);
}
};
int main()
{
using namespace thrust;
device_vector<int> example_one(6);
example_one[0] = 1; example_one[1] = 2; example_one[2] = 3;
example_one[3] = 4; example_one[4] = 5; example_one[5] = 6;
// the result will have size two
device_vector<int> example_one_result(2);
// for each datum, associate an key, which is the datum's index divided by three
// reduce the data by key
reduce_by_key(make_transform_iterator(make_counting_iterator(0u), divide_by_three()),
make_transform_iterator(make_counting_iterator(6u), divide_by_three()),
example_one.begin(),
thrust::make_discard_iterator(),
example_one_result.begin());
std::cout << "example one input: [ ";
thrust::copy(example_one.begin(), example_one.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "]" << std::endl;
std::cout << "example one result: [ ";
thrust::copy(example_one_result.begin(), example_one_result.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "]" << std::endl;
device_vector<int> example_two(9);
example_two[0] = 1; example_two[1] = 2; example_two[2] = 3;
example_two[3] = 4; example_two[4] = 5; example_two[5] = 6;
example_two[6] = 7; example_two[7] = 8; example_two[8] = 9;
// the result will be the same size
device_vector<int> example_two_result(9);
// gather using the mapping defined by deinterleave_index
gather(make_transform_iterator(make_counting_iterator(0u), deinterleave_index()),
make_transform_iterator(make_counting_iterator(9u), deinterleave_index()),
example_two.begin(),
example_two_result.begin());
std::cout << "example two input: [ ";
thrust::copy(example_two.begin(), example_two.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "]" << std::endl;
std::cout << "example two result: [ ";
thrust::copy(example_two_result.begin(), example_two_result.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "]" << std::endl;
return 0;
}
输出:
$ nvcc test.cu -run
example one input: [ 1 2 3 4 5 6 ]
example one result: [ 6 15 ]
example two input: [ 1 2 3 4 5 6 7 8 9 ]
example two result: [ 1 4 7 2 5 8 3 6 9 ]