Cuda 推力减小装置不适用于非等输入/输出类型
我试图用推力来减少一组值的最小值和最大值,我似乎被卡住了。给定一个浮点数组,我希望在一次过程中减少它们的最小值和最大值,但是使用推力的reduce方法,我得到了所有模板编译错误的母亲(或者至少是阿姨) 我的原始代码包含5个分布在2个float4数组上的值列表,我希望减少这些值,但我将其归结为这个简短的示例Cuda 推力减小装置不适用于非等输入/输出类型,cuda,thrust,Cuda,Thrust,我试图用推力来减少一组值的最小值和最大值,我似乎被卡住了。给定一个浮点数组,我希望在一次过程中减少它们的最小值和最大值,但是使用推力的reduce方法,我得到了所有模板编译错误的母亲(或者至少是阿姨) 我的原始代码包含5个分布在2个float4数组上的值列表,我希望减少这些值,但我将其归结为这个简短的示例 struct ReduceMinMax { __host__ __device__ float2 operator()(float lhs, float rhs) {
struct ReduceMinMax {
__host__ __device__
float2 operator()(float lhs, float rhs) {
return make_float2(Min(lhs, rhs), Max(lhs, rhs));
}
};
int main(int argc, char *argv[]){
thrust::device_vector<float> hat(4);
hat[0] = 3;
hat[1] = 5;
hat[2] = 6;
hat[3] = 1;
ReduceMinMax binary_op_of_dooooom;
thrust::reduce(hat.begin(), hat.end(), 4.0f, binary_op_of_dooooom);
}
struct reduceMinax{
__主机设备__
浮动2运算符()(浮动左侧,浮动右侧){
返回make_float2(最小值(左、右)、最大值(左、右);
}
};
int main(int argc,char*argv[]){
推力:装置_向量帽(4);
hat[0]=3;
hat[1]=5;
hat[2]=6;
hat[3]=1;
约化最大二进制数;
推力:减少(hat.begin(),hat.end(),4.0f,二进制的);
}
如果我把它分成两部分,它当然会起作用。我的问题是:是否有可能在一次推力下同时减少最小值和最大值,以及如何减少?如果没有,那么实现上述减排的最有效方式是什么?变换迭代器是否能帮助我(如果是,那么缩减是否为一次缩减?)
其他一些信息:
我使用的是推力1.5(由CUDA 4.2.7提供)
我的实际代码是使用reduce_by_键,而不仅仅是reduce键。
我在写这个问题时发现了transform\u reduce,但其中没有考虑键。正如Talonmes所指出的,您的归约没有编译,因为
推力::reduce
期望二进制运算符的参数类型与其结果类型匹配,但是reduceMinax
的参数类型是浮点型,而其结果类型为float2
推力::最小最大_元素
直接执行此操作,但如有必要,您可以改为使用推力::内部_产品
,它概括了推力::减少
:
#include <thrust/inner_product.h>
#include <thrust/device_vector.h>
#include <thrust/extrema.h>
#include <cassert>
struct minmax_float
{
__host__ __device__
float2 operator()(float lhs, float rhs)
{
return make_float2(thrust::min(lhs, rhs), thrust::max(lhs, rhs));
}
};
struct minmax_float2
{
__host__ __device__
float2 operator()(float2 lhs, float2 rhs)
{
return make_float2(thrust::min(lhs.x, rhs.x), thrust::max(lhs.y, rhs.y));
}
};
float2 minmax1(const thrust::device_vector<float> &x)
{
return thrust::inner_product(x.begin(), x.end(), x.begin(), make_float2(4.0, 4.0f), minmax_float2(), minmax_float());
}
float2 minmax2(const thrust::device_vector<float> &x)
{
using namespace thrust;
pair<device_vector<float>::const_iterator, device_vector<float>::const_iterator> ptr_to_result;
ptr_to_result = minmax_element(x.begin(), x.end());
return make_float2(*ptr_to_result.first, *ptr_to_result.second);
}
int main()
{
thrust::device_vector<float> hat(4);
hat[0] = 3;
hat[1] = 5;
hat[2] = 6;
hat[3] = 1;
float2 result1 = minmax1(hat);
float2 result2 = minmax2(hat);
assert(result1.x == result2.x);
assert(result1.y == result2.y);
}
#包括
#包括
#包括
#包括
结构最小最大浮点数
{
__主机设备__
浮动2运算符()(浮动左侧,浮动右侧)
{
返回使浮2(推力:最小(左、右)、推力:最大(左、右);
}
};
结构最小最大浮点数2
{
__主机设备__
浮动2运算符()(浮动2左侧,浮动2右侧)
{
返回使_浮动2(推力:最小(左S.x,右S.x),推力:最大(左S.y,右S.y));
}
};
float2 minmax1(常量推力::设备向量和x)
{
返回推力:内积(x.begin(),x.end(),x.begin(),make_float2(4.0,4.0f),minmax_float2(),minmax_float());
}
float2 minmax2(常量推力::设备向量和x)
{
使用名称空间推力;
将ptr_与_结果配对;
ptr_to_result=minmax_元素(x.begin(),x.end());
返回make_float2(*ptr_to_result.first,*ptr_to_result.second);
}
int main()
{
推力:装置_向量帽(4);
hat[0]=3;
hat[1]=5;
hat[2]=6;
hat[3]=1;
float2 result1=minmax1(hat);
float2 result2=minmax2(hat);
断言(result1.x==result2.x);
断言(result1.y==result2.y);
}
二进制运算符函数的返回类型不能与其参数类型不同。无论如何,您为什么要使用struch::reduce
?您不能使用推力::最小值元素?