我需要总结一下存储在数组中的100000
值,但有条件。
有没有办法在CUDA中做到这一点以产生快速结果?
任何人都可以发布一个小代码吗?
答案 0 :(得分:4)
我认为,要执行条件缩减,您可以直接将条件引入加法0
(false)或1
(true)到加数。换句话说,假设您要满足的条件是加数小于10.f
。在这种情况下,借用Optimizing Parallel Reduction in CUDA by M. Harris的第一个代码,然后上面的意思是
__global__ void reduce0(int *g_idata, int *g_odata) {
extern __shared__ int sdata[];
// each thread loads one element from global to shared mem
unsigned int tid = threadIdx.x;
unsigned int i = blockIdx.x*blockDim.x + threadIdx.x;
sdata[tid] = g_idata[i]*(g_data[i]<10.f);
__syncthreads();
// do reduction in shared mem
for(unsigned int s=1; s < blockDim.x; s *= 2) {
if (tid % (2*s) == 0) {
sdata[tid] += sdata[tid + s];
}
__syncthreads();
}
// write result for this block to global mem
if (tid == 0) g_odata[blockIdx.x] = sdata[0];
}
如果您希望使用CUDA Thrust执行条件缩减,则可以使用thrust::transform_reduce
执行相同操作。或者,您可以创建一个新的向量d_b
,复制d_a
的所有元素,使thrust::copy_if
满足谓词,然后在thrust::reduce
上应用d_b
。我没有检查哪种解决方案表现最佳。也许,第二种解决方案在稀疏阵列上表现更好。下面是一个实现这两种方法的例子。
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/count.h>
#include <thrust/copy.h>
// --- Operator for the first approach
struct conditional_operator {
__host__ __device__ float operator()(const float a) const {
return a*(a<10.f);
}
};
// --- Operator for the second approach
struct is_smaller_than_10 {
__host__ __device__ bool operator()(const float a) const {
return (a<10.f);
}
};
void main(void)
{
int N = 20;
// --- Host side allocation and vector initialization
thrust::host_vector<float> h_a(N,1.f);
h_a[0] = 20.f;
h_a[1] = 20.f;
// --- Device side allocation and vector initialization
thrust::device_vector<float> d_a(h_a);
// --- First approach
float sum = thrust::transform_reduce(d_a.begin(), d_a.end(), conditional_operator(), 0.f, thrust::plus<float>());
printf("Result = %f\n",sum);
// --- Second approach
int N_prime = thrust::count_if(d_a.begin(), d_a.end(), is_smaller_than_10());
thrust::device_vector<float> d_b(N_prime);
thrust::copy_if(d_a.begin(), d_a.begin() + N, d_b.begin(), is_smaller_than_10());
sum = thrust::reduce(d_b.begin(), d_b.begin() + N_prime, 0.f);
printf("Result = %f\n",sum);
getchar();
}