什么是在推力中使用交错数据的最佳方法,比方说我想添加交错长度等于3的值,例如:
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
会给
[6, 15]
Run Code Online (Sandbox Code Playgroud)
或者对数据进行去交织,所以
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
会给
[1, 4, 7, 2, 5, 8, 3, 6, 9]
Run Code Online (Sandbox Code Playgroud)
谢谢
这里有两个问题.第一部分询问如何对数据集执行结构化缩减,第二部分询问如何在给定映射的情况下对数据集重新排序.
第一个问题可以通过将数据集逻辑分区为常规大小的子集的集合,然后对每个子集执行减少来解决.在推力中,这可以通过reduce_by_key与变换的组合来完成counting_iterator.我们的想法是用每个数据的子集索引"键入"每个数据.reduce_by_key用相同的键对每个连续的数据求和.
第二个问题可以通过置换数据集的顺序来解决.您可以通过拨打电话来完成此操作gather.这里,变换后的counting_iterator可以将索引从原始阵列的映射传送到置换阵列.您还可以transform使用a 将这样的聚集操作与其他算法(例如)融合permutation_iterator.查看示例程序,了解有关如何执行此操作的建议.
也就是说,由于内存合并问题,在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;
}
Run Code Online (Sandbox Code Playgroud)
并输出:
$ 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 ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
863 次 |
| 最近记录: |