[英]Fastest way to sum dot product of vector of unsigned 64 bit integers using 192/256 bit integer?
[英]Using thrust::reduce to compute the sum over a vector of 8 bit integers without overflow
我有一个uint8_t
类型的设备向量,如果可能的话,我想使用thrust::reduce
计算一个总和。 问题是我溢出了,因为总和将远大于 255。我认为下面的代码将通过将结果存储为 32 位整数来计算总和,但似乎并非如此。 有没有什么好方法可以做到这一点?
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>() );
我认为唯一可行的解决方案是在归约中的累积操作之前使用thrust::transform_reduce
将 8 位输入数据显式转换为 32 位数量。 所以我会期待这样的事情:
#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;
}
更像你的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.