thrust :: min_element不适用于float4 device_vector,而它适用于host_vector

iga*_*l k 1 cuda thrust

我试图使用Thrust和CUDA找到数组中的最小数字.
以下设备示例返回0:

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

但是,这个主机版本完美运行:

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();
Run Code Online (Sandbox Code Playgroud)

comperator类型:

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

我只是想补充一下,push :: sort与同一个谓词一起使用.

Jar*_*ock 5

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

解决方法是使用没有对齐的类型,例如my_float4:

struct my_float4
{
  float x, y, z, w;
};
Run Code Online (Sandbox Code Playgroud)