thrust
Functions
Counting

Functions

template<typename DerivedPolicy , typename InputIterator , typename EqualityComparable >
__host__ __device__
thrust::iterator_traits
< InputIterator >
::difference_type 
thrust::count (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, const EqualityComparable &value)
 
template<typename InputIterator , typename EqualityComparable >
thrust::iterator_traits
< InputIterator >
::difference_type 
thrust::count (InputIterator first, InputIterator last, const EqualityComparable &value)
 
template<typename DerivedPolicy , typename InputIterator , typename Predicate >
__host__ __device__
thrust::iterator_traits
< InputIterator >
::difference_type 
thrust::count_if (const thrust::detail::execution_policy_base< DerivedPolicy > &exec, InputIterator first, InputIterator last, Predicate pred)
 
template<typename InputIterator , typename Predicate >
thrust::iterator_traits
< InputIterator >
::difference_type 
thrust::count_if (InputIterator first, InputIterator last, Predicate pred)
 

Detailed Description

Function Documentation

template<typename DerivedPolicy , typename InputIterator , typename EqualityComparable >
__host__ __device__ thrust::iterator_traits<InputIterator>::difference_type thrust::count ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
InputIterator  first,
InputIterator  last,
const EqualityComparable &  value 
)

count finds the number of elements in [first,last) that are equal to value. More precisely, count returns the number of iterators i in [first, last) such that *i == value.

The algorithm's execution is parallelized as determined by exec.

Parameters
execThe execution policy to use for parallelization.
firstThe beginning of the sequence.
lastThe end of the sequence.
valueThe value to be counted.
Returns
The number of elements equal to value.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
InputIteratormust be a model of Input Iterator and InputIterator's value_type must be a model of must be a model of Equality Comparable.
EqualityComparablemust be a model of Equality Comparable and can be compared for equality with InputIterator's value_type

The following code snippet demonstrates how to use count to count the number of instances in a range of a value of interest using the thrust::device execution policy:

#include <thrust/count.h>
...
// put 3 1s in a device_vector
thrust::device_vector<int> vec(5,0);
vec[1] = 1;
vec[3] = 1;
vec[4] = 1;
// count the 1s
int result = thrust::count(thrust::device, vec.begin(), vec.end(), 1);
// result == 3
See Also
http://www.sgi.com/tech/stl/count.html
template<typename InputIterator , typename EqualityComparable >
thrust::iterator_traits<InputIterator>::difference_type thrust::count ( InputIterator  first,
InputIterator  last,
const EqualityComparable &  value 
)

count finds the number of elements in [first,last) that are equal to value. More precisely, count returns the number of iterators i in [first, last) such that *i == value.

Parameters
firstThe beginning of the sequence.
lastThe end of the sequence.
valueThe value to be counted.
Returns
The number of elements equal to value.
Template Parameters
InputIteratormust be a model of Input Iterator and InputIterator's value_type must be a model of must be a model of Equality Comparable.
EqualityComparablemust be a model of Equality Comparable and can be compared for equality with InputIterator's value_type

The following code snippet demonstrates how to use count to count the number of instances in a range of a value of interest.

#include <thrust/count.h>
...
// put 3 1s in a device_vector
thrust::device_vector<int> vec(5,0);
vec[1] = 1;
vec[3] = 1;
vec[4] = 1;
// count the 1s
int result = thrust::count(vec.begin(), vec.end(), 1);
// result == 3
See Also
http://www.sgi.com/tech/stl/count.html
template<typename DerivedPolicy , typename InputIterator , typename Predicate >
__host__ __device__ thrust::iterator_traits<InputIterator>::difference_type thrust::count_if ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
InputIterator  first,
InputIterator  last,
Predicate  pred 
)

count_if finds the number of elements in [first,last) for which a predicate is true. More precisely, count_if returns the number of iterators i in [first, last) such that pred(*i) == true.

The algorithm's execution is parallelized as determined by exec.

Parameters
execThe execution policy to use for parallelization.
firstThe beginning of the sequence.
lastThe end of the sequence.
predThe predicate.
Returns
The number of elements where pred is true.
Template Parameters
DerivedPolicyThe name of the derived execution policy.
InputIteratormust be a model of Input Iterator and InputIterator's value_type must be convertible to Predicate's argument_type.
Predicatemust be a model of Predicate.

The following code snippet demonstrates how to use count to count the number of odd numbers in a range using the thrust::device execution policy:

#include <thrust/count.h>
...
struct is_odd
{
__host__ __device__
bool operator()(int &x)
{
return x & 1;
}
};
...
// fill a device_vector with even & odd numbers
thrust::device_vector<int> vec(5);
vec[0] = 0;
vec[1] = 1;
vec[2] = 2;
vec[3] = 3;
vec[4] = 4;
// count the odd elements in vec
int result = thrust::count_if(thrust::device, vec.begin(), vec.end(), is_odd());
// result == 2
See Also
http://www.sgi.com/tech/stl/count.html
template<typename InputIterator , typename Predicate >
thrust::iterator_traits<InputIterator>::difference_type thrust::count_if ( InputIterator  first,
InputIterator  last,
Predicate  pred 
)

count_if finds the number of elements in [first,last) for which a predicate is true. More precisely, count_if returns the number of iterators i in [first, last) such that pred(*i) == true.

Parameters
firstThe beginning of the sequence.
lastThe end of the sequence.
predThe predicate.
Returns
The number of elements where pred is true.
Template Parameters
InputIteratormust be a model of Input Iterator and InputIterator's value_type must be convertible to Predicate's argument_type.
Predicatemust be a model of Predicate.

The following code snippet demonstrates how to use count to count the number of odd numbers in a range.

#include <thrust/count.h>
...
struct is_odd
{
__host__ __device__
bool operator()(int &x)
{
return x & 1;
}
};
...
// fill a device_vector with even & odd numbers
thrust::device_vector<int> vec(5);
vec[0] = 0;
vec[1] = 1;
vec[2] = 2;
vec[3] = 3;
vec[4] = 4;
// count the odd elements in vec
int result = thrust::count_if(vec.begin(), vec.end(), is_odd());
// result == 2
See Also
http://www.sgi.com/tech/stl/count.html