一种可能的方法是使用thrust。
一个可能的序列将是这样的:
- 使用 thrust::reduce_by_key 以产生用于每个范围的最大值。
- 使用thrust :: adjacent_difference描绘每个范围的起始点
- 对步骤2的结果使用包含式扫描来生成聚集索引,即将用于选择缩小值的索引(来自步骤1)将会出现在输出向量的每个位置。
- 使用thrust::gather_if使用步骤3中生成的聚集索引,选择性地将缩小的值放置到输出向量中的适当位置(B向量中存在1)。
这里的一个充分的工作代码演示此,使用像您的示例A和B载体:
关于示例
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/adjacent_difference.h>
#include <thrust/reduce.h>
#include <thrust/copy.h>
#include <thrust/transform_scan.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/functional.h>
#define DSIZE 8
template <typename T>
struct abs_val : public thrust::unary_function<T, T>
{
__host__ __device__
T operator()(const T& x) const
{
if (x<0) return -x;
else return x;
}
};
template <typename T>
struct subtr : public thrust::unary_function<T, T>
{
const T val;
subtr(T _val): val(_val) {}
__host__ __device__
T operator()(const T& x) const
{
return x-val;
}
};
int main(){
int A[DSIZE] = {45,21,764,234,7,0,12,55};
int B[DSIZE] = {1,1,1,0,1,1,0,0};
thrust::device_vector<int> dA(A, A+DSIZE);
thrust::device_vector<int> dB(B, B+DSIZE);
thrust::device_vector<int> dRed(DSIZE);
thrust::device_vector<int> diffB(DSIZE);
thrust::device_vector<int> dRes(DSIZE);
thrust::reduce_by_key(dB.begin(), dB.end(), dA.begin(), thrust::make_discard_iterator(), dRed.begin(), thrust::equal_to<int>(), thrust::maximum<int>());
thrust::adjacent_difference(dB.begin(), dB.end(), diffB.begin());
thrust::transform_inclusive_scan(diffB.begin(), diffB.end(), diffB.begin(), abs_val<int>(), thrust::plus<int>());
thrust::gather_if(thrust::make_transform_iterator(diffB.begin(), subtr<int>(B[0])), thrust::make_transform_iterator(diffB.end(), subtr<int>(B[0])), dB.begin(), dRed.begin(), dRes.begin());
thrust::copy(dRes.begin(), dRes.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
注:
- reduce_by_key正在产生降低的值(最大值),用于每个 连续的0序列或 B中的1个序列。您只需真正需要 这1个序列的最大值。我们将通过gather_if函数丢弃最大值为0的序列 。
- 我允许该B载体可以与任一 1个序列或0的序列开始,通过使用transform_iterator 处理步骤2的矢量结果,减去从每个B载体的第一 值的可能性收集索引。
- adjacent_difference操作将产生1或-1到 划定新序列的开始。为了扫描目的(即生成聚集索引),我使用带有abs_val函子的变换包含变换函数variant_inclusive_scan来平等对待它们。
上面的代码应该产生的结果符合期望C
输出向量,就像这样:
$ nvcc -arch=sm_20 -o t53 t53.cu
$ ./t53
764 764 764 0 7 7 0 0
$
我们可以使用thrust::placeholders进一步简化上面的代码,省去了多余的仿函数定义的需要:
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/adjacent_difference.h>
#include <thrust/reduce.h>
#include <thrust/copy.h>
#include <thrust/transform_scan.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/functional.h>
#define DSIZE 2000000
using namespace thrust::placeholders;
typedef int mytype;
int main(){
mytype *A = (mytype *)malloc(DSIZE*sizeof(mytype));
int *B = (int *)malloc(DSIZE*sizeof(int));
for (int i = 0; i < DSIZE; i++){
A[i] = (rand()/(float)RAND_MAX)*10.0f;
B[i] = rand()%2;}
thrust::device_vector<mytype> dA(A, A+DSIZE);
thrust::device_vector<int> dB(B, B+DSIZE);
thrust::device_vector<mytype> dRed(DSIZE);
thrust::device_vector<int> diffB(DSIZE);
thrust::device_vector<mytype> dRes(DSIZE);
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
thrust::reduce_by_key(dB.begin(), dB.end(), dA.begin(), thrust::make_discard_iterator(), dRed.begin(), thrust::equal_to<mytype>(), thrust::maximum<mytype>());
thrust::adjacent_difference(dB.begin(), dB.end(), diffB.begin());
thrust::transform_inclusive_scan(diffB.begin(), diffB.end(), diffB.begin(), _1*_1, thrust::plus<int>());
thrust::gather_if(thrust::make_transform_iterator(diffB.begin(), _1 - B[0]), thrust::make_transform_iterator(diffB.end(), _1 - B[0]), dB.begin(), dRed.begin(), dRes.begin());
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float et;
cudaEventElapsedTime(&et, start, stop);
std::cout<< "elapsed time: " << et << "ms " << std::endl;
thrust::copy(dRes.begin(), dRes.begin()+10, std::ostream_iterator<mytype>(std::cout, " "));
std::cout << std::endl;
return 0;
}
(I已经修改了上述占位符代码还包括生成一个较大尺寸的数据集的,以及一些基本定时APPA鼠标)
您可以使用[thrust](https://github.com/thrust/thrust/wiki/Quick-Start-Guide)函数[reduce_by_key](http://thrust.github.io/doc /group__reductions.html#ga1fd25c0e5e4cc0a6ab0dcb1f7f13a2ad)来帮助解决这个问题。 – 2014-09-01 12:07:25
这将有助于找到区域的最大值,但是我是不是仍然会丢失我的位置,或者不得不基本遍历整个阵列来填充区域不平行的区域 – user2719805 2014-09-01 13:27:24
是的,我没有暗示这是一个完整的解决方案。我已经添加了一个答案,说明如何在完整的解决方案中使用它。 – 2014-09-01 15:02:07