Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
find_if_not.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/find_if_not.hpp
32 //
33 // Extension of std::find_if_not that recognizes device memory and can be called
34 // from 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_FIND_IF_NOT_HPP
41 #define ECUDA_ALGO_FIND_IF_NOT_HPP
42 
43 #ifdef ECUDA_CPP11_AVAILABLE
44 
45 #include <algorithm>
46 #include <iterator>
47 #include <vector>
48 
49 #include "../global.hpp"
50 #include "../iterator.hpp"
51 
52 namespace ecuda {
53 
55 namespace impl {
56 
58 template<class InputIterator,class UnaryPredicate>
59 __HOST__ __DEVICE__ InputIterator
60 find_if_not( InputIterator first, InputIterator last, UnaryPredicate p, ecuda::true_type ) // device memory
61 {
62  #ifdef __CUDA_ARCH__
63  while( first != last ) {
64  if( p(*first) ) return first;
65  ++first;
66  }
67  return first;
68  #else
69  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator>::value_type>::type value_type;
70  std::vector< value_type, host_allocator<value_type> > v( ecuda::distance(first,last) );
71  ecuda::copy( first, last, v.begin() );
72  const typename ecuda::iterator_traits<InputIterator>::difference_type index = std::distance( v.begin(), std::find_if_not( v.begin(), v.end(), p ) );
73  ecuda::advance( first, index );
74  return first;
75  #endif
76 }
77 
79 template<class InputIterator,class UnaryPredicate>
80 inline __HOST__ __DEVICE__ InputIterator
81 find_if_not( InputIterator first, InputIterator last, UnaryPredicate p, ecuda::false_type ) // host memory
82 {
83  #ifdef __CUDA_ARCH__
84  return last; // never called from device code
85  #else
86  return std::find_if_not( first, last, p );
87  #endif
88 }
89 
90 } // namespace impl
92 
94 template<class InputIterator,class UnaryPredicate>
95 inline __HOST__ __DEVICE__ InputIterator
96 find_if_not( InputIterator first, InputIterator last, UnaryPredicate p )
97 {
98  return impl::find_if_not( first, last, p, typename ecuda::iterator_traits<InputIterator>::is_device_iterator() );
99 }
100 
102 template<class InputIterator,class UnaryPredicate>
103 inline __HOST__ __DEVICE__ bool
104 all_of( InputIterator first, InputIterator last, UnaryPredicate p )
105 {
106  return ecuda::find_if_not( first, last, p ) == last;
107 }
108 
109 } // namespace ecuda
110 
111 #endif // ECUDA_CPP11_AVAILABLE
112 
113 #endif
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
Definition: copy.hpp:801
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ void advance(InputIterator &iterator, Distance n)
Increments given iterator by n elements.
Definition: iterator.hpp:574
#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__ std::iterator_traits< Iterator >::difference_type distance(const Iterator &first, const Iterator &last)
Definition: iterator.hpp:627