Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
count.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.hpp
32 //
33 // Extension of std::count 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_HPP
41 #define ECUDA_ALGO_COUNT_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,typename T>
56 count( InputIterator first, InputIterator last, const T& value, 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( first, last, value );
63  #endif
64 }
65 
67 template<class InputIterator,typename T>
69 count( InputIterator first, InputIterator last, const T& value, ecuda::true_type ) // device memory
70 {
71  #ifdef __CUDA_ARCH__
73  while( first != last ) {
74  if( *first == value ) ++n;
75  ++first;
76  }
77  return n;
78  #else
79  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator>::value_type>::type value_type;
80  std::vector< value_type, host_allocator<value_type> > v( ecuda::distance(first,last) );
81  ecuda::copy( first, last, v.begin() );
82  return std::count( v.begin(), v.end(), value );
83  #endif
84 }
85 
86 } // namespace impl
88 
90 template<class InputIterator,typename T>
92 count( InputIterator first, InputIterator last, const T& value )
93 {
94  return impl::count( first, last, value, typename ecuda::iterator_traits<InputIterator>::is_device_iterator() );
95 }
96 
97 } // namespace ecuda
98 
99 #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
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__ ecuda::iterator_traits< InputIterator >::difference_type count(InputIterator first, InputIterator last, const T &value)
Definition: count.hpp:92
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ std::iterator_traits< Iterator >::difference_type distance(const Iterator &first, const Iterator &last)
Definition: iterator.hpp:627