[英]Using thrust::reduce to compute the sum over a vector of 8 bit integers without overflow
I've got a device vector of type uint8_t
and I want to compute a sum over it using thrust::reduce
if possible.我有一个uint8_t
类型的设备向量,如果可能的话,我想使用thrust::reduce
计算一个总和。 The problem is that I get overflow, since the sum will be much larger than 255. I thought the code below would compute the sum, by storing the results as 32 bit integers, but it doesn't seem to be the case.问题是我溢出了,因为总和将远大于 255。我认为下面的代码将通过将结果存储为 32 位整数来计算总和,但似乎并非如此。 Is there a good way to accomplish this?有没有什么好方法可以做到这一点?
uint8_t * flags_d;
...
const int32_t N_CMP_BLOCKS = thrust::reduce(
thrust::device_pointer_cast( flags_d ),
thrust::device_pointer_cast( flags_d ) + N,
(int32_t) 0,
thrust::plus<int32_t>() );
I think the only solution that will work is to use thrust::transform_reduce
to explicitly cast the 8 bit input data to a 32 bit quantity before the accumulation operation in the reduction.我认为唯一可行的解决方案是在归约中的累积操作之前使用thrust::transform_reduce
将 8 位输入数据显式转换为 32 位数量。 So I would expect something like this:所以我会期待这样的事情:
#include <thrust/transform_reduce.h>
#include <thrust/functional.h>
#include <thrust/execution_policy.h>
template<typename T1, typename T2>
struct char2int
{
__host__ __device__ T2 operator()(const T1 &x) const
{
return static_cast<T2>(x);
}
};
int main()
{
unsigned char data[6] = {128, 100, 200, 102, 101, 123};
int result = thrust::transform_reduce(thrust::host,
data, data + 6,
char2int<unsigned char,int>(),
0,
thrust::plus<int>());
std::cout << "Result is " << result << std::endl;
return 0;
}
to be more like what you had in mind.更像你的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.