Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why thrust::count and thrust::count_if give diffenrent results for counting how many elements are zero in device vector?

I have a thrust device vector with 16.679.056 float elements in it.

The result of int result_count_0 = thrust::count(d_result.begin(),d_result.end(), 0); is 14.786.283
But at the same time the following approach with thrust::count_if gives a different result (15.733.822)

struct is_zero
{
  __host__ __device__
  bool operator()(int x)
  {
    return x == 0;
  }
};

...
    is_zero iszero;
    int result_count_isZero = thrust::count_if(d_result.begin(),d_result.end(),iszero);

Why is this so? The result of count_if makes more sense, because the number of non zero elements is 945.234 (945234 + 15733822 is 16679056, the exakt size of my device vector).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It seems the difference exists due to tuancations of float numbers to numbers of the type int in the function object

bool operator()(int x)
{
  return x == 0;
}

Change the type of the parameter in the operator function to the type of stored objects.

Here is a demonstration program that uses standard algorithms std::count and std::count_if that demonstrates the reason of the difference.

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    struct is_zero
    {
        bool operator()( int x ) const
        {
            return x == 0;
        }
    };

    std::vector<float> v = { 1.0f, 0.0f, 0.5f, -0.9f };

    std::cout << std::count( v.begin(), v.end(), 0.0f ) << '\n';
    std::cout << std::count_if( v.begin(), v.end(), is_zero() ) << '\n';
}

The program output is

1
3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading