Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
find_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/find_if.hpp
32 //
33 // Extension of std::find_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_FIND_IF_HPP
41 #define ECUDA_ALGO_FIND_IF_HPP
42 
43 #include <algorithm>
44 #include <iterator>
45 #include <vector>
46 
47 #include "../global.hpp"
48 #include "../iterator.hpp"
49 
50 namespace ecuda {
51 
53 namespace impl {
54 
56 template<class InputIterator,class UnaryPredicate>
57 __HOST__ __DEVICE__ InputIterator
58 find_if( InputIterator first, InputIterator last, UnaryPredicate p, ecuda::true_type ) // device memory
59 {
60  #ifdef __CUDA_ARCH__
61  while( first != last ) {
62  if( p(*first) ) return first;
63  ++first;
64  }
65  return first;
66  #else
67  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator>::value_type>::type value_type;
68  std::vector< value_type, host_allocator<value_type> > v( ecuda::distance(first,last) );
69  ecuda::copy( first, last, v.begin() );
70  const typename ecuda::iterator_traits<InputIterator>::difference_type index = std::distance( v.begin(), std::find_if( v.begin(), v.end(), p ) );
71  ecuda::advance( first, index );
72  return first;
73  #endif
74 }
75 
77 template<class InputIterator,class UnaryPredicate>
78 inline __HOST__ __DEVICE__ InputIterator
79 find_if( InputIterator first, InputIterator last, UnaryPredicate p, ecuda::false_type ) // host memory
80 {
81  #ifdef __CUDA_ARCH__
82  return last; // never called on device code
83  #else
84  return std::find_if( first, last, p );
85  #endif
86 }
87 
88 } // namespace impl
90 
92 template<class InputIterator,class UnaryPredicate>
93 inline __HOST__ __DEVICE__ InputIterator
94 find_if( InputIterator first, InputIterator last, UnaryPredicate p )
95 {
97 }
98 
99 } // namespace ecuda
100 
101 #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
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate p)
Definition: find_if.hpp:94
#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