cuda - 如何使用 Thrust 减少数组中不连续的数字段
问题描述
我正在实现一个代码来总结非连续段(总结“A”中每个数组“a”的相同颜色的段中的数字,如下所示:
有什么想法可以通过推力有效地做到这一点吗?
非常感谢你
注意:图片仅代表一个数组“a”。大数组“A”包含许多数组“a”
解决方案
在一般情况下,如果事先不知道数据的排序和按段分组,一般建议是使用thrust::sort_by_key
将相似段分组在一起,然后使用thrust::reduce_by_key
对段求和。这里给出了例子。
但是,如果输入数据段遵循已知的重复模式,如这里建议的那样,我们可以通过使用 athrust::permutation_iterator
将相似的段“聚集”在一起作为 的输入来消除排序步骤thrust::reduce_by_key
。
使用问题中的示例数据,困难的部分是创建置换迭代器。为此,使用问题中给出的段类型(3)、段长度(3)和每个段类型的段数(3)的特定数量,我们需要一个映射“向量”(即迭代器)用于我们的置换迭代器具有以下顺序:
0 1 2 9 10 11 18 19 20 3 4 5 12 13 14 21 22 23 ...
然后,该序列将“映射”或重新排列输入数组,以便将所有相似的段组合在一起。我确信有多种方法可以创建这样的序列,但我选择的方法如下。我们将从标准计数迭代器序列开始,然后对其应用变换函子(使用make_transform_iterator
),以便我们创建上述序列。我选择使用以下方法进行操作,按逐步顺序排列,显示添加在一起的组件:
counting iterator: (_1) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
---------------------------------------------------------------------------------------------------
((_1/seg_len)%seg_types)*(seg_len*seg_types): 0 0 0 9 9 9 18 18 18 0 0 0 9 9 9 18 18 18 ...
_1%seg_len: 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 ...
_1/(seg_len*seg_types)*seg_len: 0 0 0 0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 ...
Sum: 0 1 2 9 10 11 18 19 20 3 4 5 12 13 14 21 22 23 ...
这是一个完整的示例:
$ cat t457.cu
#include <thrust/reduce.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <iostream>
typedef int dtype;
const int seg_len = 3;
const int seg_types = 3;
using namespace thrust::placeholders;
int main(){
dtype data[] = {10,16,14,2,4,4,1,2,1,8,2,10,3,1,6,8,0,2,9,1,0,3,5,2,3,2,1};
// 0 1 2 9 10 11 18 19 20 3 4 5 12 13 14 21 22 23 ...
// ((_1/seg_len)%seg_types)*(seg_len*seg_types) + _1%seg_len + (_1/(seg_len*seg_types)*seg_len
int ads = sizeof(data)/sizeof(data[0]);
int num_groups = ads/(seg_len*seg_types); // ads is expected to be whole-number divisible by seg_len*seg_types
int ds = num_groups*(seg_len*seg_types); // handle the case when it is not
thrust::device_vector<dtype> d_data(data, data+ds);
thrust::device_vector<dtype> d_result(seg_types);
thrust::reduce_by_key(thrust::make_transform_iterator(thrust::counting_iterator<int>(0), _1/(ds/seg_types)), thrust::make_transform_iterator(thrust::counting_iterator<int>(ds), _1/(ds/seg_types)), thrust::make_permutation_iterator(d_data.begin(), thrust::make_transform_iterator(thrust::counting_iterator<int>(0), ((_1/seg_len)%seg_types)*(seg_len*seg_types) + _1%seg_len + (_1/(seg_len*seg_types)*seg_len))), thrust::make_discard_iterator(), d_result.begin());
thrust::copy(d_result.begin(), d_result.end(), std::ostream_iterator<dtype>(std::cout, ","));
std::cout << std::endl;
}
$ nvcc -o t457 t457.cu
$ ./t457
70,30,20,
$
推荐阅读
- javascript - 如何在手机浏览器中显示谷歌相册?
- python - 在python中逐行将数据写入csv文件
- angular - 带有映射的孩子的Angular 8 http子请求
- javascript - 单击 asp:linkbutton 时如何使用 data-id 填充隐藏字段
- php - 尝试模拟 Laravel Socialite Google Login 进行集成测试
- python - 在 Python 中将变量指针传递给字典
- spring - 用于 Google Cloud SQL Postgres 的 Spring JPA applicationContext.xml
- python - 机器字典理解或现有字典的深度复制更容易什么?
- javascript - 如果我在页面中添加空白,会发生什么?
- python - 为什么我的 8kHz wav 文件的 mel 特征在 sr = 16kHz 和 44.1kHz 中提取的不同