1

I'm trying to find the minimum number in a array using Thrust and CUDA.
The following device example returns with 0 :

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

However, this host version works perfectly:

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(); 

the comperator type :

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

I just wanted to add that thrust::sort works with the same predicate.

2
  • 1
    What happens if you try this with my_float4, i.e. struct my_float4 { float x,y,z,w; }; ? Commented Feb 13, 2012 at 1:40
  • that did the trick! i found out that solution few mins after i wrote the original post... i actually defined a new float4 struct of my own, sine the new struct is basically equal to float4(in bytes) so i had to change almost nothing Commented Feb 13, 2012 at 6:26

1 Answer 1

5

Unfortunately, nvcc disagrees with some host compilers (some 64 bit versions of MSVC, if I recall correctly) about the size of certain aligned types. float4 is one of these. This often results in undefined behavior.

The work-around is to use types without alignment, for example my_float4:

struct my_float4 { float x, y, z, w; }; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.