Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
lexicographical_compare.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/lexicographical_compare.hpp
32 //
33 // Extension of std::lexicographical_compare that recognizes device memory and
34 // can be called from host or device code.
35 //
36 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
37 //----------------------------------------------------------------------------
38 #pragma once
39 #ifndef ECUDA_ALGO_LEXICOGRAPHICAL_COMPARE_HPP
40 #define ECUDA_ALGO_LEXICOGRAPHICAL_COMPARE_HPP
41 
42 #include <iterator>
43 #include <utility>
44 #include <vector>
45 
46 #include "../global.hpp"
47 #include "../allocators.hpp"
48 #include "../iterator.hpp"
49 #include "../utility.hpp"
50 
51 namespace ecuda {
52 
53 // forward declaration
54 template<class InputIterator1,class InputIterator2> __HOST__ __DEVICE__ inline bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 );
55 
57 namespace impl {
58 
59 template<class InputIterator1,class InputIterator2>
60 __HOST__ __DEVICE__ inline bool
61 lexicographical_compare( InputIterator1 first1, InputIterator1 last1,
62  InputIterator2 first2, InputIterator2 last2,
63  pair<ecuda::false_type,ecuda::false_type>
64 )
65 {
66  #ifdef __CUDA_ARCH__
67  return false; // never actually gets called, just here to trick nvcc
68  #else
69  return std::lexicographical_compare( first1, last1, first2, last2 );
70  #endif
71 }
72 
73 template<class InputIterator1,class InputIterator2>
74 __HOST__ __DEVICE__ inline bool
75 lexicographical_compare( InputIterator1 first1, InputIterator1 last1,
76  InputIterator2 first2, InputIterator2 last2,
77  pair<ecuda::true_type,ecuda::false_type>
78 )
79 {
80  #ifdef __CUDA_ARCH__
81  return false; // never actually gets called, just here to trick nvcc
82  #else
83  return ::ecuda::lexicographical_compare( first2, last2, first1, last1 ); // switch positions to resolve to function below
84  #endif
85 }
86 
87 template<class InputIterator1,class InputIterator2>
88 __HOST__ __DEVICE__ inline bool
89 lexicographical_compare( InputIterator1 first1, InputIterator1 last1,
90  InputIterator2 first2, InputIterator2 last2,
91  pair<ecuda::false_type,ecuda::true_type>
92 )
93 {
94  #ifdef __CUDA_ARCH__
95  return false; // never actually gets called, just here to trick nvcc
96  #else
97  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator2>::value_type>::type value_type;
98  {
99  typedef typename ecuda::iterator_traits<InputIterator2>::iterator_category iterator_category;
100  typedef typename ecuda::iterator_traits<InputIterator2>::is_contiguous iterator_contiguity;
101  const bool isSomeKindOfContiguous =
102  ecuda::is_same<iterator_contiguity,ecuda::true_type>::value ||
103  ecuda::is_same<iterator_category,device_contiguous_block_iterator_tag>::value;
104  ECUDA_STATIC_ASSERT(isSomeKindOfContiguous,CANNOT_LEXICOGRAPHICALLY_COMPARE_RANGE_REPRESENTED_BY_NONCONTIGUOUS_DEVICE_MEMORY);
105  }
106  std::vector< value_type, host_allocator<value_type> > v( ::ecuda::distance(first2,last2) );
107  ::ecuda::copy( first2, last2, v.begin() );
108  return ::ecuda::lexicographical_compare( first1, last1, v.begin(), v.end() );
109  #endif
110 }
111 
113 template<class InputIterator1,class InputIterator2>
114 __HOST__ __DEVICE__ inline
115 bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1,
116  InputIterator2 first2, InputIterator2 last2,
117  pair<ecuda::true_type,ecuda::true_type> // compare device to device
118 )
119 {
120  #ifdef __CUDA_ARCH__
121  for( ; (first1 != last1) and (first2 != last2); ++first1, ++first2 ) {
122  if( *first1 < *first2 ) return true;
123  if( *first2 < *first1 ) return false;
124  }
125  return (first1 == last1) and (first2 != last2);
126  #else
127  // strip const qualifiers if present because std::vector<const T> is useless
128  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator1>::value_type>::type valtype1;
129  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<InputIterator2>::value_type>::type valtype2;
130  std::vector< valtype1, host_allocator<valtype1> > v1( ecuda::distance( first1, last1 ) );
131  std::vector< valtype2, host_allocator<valtype2> > v2( ecuda::distance( first2, last2 ) );
132  ecuda::copy( first1, last1, v1.begin() );
133  ecuda::copy( first2, last2, v2.begin() );
134  return std::lexicographical_compare( v1.begin(), v1.end(), v2.begin(), v2.end() );
135  #endif
136 }
137 
138 } // namespace impl
140 
142 template<class InputIterator1,class InputIterator2>
143 __HOST__ __DEVICE__ inline bool lexicographical_compare( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 ) {
145 }
146 
147 } // namespace ecuda
148 
149 #endif
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
Definition: copy.hpp:801
base_type::iterator_category iterator_category
Definition: iterator.hpp:437
#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 ECUDA_STATIC_ASSERT(x, msg)
Definition: global.hpp:191
#define __DEVICE__
Definition: global.hpp:151
__HOST__ __DEVICE__ bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
#define ECUDA_SUPPRESS_HD_WARNINGS
Definition: global.hpp:58
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2)
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ std::iterator_traits< Iterator >::difference_type distance(const Iterator &first, const Iterator &last)
Definition: iterator.hpp:627