2012-02-12 78 views
1

我试图在使用Thrust和CUDA的数组中找到最小数量。
以下设备示例返回0:thrust :: min_element在float4 device_vector上不起作用,而在host_vector上却起作用

thrust::device_vector<float4>::iterator it = thrust::min_element(IntsOnDev.begin(),IntsOnDev.end(),equalOperator());  
int pos = it - IntsOnDev.begin(); 

然而,这台主机版本完美的作品:

thrust::host_vector<float4>arr = IntsOnDev; 
thrust::host_vector<float4>::iterator it2 = thrust::min_element(arr.begin(),arr.end(),equalOperator()); 
int pos2 = it2 - arr.begin(); 

的comperator类型:

struct equalOperator 
{ 
    __host__ __device__ 
    bool operator()(const float4 x,const float4 y) const 
    { 
     return (x.w < y.w); 
    } 
}; 

我只是想补充一点,推力:: sort使用相同的谓词。

+1

会发生什么事,如果你尝试这种与'''my_float4''',即'''结构my_float4 {浮法X, Y,Z,W; };'''? – 2012-02-13 01:40:13

+0

做到了!我发现解决方案几分钟后,我写了原来的帖子...我实际上定义了一个新的我自己的float4结构,正弦新的结构基本上等于float4(以字节为单位),所以我不得不改变几乎没有 – 2012-02-13 06:26:54

回答

5

不幸的是,nvcc不同意某些主机编译器(如果我正确记得,MSVC的某些64位版本)有关某些对齐类型的大小。 float4就是其中之一。这通常会导致未定义的行为。

的解决办法是使用类型不对齐,例如my_float4

struct my_float4 
{ 
    float x, y, z, w; 
};