Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
reverse.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/reverse.hpp
32 //
33 // Extension of std::reverse 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 #pragma once
39 #ifndef ECUDA_ALGO_REVERSE_HPP
40 #define ECUDA_ALGO_REVERSE_HPP
41 
42 #include <iterator>
43 #include <vector>
44 
45 #include "../global.hpp"
46 #include "../iterator.hpp"
47 //#include "../utility.hpp"
48 #include "../algorithm.hpp"
49 
50 namespace ecuda {
51 
52 // forward declaration
53 template<class BidirectionalIterator> __HOST__ __DEVICE__ inline void reverse( BidirectionalIterator first, BidirectionalIterator last );
54 
56 namespace impl {
57 
58 template<class ForwardIterator>
59 __HOST__ __DEVICE__ inline void
60 reverse( ForwardIterator first, ForwardIterator last,
61  ecuda::false_type // host memory
62 )
63 {
64  #ifdef __CUDA_ARCH__
65  // never called from device code
66  #else
67  std::reverse( first, last );
68  #endif
69 }
70 
71 template<class ForwardIterator>
72 __HOST__ __DEVICE__ inline void
73 reverse( ForwardIterator first, ForwardIterator last,
74  ecuda::true_type // device memory
75 )
76 {
77  #ifdef __CUDA_ARCH__
78  while( (first!=last) and (first!=--last) ) {
79  ecuda::swap( *first, *last );
80  ++first;
81  }
82  #else
83  {
84  typedef typename ecuda::iterator_traits<ForwardIterator>::iterator_category iterator_category;
85  typedef typename ecuda::iterator_traits<ForwardIterator>::is_contiguous iterator_contiguity;
86  const bool isSomeKindOfContiguous =
87  ecuda::is_same<iterator_contiguity,ecuda::true_type>::value ||
88  ecuda::is_same<iterator_category,device_contiguous_block_iterator_tag>::value;
89  ECUDA_STATIC_ASSERT(isSomeKindOfContiguous,CANNOT_REVERSE_RANGE_REPRESENTED_BY_NONCONTIGUOUS_DEVICE_MEMORY);
90  }
91  typedef typename ecuda::remove_const<typename ecuda::iterator_traits<ForwardIterator>::value_type>::type value_type;
92  std::vector< value_type, host_allocator<value_type> > v( ::ecuda::distance( first, last ) );
93  ::ecuda::copy( first, last, v.begin() );
94  ::ecuda::reverse( v.begin(), v.end() );
95  ::ecuda::copy( v.begin(), v.end(), first );
96  #endif
97 }
98 
99 } // namespace impl
101 
103 template<class BidirectionalIterator>
104 __HOST__ __DEVICE__ inline void reverse( BidirectionalIterator first, BidirectionalIterator last ) {
106  first, last,
108  );
109 }
110 
111 } // namespace ecuda
112 
113 #endif
__HOST__ __DEVICE__ OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
Replacement for std::copy.
Definition: copy.hpp:801
__HOST__ __DEVICE__ void reverse(BidirectionalIterator first, BidirectionalIterator last)
Definition: reverse.hpp:104
base_type::iterator_category iterator_category
Definition: iterator.hpp:437
#define __HOST__
Definition: global.hpp:150
__HOST__ __DEVICE__ void swap(T &a, T &b) __NOEXCEPT__
Definition: algorithm.hpp:54
ecuda::false_type is_device_iterator
Definition: iterator.hpp:441
ECUDA_SUPPRESS_HD_WARNINGS __HOST__ __DEVICE__ void reverse(BidirectionalIterator first, BidirectionalIterator last)
Definition: reverse.hpp:104
#define ECUDA_STATIC_ASSERT(x, msg)
Definition: global.hpp:191
#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