Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
mismatch.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/mismatch.hpp
32 //
33 // Extension of std::mismatch 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_MISMATCH_HPP
41 #define ECUDA_ALGO_MISMATCH_HPP
42 
43 #include <algorithm>
44 #include <vector>
45 
46 #include "../global.hpp"
47 #include "../iterator.hpp"
48 #include "../utility.hpp"
49 
50 namespace ecuda {
51 
53 namespace impl {
54 
55 template<class InputIterator1,class InputIterator2>
57 mismatch( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, ecuda::pair<ecuda::false_type,ecuda::false_type> ) // host/host memory
58 {
59  #ifdef __CUDA_ARCH__
60  return ecuda::pair<InputIterator1,InputIterator2>(first1,first2); // never called from device code
61  #else
62  // just defer to STL
63  std::pair<InputIterator1,InputIterator2> p = std::mismatch( first1, last1, first2 );
65  #endif
66 }
67 
69 template<class InputIterator1,class InputIterator2>
71 mismatch( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, ecuda::pair<ecuda::false_type,ecuda::true_type> ) // host/device memory
72 {
73  #ifdef __CUDA_ARCH__
74  return ecuda::pair<InputIterator1,InputIterator2>(first1,first2); // never called from device code
75  #else
76  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator2>::value_type>::type value_type2;
77  typedef std::vector< value_type2, host_allocator<value_type2> > vector_type2;
78  vector_type2 v2( ecuda::distance(first1,last1) );
79  ecuda::copy( first2, first2+v2.size(), v2.begin() );
80  std::pair<InputIterator1,typename vector_type2::iterator> p = std::mismatch( first1, last1, v2.begin() );
81  ecuda::advance( first2, ecuda::distance(v2.begin(),p.second) );
82  return ecuda::pair<InputIterator1,InputIterator2>( p.first, first2 );
83  #endif
84 }
85 
86 template<class InputIterator1,class InputIterator2>
88 mismatch( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, ecuda::pair<ecuda::true_type,ecuda::false_type> ) // device/host memory
89 {
90  #ifdef __CUDA_ARCH__
91  return ecuda::pair<InputIterator1,InputIterator2>(first1,first2); // never called from device code
92  #else
93  InputIterator2 last2 = first2;
94  ecuda::advance( last2, ecuda::distance(first1,last1) );
97  #endif
98 }
99 
101 template<class InputIterator1,class InputIterator2>
103 mismatch( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, ecuda::pair<ecuda::true_type,ecuda::true_type> ) // device/device memory
104 {
105  #ifdef __CUDA_ARCH__
106  while( (first1 != last1) && (*first1 == *first2) ) { ++first1; ++first2; }
107  return ecuda::pair<InputIterator1,InputIterator2>(first1,first2);
108  #else
109  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator1>::value_type>::type value_type1;
110  typedef std::vector< value_type1, host_allocator<value_type1> > vector_type1;
111  vector_type1 v1( ecuda::distance(first1,last1) );
112  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator2>::value_type>::type value_type2;
113  typedef std::vector< value_type2, host_allocator<value_type2> > vector_type2;
114  vector_type2 v2( v1.size() );
115  ecuda::copy( first1, last1, v1.begin() );
116  InputIterator2 last2 = first2;
117  ecuda::advance( last2, ecuda::distance(first1,last1) );
118  ecuda::copy( first2, last2, v2.begin() );
119  std::pair<typename vector_type1::iterator,typename vector_type2::iterator> p = std::mismatch( v1.begin(), v1.end(), v2.begin() );
120  ecuda::advance( first1, ecuda::distance(v1.begin(),p.first) );
121  ecuda::advance( first2, ecuda::distance(v2.begin(),p.second) );
122  return ecuda::pair<InputIterator1,InputIterator2>( first1, first2 );
123  #endif
124 }
125 
126 } // namespace impl
128 
130 template<class InputIterator1,class InputIterator2>
132 mismatch( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
133 {
134  return impl::mismatch(
135  first1, last1,
136  first2,
138  );
139 }
140 
141 } // namespace ecuda
142 
143 #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__ ecuda::pair< InputIterator1, InputIterator2 > mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
Definition: mismatch.hpp:132
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
Couples together a pair of values.
Definition: utility.hpp:53
#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