Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
count_if.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015, Scott Zuyderduyn
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // algo/count_if.hpp
32 //
33 // Extension of std::count_if that recognizes device memory and can be called from
34 // host or device code.
35 //
36 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
37 //----------------------------------------------------------------------------
38 
39 #pragma once
40 #ifndef ECUDA_ALGO_COUNT_IF_HPP
41 #define ECUDA_ALGO_COUNT_IF_HPP
42 
43 #include <algorithm>
44 #include <vector>
45 
46 #include "../global.hpp"
47 #include "../iterator.hpp"
48 
49 namespace ecuda {
50 
52 namespace impl {
53 
54 template<class InputIterator,class UnaryPredicate>
56 count_if( InputIterator first, InputIterator last, UnaryPredicate p, ecuda::false_type ) // host memory
57 {
58  #ifdef __CUDA_ARCH__
59  return 0; // never called from device code
60  #else
61  // just defer to STL
62  return std::count_if( first, last, p );
63  #endif
64 }
65 
66 template<class InputIterator,class UnaryPredicate>
68 count_if( InputIterator first, InputIterator last, UnaryPredicate p, ecuda::true_type ) // device memory
69 {
70  #ifdef __CUDA_ARCH__
72  while( first != last ) {
73  if( p(*first) ) ++n;
74  ++first;
75  }
76  return n;
77  #else
78  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator>::value_type>::type value_type;
79  std::vector< value_type, host_allocator<value_type> > v( ecuda::distance(first,last) );
80  ecuda::copy( first, last, v.begin() );
81  return std::count_if( v.begin(), v.end(), p );
82  #endif
83 }
84 
85 } // namespace impl
87 
89 template<class InputIterator,class UnaryPredicate>
91 count_if( InputIterator first, InputIterator last, UnaryPredicate p )
92 {
94 }
95 
96 } // namespace ecuda
97 
98 #endif
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
Definition: copy.hpp:801
#define __HOST__
Definition: global.hpp:150
ecuda::false_type is_device_iterator
Definition: iterator.hpp:441
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ ecuda::iterator_traits< InputIterator >::difference_type count_if(InputIterator first, InputIterator last, UnaryPredicate p)
Definition: count_if.hpp:91
base_type::difference_type difference_type
Definition: iterator.hpp:436
#define __DEVICE__
Definition: global.hpp:151
#define ECUDA_SUPPRESS_HD_WARNINGS
Definition: global.hpp:58
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ std::iterator_traits< Iterator >::difference_type distance(const Iterator &first, const Iterator &last)
Definition: iterator.hpp:627